langchain 0.0.147 → 0.0.148

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 (110) 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/chat_models/{googlevertexai.cjs → googlevertexai/common.cjs} +14 -26
  10. package/dist/chat_models/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -22
  11. package/dist/chat_models/{googlevertexai.js → googlevertexai/common.js} +12 -24
  12. package/dist/chat_models/googlevertexai/index.cjs +36 -0
  13. package/dist/chat_models/googlevertexai/index.d.ts +21 -0
  14. package/dist/chat_models/googlevertexai/index.js +31 -0
  15. package/dist/chat_models/googlevertexai/web.cjs +33 -0
  16. package/dist/chat_models/googlevertexai/web.d.ts +19 -0
  17. package/dist/chat_models/googlevertexai/web.js +28 -0
  18. package/dist/document_loaders/web/notionapi.cjs +93 -70
  19. package/dist/document_loaders/web/notionapi.d.ts +33 -1
  20. package/dist/document_loaders/web/notionapi.js +89 -71
  21. package/dist/embeddings/googlevertexai.cjs +5 -1
  22. package/dist/embeddings/googlevertexai.d.ts +2 -1
  23. package/dist/embeddings/googlevertexai.js +5 -1
  24. package/dist/evaluation/agents/index.cjs +17 -0
  25. package/dist/evaluation/agents/index.d.ts +1 -0
  26. package/dist/evaluation/agents/index.js +1 -0
  27. package/dist/evaluation/agents/prompt.cjs +132 -0
  28. package/dist/evaluation/agents/prompt.d.ts +6 -0
  29. package/dist/evaluation/agents/prompt.js +129 -0
  30. package/dist/evaluation/agents/trajectory.cjs +189 -0
  31. package/dist/evaluation/agents/trajectory.d.ts +54 -0
  32. package/dist/evaluation/agents/trajectory.js +184 -0
  33. package/dist/evaluation/base.cjs +274 -0
  34. package/dist/evaluation/base.d.ts +232 -0
  35. package/dist/evaluation/base.js +263 -0
  36. package/dist/evaluation/comparison/index.cjs +17 -0
  37. package/dist/evaluation/comparison/index.d.ts +1 -0
  38. package/dist/evaluation/comparison/index.js +1 -0
  39. package/dist/evaluation/comparison/pairwise.cjs +244 -0
  40. package/dist/evaluation/comparison/pairwise.d.ts +50 -0
  41. package/dist/evaluation/comparison/pairwise.js +238 -0
  42. package/dist/evaluation/comparison/prompt.cjs +74 -0
  43. package/dist/evaluation/comparison/prompt.d.ts +21 -0
  44. package/dist/evaluation/comparison/prompt.js +71 -0
  45. package/dist/evaluation/criteria/criteria.cjs +259 -0
  46. package/dist/evaluation/criteria/criteria.d.ts +73 -0
  47. package/dist/evaluation/criteria/criteria.js +253 -0
  48. package/dist/evaluation/criteria/index.cjs +17 -0
  49. package/dist/evaluation/criteria/index.d.ts +1 -0
  50. package/dist/evaluation/criteria/index.js +1 -0
  51. package/dist/evaluation/criteria/prompt.cjs +36 -0
  52. package/dist/evaluation/criteria/prompt.d.ts +12 -0
  53. package/dist/evaluation/criteria/prompt.js +33 -0
  54. package/dist/evaluation/embedding_distance/base.cjs +163 -0
  55. package/dist/evaluation/embedding_distance/base.d.ts +78 -0
  56. package/dist/evaluation/embedding_distance/base.js +156 -0
  57. package/dist/evaluation/embedding_distance/index.cjs +17 -0
  58. package/dist/evaluation/embedding_distance/index.d.ts +1 -0
  59. package/dist/evaluation/embedding_distance/index.js +1 -0
  60. package/dist/evaluation/index.cjs +6 -0
  61. package/dist/evaluation/index.d.ts +6 -0
  62. package/dist/evaluation/index.js +6 -0
  63. package/dist/evaluation/loader.cjs +60 -0
  64. package/dist/evaluation/loader.d.ts +27 -0
  65. package/dist/evaluation/loader.js +56 -0
  66. package/dist/evaluation/types.cjs +2 -0
  67. package/dist/evaluation/types.d.ts +35 -0
  68. package/dist/evaluation/types.js +1 -0
  69. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +5 -1
  70. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
  71. package/dist/experimental/multimodal_embeddings/googlevertexai.js +5 -1
  72. package/dist/llms/{googlevertexai.js → googlevertexai/common.cjs} +21 -17
  73. package/dist/llms/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -23
  74. package/dist/llms/{googlevertexai.cjs → googlevertexai/common.js} +17 -21
  75. package/dist/llms/googlevertexai/index.cjs +34 -0
  76. package/dist/llms/googlevertexai/index.d.ts +26 -0
  77. package/dist/llms/googlevertexai/index.js +30 -0
  78. package/dist/llms/googlevertexai/web.cjs +31 -0
  79. package/dist/llms/googlevertexai/web.d.ts +24 -0
  80. package/dist/llms/googlevertexai/web.js +27 -0
  81. package/dist/load/import_constants.cjs +2 -0
  82. package/dist/load/import_constants.js +2 -0
  83. package/dist/load/import_map.cjs +2 -1
  84. package/dist/load/import_map.d.ts +1 -0
  85. package/dist/load/import_map.js +1 -0
  86. package/dist/load/serializable.cjs +23 -4
  87. package/dist/load/serializable.js +23 -4
  88. package/dist/retrievers/multi_query.cjs +140 -0
  89. package/dist/retrievers/multi_query.d.ts +33 -0
  90. package/dist/retrievers/multi_query.js +136 -0
  91. package/dist/types/googlevertexai-types.d.ts +11 -4
  92. package/dist/util/googlevertexai-connection.cjs +14 -15
  93. package/dist/util/googlevertexai-connection.d.ts +7 -7
  94. package/dist/util/googlevertexai-connection.js +14 -15
  95. package/dist/util/googlevertexai-webauth.cjs +56 -0
  96. package/dist/util/googlevertexai-webauth.d.ts +25 -0
  97. package/dist/util/googlevertexai-webauth.js +52 -0
  98. package/dist/vectorstores/googlevertexai.cjs +9 -8
  99. package/dist/vectorstores/googlevertexai.d.ts +8 -7
  100. package/dist/vectorstores/googlevertexai.js +9 -8
  101. package/llms/googlevertexai/web.cjs +1 -0
  102. package/llms/googlevertexai/web.d.ts +1 -0
  103. package/llms/googlevertexai/web.js +1 -0
  104. package/llms/googlevertexai.cjs +1 -1
  105. package/llms/googlevertexai.d.ts +1 -1
  106. package/llms/googlevertexai.js +1 -1
  107. package/package.json +32 -3
  108. package/retrievers/multi_query.cjs +1 -0
  109. package/retrievers/multi_query.d.ts +1 -0
  110. package/retrievers/multi_query.js +1 -0
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PROMPT_WITH_REFERENCES = exports.CRITERIA_PROMPT = void 0;
4
+ const index_js_1 = require("../../prompts/index.cjs");
5
+ const template = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:
6
+ [BEGIN DATA]
7
+ ***
8
+ [Input]: {input}
9
+ ***
10
+ [Submission]: {output}
11
+ ***
12
+ [Criteria]: {criteria}
13
+ ***
14
+ [END DATA]
15
+ Does the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character "Y" or "N" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;
16
+ exports.CRITERIA_PROMPT = new index_js_1.PromptTemplate({
17
+ inputVariables: ["input", "output", "criteria"],
18
+ template,
19
+ });
20
+ const referenceTemplate = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:
21
+ [BEGIN DATA]
22
+ ***
23
+ [Input]: {input}
24
+ ***
25
+ [Submission]: {output}
26
+ ***
27
+ [Criteria]: {criteria}
28
+ ***
29
+ [Reference]: {reference}
30
+ ***
31
+ [END DATA]
32
+ Does the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character "Y" or "N" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;
33
+ exports.PROMPT_WITH_REFERENCES = new index_js_1.PromptTemplate({
34
+ inputVariables: ["input", "output", "criteria", "reference"],
35
+ template: referenceTemplate,
36
+ });
@@ -0,0 +1,12 @@
1
+ import { PromptTemplate } from "../../prompts/index.js";
2
+ export declare const CRITERIA_PROMPT: PromptTemplate<{
3
+ output: any;
4
+ input: any;
5
+ criteria: any;
6
+ }, any>;
7
+ export declare const PROMPT_WITH_REFERENCES: PromptTemplate<{
8
+ output: any;
9
+ input: any;
10
+ criteria: any;
11
+ reference: any;
12
+ }, any>;
@@ -0,0 +1,33 @@
1
+ import { PromptTemplate } from "../../prompts/index.js";
2
+ const template = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:
3
+ [BEGIN DATA]
4
+ ***
5
+ [Input]: {input}
6
+ ***
7
+ [Submission]: {output}
8
+ ***
9
+ [Criteria]: {criteria}
10
+ ***
11
+ [END DATA]
12
+ Does the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character "Y" or "N" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;
13
+ export const CRITERIA_PROMPT = /* #__PURE__ */ new PromptTemplate({
14
+ inputVariables: ["input", "output", "criteria"],
15
+ template,
16
+ });
17
+ const referenceTemplate = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:
18
+ [BEGIN DATA]
19
+ ***
20
+ [Input]: {input}
21
+ ***
22
+ [Submission]: {output}
23
+ ***
24
+ [Criteria]: {criteria}
25
+ ***
26
+ [Reference]: {reference}
27
+ ***
28
+ [END DATA]
29
+ Does the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character "Y" or "N" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;
30
+ export const PROMPT_WITH_REFERENCES = /* #__PURE__ */ new PromptTemplate({
31
+ inputVariables: ["input", "output", "criteria", "reference"],
32
+ template: referenceTemplate,
33
+ });
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PairwiseEmbeddingDistanceEvalChain = exports.EmbeddingDistanceEvalChain = exports.computeEvaluationScore = exports.getDistanceCalculationFunction = void 0;
4
+ const ml_distance_1 = require("ml-distance");
5
+ const base_js_1 = require("../base.cjs");
6
+ const openai_js_1 = require("../../embeddings/openai.cjs");
7
+ /**
8
+ * Get the distance function for the given distance type.
9
+ * @param distance The distance type.
10
+ * @return The distance function.
11
+ */
12
+ function getDistanceCalculationFunction(distanceType) {
13
+ const distanceFunctions = {
14
+ cosine: (X, Y) => 1.0 - ml_distance_1.similarity.cosine(X, Y),
15
+ euclidean: ml_distance_1.distance.euclidean,
16
+ manhattan: ml_distance_1.distance.manhattan,
17
+ chebyshev: ml_distance_1.distance.chebyshev,
18
+ };
19
+ return distanceFunctions[distanceType];
20
+ }
21
+ exports.getDistanceCalculationFunction = getDistanceCalculationFunction;
22
+ /**
23
+ * Compute the score based on the distance metric.
24
+ * @param vectors The input vectors.
25
+ * @param distanceMetric The distance metric.
26
+ * @return The computed score.
27
+ */
28
+ function computeEvaluationScore(vectors, distanceMetric) {
29
+ const metricFunction = getDistanceCalculationFunction(distanceMetric);
30
+ return metricFunction(vectors[0], vectors[1]);
31
+ }
32
+ exports.computeEvaluationScore = computeEvaluationScore;
33
+ /**
34
+ * Use embedding distances to score semantic difference between
35
+ * a prediction and reference.
36
+ */
37
+ class EmbeddingDistanceEvalChain extends base_js_1.StringEvaluator {
38
+ constructor(fields) {
39
+ super();
40
+ Object.defineProperty(this, "requiresReference", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: true
45
+ });
46
+ Object.defineProperty(this, "requiresInput", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: false
51
+ });
52
+ Object.defineProperty(this, "outputKey", {
53
+ enumerable: true,
54
+ configurable: true,
55
+ writable: true,
56
+ value: "score"
57
+ });
58
+ Object.defineProperty(this, "embedding", {
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true,
62
+ value: void 0
63
+ });
64
+ Object.defineProperty(this, "distanceMetric", {
65
+ enumerable: true,
66
+ configurable: true,
67
+ writable: true,
68
+ value: "cosine"
69
+ });
70
+ this.embedding = fields?.embedding || new openai_js_1.OpenAIEmbeddings();
71
+ this.distanceMetric = fields?.distanceMetric || "cosine";
72
+ }
73
+ _chainType() {
74
+ return `embedding_${this.distanceMetric}_distance`;
75
+ }
76
+ async _evaluateStrings(args, config) {
77
+ const result = await this.call(args, config);
78
+ return { [this.outputKey]: result[this.outputKey] };
79
+ }
80
+ get inputKeys() {
81
+ return ["reference", "prediction"];
82
+ }
83
+ get outputKeys() {
84
+ return [this.outputKey];
85
+ }
86
+ async _call(values, _runManager) {
87
+ const { prediction, reference } = values;
88
+ if (!this.embedding)
89
+ throw new Error("Embedding is undefined");
90
+ const vectors = await this.embedding.embedDocuments([
91
+ prediction,
92
+ reference,
93
+ ]);
94
+ const score = computeEvaluationScore(vectors, this.distanceMetric);
95
+ return { [this.outputKey]: score };
96
+ }
97
+ }
98
+ exports.EmbeddingDistanceEvalChain = EmbeddingDistanceEvalChain;
99
+ /**
100
+ * Use embedding distances to score semantic difference between two predictions.
101
+ */
102
+ class PairwiseEmbeddingDistanceEvalChain extends base_js_1.PairwiseStringEvaluator {
103
+ constructor(fields) {
104
+ super();
105
+ Object.defineProperty(this, "requiresReference", {
106
+ enumerable: true,
107
+ configurable: true,
108
+ writable: true,
109
+ value: false
110
+ });
111
+ Object.defineProperty(this, "requiresInput", {
112
+ enumerable: true,
113
+ configurable: true,
114
+ writable: true,
115
+ value: false
116
+ });
117
+ Object.defineProperty(this, "outputKey", {
118
+ enumerable: true,
119
+ configurable: true,
120
+ writable: true,
121
+ value: "score"
122
+ });
123
+ Object.defineProperty(this, "embedding", {
124
+ enumerable: true,
125
+ configurable: true,
126
+ writable: true,
127
+ value: void 0
128
+ });
129
+ Object.defineProperty(this, "distanceMetric", {
130
+ enumerable: true,
131
+ configurable: true,
132
+ writable: true,
133
+ value: "cosine"
134
+ });
135
+ this.embedding = fields?.embedding || new openai_js_1.OpenAIEmbeddings();
136
+ this.distanceMetric = fields?.distanceMetric || "cosine";
137
+ }
138
+ _chainType() {
139
+ return `pairwise_embedding_${this.distanceMetric}_distance`;
140
+ }
141
+ async _evaluateStringPairs(args, config) {
142
+ const result = await this.call(args, config);
143
+ return { [this.outputKey]: result[this.outputKey] };
144
+ }
145
+ get inputKeys() {
146
+ return ["prediction", "predictionB"];
147
+ }
148
+ get outputKeys() {
149
+ return [this.outputKey];
150
+ }
151
+ async _call(values, _runManager) {
152
+ const { prediction, predictionB } = values;
153
+ if (!this.embedding)
154
+ throw new Error("Embedding is undefined");
155
+ const vectors = await this.embedding.embedDocuments([
156
+ prediction,
157
+ predictionB,
158
+ ]);
159
+ const score = computeEvaluationScore(vectors, this.distanceMetric);
160
+ return { [this.outputKey]: score };
161
+ }
162
+ }
163
+ exports.PairwiseEmbeddingDistanceEvalChain = PairwiseEmbeddingDistanceEvalChain;
@@ -0,0 +1,78 @@
1
+ import { PairwiseStringEvaluator, PairwiseStringEvaluatorArgs, StringEvaluator, StringEvaluatorArgs } from "../base.js";
2
+ import { ChainValues } from "../../schema/index.js";
3
+ import { CallbackManagerForChainRun, Callbacks } from "../../callbacks/index.js";
4
+ import { BaseCallbackConfig } from "../../callbacks/manager.js";
5
+ import { Embeddings } from "../../embeddings/base.js";
6
+ /**
7
+ *
8
+ * Embedding Distance Metric.
9
+ *
10
+ * COSINE: Cosine distance metric.
11
+ * EUCLIDEAN: Euclidean distance metric.
12
+ * MANHATTAN: Manhattan distance metric.
13
+ * CHEBYSHEV: Chebyshev distance metric.
14
+ * HAMMING: Hamming distance metric.
15
+ */
16
+ export type EmbeddingDistanceType = "cosine" | "euclidean" | "manhattan" | "chebyshev";
17
+ /**
18
+ * Embedding Distance Evaluation Chain Input.
19
+ */
20
+ export interface EmbeddingDistanceEvalChainInput {
21
+ /**
22
+ * The embedding objects to vectorize the outputs.
23
+ */
24
+ embedding?: Embeddings;
25
+ /**
26
+ * The distance metric to use
27
+ * for comparing the embeddings.
28
+ */
29
+ distanceMetric?: EmbeddingDistanceType;
30
+ }
31
+ type VectorFunction = (xVector: number[], yVector: number[]) => number;
32
+ /**
33
+ * Get the distance function for the given distance type.
34
+ * @param distance The distance type.
35
+ * @return The distance function.
36
+ */
37
+ export declare function getDistanceCalculationFunction(distanceType: EmbeddingDistanceType): VectorFunction;
38
+ /**
39
+ * Compute the score based on the distance metric.
40
+ * @param vectors The input vectors.
41
+ * @param distanceMetric The distance metric.
42
+ * @return The computed score.
43
+ */
44
+ export declare function computeEvaluationScore(vectors: number[][], distanceMetric: EmbeddingDistanceType): number;
45
+ /**
46
+ * Use embedding distances to score semantic difference between
47
+ * a prediction and reference.
48
+ */
49
+ export declare class EmbeddingDistanceEvalChain extends StringEvaluator implements EmbeddingDistanceEvalChainInput {
50
+ requiresReference: boolean;
51
+ requiresInput: boolean;
52
+ outputKey: string;
53
+ embedding?: Embeddings;
54
+ distanceMetric: EmbeddingDistanceType;
55
+ constructor(fields: EmbeddingDistanceEvalChainInput);
56
+ _chainType(): "embedding_cosine_distance" | "embedding_euclidean_distance" | "embedding_manhattan_distance" | "embedding_chebyshev_distance";
57
+ _evaluateStrings(args: StringEvaluatorArgs, config: Callbacks | BaseCallbackConfig | undefined): Promise<ChainValues>;
58
+ get inputKeys(): string[];
59
+ get outputKeys(): string[];
60
+ _call(values: ChainValues, _runManager: CallbackManagerForChainRun | undefined): Promise<ChainValues>;
61
+ }
62
+ /**
63
+ * Use embedding distances to score semantic difference between two predictions.
64
+ */
65
+ export declare class PairwiseEmbeddingDistanceEvalChain extends PairwiseStringEvaluator implements EmbeddingDistanceEvalChainInput {
66
+ requiresReference: boolean;
67
+ requiresInput: boolean;
68
+ outputKey: string;
69
+ embedding?: Embeddings;
70
+ distanceMetric: EmbeddingDistanceType;
71
+ constructor(fields: EmbeddingDistanceEvalChainInput);
72
+ _chainType(): "pairwise_embedding_cosine_distance" | "pairwise_embedding_euclidean_distance" | "pairwise_embedding_manhattan_distance" | "pairwise_embedding_chebyshev_distance";
73
+ _evaluateStringPairs(args: PairwiseStringEvaluatorArgs, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
74
+ get inputKeys(): string[];
75
+ get outputKeys(): string[];
76
+ _call(values: ChainValues, _runManager: CallbackManagerForChainRun | undefined): Promise<ChainValues>;
77
+ }
78
+ export {};
@@ -0,0 +1,156 @@
1
+ import { distance, similarity } from "ml-distance";
2
+ import { PairwiseStringEvaluator, StringEvaluator, } from "../base.js";
3
+ import { OpenAIEmbeddings } from "../../embeddings/openai.js";
4
+ /**
5
+ * Get the distance function for the given distance type.
6
+ * @param distance The distance type.
7
+ * @return The distance function.
8
+ */
9
+ export function getDistanceCalculationFunction(distanceType) {
10
+ const distanceFunctions = {
11
+ cosine: (X, Y) => 1.0 - similarity.cosine(X, Y),
12
+ euclidean: distance.euclidean,
13
+ manhattan: distance.manhattan,
14
+ chebyshev: distance.chebyshev,
15
+ };
16
+ return distanceFunctions[distanceType];
17
+ }
18
+ /**
19
+ * Compute the score based on the distance metric.
20
+ * @param vectors The input vectors.
21
+ * @param distanceMetric The distance metric.
22
+ * @return The computed score.
23
+ */
24
+ export function computeEvaluationScore(vectors, distanceMetric) {
25
+ const metricFunction = getDistanceCalculationFunction(distanceMetric);
26
+ return metricFunction(vectors[0], vectors[1]);
27
+ }
28
+ /**
29
+ * Use embedding distances to score semantic difference between
30
+ * a prediction and reference.
31
+ */
32
+ export class EmbeddingDistanceEvalChain extends StringEvaluator {
33
+ constructor(fields) {
34
+ super();
35
+ Object.defineProperty(this, "requiresReference", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: true
40
+ });
41
+ Object.defineProperty(this, "requiresInput", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: false
46
+ });
47
+ Object.defineProperty(this, "outputKey", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: "score"
52
+ });
53
+ Object.defineProperty(this, "embedding", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "distanceMetric", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: "cosine"
64
+ });
65
+ this.embedding = fields?.embedding || new OpenAIEmbeddings();
66
+ this.distanceMetric = fields?.distanceMetric || "cosine";
67
+ }
68
+ _chainType() {
69
+ return `embedding_${this.distanceMetric}_distance`;
70
+ }
71
+ async _evaluateStrings(args, config) {
72
+ const result = await this.call(args, config);
73
+ return { [this.outputKey]: result[this.outputKey] };
74
+ }
75
+ get inputKeys() {
76
+ return ["reference", "prediction"];
77
+ }
78
+ get outputKeys() {
79
+ return [this.outputKey];
80
+ }
81
+ async _call(values, _runManager) {
82
+ const { prediction, reference } = values;
83
+ if (!this.embedding)
84
+ throw new Error("Embedding is undefined");
85
+ const vectors = await this.embedding.embedDocuments([
86
+ prediction,
87
+ reference,
88
+ ]);
89
+ const score = computeEvaluationScore(vectors, this.distanceMetric);
90
+ return { [this.outputKey]: score };
91
+ }
92
+ }
93
+ /**
94
+ * Use embedding distances to score semantic difference between two predictions.
95
+ */
96
+ export class PairwiseEmbeddingDistanceEvalChain extends PairwiseStringEvaluator {
97
+ constructor(fields) {
98
+ super();
99
+ Object.defineProperty(this, "requiresReference", {
100
+ enumerable: true,
101
+ configurable: true,
102
+ writable: true,
103
+ value: false
104
+ });
105
+ Object.defineProperty(this, "requiresInput", {
106
+ enumerable: true,
107
+ configurable: true,
108
+ writable: true,
109
+ value: false
110
+ });
111
+ Object.defineProperty(this, "outputKey", {
112
+ enumerable: true,
113
+ configurable: true,
114
+ writable: true,
115
+ value: "score"
116
+ });
117
+ Object.defineProperty(this, "embedding", {
118
+ enumerable: true,
119
+ configurable: true,
120
+ writable: true,
121
+ value: void 0
122
+ });
123
+ Object.defineProperty(this, "distanceMetric", {
124
+ enumerable: true,
125
+ configurable: true,
126
+ writable: true,
127
+ value: "cosine"
128
+ });
129
+ this.embedding = fields?.embedding || new OpenAIEmbeddings();
130
+ this.distanceMetric = fields?.distanceMetric || "cosine";
131
+ }
132
+ _chainType() {
133
+ return `pairwise_embedding_${this.distanceMetric}_distance`;
134
+ }
135
+ async _evaluateStringPairs(args, config) {
136
+ const result = await this.call(args, config);
137
+ return { [this.outputKey]: result[this.outputKey] };
138
+ }
139
+ get inputKeys() {
140
+ return ["prediction", "predictionB"];
141
+ }
142
+ get outputKeys() {
143
+ return [this.outputKey];
144
+ }
145
+ async _call(values, _runManager) {
146
+ const { prediction, predictionB } = values;
147
+ if (!this.embedding)
148
+ throw new Error("Embedding is undefined");
149
+ const vectors = await this.embedding.embedDocuments([
150
+ prediction,
151
+ predictionB,
152
+ ]);
153
+ const score = computeEvaluationScore(vectors, this.distanceMetric);
154
+ return { [this.outputKey]: score };
155
+ }
156
+ }
@@ -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("./base.cjs"), exports);
@@ -0,0 +1 @@
1
+ export * from "./base.js";
@@ -0,0 +1 @@
1
+ export * from "./base.js";
@@ -15,3 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./qa/index.cjs"), exports);
18
+ __exportStar(require("./criteria/index.cjs"), exports);
19
+ __exportStar(require("./agents/index.cjs"), exports);
20
+ __exportStar(require("./embedding_distance/index.cjs"), exports);
21
+ __exportStar(require("./comparison/index.cjs"), exports);
22
+ __exportStar(require("./types.cjs"), exports);
23
+ __exportStar(require("./loader.cjs"), exports);
@@ -1 +1,7 @@
1
1
  export * from "./qa/index.js";
2
+ export * from "./criteria/index.js";
3
+ export * from "./agents/index.js";
4
+ export * from "./embedding_distance/index.js";
5
+ export * from "./comparison/index.js";
6
+ export * from "./types.js";
7
+ export * from "./loader.js";
@@ -1 +1,7 @@
1
1
  export * from "./qa/index.js";
2
+ export * from "./criteria/index.js";
3
+ export * from "./agents/index.js";
4
+ export * from "./embedding_distance/index.js";
5
+ export * from "./comparison/index.js";
6
+ export * from "./types.js";
7
+ export * from "./loader.js";
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadEvaluator = void 0;
4
+ const index_js_1 = require("./criteria/index.cjs");
5
+ const openai_js_1 = require("../chat_models/openai.cjs");
6
+ const index_js_2 = require("./comparison/index.cjs");
7
+ const index_js_3 = require("./embedding_distance/index.cjs");
8
+ const index_js_4 = require("./agents/index.cjs");
9
+ const base_js_1 = require("../chat_models/base.cjs");
10
+ /**
11
+ * Load the requested evaluation chain specified by a string
12
+ * @param type The type of evaluator to load.
13
+ * @param options
14
+ * - llm The language model to use for the evaluator.
15
+ * - criteria The criteria to use for the evaluator.
16
+ * - agentTools A list of tools available to the agent,for TrajectoryEvalChain.
17
+ */
18
+ async function loadEvaluator(type, options) {
19
+ const { llm, chainOptions, criteria, agentTools } = options || {};
20
+ const llm_ = llm ??
21
+ new openai_js_1.ChatOpenAI({
22
+ modelName: "gpt-4",
23
+ temperature: 0.0,
24
+ });
25
+ let evaluator;
26
+ switch (type) {
27
+ case "criteria":
28
+ evaluator = await index_js_1.CriteriaEvalChain.fromLLM(llm_, criteria, chainOptions);
29
+ break;
30
+ case "labeled_criteria":
31
+ evaluator = await index_js_1.LabeledCriteriaEvalChain.fromLLM(llm_, criteria, chainOptions);
32
+ break;
33
+ case "pairwise_string":
34
+ evaluator = await index_js_2.PairwiseStringEvalChain.fromLLM(llm_, criteria, chainOptions);
35
+ break;
36
+ case "labeled_pairwise_string":
37
+ evaluator = await index_js_2.LabeledPairwiseStringEvalChain.fromLLM(llm_, criteria, chainOptions);
38
+ break;
39
+ case "trajectory":
40
+ // eslint-disable-next-line no-instanceof/no-instanceof
41
+ if (!(llm_ instanceof base_js_1.BaseChatModel)) {
42
+ throw new Error("LLM must be an instance of a base chat model.");
43
+ }
44
+ evaluator = await index_js_4.TrajectoryEvalChain.fromLLM(llm_, agentTools, chainOptions);
45
+ break;
46
+ case "embedding_distance":
47
+ evaluator = new index_js_3.EmbeddingDistanceEvalChain({
48
+ embedding: options?.embedding,
49
+ distanceMetric: options?.distanceMetric,
50
+ });
51
+ break;
52
+ case "pairwise_embedding_distance":
53
+ evaluator = new index_js_3.PairwiseEmbeddingDistanceEvalChain({});
54
+ break;
55
+ default:
56
+ throw new Error(`Unknown type: ${type}`);
57
+ }
58
+ return evaluator;
59
+ }
60
+ exports.loadEvaluator = loadEvaluator;
@@ -0,0 +1,27 @@
1
+ import { BaseLanguageModel } from "../base_language/index.js";
2
+ import { CriteriaLike } from "./criteria/index.js";
3
+ import type { EvaluatorType } from "./types.js";
4
+ import { StructuredTool } from "../tools/index.js";
5
+ import { LLMEvalChainInput } from "./base.js";
6
+ import { EmbeddingDistanceEvalChainInput } from "./embedding_distance/index.js";
7
+ export type LoadEvaluatorOptions = EmbeddingDistanceEvalChainInput & {
8
+ llm?: BaseLanguageModel;
9
+ chainOptions?: Partial<Omit<LLMEvalChainInput, "llm">>;
10
+ /**
11
+ * The criteria to use for the evaluator.
12
+ */
13
+ criteria?: CriteriaLike;
14
+ /**
15
+ * A list of tools available to the agent, for TrajectoryEvalChain.
16
+ */
17
+ agentTools?: StructuredTool[];
18
+ };
19
+ /**
20
+ * Load the requested evaluation chain specified by a string
21
+ * @param type The type of evaluator to load.
22
+ * @param options
23
+ * - llm The language model to use for the evaluator.
24
+ * - criteria The criteria to use for the evaluator.
25
+ * - agentTools A list of tools available to the agent,for TrajectoryEvalChain.
26
+ */
27
+ export declare function loadEvaluator<T extends keyof EvaluatorType>(type: T, options?: LoadEvaluatorOptions): Promise<EvaluatorType[T]>;