langchain 0.0.147 → 0.0.149
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/chat_models/googlevertexai/web.cjs +1 -0
- package/chat_models/googlevertexai/web.d.ts +1 -0
- package/chat_models/googlevertexai/web.js +1 -0
- package/chat_models/googlevertexai.cjs +1 -1
- package/chat_models/googlevertexai.d.ts +1 -1
- package/chat_models/googlevertexai.js +1 -1
- package/dist/chains/constitutional_ai/constitutional_principle.cjs +272 -1
- package/dist/chains/constitutional_ai/constitutional_principle.js +272 -1
- package/dist/chains/question_answering/load.cjs +12 -4
- package/dist/chains/question_answering/load.d.ts +2 -0
- package/dist/chains/question_answering/load.js +12 -4
- package/dist/chains/summarization/load.cjs +8 -4
- package/dist/chains/summarization/load.d.ts +2 -0
- package/dist/chains/summarization/load.js +8 -4
- package/dist/chat_models/{googlevertexai.cjs → googlevertexai/common.cjs} +14 -26
- package/dist/chat_models/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -22
- package/dist/chat_models/{googlevertexai.js → googlevertexai/common.js} +12 -24
- package/dist/chat_models/googlevertexai/index.cjs +36 -0
- package/dist/chat_models/googlevertexai/index.d.ts +21 -0
- package/dist/chat_models/googlevertexai/index.js +31 -0
- package/dist/chat_models/googlevertexai/web.cjs +33 -0
- package/dist/chat_models/googlevertexai/web.d.ts +19 -0
- package/dist/chat_models/googlevertexai/web.js +28 -0
- package/dist/document_loaders/web/notionapi.cjs +93 -70
- package/dist/document_loaders/web/notionapi.d.ts +33 -1
- package/dist/document_loaders/web/notionapi.js +89 -71
- package/dist/embeddings/googlevertexai.cjs +5 -1
- package/dist/embeddings/googlevertexai.d.ts +2 -1
- package/dist/embeddings/googlevertexai.js +5 -1
- package/dist/evaluation/agents/index.cjs +17 -0
- package/dist/evaluation/agents/index.d.ts +1 -0
- package/dist/evaluation/agents/index.js +1 -0
- package/dist/evaluation/agents/prompt.cjs +132 -0
- package/dist/evaluation/agents/prompt.d.ts +6 -0
- package/dist/evaluation/agents/prompt.js +129 -0
- package/dist/evaluation/agents/trajectory.cjs +189 -0
- package/dist/evaluation/agents/trajectory.d.ts +54 -0
- package/dist/evaluation/agents/trajectory.js +184 -0
- package/dist/evaluation/base.cjs +274 -0
- package/dist/evaluation/base.d.ts +232 -0
- package/dist/evaluation/base.js +263 -0
- package/dist/evaluation/comparison/index.cjs +17 -0
- package/dist/evaluation/comparison/index.d.ts +1 -0
- package/dist/evaluation/comparison/index.js +1 -0
- package/dist/evaluation/comparison/pairwise.cjs +244 -0
- package/dist/evaluation/comparison/pairwise.d.ts +50 -0
- package/dist/evaluation/comparison/pairwise.js +238 -0
- package/dist/evaluation/comparison/prompt.cjs +74 -0
- package/dist/evaluation/comparison/prompt.d.ts +21 -0
- package/dist/evaluation/comparison/prompt.js +71 -0
- package/dist/evaluation/criteria/criteria.cjs +259 -0
- package/dist/evaluation/criteria/criteria.d.ts +73 -0
- package/dist/evaluation/criteria/criteria.js +253 -0
- package/dist/evaluation/criteria/index.cjs +17 -0
- package/dist/evaluation/criteria/index.d.ts +1 -0
- package/dist/evaluation/criteria/index.js +1 -0
- package/dist/evaluation/criteria/prompt.cjs +36 -0
- package/dist/evaluation/criteria/prompt.d.ts +12 -0
- package/dist/evaluation/criteria/prompt.js +33 -0
- package/dist/evaluation/embedding_distance/base.cjs +163 -0
- package/dist/evaluation/embedding_distance/base.d.ts +78 -0
- package/dist/evaluation/embedding_distance/base.js +156 -0
- package/dist/evaluation/embedding_distance/index.cjs +17 -0
- package/dist/evaluation/embedding_distance/index.d.ts +1 -0
- package/dist/evaluation/embedding_distance/index.js +1 -0
- package/dist/evaluation/index.cjs +6 -0
- package/dist/evaluation/index.d.ts +6 -0
- package/dist/evaluation/index.js +6 -0
- package/dist/evaluation/loader.cjs +60 -0
- package/dist/evaluation/loader.d.ts +27 -0
- package/dist/evaluation/loader.js +56 -0
- package/dist/evaluation/types.cjs +2 -0
- package/dist/evaluation/types.d.ts +35 -0
- package/dist/evaluation/types.js +1 -0
- package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +5 -1
- package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
- package/dist/experimental/multimodal_embeddings/googlevertexai.js +5 -1
- package/dist/llms/bedrock.cjs +9 -1
- package/dist/llms/bedrock.d.ts +3 -0
- package/dist/llms/bedrock.js +9 -1
- package/dist/llms/{googlevertexai.js → googlevertexai/common.cjs} +21 -17
- package/dist/llms/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -23
- package/dist/llms/{googlevertexai.cjs → googlevertexai/common.js} +17 -21
- package/dist/llms/googlevertexai/index.cjs +34 -0
- package/dist/llms/googlevertexai/index.d.ts +26 -0
- package/dist/llms/googlevertexai/index.js +30 -0
- package/dist/llms/googlevertexai/web.cjs +31 -0
- package/dist/llms/googlevertexai/web.d.ts +24 -0
- package/dist/llms/googlevertexai/web.js +27 -0
- package/dist/load/import_constants.cjs +2 -0
- package/dist/load/import_constants.js +2 -0
- package/dist/load/import_map.cjs +2 -1
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/load/serializable.cjs +23 -4
- package/dist/load/serializable.js +23 -4
- package/dist/retrievers/multi_query.cjs +140 -0
- package/dist/retrievers/multi_query.d.ts +33 -0
- package/dist/retrievers/multi_query.js +136 -0
- package/dist/retrievers/self_query/base.cjs +1 -1
- package/dist/retrievers/self_query/base.js +2 -2
- package/dist/retrievers/self_query/functional.cjs +1 -1
- package/dist/retrievers/self_query/functional.js +2 -2
- package/dist/retrievers/self_query/utils.cjs +46 -6
- package/dist/retrievers/self_query/utils.d.ts +7 -0
- package/dist/retrievers/self_query/utils.js +44 -5
- package/dist/schema/runnable/base.cjs +910 -0
- package/dist/schema/runnable/base.d.ts +300 -0
- package/dist/schema/runnable/base.js +896 -0
- package/dist/schema/runnable/index.cjs +19 -926
- package/dist/schema/runnable/index.d.ts +4 -298
- package/dist/schema/runnable/index.js +3 -914
- package/dist/schema/runnable/passthrough.cjs +31 -0
- package/dist/schema/runnable/passthrough.d.ts +11 -0
- package/dist/schema/runnable/passthrough.js +27 -0
- package/dist/schema/runnable/router.cjs +74 -0
- package/dist/schema/runnable/router.d.ts +29 -0
- package/dist/schema/runnable/router.js +70 -0
- package/dist/types/googlevertexai-types.d.ts +11 -4
- package/dist/util/googlevertexai-connection.cjs +14 -15
- package/dist/util/googlevertexai-connection.d.ts +7 -7
- package/dist/util/googlevertexai-connection.js +14 -15
- package/dist/util/googlevertexai-webauth.cjs +56 -0
- package/dist/util/googlevertexai-webauth.d.ts +25 -0
- package/dist/util/googlevertexai-webauth.js +52 -0
- package/dist/vectorstores/googlevertexai.cjs +9 -8
- package/dist/vectorstores/googlevertexai.d.ts +8 -7
- package/dist/vectorstores/googlevertexai.js +9 -8
- package/dist/vectorstores/opensearch.cjs +4 -2
- package/dist/vectorstores/opensearch.d.ts +4 -1
- package/dist/vectorstores/opensearch.js +4 -2
- package/llms/googlevertexai/web.cjs +1 -0
- package/llms/googlevertexai/web.d.ts +1 -0
- package/llms/googlevertexai/web.js +1 -0
- package/llms/googlevertexai.cjs +1 -1
- package/llms/googlevertexai.d.ts +1 -1
- package/llms/googlevertexai.js +1 -1
- package/package.json +32 -3
- package/retrievers/multi_query.cjs +1 -0
- package/retrievers/multi_query.d.ts +1 -0
- package/retrievers/multi_query.js +1 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { BaseLLMOutputParser } from "../../schema/output_parser.js";
|
|
2
|
+
import { AgentTrajectoryEvaluator, } from "../base.js";
|
|
3
|
+
import { RUN_KEY, } from "../../schema/index.js";
|
|
4
|
+
import { EVAL_CHAT_PROMPT, TOOL_FREE_EVAL_CHAT_PROMPT } from "./prompt.js";
|
|
5
|
+
/**
|
|
6
|
+
* A parser for the output of the TrajectoryEvalChain.
|
|
7
|
+
*/
|
|
8
|
+
export class TrajectoryOutputParser extends BaseLLMOutputParser {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: ["langchain", "evaluation", "agents"]
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
static lc_name() {
|
|
19
|
+
return "TrajectoryOutputParser";
|
|
20
|
+
}
|
|
21
|
+
parseResult(generations, _callbacks) {
|
|
22
|
+
const { text } = generations[0];
|
|
23
|
+
if (!text.includes("Score:")) {
|
|
24
|
+
throw new Error(`Could not find score in model eval output: ${text}`);
|
|
25
|
+
}
|
|
26
|
+
let [reasoning, scoreStr] = text.split("Score:", 2);
|
|
27
|
+
reasoning = reasoning.trim();
|
|
28
|
+
scoreStr = scoreStr.trim();
|
|
29
|
+
// Use regex to extract the score.
|
|
30
|
+
// This will get the number in the string, even if it is a float or more than 10.
|
|
31
|
+
// E.g. "Score: 1" will return 1, "Score: 3.5" will return 3.5, and
|
|
32
|
+
// "Score: 10" will return 10.
|
|
33
|
+
// The score should be an integer digit in the range 1-5.
|
|
34
|
+
const scoreMatch = scoreStr.match(/(\d+(\.\d+)?)/);
|
|
35
|
+
if (scoreMatch === null || scoreMatch[1].includes(".")) {
|
|
36
|
+
throw new Error(`Score is not an integer digit in the range 1-5: ${text}`);
|
|
37
|
+
}
|
|
38
|
+
const score = +scoreMatch[1];
|
|
39
|
+
if (score < 1 || score > 5) {
|
|
40
|
+
throw new Error(`Score is not a digit in the range 1-5: ${text}`);
|
|
41
|
+
}
|
|
42
|
+
const normalizedScore = (score - 1) / 4;
|
|
43
|
+
return Promise.resolve({
|
|
44
|
+
reasoning,
|
|
45
|
+
score: normalizedScore,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A chain for evaluating ReAct style agents.
|
|
51
|
+
*
|
|
52
|
+
* This chain is used to evaluate ReAct style agents by reasoning about
|
|
53
|
+
* the sequence of actions taken and their outcomes.
|
|
54
|
+
*/
|
|
55
|
+
export class TrajectoryEvalChain extends AgentTrajectoryEvaluator {
|
|
56
|
+
constructor() {
|
|
57
|
+
super(...arguments);
|
|
58
|
+
Object.defineProperty(this, "criterionName", {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
writable: true,
|
|
62
|
+
value: void 0
|
|
63
|
+
});
|
|
64
|
+
Object.defineProperty(this, "evaluationName", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: this.criterionName
|
|
69
|
+
});
|
|
70
|
+
Object.defineProperty(this, "requiresInput", {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: true,
|
|
73
|
+
writable: true,
|
|
74
|
+
value: true
|
|
75
|
+
});
|
|
76
|
+
Object.defineProperty(this, "requiresReference", {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: true,
|
|
79
|
+
writable: true,
|
|
80
|
+
value: false
|
|
81
|
+
});
|
|
82
|
+
Object.defineProperty(this, "outputParser", {
|
|
83
|
+
enumerable: true,
|
|
84
|
+
configurable: true,
|
|
85
|
+
writable: true,
|
|
86
|
+
value: new TrajectoryOutputParser()
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
static lc_name() {
|
|
90
|
+
return "TrajectoryEvalChain";
|
|
91
|
+
}
|
|
92
|
+
static resolveTrajectoryPrompt(prompt, agentTools) {
|
|
93
|
+
let _prompt;
|
|
94
|
+
if (prompt) {
|
|
95
|
+
_prompt = prompt;
|
|
96
|
+
}
|
|
97
|
+
else if (agentTools) {
|
|
98
|
+
_prompt = EVAL_CHAT_PROMPT;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
_prompt = TOOL_FREE_EVAL_CHAT_PROMPT;
|
|
102
|
+
}
|
|
103
|
+
return _prompt;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get the description of the agent tools.
|
|
107
|
+
*
|
|
108
|
+
* @returns The description of the agent tools.
|
|
109
|
+
*/
|
|
110
|
+
static toolsDescription(agentTools) {
|
|
111
|
+
return agentTools
|
|
112
|
+
.map((tool, i) => `Tool ${i + 1}: ${tool.name}\n Description: ${tool.description}`)
|
|
113
|
+
.join("\n\n");
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Create a new TrajectoryEvalChain.
|
|
117
|
+
* @param llm
|
|
118
|
+
* @param agentTools - The tools used by the agent.
|
|
119
|
+
* @param chainOptions - The options for the chain.
|
|
120
|
+
*/
|
|
121
|
+
static async fromLLM(llm, agentTools, chainOptions) {
|
|
122
|
+
let prompt = this.resolveTrajectoryPrompt(chainOptions?.prompt, agentTools);
|
|
123
|
+
if (agentTools) {
|
|
124
|
+
const toolDescriptions = this.toolsDescription(agentTools);
|
|
125
|
+
prompt = await prompt.partial({ toolDescriptions });
|
|
126
|
+
}
|
|
127
|
+
const options = chainOptions;
|
|
128
|
+
if (options) {
|
|
129
|
+
// remove prompt from chainOptions
|
|
130
|
+
delete options.prompt;
|
|
131
|
+
}
|
|
132
|
+
return new this({
|
|
133
|
+
llm,
|
|
134
|
+
prompt,
|
|
135
|
+
...options,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
_prepareOutput(result) {
|
|
139
|
+
const parsed = result[this.outputKey];
|
|
140
|
+
if (RUN_KEY in result && result[RUN_KEY]) {
|
|
141
|
+
parsed[RUN_KEY] = result[RUN_KEY];
|
|
142
|
+
}
|
|
143
|
+
return parsed;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Get the agent trajectory as a formatted string.
|
|
147
|
+
*
|
|
148
|
+
* @param steps - The agent trajectory.
|
|
149
|
+
* @returns The formatted agent trajectory.
|
|
150
|
+
*/
|
|
151
|
+
getAgentTrajectory(steps) {
|
|
152
|
+
return steps
|
|
153
|
+
.map((step, i) => {
|
|
154
|
+
const { action, observation } = step;
|
|
155
|
+
return (`Step ${i + 1}:\n` +
|
|
156
|
+
`Tool used: ${action.tool}\n` +
|
|
157
|
+
`Tool input: ${action.toolInput}\n` +
|
|
158
|
+
`Tool output: ${observation}`);
|
|
159
|
+
})
|
|
160
|
+
.join("\n\n");
|
|
161
|
+
}
|
|
162
|
+
formatReference(reference) {
|
|
163
|
+
if (!reference) {
|
|
164
|
+
return "";
|
|
165
|
+
}
|
|
166
|
+
return `
|
|
167
|
+
The following is the expected answer. Use this to measure correctness:
|
|
168
|
+
[GROUND_TRUTH]
|
|
169
|
+
${reference}
|
|
170
|
+
[END_GROUND_TRUTH]
|
|
171
|
+
`;
|
|
172
|
+
}
|
|
173
|
+
async _evaluateAgentTrajectory(args, callOptions, config) {
|
|
174
|
+
const { input, prediction, reference, agentTrajectory } = args;
|
|
175
|
+
const inputs = {
|
|
176
|
+
question: input,
|
|
177
|
+
agentTrajectory: this.getAgentTrajectory(agentTrajectory),
|
|
178
|
+
answer: prediction,
|
|
179
|
+
reference: this.formatReference(reference),
|
|
180
|
+
};
|
|
181
|
+
const result = await this.call({ ...inputs, ...callOptions }, config);
|
|
182
|
+
return this._prepareOutput(result);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentTrajectoryEvaluator = exports.LLMPairwiseStringEvaluator = exports.PairwiseStringEvaluator = exports.StringEvaluator = exports.LLMStringEvaluator = exports.EvalChain = exports.LLMEvalChain = exports.eqSet = void 0;
|
|
4
|
+
const index_js_1 = require("../chains/index.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Compare two sets for equality
|
|
7
|
+
*
|
|
8
|
+
* @param xs
|
|
9
|
+
* @param ys
|
|
10
|
+
*/
|
|
11
|
+
const eqSet = (xs, ys) => xs.size === ys.size && [...xs].every((x) => ys.has(x));
|
|
12
|
+
exports.eqSet = eqSet;
|
|
13
|
+
/**
|
|
14
|
+
* Base llm chain class for evaluators.
|
|
15
|
+
*/
|
|
16
|
+
class LLMEvalChain extends index_js_1.LLMChain {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(...arguments);
|
|
19
|
+
Object.defineProperty(this, "requiresInput", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: false
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, "requiresReference", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: false
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "skipInputWarning", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: `Ignoring input in ${this.constructor.name}, as it is not expected.`
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(this, "skipReferenceWarning", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: `Ignoring reference in ${this.constructor.name}, as it is not expected.`
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Check if the evaluation arguments are valid.
|
|
46
|
+
* @param reference The reference label.
|
|
47
|
+
* @param input The input string.
|
|
48
|
+
* @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.
|
|
49
|
+
*/
|
|
50
|
+
checkEvaluationArgs(reference, input) {
|
|
51
|
+
if (this.requiresInput && input == null) {
|
|
52
|
+
throw new Error(`${this.constructor.name} requires an input string.`);
|
|
53
|
+
}
|
|
54
|
+
else if (input != null && !this.requiresInput) {
|
|
55
|
+
console.warn(this.skipInputWarning);
|
|
56
|
+
}
|
|
57
|
+
if (this.requiresReference && reference == null) {
|
|
58
|
+
throw new Error(`${this.constructor.name} requires a reference string.`);
|
|
59
|
+
}
|
|
60
|
+
else if (reference != null && !this.requiresReference) {
|
|
61
|
+
console.warn(this.skipReferenceWarning);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.LLMEvalChain = LLMEvalChain;
|
|
66
|
+
/**
|
|
67
|
+
* Base chain class for evaluators.
|
|
68
|
+
*/
|
|
69
|
+
class EvalChain extends index_js_1.BaseChain {
|
|
70
|
+
constructor() {
|
|
71
|
+
super(...arguments);
|
|
72
|
+
Object.defineProperty(this, "requiresInput", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
configurable: true,
|
|
75
|
+
writable: true,
|
|
76
|
+
value: false
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(this, "requiresReference", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
configurable: true,
|
|
81
|
+
writable: true,
|
|
82
|
+
value: false
|
|
83
|
+
});
|
|
84
|
+
Object.defineProperty(this, "skipInputWarning", {
|
|
85
|
+
enumerable: true,
|
|
86
|
+
configurable: true,
|
|
87
|
+
writable: true,
|
|
88
|
+
value: `Ignoring input in ${this.constructor.name}, as it is not expected.`
|
|
89
|
+
});
|
|
90
|
+
Object.defineProperty(this, "skipReferenceWarning", {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
configurable: true,
|
|
93
|
+
writable: true,
|
|
94
|
+
value: `Ignoring reference in ${this.constructor.name}, as it is not expected.`
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check if the evaluation arguments are valid.
|
|
99
|
+
* @param reference The reference label.
|
|
100
|
+
* @param input The input string.
|
|
101
|
+
* @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.
|
|
102
|
+
*/
|
|
103
|
+
checkEvaluationArgs(reference, input) {
|
|
104
|
+
if (this.requiresInput && input == null) {
|
|
105
|
+
throw new Error(`${this.constructor.name} requires an input string.`);
|
|
106
|
+
}
|
|
107
|
+
else if (input != null && !this.requiresInput) {
|
|
108
|
+
console.warn(this.skipInputWarning);
|
|
109
|
+
}
|
|
110
|
+
if (this.requiresReference && reference == null) {
|
|
111
|
+
throw new Error(`${this.constructor.name} requires a reference string.`);
|
|
112
|
+
}
|
|
113
|
+
else if (reference != null && !this.requiresReference) {
|
|
114
|
+
console.warn(this.skipReferenceWarning);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.EvalChain = EvalChain;
|
|
119
|
+
/**
|
|
120
|
+
* Grade, tag, or otherwise evaluate predictions relative to their inputs
|
|
121
|
+
* and/or reference labels
|
|
122
|
+
*/
|
|
123
|
+
class LLMStringEvaluator extends LLMEvalChain {
|
|
124
|
+
constructor() {
|
|
125
|
+
super(...arguments);
|
|
126
|
+
/**
|
|
127
|
+
* The name of the evaluation.
|
|
128
|
+
*/
|
|
129
|
+
Object.defineProperty(this, "evaluationName", {
|
|
130
|
+
enumerable: true,
|
|
131
|
+
configurable: true,
|
|
132
|
+
writable: true,
|
|
133
|
+
value: this.constructor.name
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Evaluate Chain or LLM output, based on optional input and label.
|
|
138
|
+
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
|
|
139
|
+
* - score: the score of the evaluation, if applicable.
|
|
140
|
+
* - value: the string value of the evaluation, if applicable.
|
|
141
|
+
* - reasoning: the reasoning for the evaluation, if applicable.
|
|
142
|
+
* @param args
|
|
143
|
+
* @param callOptions
|
|
144
|
+
* @param config
|
|
145
|
+
*/
|
|
146
|
+
evaluateStrings(args, callOptions, config) {
|
|
147
|
+
this.checkEvaluationArgs(args.reference, args.input);
|
|
148
|
+
return this._evaluateStrings(args, callOptions, config);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.LLMStringEvaluator = LLMStringEvaluator;
|
|
152
|
+
/**
|
|
153
|
+
* Grade, tag, or otherwise evaluate predictions relative to their inputs
|
|
154
|
+
* and/or reference labels
|
|
155
|
+
*/
|
|
156
|
+
class StringEvaluator extends EvalChain {
|
|
157
|
+
constructor() {
|
|
158
|
+
super(...arguments);
|
|
159
|
+
/**
|
|
160
|
+
* The name of the evaluation.
|
|
161
|
+
*/
|
|
162
|
+
Object.defineProperty(this, "evaluationName", {
|
|
163
|
+
enumerable: true,
|
|
164
|
+
configurable: true,
|
|
165
|
+
writable: true,
|
|
166
|
+
value: this.constructor.name
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Evaluate Chain or LLM output, based on optional input and label.
|
|
171
|
+
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
|
|
172
|
+
* - score: the score of the evaluation, if applicable.
|
|
173
|
+
* - value: the string value of the evaluation, if applicable.
|
|
174
|
+
* - reasoning: the reasoning for the evaluation, if applicable.
|
|
175
|
+
* @param args
|
|
176
|
+
* @param config
|
|
177
|
+
*/
|
|
178
|
+
evaluateStrings(args, config) {
|
|
179
|
+
this.checkEvaluationArgs(args.reference, args.input);
|
|
180
|
+
return this._evaluateStrings(args, config);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
exports.StringEvaluator = StringEvaluator;
|
|
184
|
+
/**
|
|
185
|
+
* Compare the output of two models (or two outputs of the same model).
|
|
186
|
+
*/
|
|
187
|
+
class PairwiseStringEvaluator extends EvalChain {
|
|
188
|
+
constructor() {
|
|
189
|
+
super(...arguments);
|
|
190
|
+
/**
|
|
191
|
+
* The name of the evaluation.
|
|
192
|
+
*/
|
|
193
|
+
Object.defineProperty(this, "evaluationName", {
|
|
194
|
+
enumerable: true,
|
|
195
|
+
configurable: true,
|
|
196
|
+
writable: true,
|
|
197
|
+
value: this.constructor.name
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Evaluate the output string pairs.
|
|
202
|
+
* @param args
|
|
203
|
+
* @param config
|
|
204
|
+
* @return A dictionary containing the preference, scores, and/or other information.
|
|
205
|
+
*/
|
|
206
|
+
evaluateStringPairs(args, config) {
|
|
207
|
+
return this._evaluateStringPairs(args, config);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.PairwiseStringEvaluator = PairwiseStringEvaluator;
|
|
211
|
+
/**
|
|
212
|
+
* Compare the output of two models (or two outputs of the same model).
|
|
213
|
+
*/
|
|
214
|
+
class LLMPairwiseStringEvaluator extends LLMEvalChain {
|
|
215
|
+
constructor() {
|
|
216
|
+
super(...arguments);
|
|
217
|
+
/**
|
|
218
|
+
* The name of the evaluation.
|
|
219
|
+
*/
|
|
220
|
+
Object.defineProperty(this, "evaluationName", {
|
|
221
|
+
enumerable: true,
|
|
222
|
+
configurable: true,
|
|
223
|
+
writable: true,
|
|
224
|
+
value: this.constructor.name
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Evaluate the output string pairs.
|
|
229
|
+
* @param args
|
|
230
|
+
* @param callOptions
|
|
231
|
+
* @param config
|
|
232
|
+
* @return A dictionary containing the preference, scores, and/or other information.
|
|
233
|
+
*/
|
|
234
|
+
evaluateStringPairs(args, callOptions, config) {
|
|
235
|
+
this.checkEvaluationArgs(args.reference, args.input);
|
|
236
|
+
return this._evaluateStringPairs(args, callOptions, config);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
exports.LLMPairwiseStringEvaluator = LLMPairwiseStringEvaluator;
|
|
240
|
+
/**
|
|
241
|
+
* Interface for evaluating agent trajectories.
|
|
242
|
+
*/
|
|
243
|
+
class AgentTrajectoryEvaluator extends LLMEvalChain {
|
|
244
|
+
constructor() {
|
|
245
|
+
super(...arguments);
|
|
246
|
+
Object.defineProperty(this, "requiresInput", {
|
|
247
|
+
enumerable: true,
|
|
248
|
+
configurable: true,
|
|
249
|
+
writable: true,
|
|
250
|
+
value: true
|
|
251
|
+
});
|
|
252
|
+
/**
|
|
253
|
+
* The name of the evaluation.
|
|
254
|
+
*/
|
|
255
|
+
Object.defineProperty(this, "evaluationName", {
|
|
256
|
+
enumerable: true,
|
|
257
|
+
configurable: true,
|
|
258
|
+
writable: true,
|
|
259
|
+
value: this.constructor.name
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Evaluate a trajectory.
|
|
264
|
+
* @return The evaluation result.
|
|
265
|
+
* @param args
|
|
266
|
+
* @param callOptions
|
|
267
|
+
* @param config
|
|
268
|
+
*/
|
|
269
|
+
evaluateAgentTrajectory(args, callOptions, config) {
|
|
270
|
+
this.checkEvaluationArgs(args.reference, args.input);
|
|
271
|
+
return this._evaluateAgentTrajectory(args, callOptions, config);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
exports.AgentTrajectoryEvaluator = AgentTrajectoryEvaluator;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { BaseChain, LLMChain, LLMChainInput } from "../chains/index.js";
|
|
2
|
+
import { AgentStep, ChainValues } from "../schema/index.js";
|
|
3
|
+
import { BaseLanguageModel } from "../base_language/index.js";
|
|
4
|
+
import { Callbacks } from "../callbacks/index.js";
|
|
5
|
+
import { BaseCallbackConfig } from "../callbacks/manager.js";
|
|
6
|
+
/**
|
|
7
|
+
* Base input for evaluators.
|
|
8
|
+
*/
|
|
9
|
+
export interface LLMEvalChainInput<T extends EvalOutputType = EvalOutputType, L extends BaseLanguageModel = BaseLanguageModel> extends LLMChainInput<T, L> {
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Compare two sets for equality
|
|
13
|
+
*
|
|
14
|
+
* @param xs
|
|
15
|
+
* @param ys
|
|
16
|
+
*/
|
|
17
|
+
export declare const eqSet: (xs: Set<string>, ys: Set<string>) => boolean;
|
|
18
|
+
/**
|
|
19
|
+
* The type of the output of an evaluation evaluator.
|
|
20
|
+
*/
|
|
21
|
+
export type EvalOutputType = Record<string, string | number | boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Base llm chain class for evaluators.
|
|
24
|
+
*/
|
|
25
|
+
export declare abstract class LLMEvalChain<T extends EvalOutputType = EvalOutputType, L extends BaseLanguageModel = BaseLanguageModel> extends LLMChain<T, L> {
|
|
26
|
+
requiresInput?: boolean;
|
|
27
|
+
requiresReference?: boolean;
|
|
28
|
+
skipInputWarning?: string;
|
|
29
|
+
skipReferenceWarning?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Check if the evaluation arguments are valid.
|
|
32
|
+
* @param reference The reference label.
|
|
33
|
+
* @param input The input string.
|
|
34
|
+
* @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.
|
|
35
|
+
*/
|
|
36
|
+
checkEvaluationArgs(reference?: string, input?: string): void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Base chain class for evaluators.
|
|
40
|
+
*/
|
|
41
|
+
export declare abstract class EvalChain<RunInput extends ChainValues = ChainValues, RunOutput extends ChainValues = ChainValues> extends BaseChain<RunInput, RunOutput> {
|
|
42
|
+
requiresInput?: boolean;
|
|
43
|
+
requiresReference?: boolean;
|
|
44
|
+
skipInputWarning?: string;
|
|
45
|
+
skipReferenceWarning?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Check if the evaluation arguments are valid.
|
|
48
|
+
* @param reference The reference label.
|
|
49
|
+
* @param input The input string.
|
|
50
|
+
* @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.
|
|
51
|
+
*/
|
|
52
|
+
checkEvaluationArgs(reference?: string, input?: string): void;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @field prediction The output string from the model.
|
|
56
|
+
* @field reference The expected output / reference string.
|
|
57
|
+
* @field input The input string.
|
|
58
|
+
*/
|
|
59
|
+
export interface StringEvaluatorArgs {
|
|
60
|
+
prediction: string;
|
|
61
|
+
reference?: string;
|
|
62
|
+
input?: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @field prediction The output string from the first model.
|
|
66
|
+
* @field predictionB The output string from the second model.
|
|
67
|
+
*/
|
|
68
|
+
export interface PairwiseStringEvaluatorArgs {
|
|
69
|
+
prediction: string;
|
|
70
|
+
predictionB: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @field The input string.
|
|
74
|
+
* @field prediction The output string from the first model.
|
|
75
|
+
* @field predictionB The output string from the second model.
|
|
76
|
+
* @field reference The expected output / reference string.
|
|
77
|
+
*/
|
|
78
|
+
export interface LLMPairwiseStringEvaluatorArgs {
|
|
79
|
+
input: string;
|
|
80
|
+
prediction: string;
|
|
81
|
+
predictionB: string;
|
|
82
|
+
reference?: string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Args for AgentTrajectoryEvaluator
|
|
86
|
+
* @field input The input to the agent.
|
|
87
|
+
* @field prediction The final predicted response.
|
|
88
|
+
* @field reference The reference answer.
|
|
89
|
+
* @field agentTrajectory The intermediate steps forming the agent trajectory.
|
|
90
|
+
*/
|
|
91
|
+
export interface LLMTrajectoryEvaluatorArgs {
|
|
92
|
+
input: string;
|
|
93
|
+
prediction: string;
|
|
94
|
+
reference?: string;
|
|
95
|
+
agentTrajectory: AgentStep[];
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Grade, tag, or otherwise evaluate predictions relative to their inputs
|
|
99
|
+
* and/or reference labels
|
|
100
|
+
*/
|
|
101
|
+
export declare abstract class LLMStringEvaluator<T extends EvalOutputType = EvalOutputType, L extends BaseLanguageModel = BaseLanguageModel> extends LLMEvalChain<T, L> {
|
|
102
|
+
/**
|
|
103
|
+
* The name of the evaluation.
|
|
104
|
+
*/
|
|
105
|
+
evaluationName?: string;
|
|
106
|
+
/**
|
|
107
|
+
* Evaluate Chain or LLM output, based on optional input and label.
|
|
108
|
+
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
|
|
109
|
+
* - score: the score of the evaluation, if applicable.
|
|
110
|
+
* - value: the string value of the evaluation, if applicable.
|
|
111
|
+
* - reasoning: the reasoning for the evaluation, if applicable.
|
|
112
|
+
* @param args
|
|
113
|
+
* @param callOptions
|
|
114
|
+
* @param config
|
|
115
|
+
*/
|
|
116
|
+
abstract _evaluateStrings(args: StringEvaluatorArgs, callOptions?: this["llm"]["CallOptions"], config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
117
|
+
/**
|
|
118
|
+
* Evaluate Chain or LLM output, based on optional input and label.
|
|
119
|
+
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
|
|
120
|
+
* - score: the score of the evaluation, if applicable.
|
|
121
|
+
* - value: the string value of the evaluation, if applicable.
|
|
122
|
+
* - reasoning: the reasoning for the evaluation, if applicable.
|
|
123
|
+
* @param args
|
|
124
|
+
* @param callOptions
|
|
125
|
+
* @param config
|
|
126
|
+
*/
|
|
127
|
+
evaluateStrings(args: StringEvaluatorArgs, callOptions?: this["llm"]["CallOptions"], config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Grade, tag, or otherwise evaluate predictions relative to their inputs
|
|
131
|
+
* and/or reference labels
|
|
132
|
+
*/
|
|
133
|
+
export declare abstract class StringEvaluator extends EvalChain {
|
|
134
|
+
/**
|
|
135
|
+
* The name of the evaluation.
|
|
136
|
+
*/
|
|
137
|
+
evaluationName?: string;
|
|
138
|
+
/**
|
|
139
|
+
* Evaluate Chain or LLM output, based on optional input and label.
|
|
140
|
+
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
|
|
141
|
+
* - score: the score of the evaluation, if applicable.
|
|
142
|
+
* - value: the string value of the evaluation, if applicable.
|
|
143
|
+
* - reasoning: the reasoning for the evaluation, if applicable.
|
|
144
|
+
* @param args
|
|
145
|
+
* @param config
|
|
146
|
+
*/
|
|
147
|
+
abstract _evaluateStrings(args: StringEvaluatorArgs, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
148
|
+
/**
|
|
149
|
+
* Evaluate Chain or LLM output, based on optional input and label.
|
|
150
|
+
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
|
|
151
|
+
* - score: the score of the evaluation, if applicable.
|
|
152
|
+
* - value: the string value of the evaluation, if applicable.
|
|
153
|
+
* - reasoning: the reasoning for the evaluation, if applicable.
|
|
154
|
+
* @param args
|
|
155
|
+
* @param config
|
|
156
|
+
*/
|
|
157
|
+
evaluateStrings(args: StringEvaluatorArgs, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Compare the output of two models (or two outputs of the same model).
|
|
161
|
+
*/
|
|
162
|
+
export declare abstract class PairwiseStringEvaluator extends EvalChain {
|
|
163
|
+
/**
|
|
164
|
+
* The name of the evaluation.
|
|
165
|
+
*/
|
|
166
|
+
evaluationName?: string;
|
|
167
|
+
/**
|
|
168
|
+
* Evaluate the output string pairs.
|
|
169
|
+
* @param args
|
|
170
|
+
* @param config
|
|
171
|
+
* @return A dictionary containing the preference, scores, and/or other information.
|
|
172
|
+
*/
|
|
173
|
+
abstract _evaluateStringPairs(args: PairwiseStringEvaluatorArgs, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
174
|
+
/**
|
|
175
|
+
* Evaluate the output string pairs.
|
|
176
|
+
* @param args
|
|
177
|
+
* @param config
|
|
178
|
+
* @return A dictionary containing the preference, scores, and/or other information.
|
|
179
|
+
*/
|
|
180
|
+
evaluateStringPairs(args: PairwiseStringEvaluatorArgs, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Compare the output of two models (or two outputs of the same model).
|
|
184
|
+
*/
|
|
185
|
+
export declare abstract class LLMPairwiseStringEvaluator extends LLMEvalChain {
|
|
186
|
+
/**
|
|
187
|
+
* The name of the evaluation.
|
|
188
|
+
*/
|
|
189
|
+
evaluationName?: string;
|
|
190
|
+
/**
|
|
191
|
+
* Evaluate the output string pairs.
|
|
192
|
+
* @param args
|
|
193
|
+
* @param callOptions
|
|
194
|
+
* @param config
|
|
195
|
+
* @return A dictionary containing the preference, scores, and/or other information.
|
|
196
|
+
*/
|
|
197
|
+
abstract _evaluateStringPairs(args: LLMPairwiseStringEvaluatorArgs, callOptions?: this["llm"]["CallOptions"], config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
198
|
+
/**
|
|
199
|
+
* Evaluate the output string pairs.
|
|
200
|
+
* @param args
|
|
201
|
+
* @param callOptions
|
|
202
|
+
* @param config
|
|
203
|
+
* @return A dictionary containing the preference, scores, and/or other information.
|
|
204
|
+
*/
|
|
205
|
+
evaluateStringPairs(args: LLMPairwiseStringEvaluatorArgs, callOptions?: this["llm"]["CallOptions"], config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Interface for evaluating agent trajectories.
|
|
209
|
+
*/
|
|
210
|
+
export declare abstract class AgentTrajectoryEvaluator extends LLMEvalChain {
|
|
211
|
+
requiresInput: boolean;
|
|
212
|
+
/**
|
|
213
|
+
* The name of the evaluation.
|
|
214
|
+
*/
|
|
215
|
+
evaluationName?: string;
|
|
216
|
+
/**
|
|
217
|
+
* Evaluate a trajectory.
|
|
218
|
+
* @return The evaluation result.
|
|
219
|
+
* @param args
|
|
220
|
+
* @param callOptions
|
|
221
|
+
* @param config
|
|
222
|
+
*/
|
|
223
|
+
abstract _evaluateAgentTrajectory(args: LLMTrajectoryEvaluatorArgs, callOptions?: this["llm"]["CallOptions"], config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
224
|
+
/**
|
|
225
|
+
* Evaluate a trajectory.
|
|
226
|
+
* @return The evaluation result.
|
|
227
|
+
* @param args
|
|
228
|
+
* @param callOptions
|
|
229
|
+
* @param config
|
|
230
|
+
*/
|
|
231
|
+
evaluateAgentTrajectory(args: LLMTrajectoryEvaluatorArgs, callOptions?: this["llm"]["CallOptions"], config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
|
|
232
|
+
}
|