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,132 @@
1
+ "use strict";
2
+ /**
3
+ * Prompt for trajectory evaluation chain.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TOOL_FREE_EVAL_CHAT_PROMPT = exports.EVAL_CHAT_PROMPT = void 0;
7
+ const index_js_1 = require("../../prompts/index.cjs");
8
+ const EVAL_TEMPLATE = `An AI language model has been given access to the following set of tools to help answer a user's question.
9
+
10
+ The tools given to the AI model are:
11
+ [TOOL_DESCRIPTIONS]
12
+ {toolDescriptions}
13
+ [END_TOOL_DESCRIPTIONS]
14
+
15
+ The question the human asked the AI model was:
16
+ [QUESTION]
17
+ {question}
18
+ [END_QUESTION]{reference}
19
+
20
+ The AI language model decided to use the following set of tools to answer the question:
21
+ [AGENT_TRAJECTORY]
22
+ {agentTrajectory}
23
+ [END_AGENT_TRAJECTORY]
24
+
25
+ The AI language model's final answer to the question was:
26
+ [RESPONSE]
27
+ {answer}
28
+ [END_RESPONSE]
29
+
30
+ Let's do a detailed evaluation of the AI language model's answer step by step.
31
+
32
+ We consider the following criteria before giving a score from 1 to 5:
33
+
34
+ i. Is the final answer helpful?
35
+ ii. Does the AI language use a logical sequence of tools to answer the question?
36
+ iii. Does the AI language model use the tools in a helpful way?
37
+ iv. Does the AI language model use too many steps to answer the question?
38
+ v. Are the appropriate tools used to answer the question?`;
39
+ const EXAMPLE_INPUT = `An AI language model has been given access to the following set of tools to help answer a user's question.
40
+
41
+ The tools given to the AI model are:
42
+ [TOOL_DESCRIPTIONS]
43
+ Tool 1:
44
+ Name: Search
45
+ Description: useful for when you need to ask with search
46
+
47
+ Tool 2:
48
+ Name: Lookup
49
+ Description: useful for when you need to ask with lookup
50
+
51
+ Tool 3:
52
+ Name: Calculator
53
+ Description: useful for doing calculations
54
+
55
+ Tool 4:
56
+ Name: Search the Web (SerpAPI)
57
+ Description: useful for when you need to answer questions about current events
58
+ [END_TOOL_DESCRIPTIONS]
59
+
60
+ The question the human asked the AI model was: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?
61
+
62
+ The AI language model decided to use the following set of tools to answer the question:
63
+ [AGENT_TRAJECTORY]
64
+ Step 1:
65
+ Tool used: Search the Web (SerpAPI)
66
+ Tool input: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?
67
+ Tool output: The Statue of Liberty was given to the United States by France, as a symbol of the two countries' friendship. It was erected atop an American-designed ...
68
+ [END_AGENT_TRAJECTORY]
69
+
70
+ [RESPONSE]
71
+ The AI language model's final answer to the question was: There are different ways to measure the length of the United States, but if we use the distance between the Statue of Liberty and the westernmost point of the contiguous United States (Cape Alava, Washington), which is approximately 2,857 miles (4,596 km), and assume that the Statue of Liberty is 305 feet (93 meters) tall, then the statue would stretch across the United States approximately 17.5 times if laid end to end.
72
+ [END_RESPONSE]
73
+
74
+ Let's do a detailed evaluation of the AI language model's answer step by step.
75
+
76
+ We consider the following criteria before giving a score from 1 to 5:
77
+
78
+ i. Is the final answer helpful?
79
+ ii. Does the AI language use a logical sequence of tools to answer the question?
80
+ iii. Does the AI language model use the tools in a helpful way?
81
+ iv. Does the AI language model use too many steps to answer the question?
82
+ v. Are the appropriate tools used to answer the question?`;
83
+ const EXAMPLE_OUTPUT = `First, let's evaluate the final answer. The final uses good reasoning but is wrong. 2,857 divided by 305 is not 17.5.\
84
+ The model should have used the calculator to figure this out. Second does the model use a logical sequence of tools to answer the question?\
85
+ The way model uses the search is not helpful. The model should have used the search tool to figure the width of the US or the height of the statue.\
86
+ The model didn't use the calculator tool and gave an incorrect answer. The search API should be used for current events or specific questions.\
87
+ The tools were not used in a helpful way. The model did not use too many steps to answer the question.\
88
+ The model did not use the appropriate tools to answer the question.\
89
+
90
+ Judgment: Given the good reasoning in the final answer but otherwise poor performance, we give the model a score of 2.
91
+
92
+ Score: 2`;
93
+ exports.EVAL_CHAT_PROMPT =
94
+ /* #__PURE__ */ index_js_1.ChatPromptTemplate.fromPromptMessages([
95
+ /* #__PURE__ */ index_js_1.SystemMessagePromptTemplate.fromTemplate("You are a helpful assistant that evaluates language models."),
96
+ /* #__PURE__ */ index_js_1.HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),
97
+ /* #__PURE__ */ index_js_1.AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),
98
+ /* #__PURE__ */ index_js_1.HumanMessagePromptTemplate.fromTemplate(EVAL_TEMPLATE),
99
+ ]);
100
+ const TOOL_FREE_EVAL_TEMPLATE = `An AI language model has been given access to a set of tools to help answer a user's question.
101
+
102
+ The question the human asked the AI model was:
103
+ [QUESTION]
104
+ {question}
105
+ [END_QUESTION]{reference}
106
+
107
+ The AI language model decided to use the following set of tools to answer the question:
108
+ [AGENT_TRAJECTORY]
109
+ {agentTrajectory}
110
+ [END_AGENT_TRAJECTORY]
111
+
112
+ The AI language model's final answer to the question was:
113
+ [RESPONSE]
114
+ {answer}
115
+ [END_RESPONSE]
116
+
117
+ Let's do a detailed evaluation of the AI language model's answer step by step.
118
+
119
+ We consider the following criteria before giving a score from 1 to 5:
120
+
121
+ i. Is the final answer helpful?
122
+ ii. Does the AI language use a logical sequence of tools to answer the question?
123
+ iii. Does the AI language model use the tools in a helpful way?
124
+ iv. Does the AI language model use too many steps to answer the question?
125
+ v. Are the appropriate tools used to answer the question?`;
126
+ exports.TOOL_FREE_EVAL_CHAT_PROMPT =
127
+ /* #__PURE__ */ index_js_1.ChatPromptTemplate.fromPromptMessages([
128
+ /* #__PURE__ */ index_js_1.SystemMessagePromptTemplate.fromTemplate("You are a helpful assistant that evaluates language models."),
129
+ /* #__PURE__ */ index_js_1.HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),
130
+ /* #__PURE__ */ index_js_1.AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),
131
+ /* #__PURE__ */ index_js_1.HumanMessagePromptTemplate.fromTemplate(TOOL_FREE_EVAL_TEMPLATE),
132
+ ]);
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Prompt for trajectory evaluation chain.
3
+ */
4
+ import { ChatPromptTemplate } from "../../prompts/index.js";
5
+ export declare const EVAL_CHAT_PROMPT: ChatPromptTemplate<any, any>;
6
+ export declare const TOOL_FREE_EVAL_CHAT_PROMPT: ChatPromptTemplate<any, any>;
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Prompt for trajectory evaluation chain.
3
+ */
4
+ import { AIMessagePromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, } from "../../prompts/index.js";
5
+ const EVAL_TEMPLATE = `An AI language model has been given access to the following set of tools to help answer a user's question.
6
+
7
+ The tools given to the AI model are:
8
+ [TOOL_DESCRIPTIONS]
9
+ {toolDescriptions}
10
+ [END_TOOL_DESCRIPTIONS]
11
+
12
+ The question the human asked the AI model was:
13
+ [QUESTION]
14
+ {question}
15
+ [END_QUESTION]{reference}
16
+
17
+ The AI language model decided to use the following set of tools to answer the question:
18
+ [AGENT_TRAJECTORY]
19
+ {agentTrajectory}
20
+ [END_AGENT_TRAJECTORY]
21
+
22
+ The AI language model's final answer to the question was:
23
+ [RESPONSE]
24
+ {answer}
25
+ [END_RESPONSE]
26
+
27
+ Let's do a detailed evaluation of the AI language model's answer step by step.
28
+
29
+ We consider the following criteria before giving a score from 1 to 5:
30
+
31
+ i. Is the final answer helpful?
32
+ ii. Does the AI language use a logical sequence of tools to answer the question?
33
+ iii. Does the AI language model use the tools in a helpful way?
34
+ iv. Does the AI language model use too many steps to answer the question?
35
+ v. Are the appropriate tools used to answer the question?`;
36
+ const EXAMPLE_INPUT = `An AI language model has been given access to the following set of tools to help answer a user's question.
37
+
38
+ The tools given to the AI model are:
39
+ [TOOL_DESCRIPTIONS]
40
+ Tool 1:
41
+ Name: Search
42
+ Description: useful for when you need to ask with search
43
+
44
+ Tool 2:
45
+ Name: Lookup
46
+ Description: useful for when you need to ask with lookup
47
+
48
+ Tool 3:
49
+ Name: Calculator
50
+ Description: useful for doing calculations
51
+
52
+ Tool 4:
53
+ Name: Search the Web (SerpAPI)
54
+ Description: useful for when you need to answer questions about current events
55
+ [END_TOOL_DESCRIPTIONS]
56
+
57
+ The question the human asked the AI model was: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?
58
+
59
+ The AI language model decided to use the following set of tools to answer the question:
60
+ [AGENT_TRAJECTORY]
61
+ Step 1:
62
+ Tool used: Search the Web (SerpAPI)
63
+ Tool input: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?
64
+ Tool output: The Statue of Liberty was given to the United States by France, as a symbol of the two countries' friendship. It was erected atop an American-designed ...
65
+ [END_AGENT_TRAJECTORY]
66
+
67
+ [RESPONSE]
68
+ The AI language model's final answer to the question was: There are different ways to measure the length of the United States, but if we use the distance between the Statue of Liberty and the westernmost point of the contiguous United States (Cape Alava, Washington), which is approximately 2,857 miles (4,596 km), and assume that the Statue of Liberty is 305 feet (93 meters) tall, then the statue would stretch across the United States approximately 17.5 times if laid end to end.
69
+ [END_RESPONSE]
70
+
71
+ Let's do a detailed evaluation of the AI language model's answer step by step.
72
+
73
+ We consider the following criteria before giving a score from 1 to 5:
74
+
75
+ i. Is the final answer helpful?
76
+ ii. Does the AI language use a logical sequence of tools to answer the question?
77
+ iii. Does the AI language model use the tools in a helpful way?
78
+ iv. Does the AI language model use too many steps to answer the question?
79
+ v. Are the appropriate tools used to answer the question?`;
80
+ const EXAMPLE_OUTPUT = `First, let's evaluate the final answer. The final uses good reasoning but is wrong. 2,857 divided by 305 is not 17.5.\
81
+ The model should have used the calculator to figure this out. Second does the model use a logical sequence of tools to answer the question?\
82
+ The way model uses the search is not helpful. The model should have used the search tool to figure the width of the US or the height of the statue.\
83
+ The model didn't use the calculator tool and gave an incorrect answer. The search API should be used for current events or specific questions.\
84
+ The tools were not used in a helpful way. The model did not use too many steps to answer the question.\
85
+ The model did not use the appropriate tools to answer the question.\
86
+
87
+ Judgment: Given the good reasoning in the final answer but otherwise poor performance, we give the model a score of 2.
88
+
89
+ Score: 2`;
90
+ export const EVAL_CHAT_PROMPT =
91
+ /* #__PURE__ */ ChatPromptTemplate.fromPromptMessages([
92
+ /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate("You are a helpful assistant that evaluates language models."),
93
+ /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),
94
+ /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),
95
+ /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EVAL_TEMPLATE),
96
+ ]);
97
+ const TOOL_FREE_EVAL_TEMPLATE = `An AI language model has been given access to a set of tools to help answer a user's question.
98
+
99
+ The question the human asked the AI model was:
100
+ [QUESTION]
101
+ {question}
102
+ [END_QUESTION]{reference}
103
+
104
+ The AI language model decided to use the following set of tools to answer the question:
105
+ [AGENT_TRAJECTORY]
106
+ {agentTrajectory}
107
+ [END_AGENT_TRAJECTORY]
108
+
109
+ The AI language model's final answer to the question was:
110
+ [RESPONSE]
111
+ {answer}
112
+ [END_RESPONSE]
113
+
114
+ Let's do a detailed evaluation of the AI language model's answer step by step.
115
+
116
+ We consider the following criteria before giving a score from 1 to 5:
117
+
118
+ i. Is the final answer helpful?
119
+ ii. Does the AI language use a logical sequence of tools to answer the question?
120
+ iii. Does the AI language model use the tools in a helpful way?
121
+ iv. Does the AI language model use too many steps to answer the question?
122
+ v. Are the appropriate tools used to answer the question?`;
123
+ export const TOOL_FREE_EVAL_CHAT_PROMPT =
124
+ /* #__PURE__ */ ChatPromptTemplate.fromPromptMessages([
125
+ /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate("You are a helpful assistant that evaluates language models."),
126
+ /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),
127
+ /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),
128
+ /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(TOOL_FREE_EVAL_TEMPLATE),
129
+ ]);
@@ -0,0 +1,189 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TrajectoryEvalChain = exports.TrajectoryOutputParser = 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
+ /**
9
+ * A parser for the output of the TrajectoryEvalChain.
10
+ */
11
+ class TrajectoryOutputParser extends output_parser_js_1.BaseLLMOutputParser {
12
+ constructor() {
13
+ super(...arguments);
14
+ Object.defineProperty(this, "lc_namespace", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: ["langchain", "evaluation", "agents"]
19
+ });
20
+ }
21
+ static lc_name() {
22
+ return "TrajectoryOutputParser";
23
+ }
24
+ parseResult(generations, _callbacks) {
25
+ const { text } = generations[0];
26
+ if (!text.includes("Score:")) {
27
+ throw new Error(`Could not find score in model eval output: ${text}`);
28
+ }
29
+ let [reasoning, scoreStr] = text.split("Score:", 2);
30
+ reasoning = reasoning.trim();
31
+ scoreStr = scoreStr.trim();
32
+ // Use regex to extract the score.
33
+ // This will get the number in the string, even if it is a float or more than 10.
34
+ // E.g. "Score: 1" will return 1, "Score: 3.5" will return 3.5, and
35
+ // "Score: 10" will return 10.
36
+ // The score should be an integer digit in the range 1-5.
37
+ const scoreMatch = scoreStr.match(/(\d+(\.\d+)?)/);
38
+ if (scoreMatch === null || scoreMatch[1].includes(".")) {
39
+ throw new Error(`Score is not an integer digit in the range 1-5: ${text}`);
40
+ }
41
+ const score = +scoreMatch[1];
42
+ if (score < 1 || score > 5) {
43
+ throw new Error(`Score is not a digit in the range 1-5: ${text}`);
44
+ }
45
+ const normalizedScore = (score - 1) / 4;
46
+ return Promise.resolve({
47
+ reasoning,
48
+ score: normalizedScore,
49
+ });
50
+ }
51
+ }
52
+ exports.TrajectoryOutputParser = TrajectoryOutputParser;
53
+ /**
54
+ * A chain for evaluating ReAct style agents.
55
+ *
56
+ * This chain is used to evaluate ReAct style agents by reasoning about
57
+ * the sequence of actions taken and their outcomes.
58
+ */
59
+ class TrajectoryEvalChain extends base_js_1.AgentTrajectoryEvaluator {
60
+ constructor() {
61
+ super(...arguments);
62
+ Object.defineProperty(this, "criterionName", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: void 0
67
+ });
68
+ Object.defineProperty(this, "evaluationName", {
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true,
72
+ value: this.criterionName
73
+ });
74
+ Object.defineProperty(this, "requiresInput", {
75
+ enumerable: true,
76
+ configurable: true,
77
+ writable: true,
78
+ value: true
79
+ });
80
+ Object.defineProperty(this, "requiresReference", {
81
+ enumerable: true,
82
+ configurable: true,
83
+ writable: true,
84
+ value: false
85
+ });
86
+ Object.defineProperty(this, "outputParser", {
87
+ enumerable: true,
88
+ configurable: true,
89
+ writable: true,
90
+ value: new TrajectoryOutputParser()
91
+ });
92
+ }
93
+ static lc_name() {
94
+ return "TrajectoryEvalChain";
95
+ }
96
+ static resolveTrajectoryPrompt(prompt, agentTools) {
97
+ let _prompt;
98
+ if (prompt) {
99
+ _prompt = prompt;
100
+ }
101
+ else if (agentTools) {
102
+ _prompt = prompt_js_1.EVAL_CHAT_PROMPT;
103
+ }
104
+ else {
105
+ _prompt = prompt_js_1.TOOL_FREE_EVAL_CHAT_PROMPT;
106
+ }
107
+ return _prompt;
108
+ }
109
+ /**
110
+ * Get the description of the agent tools.
111
+ *
112
+ * @returns The description of the agent tools.
113
+ */
114
+ static toolsDescription(agentTools) {
115
+ return agentTools
116
+ .map((tool, i) => `Tool ${i + 1}: ${tool.name}\n Description: ${tool.description}`)
117
+ .join("\n\n");
118
+ }
119
+ /**
120
+ * Create a new TrajectoryEvalChain.
121
+ * @param llm
122
+ * @param agentTools - The tools used by the agent.
123
+ * @param chainOptions - The options for the chain.
124
+ */
125
+ static async fromLLM(llm, agentTools, chainOptions) {
126
+ let prompt = this.resolveTrajectoryPrompt(chainOptions?.prompt, agentTools);
127
+ if (agentTools) {
128
+ const toolDescriptions = this.toolsDescription(agentTools);
129
+ prompt = await prompt.partial({ toolDescriptions });
130
+ }
131
+ const options = chainOptions;
132
+ if (options) {
133
+ // remove prompt from chainOptions
134
+ delete options.prompt;
135
+ }
136
+ return new this({
137
+ llm,
138
+ prompt,
139
+ ...options,
140
+ });
141
+ }
142
+ _prepareOutput(result) {
143
+ const parsed = result[this.outputKey];
144
+ if (index_js_1.RUN_KEY in result && result[index_js_1.RUN_KEY]) {
145
+ parsed[index_js_1.RUN_KEY] = result[index_js_1.RUN_KEY];
146
+ }
147
+ return parsed;
148
+ }
149
+ /**
150
+ * Get the agent trajectory as a formatted string.
151
+ *
152
+ * @param steps - The agent trajectory.
153
+ * @returns The formatted agent trajectory.
154
+ */
155
+ getAgentTrajectory(steps) {
156
+ return steps
157
+ .map((step, i) => {
158
+ const { action, observation } = step;
159
+ return (`Step ${i + 1}:\n` +
160
+ `Tool used: ${action.tool}\n` +
161
+ `Tool input: ${action.toolInput}\n` +
162
+ `Tool output: ${observation}`);
163
+ })
164
+ .join("\n\n");
165
+ }
166
+ formatReference(reference) {
167
+ if (!reference) {
168
+ return "";
169
+ }
170
+ return `
171
+ The following is the expected answer. Use this to measure correctness:
172
+ [GROUND_TRUTH]
173
+ ${reference}
174
+ [END_GROUND_TRUTH]
175
+ `;
176
+ }
177
+ async _evaluateAgentTrajectory(args, callOptions, config) {
178
+ const { input, prediction, reference, agentTrajectory } = args;
179
+ const inputs = {
180
+ question: input,
181
+ agentTrajectory: this.getAgentTrajectory(agentTrajectory),
182
+ answer: prediction,
183
+ reference: this.formatReference(reference),
184
+ };
185
+ const result = await this.call({ ...inputs, ...callOptions }, config);
186
+ return this._prepareOutput(result);
187
+ }
188
+ }
189
+ exports.TrajectoryEvalChain = TrajectoryEvalChain;
@@ -0,0 +1,54 @@
1
+ import { BaseLLMOutputParser } from "../../schema/output_parser.js";
2
+ import { AgentTrajectoryEvaluator, EvalOutputType, LLMEvalChainInput, LLMTrajectoryEvaluatorArgs } from "../base.js";
3
+ import { AgentStep, ChainValues, ChatGeneration, Generation } from "../../schema/index.js";
4
+ import { Callbacks } from "../../callbacks/index.js";
5
+ import { BaseCallbackConfig } from "../../callbacks/manager.js";
6
+ import { BasePromptTemplate } from "../../prompts/index.js";
7
+ import { StructuredTool } from "../../tools/index.js";
8
+ import { BaseChatModel } from "../../chat_models/base.js";
9
+ /**
10
+ * A parser for the output of the TrajectoryEvalChain.
11
+ */
12
+ export declare class TrajectoryOutputParser 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 evaluating ReAct style agents.
19
+ *
20
+ * This chain is used to evaluate ReAct style agents by reasoning about
21
+ * the sequence of actions taken and their outcomes.
22
+ */
23
+ export declare class TrajectoryEvalChain extends AgentTrajectoryEvaluator {
24
+ static lc_name(): string;
25
+ criterionName?: string;
26
+ evaluationName?: string;
27
+ requiresInput: boolean;
28
+ requiresReference: boolean;
29
+ outputParser: TrajectoryOutputParser;
30
+ static resolveTrajectoryPrompt(prompt?: BasePromptTemplate | undefined, agentTools?: StructuredTool[]): import("../../prompts/chat.js").ChatPromptTemplate<any, any> | BasePromptTemplate<any, import("../../schema/index.js").BasePromptValue, any>;
31
+ /**
32
+ * Get the description of the agent tools.
33
+ *
34
+ * @returns The description of the agent tools.
35
+ */
36
+ static toolsDescription(agentTools: StructuredTool[]): string;
37
+ /**
38
+ * Create a new TrajectoryEvalChain.
39
+ * @param llm
40
+ * @param agentTools - The tools used by the agent.
41
+ * @param chainOptions - The options for the chain.
42
+ */
43
+ static fromLLM(llm: BaseChatModel, agentTools?: StructuredTool[], chainOptions?: Partial<Omit<LLMEvalChainInput, "llm">>): Promise<TrajectoryEvalChain>;
44
+ _prepareOutput(result: ChainValues): any;
45
+ /**
46
+ * Get the agent trajectory as a formatted string.
47
+ *
48
+ * @param steps - The agent trajectory.
49
+ * @returns The formatted agent trajectory.
50
+ */
51
+ getAgentTrajectory(steps: AgentStep[]): string;
52
+ formatReference(reference?: string): string;
53
+ _evaluateAgentTrajectory(args: LLMTrajectoryEvaluatorArgs, callOptions: this["llm"]["CallOptions"], config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
54
+ }