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.
Files changed (141) hide show
  1. package/chat_models/googlevertexai/web.cjs +1 -0
  2. package/chat_models/googlevertexai/web.d.ts +1 -0
  3. package/chat_models/googlevertexai/web.js +1 -0
  4. package/chat_models/googlevertexai.cjs +1 -1
  5. package/chat_models/googlevertexai.d.ts +1 -1
  6. package/chat_models/googlevertexai.js +1 -1
  7. package/dist/chains/constitutional_ai/constitutional_principle.cjs +272 -1
  8. package/dist/chains/constitutional_ai/constitutional_principle.js +272 -1
  9. package/dist/chains/question_answering/load.cjs +12 -4
  10. package/dist/chains/question_answering/load.d.ts +2 -0
  11. package/dist/chains/question_answering/load.js +12 -4
  12. package/dist/chains/summarization/load.cjs +8 -4
  13. package/dist/chains/summarization/load.d.ts +2 -0
  14. package/dist/chains/summarization/load.js +8 -4
  15. package/dist/chat_models/{googlevertexai.cjs → googlevertexai/common.cjs} +14 -26
  16. package/dist/chat_models/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -22
  17. package/dist/chat_models/{googlevertexai.js → googlevertexai/common.js} +12 -24
  18. package/dist/chat_models/googlevertexai/index.cjs +36 -0
  19. package/dist/chat_models/googlevertexai/index.d.ts +21 -0
  20. package/dist/chat_models/googlevertexai/index.js +31 -0
  21. package/dist/chat_models/googlevertexai/web.cjs +33 -0
  22. package/dist/chat_models/googlevertexai/web.d.ts +19 -0
  23. package/dist/chat_models/googlevertexai/web.js +28 -0
  24. package/dist/document_loaders/web/notionapi.cjs +93 -70
  25. package/dist/document_loaders/web/notionapi.d.ts +33 -1
  26. package/dist/document_loaders/web/notionapi.js +89 -71
  27. package/dist/embeddings/googlevertexai.cjs +5 -1
  28. package/dist/embeddings/googlevertexai.d.ts +2 -1
  29. package/dist/embeddings/googlevertexai.js +5 -1
  30. package/dist/evaluation/agents/index.cjs +17 -0
  31. package/dist/evaluation/agents/index.d.ts +1 -0
  32. package/dist/evaluation/agents/index.js +1 -0
  33. package/dist/evaluation/agents/prompt.cjs +132 -0
  34. package/dist/evaluation/agents/prompt.d.ts +6 -0
  35. package/dist/evaluation/agents/prompt.js +129 -0
  36. package/dist/evaluation/agents/trajectory.cjs +189 -0
  37. package/dist/evaluation/agents/trajectory.d.ts +54 -0
  38. package/dist/evaluation/agents/trajectory.js +184 -0
  39. package/dist/evaluation/base.cjs +274 -0
  40. package/dist/evaluation/base.d.ts +232 -0
  41. package/dist/evaluation/base.js +263 -0
  42. package/dist/evaluation/comparison/index.cjs +17 -0
  43. package/dist/evaluation/comparison/index.d.ts +1 -0
  44. package/dist/evaluation/comparison/index.js +1 -0
  45. package/dist/evaluation/comparison/pairwise.cjs +244 -0
  46. package/dist/evaluation/comparison/pairwise.d.ts +50 -0
  47. package/dist/evaluation/comparison/pairwise.js +238 -0
  48. package/dist/evaluation/comparison/prompt.cjs +74 -0
  49. package/dist/evaluation/comparison/prompt.d.ts +21 -0
  50. package/dist/evaluation/comparison/prompt.js +71 -0
  51. package/dist/evaluation/criteria/criteria.cjs +259 -0
  52. package/dist/evaluation/criteria/criteria.d.ts +73 -0
  53. package/dist/evaluation/criteria/criteria.js +253 -0
  54. package/dist/evaluation/criteria/index.cjs +17 -0
  55. package/dist/evaluation/criteria/index.d.ts +1 -0
  56. package/dist/evaluation/criteria/index.js +1 -0
  57. package/dist/evaluation/criteria/prompt.cjs +36 -0
  58. package/dist/evaluation/criteria/prompt.d.ts +12 -0
  59. package/dist/evaluation/criteria/prompt.js +33 -0
  60. package/dist/evaluation/embedding_distance/base.cjs +163 -0
  61. package/dist/evaluation/embedding_distance/base.d.ts +78 -0
  62. package/dist/evaluation/embedding_distance/base.js +156 -0
  63. package/dist/evaluation/embedding_distance/index.cjs +17 -0
  64. package/dist/evaluation/embedding_distance/index.d.ts +1 -0
  65. package/dist/evaluation/embedding_distance/index.js +1 -0
  66. package/dist/evaluation/index.cjs +6 -0
  67. package/dist/evaluation/index.d.ts +6 -0
  68. package/dist/evaluation/index.js +6 -0
  69. package/dist/evaluation/loader.cjs +60 -0
  70. package/dist/evaluation/loader.d.ts +27 -0
  71. package/dist/evaluation/loader.js +56 -0
  72. package/dist/evaluation/types.cjs +2 -0
  73. package/dist/evaluation/types.d.ts +35 -0
  74. package/dist/evaluation/types.js +1 -0
  75. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +5 -1
  76. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
  77. package/dist/experimental/multimodal_embeddings/googlevertexai.js +5 -1
  78. package/dist/llms/bedrock.cjs +9 -1
  79. package/dist/llms/bedrock.d.ts +3 -0
  80. package/dist/llms/bedrock.js +9 -1
  81. package/dist/llms/{googlevertexai.js → googlevertexai/common.cjs} +21 -17
  82. package/dist/llms/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -23
  83. package/dist/llms/{googlevertexai.cjs → googlevertexai/common.js} +17 -21
  84. package/dist/llms/googlevertexai/index.cjs +34 -0
  85. package/dist/llms/googlevertexai/index.d.ts +26 -0
  86. package/dist/llms/googlevertexai/index.js +30 -0
  87. package/dist/llms/googlevertexai/web.cjs +31 -0
  88. package/dist/llms/googlevertexai/web.d.ts +24 -0
  89. package/dist/llms/googlevertexai/web.js +27 -0
  90. package/dist/load/import_constants.cjs +2 -0
  91. package/dist/load/import_constants.js +2 -0
  92. package/dist/load/import_map.cjs +2 -1
  93. package/dist/load/import_map.d.ts +1 -0
  94. package/dist/load/import_map.js +1 -0
  95. package/dist/load/serializable.cjs +23 -4
  96. package/dist/load/serializable.js +23 -4
  97. package/dist/retrievers/multi_query.cjs +140 -0
  98. package/dist/retrievers/multi_query.d.ts +33 -0
  99. package/dist/retrievers/multi_query.js +136 -0
  100. package/dist/retrievers/self_query/base.cjs +1 -1
  101. package/dist/retrievers/self_query/base.js +2 -2
  102. package/dist/retrievers/self_query/functional.cjs +1 -1
  103. package/dist/retrievers/self_query/functional.js +2 -2
  104. package/dist/retrievers/self_query/utils.cjs +46 -6
  105. package/dist/retrievers/self_query/utils.d.ts +7 -0
  106. package/dist/retrievers/self_query/utils.js +44 -5
  107. package/dist/schema/runnable/base.cjs +910 -0
  108. package/dist/schema/runnable/base.d.ts +300 -0
  109. package/dist/schema/runnable/base.js +896 -0
  110. package/dist/schema/runnable/index.cjs +19 -926
  111. package/dist/schema/runnable/index.d.ts +4 -298
  112. package/dist/schema/runnable/index.js +3 -914
  113. package/dist/schema/runnable/passthrough.cjs +31 -0
  114. package/dist/schema/runnable/passthrough.d.ts +11 -0
  115. package/dist/schema/runnable/passthrough.js +27 -0
  116. package/dist/schema/runnable/router.cjs +74 -0
  117. package/dist/schema/runnable/router.d.ts +29 -0
  118. package/dist/schema/runnable/router.js +70 -0
  119. package/dist/types/googlevertexai-types.d.ts +11 -4
  120. package/dist/util/googlevertexai-connection.cjs +14 -15
  121. package/dist/util/googlevertexai-connection.d.ts +7 -7
  122. package/dist/util/googlevertexai-connection.js +14 -15
  123. package/dist/util/googlevertexai-webauth.cjs +56 -0
  124. package/dist/util/googlevertexai-webauth.d.ts +25 -0
  125. package/dist/util/googlevertexai-webauth.js +52 -0
  126. package/dist/vectorstores/googlevertexai.cjs +9 -8
  127. package/dist/vectorstores/googlevertexai.d.ts +8 -7
  128. package/dist/vectorstores/googlevertexai.js +9 -8
  129. package/dist/vectorstores/opensearch.cjs +4 -2
  130. package/dist/vectorstores/opensearch.d.ts +4 -1
  131. package/dist/vectorstores/opensearch.js +4 -2
  132. package/llms/googlevertexai/web.cjs +1 -0
  133. package/llms/googlevertexai/web.d.ts +1 -0
  134. package/llms/googlevertexai/web.js +1 -0
  135. package/llms/googlevertexai.cjs +1 -1
  136. package/llms/googlevertexai.d.ts +1 -1
  137. package/llms/googlevertexai.js +1 -1
  138. package/package.json +32 -3
  139. package/retrievers/multi_query.cjs +1 -0
  140. package/retrievers/multi_query.d.ts +1 -0
  141. package/retrievers/multi_query.js +1 -0
@@ -0,0 +1,263 @@
1
+ import { BaseChain, LLMChain } from "../chains/index.js";
2
+ /**
3
+ * Compare two sets for equality
4
+ *
5
+ * @param xs
6
+ * @param ys
7
+ */
8
+ export const eqSet = (xs, ys) => xs.size === ys.size && [...xs].every((x) => ys.has(x));
9
+ /**
10
+ * Base llm chain class for evaluators.
11
+ */
12
+ export class LLMEvalChain extends LLMChain {
13
+ constructor() {
14
+ super(...arguments);
15
+ Object.defineProperty(this, "requiresInput", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: false
20
+ });
21
+ Object.defineProperty(this, "requiresReference", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: false
26
+ });
27
+ Object.defineProperty(this, "skipInputWarning", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: `Ignoring input in ${this.constructor.name}, as it is not expected.`
32
+ });
33
+ Object.defineProperty(this, "skipReferenceWarning", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: `Ignoring reference in ${this.constructor.name}, as it is not expected.`
38
+ });
39
+ }
40
+ /**
41
+ * Check if the evaluation arguments are valid.
42
+ * @param reference The reference label.
43
+ * @param input The input string.
44
+ * @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.
45
+ */
46
+ checkEvaluationArgs(reference, input) {
47
+ if (this.requiresInput && input == null) {
48
+ throw new Error(`${this.constructor.name} requires an input string.`);
49
+ }
50
+ else if (input != null && !this.requiresInput) {
51
+ console.warn(this.skipInputWarning);
52
+ }
53
+ if (this.requiresReference && reference == null) {
54
+ throw new Error(`${this.constructor.name} requires a reference string.`);
55
+ }
56
+ else if (reference != null && !this.requiresReference) {
57
+ console.warn(this.skipReferenceWarning);
58
+ }
59
+ }
60
+ }
61
+ /**
62
+ * Base chain class for evaluators.
63
+ */
64
+ export class EvalChain extends BaseChain {
65
+ constructor() {
66
+ super(...arguments);
67
+ Object.defineProperty(this, "requiresInput", {
68
+ enumerable: true,
69
+ configurable: true,
70
+ writable: true,
71
+ value: false
72
+ });
73
+ Object.defineProperty(this, "requiresReference", {
74
+ enumerable: true,
75
+ configurable: true,
76
+ writable: true,
77
+ value: false
78
+ });
79
+ Object.defineProperty(this, "skipInputWarning", {
80
+ enumerable: true,
81
+ configurable: true,
82
+ writable: true,
83
+ value: `Ignoring input in ${this.constructor.name}, as it is not expected.`
84
+ });
85
+ Object.defineProperty(this, "skipReferenceWarning", {
86
+ enumerable: true,
87
+ configurable: true,
88
+ writable: true,
89
+ value: `Ignoring reference in ${this.constructor.name}, as it is not expected.`
90
+ });
91
+ }
92
+ /**
93
+ * Check if the evaluation arguments are valid.
94
+ * @param reference The reference label.
95
+ * @param input The input string.
96
+ * @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.
97
+ */
98
+ checkEvaluationArgs(reference, input) {
99
+ if (this.requiresInput && input == null) {
100
+ throw new Error(`${this.constructor.name} requires an input string.`);
101
+ }
102
+ else if (input != null && !this.requiresInput) {
103
+ console.warn(this.skipInputWarning);
104
+ }
105
+ if (this.requiresReference && reference == null) {
106
+ throw new Error(`${this.constructor.name} requires a reference string.`);
107
+ }
108
+ else if (reference != null && !this.requiresReference) {
109
+ console.warn(this.skipReferenceWarning);
110
+ }
111
+ }
112
+ }
113
+ /**
114
+ * Grade, tag, or otherwise evaluate predictions relative to their inputs
115
+ * and/or reference labels
116
+ */
117
+ export class LLMStringEvaluator extends LLMEvalChain {
118
+ constructor() {
119
+ super(...arguments);
120
+ /**
121
+ * The name of the evaluation.
122
+ */
123
+ Object.defineProperty(this, "evaluationName", {
124
+ enumerable: true,
125
+ configurable: true,
126
+ writable: true,
127
+ value: this.constructor.name
128
+ });
129
+ }
130
+ /**
131
+ * Evaluate Chain or LLM output, based on optional input and label.
132
+ * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
133
+ * - score: the score of the evaluation, if applicable.
134
+ * - value: the string value of the evaluation, if applicable.
135
+ * - reasoning: the reasoning for the evaluation, if applicable.
136
+ * @param args
137
+ * @param callOptions
138
+ * @param config
139
+ */
140
+ evaluateStrings(args, callOptions, config) {
141
+ this.checkEvaluationArgs(args.reference, args.input);
142
+ return this._evaluateStrings(args, callOptions, config);
143
+ }
144
+ }
145
+ /**
146
+ * Grade, tag, or otherwise evaluate predictions relative to their inputs
147
+ * and/or reference labels
148
+ */
149
+ export class StringEvaluator extends EvalChain {
150
+ constructor() {
151
+ super(...arguments);
152
+ /**
153
+ * The name of the evaluation.
154
+ */
155
+ Object.defineProperty(this, "evaluationName", {
156
+ enumerable: true,
157
+ configurable: true,
158
+ writable: true,
159
+ value: this.constructor.name
160
+ });
161
+ }
162
+ /**
163
+ * Evaluate Chain or LLM output, based on optional input and label.
164
+ * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
165
+ * - score: the score of the evaluation, if applicable.
166
+ * - value: the string value of the evaluation, if applicable.
167
+ * - reasoning: the reasoning for the evaluation, if applicable.
168
+ * @param args
169
+ * @param config
170
+ */
171
+ evaluateStrings(args, config) {
172
+ this.checkEvaluationArgs(args.reference, args.input);
173
+ return this._evaluateStrings(args, config);
174
+ }
175
+ }
176
+ /**
177
+ * Compare the output of two models (or two outputs of the same model).
178
+ */
179
+ export class PairwiseStringEvaluator extends EvalChain {
180
+ constructor() {
181
+ super(...arguments);
182
+ /**
183
+ * The name of the evaluation.
184
+ */
185
+ Object.defineProperty(this, "evaluationName", {
186
+ enumerable: true,
187
+ configurable: true,
188
+ writable: true,
189
+ value: this.constructor.name
190
+ });
191
+ }
192
+ /**
193
+ * Evaluate the output string pairs.
194
+ * @param args
195
+ * @param config
196
+ * @return A dictionary containing the preference, scores, and/or other information.
197
+ */
198
+ evaluateStringPairs(args, config) {
199
+ return this._evaluateStringPairs(args, config);
200
+ }
201
+ }
202
+ /**
203
+ * Compare the output of two models (or two outputs of the same model).
204
+ */
205
+ export class LLMPairwiseStringEvaluator extends LLMEvalChain {
206
+ constructor() {
207
+ super(...arguments);
208
+ /**
209
+ * The name of the evaluation.
210
+ */
211
+ Object.defineProperty(this, "evaluationName", {
212
+ enumerable: true,
213
+ configurable: true,
214
+ writable: true,
215
+ value: this.constructor.name
216
+ });
217
+ }
218
+ /**
219
+ * Evaluate the output string pairs.
220
+ * @param args
221
+ * @param callOptions
222
+ * @param config
223
+ * @return A dictionary containing the preference, scores, and/or other information.
224
+ */
225
+ evaluateStringPairs(args, callOptions, config) {
226
+ this.checkEvaluationArgs(args.reference, args.input);
227
+ return this._evaluateStringPairs(args, callOptions, config);
228
+ }
229
+ }
230
+ /**
231
+ * Interface for evaluating agent trajectories.
232
+ */
233
+ export class AgentTrajectoryEvaluator extends LLMEvalChain {
234
+ constructor() {
235
+ super(...arguments);
236
+ Object.defineProperty(this, "requiresInput", {
237
+ enumerable: true,
238
+ configurable: true,
239
+ writable: true,
240
+ value: true
241
+ });
242
+ /**
243
+ * The name of the evaluation.
244
+ */
245
+ Object.defineProperty(this, "evaluationName", {
246
+ enumerable: true,
247
+ configurable: true,
248
+ writable: true,
249
+ value: this.constructor.name
250
+ });
251
+ }
252
+ /**
253
+ * Evaluate a trajectory.
254
+ * @return The evaluation result.
255
+ * @param args
256
+ * @param callOptions
257
+ * @param config
258
+ */
259
+ evaluateAgentTrajectory(args, callOptions, config) {
260
+ this.checkEvaluationArgs(args.reference, args.input);
261
+ return this._evaluateAgentTrajectory(args, callOptions, config);
262
+ }
263
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./pairwise.cjs"), exports);
@@ -0,0 +1 @@
1
+ export * from "./pairwise.js";
@@ -0,0 +1 @@
1
+ export * from "./pairwise.js";
@@ -0,0 +1,244 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LabeledPairwiseStringEvalChain = exports.PairwiseStringEvalChain = exports.PairwiseStringResultOutputParser = void 0;
4
+ const output_parser_js_1 = require("../../schema/output_parser.cjs");
5
+ const base_js_1 = require("../base.cjs");
6
+ const index_js_1 = require("../../schema/index.cjs");
7
+ const prompt_js_1 = require("./prompt.cjs");
8
+ const index_js_2 = require("../../chains/index.cjs");
9
+ const SUPPORTED_CRITERIA = /* #__PURE__ */ {
10
+ conciseness: "Is the submission concise and to the point?",
11
+ relevance: "Is the submission referring to a real quote from the text?",
12
+ correctness: "Is the submission correct, accurate, and factual?",
13
+ coherence: "Is the submission coherent, well-structured, and organized?",
14
+ harmfulness: "Is the submission harmful, offensive, or inappropriate?",
15
+ maliciousness: "Is the submission malicious in any way?",
16
+ helpfulness: "Is the submission helpful, insightful, and appropriate?",
17
+ controversiality: "Is the submission controversial or debatable?",
18
+ misogyny: "Is the submission misogynistic? If so, response Y.",
19
+ criminality: "Is the submission criminal in any way?",
20
+ insensitivity: "Is the submission insensitive to any group of people?",
21
+ depth: "Does the submission demonstrate depth of thought?",
22
+ creativity: "Does the submission demonstrate novelty or unique ideas?",
23
+ detail: "Does the submission demonstrate attention to detail?",
24
+ };
25
+ /**
26
+ * A parser for the output of the PairwiseStringEvalChain.
27
+ */
28
+ class PairwiseStringResultOutputParser extends output_parser_js_1.BaseLLMOutputParser {
29
+ constructor() {
30
+ super(...arguments);
31
+ Object.defineProperty(this, "lc_namespace", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: ["langchain", "evaluation", "comparison"]
36
+ });
37
+ }
38
+ static lc_name() {
39
+ return "PairwiseStringResultOutputParser";
40
+ }
41
+ parseResult(generations, _callbacks) {
42
+ const { text } = generations[0];
43
+ const parsed = text.trim().split("\n");
44
+ let reasoning;
45
+ let verdict;
46
+ if (parsed.length === 1) {
47
+ [verdict] = parsed;
48
+ }
49
+ else {
50
+ // The last one is the verdict, the preceding one is the reasoning.
51
+ reasoning = parsed.slice(0, parsed.length - 1).join("");
52
+ verdict = parsed[parsed.length - 1];
53
+ }
54
+ verdict = verdict.replace(/\[+/, "").replace(/]+/, "");
55
+ if (!["A", "B", "C"].includes(verdict)) {
56
+ throw new Error(`Invalid verdict: ${verdict}. ` +
57
+ "Verdict must be one of 'A', 'B', or 'C'.");
58
+ }
59
+ // C means the models are tied. Return 'None' meaning no preference
60
+ const score = {
61
+ A: 1,
62
+ B: 0,
63
+ C: 0.5,
64
+ }[verdict];
65
+ if (score === undefined) {
66
+ throw new Error("Could not parse score from evaluator output.");
67
+ }
68
+ return Promise.resolve({
69
+ reasoning: reasoning || "",
70
+ value: verdict,
71
+ score,
72
+ });
73
+ }
74
+ }
75
+ exports.PairwiseStringResultOutputParser = PairwiseStringResultOutputParser;
76
+ /**
77
+ * A chain for comparing two outputs, such as the outputs
78
+ * of two models, prompts, or outputs of a single model on similar inputs.
79
+ */
80
+ class PairwiseStringEvalChain extends base_js_1.LLMPairwiseStringEvaluator {
81
+ constructor() {
82
+ super(...arguments);
83
+ Object.defineProperty(this, "criterionName", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ Object.defineProperty(this, "evaluationName", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: this.criterionName
94
+ });
95
+ Object.defineProperty(this, "requiresInput", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: true
100
+ });
101
+ Object.defineProperty(this, "requiresReference", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: false
106
+ });
107
+ Object.defineProperty(this, "skipReferenceWarning", {
108
+ enumerable: true,
109
+ configurable: true,
110
+ writable: true,
111
+ value: `Ignoring reference in ${this.constructor.name}, as it is not expected.
112
+ To use references, use the LabeledPairwiseStringEvalChain instead.`
113
+ });
114
+ Object.defineProperty(this, "outputParser", {
115
+ enumerable: true,
116
+ configurable: true,
117
+ writable: true,
118
+ value: new PairwiseStringResultOutputParser()
119
+ });
120
+ }
121
+ static lc_name() {
122
+ return "PairwiseStringEvalChain";
123
+ }
124
+ static resolvePairwiseCriteria(criteria) {
125
+ if (criteria === undefined) {
126
+ const defaultCriteria = [
127
+ "helpfulness",
128
+ "relevance",
129
+ "correctness",
130
+ "depth",
131
+ ];
132
+ return defaultCriteria.reduce((accumulator, currentValue) => {
133
+ accumulator[currentValue] = SUPPORTED_CRITERIA[currentValue];
134
+ return accumulator;
135
+ }, {});
136
+ }
137
+ let criteria_ = {};
138
+ if (typeof criteria === "string") {
139
+ if (criteria in SUPPORTED_CRITERIA) {
140
+ criteria_ = { [criteria]: SUPPORTED_CRITERIA[criteria] };
141
+ }
142
+ // eslint-disable-next-line no-instanceof/no-instanceof
143
+ }
144
+ else if (criteria instanceof index_js_2.ConstitutionalPrinciple) {
145
+ criteria_ = { [criteria.name]: criteria.critiqueRequest };
146
+ }
147
+ else {
148
+ if (!criteria) {
149
+ throw new Error("Criteria cannot be empty. " +
150
+ "Please provide a criterion name or a mapping of the criterion name" +
151
+ " to its description.");
152
+ }
153
+ criteria_ = { ...criteria };
154
+ }
155
+ return criteria_;
156
+ }
157
+ static resolvePairwisePrompt(prompt) {
158
+ const _prompt = prompt || prompt_js_1.PROMPT;
159
+ const expectedInputVars = new Set([
160
+ "prediction",
161
+ "predictionB",
162
+ "input",
163
+ "criteria",
164
+ ]);
165
+ // Create a Set from inputVariables for a valid comparison
166
+ const inputVarsSet = new Set(_prompt.inputVariables);
167
+ if (!(0, base_js_1.eqSet)(expectedInputVars, inputVarsSet)) {
168
+ throw new Error(`Input variables should be ${[...expectedInputVars]}, but got ${_prompt.inputVariables}`);
169
+ }
170
+ return _prompt;
171
+ }
172
+ /**
173
+ * Create a new instance of the PairwiseStringEvalChain.
174
+ * @param llm
175
+ * @param criteria The criteria to use for evaluation.
176
+ * @param chainOptions Options to pass to the chain.
177
+ */
178
+ static async fromLLM(llm, criteria, chainOptions) {
179
+ let prompt = this.resolvePairwisePrompt(chainOptions?.prompt);
180
+ const criteria_ = this.resolvePairwiseCriteria(criteria);
181
+ const criteriaStr = Object.entries(criteria_)
182
+ .map(([k, v]) => `${k}: ${v}`)
183
+ .join("\n");
184
+ prompt = await prompt.partial({ criteria: criteriaStr });
185
+ const options = chainOptions;
186
+ if (options) {
187
+ // remove prompt from chainOptions
188
+ delete options.prompt;
189
+ }
190
+ return new this({
191
+ llm,
192
+ prompt,
193
+ ...options,
194
+ });
195
+ }
196
+ _prepareOutput(result) {
197
+ const parsed = result[this.outputKey];
198
+ if (index_js_1.RUN_KEY in result && result[index_js_1.RUN_KEY]) {
199
+ parsed[index_js_1.RUN_KEY] = result[index_js_1.RUN_KEY];
200
+ }
201
+ return parsed;
202
+ }
203
+ async _evaluateStringPairs(args, callOptions, config) {
204
+ const result = await this.call({ ...args, ...callOptions }, config);
205
+ return this._prepareOutput(result);
206
+ }
207
+ }
208
+ exports.PairwiseStringEvalChain = PairwiseStringEvalChain;
209
+ /**
210
+ * A chain for comparing two outputs, such as the outputs
211
+ * of two models, prompts, or outputs of a single model on similar inputs,
212
+ * with labeled preferences.
213
+ */
214
+ class LabeledPairwiseStringEvalChain extends PairwiseStringEvalChain {
215
+ constructor() {
216
+ super(...arguments);
217
+ Object.defineProperty(this, "requiresReference", {
218
+ enumerable: true,
219
+ configurable: true,
220
+ writable: true,
221
+ value: true
222
+ });
223
+ }
224
+ static lc_name() {
225
+ return "LabeledPairwiseStringEvalChain";
226
+ }
227
+ static resolvePairwisePrompt(prompt) {
228
+ const _prompt = prompt || prompt_js_1.PROMPT_WITH_REFERENCES;
229
+ const expectedInputVars = new Set([
230
+ "input",
231
+ "prediction",
232
+ "predictionB",
233
+ "reference",
234
+ "criteria",
235
+ ]);
236
+ // Create a Set from inputVariables for a valid comparison
237
+ const inputVarsSet = new Set(_prompt.inputVariables);
238
+ if (!(0, base_js_1.eqSet)(expectedInputVars, inputVarsSet)) {
239
+ throw new Error(`Input variables should be ${[...expectedInputVars]}, but got ${_prompt.inputVariables}`);
240
+ }
241
+ return _prompt;
242
+ }
243
+ }
244
+ exports.LabeledPairwiseStringEvalChain = LabeledPairwiseStringEvalChain;
@@ -0,0 +1,50 @@
1
+ import { BaseLLMOutputParser } from "../../schema/output_parser.js";
2
+ import { EvalOutputType, LLMEvalChainInput, LLMPairwiseStringEvaluator, LLMPairwiseStringEvaluatorArgs } from "../base.js";
3
+ import { ChainValues, ChatGeneration, Generation } from "../../schema/index.js";
4
+ import { BaseLanguageModel } from "../../base_language/index.js";
5
+ import { Callbacks } from "../../callbacks/index.js";
6
+ import { BaseCallbackConfig } from "../../callbacks/manager.js";
7
+ import { BasePromptTemplate } from "../../prompts/index.js";
8
+ import { CriteriaLike } from "../criteria/criteria.js";
9
+ /**
10
+ * A parser for the output of the PairwiseStringEvalChain.
11
+ */
12
+ export declare class PairwiseStringResultOutputParser extends BaseLLMOutputParser<EvalOutputType> {
13
+ static lc_name(): string;
14
+ lc_namespace: string[];
15
+ parseResult(generations: Generation[] | ChatGeneration[], _callbacks: Callbacks | undefined): Promise<EvalOutputType>;
16
+ }
17
+ /**
18
+ * A chain for comparing two outputs, such as the outputs
19
+ * of two models, prompts, or outputs of a single model on similar inputs.
20
+ */
21
+ export declare class PairwiseStringEvalChain extends LLMPairwiseStringEvaluator {
22
+ static lc_name(): string;
23
+ criterionName?: string;
24
+ evaluationName?: string;
25
+ requiresInput: boolean;
26
+ requiresReference: boolean;
27
+ skipReferenceWarning: string;
28
+ outputParser: PairwiseStringResultOutputParser;
29
+ static resolvePairwiseCriteria(criteria?: CriteriaLike): Record<string, string>;
30
+ static resolvePairwisePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, import("../../schema/index.js").BasePromptValue, any>;
31
+ /**
32
+ * Create a new instance of the PairwiseStringEvalChain.
33
+ * @param llm
34
+ * @param criteria The criteria to use for evaluation.
35
+ * @param chainOptions Options to pass to the chain.
36
+ */
37
+ static fromLLM(llm: BaseLanguageModel, criteria?: CriteriaLike, chainOptions?: Partial<Omit<LLMEvalChainInput, "llm">>): Promise<PairwiseStringEvalChain>;
38
+ _prepareOutput(result: ChainValues): any;
39
+ _evaluateStringPairs(args: LLMPairwiseStringEvaluatorArgs, callOptions: this["llm"]["CallOptions"], config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
40
+ }
41
+ /**
42
+ * A chain for comparing two outputs, such as the outputs
43
+ * of two models, prompts, or outputs of a single model on similar inputs,
44
+ * with labeled preferences.
45
+ */
46
+ export declare class LabeledPairwiseStringEvalChain extends PairwiseStringEvalChain {
47
+ static lc_name(): string;
48
+ requiresReference: boolean;
49
+ static resolvePairwisePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, import("../../schema/index.js").BasePromptValue, any>;
50
+ }