langchain 0.0.211 → 0.0.213
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/chains/combine_documents.cjs +1 -0
- package/chains/combine_documents.d.ts +1 -0
- package/chains/combine_documents.js +1 -0
- package/chains/history_aware_retriever.cjs +1 -0
- package/chains/history_aware_retriever.d.ts +1 -0
- package/chains/history_aware_retriever.js +1 -0
- package/chains/retrieval.cjs +1 -0
- package/chains/retrieval.d.ts +1 -0
- package/chains/retrieval.js +1 -0
- package/dist/agents/agent.cjs +1 -0
- package/dist/agents/agent.js +1 -0
- package/dist/agents/executor.cjs +28 -9
- package/dist/agents/executor.d.ts +4 -3
- package/dist/agents/executor.js +27 -8
- package/dist/agents/format_scratchpad/openai_functions.cjs +22 -1
- package/dist/agents/format_scratchpad/openai_functions.d.ts +10 -0
- package/dist/agents/format_scratchpad/openai_functions.js +21 -1
- package/dist/agents/index.cjs +11 -4
- package/dist/agents/index.d.ts +6 -3
- package/dist/agents/index.js +5 -3
- package/dist/agents/initialize.cjs +1 -1
- package/dist/agents/initialize.d.ts +1 -1
- package/dist/agents/initialize.js +1 -1
- package/dist/agents/openai/output_parser.cjs +20 -196
- package/dist/agents/openai/output_parser.d.ts +2 -111
- package/dist/agents/openai/output_parser.js +6 -193
- package/dist/agents/{openai → openai_functions}/index.cjs +78 -2
- package/dist/agents/{openai → openai_functions}/index.d.ts +75 -3
- package/dist/agents/{openai → openai_functions}/index.js +76 -1
- package/dist/agents/openai_functions/output_parser.cjs +102 -0
- package/dist/agents/openai_functions/output_parser.d.ts +56 -0
- package/dist/agents/openai_functions/output_parser.js +98 -0
- package/dist/agents/openai_tools/index.cjs +81 -0
- package/dist/agents/openai_tools/index.d.ts +80 -0
- package/dist/agents/openai_tools/index.js +77 -0
- package/dist/agents/openai_tools/output_parser.cjs +102 -0
- package/dist/agents/openai_tools/output_parser.d.ts +57 -0
- package/dist/agents/openai_tools/output_parser.js +98 -0
- package/dist/agents/react/index.cjs +75 -0
- package/dist/agents/react/index.d.ts +60 -0
- package/dist/agents/react/index.js +71 -0
- package/dist/agents/react/output_parser.cjs +0 -1
- package/dist/agents/react/output_parser.d.ts +0 -1
- package/dist/agents/react/output_parser.js +0 -1
- package/dist/agents/structured_chat/index.cjs +85 -1
- package/dist/agents/structured_chat/index.d.ts +71 -0
- package/dist/agents/structured_chat/index.js +83 -0
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.cjs +1 -1
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.js +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.cjs +1 -0
- package/dist/agents/toolkits/conversational_retrieval/tool.d.ts +1 -0
- package/dist/agents/toolkits/conversational_retrieval/tool.js +1 -0
- package/dist/agents/toolkits/json/json.cjs +2 -0
- package/dist/agents/toolkits/json/json.d.ts +2 -0
- package/dist/agents/toolkits/json/json.js +2 -0
- package/dist/agents/toolkits/openapi/openapi.cjs +2 -0
- package/dist/agents/toolkits/openapi/openapi.d.ts +2 -0
- package/dist/agents/toolkits/openapi/openapi.js +2 -0
- package/dist/agents/toolkits/vectorstore/vectorstore.cjs +2 -0
- package/dist/agents/toolkits/vectorstore/vectorstore.d.ts +2 -0
- package/dist/agents/toolkits/vectorstore/vectorstore.js +2 -0
- package/dist/agents/xml/index.cjs +75 -1
- package/dist/agents/xml/index.d.ts +65 -0
- package/dist/agents/xml/index.js +73 -0
- package/dist/chains/combine_documents/base.cjs +16 -0
- package/dist/chains/combine_documents/base.d.ts +13 -0
- package/dist/chains/combine_documents/base.js +12 -0
- package/dist/chains/combine_documents/index.cjs +5 -0
- package/dist/chains/combine_documents/index.d.ts +1 -0
- package/dist/chains/combine_documents/index.js +1 -0
- package/dist/chains/combine_documents/reduce.cjs +5 -2
- package/dist/chains/combine_documents/reduce.js +4 -1
- package/dist/chains/combine_documents/stuff.cjs +42 -0
- package/dist/chains/combine_documents/stuff.d.ts +28 -0
- package/dist/chains/combine_documents/stuff.js +38 -0
- package/dist/chains/conversational_retrieval_chain.cjs +3 -3
- package/dist/chains/conversational_retrieval_chain.js +1 -1
- package/dist/chains/history_aware_retriever.cjs +55 -0
- package/dist/chains/history_aware_retriever.d.ts +55 -0
- package/dist/chains/history_aware_retriever.js +51 -0
- package/dist/chains/retrieval.cjs +60 -0
- package/dist/chains/retrieval.d.ts +65 -0
- package/dist/chains/retrieval.js +56 -0
- package/dist/index.cjs +3 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/load/import_map.cjs +7 -3
- package/dist/load/import_map.d.ts +4 -0
- package/dist/load/import_map.js +4 -0
- package/dist/output_parsers/json.cjs +2 -78
- package/dist/output_parsers/json.d.ts +1 -1
- package/dist/output_parsers/json.js +1 -77
- package/dist/output_parsers/openai_functions.d.ts +1 -1
- package/dist/tools/retriever.cjs +17 -0
- package/dist/tools/retriever.d.ts +10 -0
- package/dist/tools/retriever.js +13 -0
- package/package.json +36 -4
- package/tools/retriever.cjs +1 -0
- package/tools/retriever.d.ts +1 -0
- package/tools/retriever.js +1 -0
- /package/dist/agents/{openai → openai_functions}/prompt.cjs +0 -0
- /package/dist/agents/{openai → openai_functions}/prompt.d.ts +0 -0
- /package/dist/agents/{openai → openai_functions}/prompt.js +0 -0
|
@@ -1,198 +1,22 @@
|
|
|
1
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
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* ```typescript
|
|
10
|
-
*
|
|
11
|
-
* const prompt = ChatPromptTemplate.fromMessages([
|
|
12
|
-
* ["ai", "You are a helpful assistant"],
|
|
13
|
-
* ["human", "{input}"],
|
|
14
|
-
* new MessagesPlaceholder("agent_scratchpad"),
|
|
15
|
-
* ]);
|
|
16
|
-
*
|
|
17
|
-
* const modelWithFunctions = new ChatOpenAI({
|
|
18
|
-
* modelName: "gpt-4",
|
|
19
|
-
* temperature: 0,
|
|
20
|
-
* }).bind({
|
|
21
|
-
* functions: tools.map((tool) => formatToOpenAIFunction(tool)),
|
|
22
|
-
* });
|
|
23
|
-
*
|
|
24
|
-
* const runnableAgent = RunnableSequence.from([
|
|
25
|
-
* {
|
|
26
|
-
* input: (i) => i.input,
|
|
27
|
-
* agent_scratchpad: (i) => formatAgentSteps(i.steps),
|
|
28
|
-
* },
|
|
29
|
-
* prompt,
|
|
30
|
-
* modelWithFunctions,
|
|
31
|
-
* new OpenAIFunctionsAgentOutputParser(),
|
|
32
|
-
* ]);
|
|
33
|
-
*
|
|
34
|
-
* const result = await runnableAgent.invoke({
|
|
35
|
-
* input: "What is the weather in New York?",
|
|
36
|
-
* steps: agentSteps,
|
|
37
|
-
* });
|
|
38
|
-
*
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
class OpenAIFunctionsAgentOutputParser extends types_js_1.AgentActionOutputParser {
|
|
42
|
-
constructor() {
|
|
43
|
-
super(...arguments);
|
|
44
|
-
Object.defineProperty(this, "lc_namespace", {
|
|
45
|
-
enumerable: true,
|
|
46
|
-
configurable: true,
|
|
47
|
-
writable: true,
|
|
48
|
-
value: ["langchain", "agents", "openai"]
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
static lc_name() {
|
|
52
|
-
return "OpenAIFunctionsAgentOutputParser";
|
|
53
|
-
}
|
|
54
|
-
async parse(text) {
|
|
55
|
-
throw new Error(`OpenAIFunctionsAgentOutputParser can only parse messages.\nPassed input: ${text}`);
|
|
56
|
-
}
|
|
57
|
-
async parseResult(generations) {
|
|
58
|
-
if ("message" in generations[0] && (0, index_js_1.isBaseMessage)(generations[0].message)) {
|
|
59
|
-
return this.parseAIMessage(generations[0].message);
|
|
60
|
-
}
|
|
61
|
-
throw new Error("parseResult on OpenAIFunctionsAgentOutputParser only works on ChatGeneration output");
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Parses the output message into a FunctionsAgentAction or AgentFinish
|
|
65
|
-
* object.
|
|
66
|
-
* @param message The BaseMessage to parse.
|
|
67
|
-
* @returns A FunctionsAgentAction or AgentFinish object.
|
|
68
|
-
*/
|
|
69
|
-
parseAIMessage(message) {
|
|
70
|
-
if (message.content && typeof message.content !== "string") {
|
|
71
|
-
throw new Error("This agent cannot parse non-string model responses.");
|
|
72
|
-
}
|
|
73
|
-
if (message.additional_kwargs.function_call) {
|
|
74
|
-
// eslint-disable-next-line prefer-destructuring
|
|
75
|
-
const function_call = message.additional_kwargs.function_call;
|
|
76
|
-
try {
|
|
77
|
-
const toolInput = function_call.arguments
|
|
78
|
-
? JSON.parse(function_call.arguments)
|
|
79
|
-
: {};
|
|
80
|
-
return {
|
|
81
|
-
tool: function_call.name,
|
|
82
|
-
toolInput,
|
|
83
|
-
log: `Invoking "${function_call.name}" with ${function_call.arguments ?? "{}"}\n${message.content}`,
|
|
84
|
-
messageLog: [message],
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
throw new output_parser_js_1.OutputParserException(`Failed to parse function arguments from chat model response. Text: "${function_call.arguments}". ${error}`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
return {
|
|
93
|
-
returnValues: { output: message.content },
|
|
94
|
-
log: message.content,
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
getFormatInstructions() {
|
|
99
|
-
throw new Error("getFormatInstructions not implemented inside OpenAIFunctionsAgentOutputParser.");
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
exports.OpenAIFunctionsAgentOutputParser = OpenAIFunctionsAgentOutputParser;
|
|
103
|
-
/**
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
*
|
|
107
|
-
* const prompt = ChatPromptTemplate.fromMessages([
|
|
108
|
-
* ["ai", "You are a helpful assistant"],
|
|
109
|
-
* ["human", "{input}"],
|
|
110
|
-
* new MessagesPlaceholder("agent_scratchpad"),
|
|
111
|
-
* ]);
|
|
112
|
-
*
|
|
113
|
-
* const runnableAgent = RunnableSequence.from([
|
|
114
|
-
* {
|
|
115
|
-
* input: (i: { input: string; steps: ToolsAgentStep[] }) => i.input,
|
|
116
|
-
* agent_scratchpad: (i: { input: string; steps: ToolsAgentStep[] }) =>
|
|
117
|
-
* formatToOpenAIToolMessages(i.steps),
|
|
118
|
-
* },
|
|
119
|
-
* prompt,
|
|
120
|
-
* new ChatOpenAI({
|
|
121
|
-
* modelName: "gpt-3.5-turbo-1106",
|
|
122
|
-
* temperature: 0,
|
|
123
|
-
* }).bind({ tools: tools.map(formatToOpenAITool) }),
|
|
124
|
-
* new OpenAIToolsAgentOutputParser(),
|
|
125
|
-
* ]).withConfig({ runName: "OpenAIToolsAgent" });
|
|
126
|
-
*
|
|
127
|
-
* const result = await runnableAgent.invoke({
|
|
128
|
-
* input:
|
|
129
|
-
* "What is the sum of the current temperature in San Francisco, New York, and Tokyo?",
|
|
130
|
-
* });
|
|
131
|
-
*
|
|
132
|
-
* ```
|
|
133
|
-
*/
|
|
134
|
-
class OpenAIToolsAgentOutputParser extends types_js_1.AgentMultiActionOutputParser {
|
|
135
|
-
constructor() {
|
|
136
|
-
super(...arguments);
|
|
137
|
-
Object.defineProperty(this, "lc_namespace", {
|
|
138
|
-
enumerable: true,
|
|
139
|
-
configurable: true,
|
|
140
|
-
writable: true,
|
|
141
|
-
value: ["langchain", "agents", "openai"]
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
static lc_name() {
|
|
145
|
-
return "OpenAIToolsAgentOutputParser";
|
|
146
|
-
}
|
|
147
|
-
async parse(text) {
|
|
148
|
-
throw new Error(`OpenAIFunctionsAgentOutputParser can only parse messages.\nPassed input: ${text}`);
|
|
149
|
-
}
|
|
150
|
-
async parseResult(generations) {
|
|
151
|
-
if ("message" in generations[0] && (0, index_js_1.isBaseMessage)(generations[0].message)) {
|
|
152
|
-
return this.parseAIMessage(generations[0].message);
|
|
153
|
-
}
|
|
154
|
-
throw new Error("parseResult on OpenAIFunctionsAgentOutputParser only works on ChatGeneration output");
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Parses the output message into a ToolsAgentAction[] or AgentFinish
|
|
158
|
-
* object.
|
|
159
|
-
* @param message The BaseMessage to parse.
|
|
160
|
-
* @returns A ToolsAgentAction[] or AgentFinish object.
|
|
161
|
-
*/
|
|
162
|
-
parseAIMessage(message) {
|
|
163
|
-
if (message.content && typeof message.content !== "string") {
|
|
164
|
-
throw new Error("This agent cannot parse non-string model responses.");
|
|
165
|
-
}
|
|
166
|
-
if (message.additional_kwargs.tool_calls) {
|
|
167
|
-
const toolCalls = message.additional_kwargs.tool_calls;
|
|
168
|
-
try {
|
|
169
|
-
return toolCalls.map((toolCall, i) => {
|
|
170
|
-
const toolInput = toolCall.function.arguments
|
|
171
|
-
? JSON.parse(toolCall.function.arguments)
|
|
172
|
-
: {};
|
|
173
|
-
const messageLog = i === 0 ? [message] : [];
|
|
174
|
-
return {
|
|
175
|
-
tool: toolCall.function.name,
|
|
176
|
-
toolInput,
|
|
177
|
-
toolCallId: toolCall.id,
|
|
178
|
-
log: `Invoking "${toolCall.function.name}" with ${toolCall.function.arguments ?? "{}"}\n${message.content}`,
|
|
179
|
-
messageLog,
|
|
180
|
-
};
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
catch (error) {
|
|
184
|
-
throw new output_parser_js_1.OutputParserException(`Failed to parse tool arguments from chat model response. Text: "${JSON.stringify(toolCalls)}". ${error}`);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
return {
|
|
189
|
-
returnValues: { output: message.content },
|
|
190
|
-
log: message.content,
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
getFormatInstructions() {
|
|
195
|
-
throw new Error("getFormatInstructions not implemented inside OpenAIToolsAgentOutputParser.");
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
exports.OpenAIToolsAgentOutputParser = OpenAIToolsAgentOutputParser;
|
|
17
|
+
// console.warn([
|
|
18
|
+
// `[WARNING]: The root "langchain/agents/openai/output_parser" entrypoint is deprecated.`,
|
|
19
|
+
// `Please use either "langchain/agents/openai/output_parser" specific entrypoint instead.`
|
|
20
|
+
// ].join("\n"));
|
|
21
|
+
__exportStar(require("../openai_functions/output_parser.cjs"), exports);
|
|
22
|
+
__exportStar(require("../openai_tools/output_parser.cjs"), exports);
|
|
@@ -1,111 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Type that represents an agent action with an optional message log.
|
|
5
|
-
*/
|
|
6
|
-
export type FunctionsAgentAction = AgentAction & {
|
|
7
|
-
messageLog?: BaseMessage[];
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
*
|
|
13
|
-
* const prompt = ChatPromptTemplate.fromMessages([
|
|
14
|
-
* ["ai", "You are a helpful assistant"],
|
|
15
|
-
* ["human", "{input}"],
|
|
16
|
-
* new MessagesPlaceholder("agent_scratchpad"),
|
|
17
|
-
* ]);
|
|
18
|
-
*
|
|
19
|
-
* const modelWithFunctions = new ChatOpenAI({
|
|
20
|
-
* modelName: "gpt-4",
|
|
21
|
-
* temperature: 0,
|
|
22
|
-
* }).bind({
|
|
23
|
-
* functions: tools.map((tool) => formatToOpenAIFunction(tool)),
|
|
24
|
-
* });
|
|
25
|
-
*
|
|
26
|
-
* const runnableAgent = RunnableSequence.from([
|
|
27
|
-
* {
|
|
28
|
-
* input: (i) => i.input,
|
|
29
|
-
* agent_scratchpad: (i) => formatAgentSteps(i.steps),
|
|
30
|
-
* },
|
|
31
|
-
* prompt,
|
|
32
|
-
* modelWithFunctions,
|
|
33
|
-
* new OpenAIFunctionsAgentOutputParser(),
|
|
34
|
-
* ]);
|
|
35
|
-
*
|
|
36
|
-
* const result = await runnableAgent.invoke({
|
|
37
|
-
* input: "What is the weather in New York?",
|
|
38
|
-
* steps: agentSteps,
|
|
39
|
-
* });
|
|
40
|
-
*
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export declare class OpenAIFunctionsAgentOutputParser extends AgentActionOutputParser {
|
|
44
|
-
lc_namespace: string[];
|
|
45
|
-
static lc_name(): string;
|
|
46
|
-
parse(text: string): Promise<AgentAction | AgentFinish>;
|
|
47
|
-
parseResult(generations: ChatGeneration[]): Promise<AgentFinish | FunctionsAgentAction>;
|
|
48
|
-
/**
|
|
49
|
-
* Parses the output message into a FunctionsAgentAction or AgentFinish
|
|
50
|
-
* object.
|
|
51
|
-
* @param message The BaseMessage to parse.
|
|
52
|
-
* @returns A FunctionsAgentAction or AgentFinish object.
|
|
53
|
-
*/
|
|
54
|
-
parseAIMessage(message: BaseMessage): FunctionsAgentAction | AgentFinish;
|
|
55
|
-
getFormatInstructions(): string;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Type that represents an agent action with an optional message log.
|
|
59
|
-
*/
|
|
60
|
-
export type ToolsAgentAction = AgentAction & {
|
|
61
|
-
toolCallId: string;
|
|
62
|
-
messageLog?: BaseMessage[];
|
|
63
|
-
};
|
|
64
|
-
export type ToolsAgentStep = AgentStep & {
|
|
65
|
-
action: ToolsAgentAction;
|
|
66
|
-
};
|
|
67
|
-
/**
|
|
68
|
-
* @example
|
|
69
|
-
* ```typescript
|
|
70
|
-
*
|
|
71
|
-
* const prompt = ChatPromptTemplate.fromMessages([
|
|
72
|
-
* ["ai", "You are a helpful assistant"],
|
|
73
|
-
* ["human", "{input}"],
|
|
74
|
-
* new MessagesPlaceholder("agent_scratchpad"),
|
|
75
|
-
* ]);
|
|
76
|
-
*
|
|
77
|
-
* const runnableAgent = RunnableSequence.from([
|
|
78
|
-
* {
|
|
79
|
-
* input: (i: { input: string; steps: ToolsAgentStep[] }) => i.input,
|
|
80
|
-
* agent_scratchpad: (i: { input: string; steps: ToolsAgentStep[] }) =>
|
|
81
|
-
* formatToOpenAIToolMessages(i.steps),
|
|
82
|
-
* },
|
|
83
|
-
* prompt,
|
|
84
|
-
* new ChatOpenAI({
|
|
85
|
-
* modelName: "gpt-3.5-turbo-1106",
|
|
86
|
-
* temperature: 0,
|
|
87
|
-
* }).bind({ tools: tools.map(formatToOpenAITool) }),
|
|
88
|
-
* new OpenAIToolsAgentOutputParser(),
|
|
89
|
-
* ]).withConfig({ runName: "OpenAIToolsAgent" });
|
|
90
|
-
*
|
|
91
|
-
* const result = await runnableAgent.invoke({
|
|
92
|
-
* input:
|
|
93
|
-
* "What is the sum of the current temperature in San Francisco, New York, and Tokyo?",
|
|
94
|
-
* });
|
|
95
|
-
*
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
export declare class OpenAIToolsAgentOutputParser extends AgentMultiActionOutputParser {
|
|
99
|
-
lc_namespace: string[];
|
|
100
|
-
static lc_name(): string;
|
|
101
|
-
parse(text: string): Promise<AgentAction[] | AgentFinish>;
|
|
102
|
-
parseResult(generations: ChatGeneration[]): Promise<AgentFinish | ToolsAgentAction[]>;
|
|
103
|
-
/**
|
|
104
|
-
* Parses the output message into a ToolsAgentAction[] or AgentFinish
|
|
105
|
-
* object.
|
|
106
|
-
* @param message The BaseMessage to parse.
|
|
107
|
-
* @returns A ToolsAgentAction[] or AgentFinish object.
|
|
108
|
-
*/
|
|
109
|
-
parseAIMessage(message: BaseMessage): ToolsAgentAction[] | AgentFinish;
|
|
110
|
-
getFormatInstructions(): string;
|
|
111
|
-
}
|
|
1
|
+
export * from "../openai_functions/output_parser.js";
|
|
2
|
+
export * from "../openai_tools/output_parser.js";
|
|
@@ -1,193 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* const prompt = ChatPromptTemplate.fromMessages([
|
|
9
|
-
* ["ai", "You are a helpful assistant"],
|
|
10
|
-
* ["human", "{input}"],
|
|
11
|
-
* new MessagesPlaceholder("agent_scratchpad"),
|
|
12
|
-
* ]);
|
|
13
|
-
*
|
|
14
|
-
* const modelWithFunctions = new ChatOpenAI({
|
|
15
|
-
* modelName: "gpt-4",
|
|
16
|
-
* temperature: 0,
|
|
17
|
-
* }).bind({
|
|
18
|
-
* functions: tools.map((tool) => formatToOpenAIFunction(tool)),
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* const runnableAgent = RunnableSequence.from([
|
|
22
|
-
* {
|
|
23
|
-
* input: (i) => i.input,
|
|
24
|
-
* agent_scratchpad: (i) => formatAgentSteps(i.steps),
|
|
25
|
-
* },
|
|
26
|
-
* prompt,
|
|
27
|
-
* modelWithFunctions,
|
|
28
|
-
* new OpenAIFunctionsAgentOutputParser(),
|
|
29
|
-
* ]);
|
|
30
|
-
*
|
|
31
|
-
* const result = await runnableAgent.invoke({
|
|
32
|
-
* input: "What is the weather in New York?",
|
|
33
|
-
* steps: agentSteps,
|
|
34
|
-
* });
|
|
35
|
-
*
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export class OpenAIFunctionsAgentOutputParser extends AgentActionOutputParser {
|
|
39
|
-
constructor() {
|
|
40
|
-
super(...arguments);
|
|
41
|
-
Object.defineProperty(this, "lc_namespace", {
|
|
42
|
-
enumerable: true,
|
|
43
|
-
configurable: true,
|
|
44
|
-
writable: true,
|
|
45
|
-
value: ["langchain", "agents", "openai"]
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
static lc_name() {
|
|
49
|
-
return "OpenAIFunctionsAgentOutputParser";
|
|
50
|
-
}
|
|
51
|
-
async parse(text) {
|
|
52
|
-
throw new Error(`OpenAIFunctionsAgentOutputParser can only parse messages.\nPassed input: ${text}`);
|
|
53
|
-
}
|
|
54
|
-
async parseResult(generations) {
|
|
55
|
-
if ("message" in generations[0] && isBaseMessage(generations[0].message)) {
|
|
56
|
-
return this.parseAIMessage(generations[0].message);
|
|
57
|
-
}
|
|
58
|
-
throw new Error("parseResult on OpenAIFunctionsAgentOutputParser only works on ChatGeneration output");
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Parses the output message into a FunctionsAgentAction or AgentFinish
|
|
62
|
-
* object.
|
|
63
|
-
* @param message The BaseMessage to parse.
|
|
64
|
-
* @returns A FunctionsAgentAction or AgentFinish object.
|
|
65
|
-
*/
|
|
66
|
-
parseAIMessage(message) {
|
|
67
|
-
if (message.content && typeof message.content !== "string") {
|
|
68
|
-
throw new Error("This agent cannot parse non-string model responses.");
|
|
69
|
-
}
|
|
70
|
-
if (message.additional_kwargs.function_call) {
|
|
71
|
-
// eslint-disable-next-line prefer-destructuring
|
|
72
|
-
const function_call = message.additional_kwargs.function_call;
|
|
73
|
-
try {
|
|
74
|
-
const toolInput = function_call.arguments
|
|
75
|
-
? JSON.parse(function_call.arguments)
|
|
76
|
-
: {};
|
|
77
|
-
return {
|
|
78
|
-
tool: function_call.name,
|
|
79
|
-
toolInput,
|
|
80
|
-
log: `Invoking "${function_call.name}" with ${function_call.arguments ?? "{}"}\n${message.content}`,
|
|
81
|
-
messageLog: [message],
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
throw new OutputParserException(`Failed to parse function arguments from chat model response. Text: "${function_call.arguments}". ${error}`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
return {
|
|
90
|
-
returnValues: { output: message.content },
|
|
91
|
-
log: message.content,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
getFormatInstructions() {
|
|
96
|
-
throw new Error("getFormatInstructions not implemented inside OpenAIFunctionsAgentOutputParser.");
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* @example
|
|
101
|
-
* ```typescript
|
|
102
|
-
*
|
|
103
|
-
* const prompt = ChatPromptTemplate.fromMessages([
|
|
104
|
-
* ["ai", "You are a helpful assistant"],
|
|
105
|
-
* ["human", "{input}"],
|
|
106
|
-
* new MessagesPlaceholder("agent_scratchpad"),
|
|
107
|
-
* ]);
|
|
108
|
-
*
|
|
109
|
-
* const runnableAgent = RunnableSequence.from([
|
|
110
|
-
* {
|
|
111
|
-
* input: (i: { input: string; steps: ToolsAgentStep[] }) => i.input,
|
|
112
|
-
* agent_scratchpad: (i: { input: string; steps: ToolsAgentStep[] }) =>
|
|
113
|
-
* formatToOpenAIToolMessages(i.steps),
|
|
114
|
-
* },
|
|
115
|
-
* prompt,
|
|
116
|
-
* new ChatOpenAI({
|
|
117
|
-
* modelName: "gpt-3.5-turbo-1106",
|
|
118
|
-
* temperature: 0,
|
|
119
|
-
* }).bind({ tools: tools.map(formatToOpenAITool) }),
|
|
120
|
-
* new OpenAIToolsAgentOutputParser(),
|
|
121
|
-
* ]).withConfig({ runName: "OpenAIToolsAgent" });
|
|
122
|
-
*
|
|
123
|
-
* const result = await runnableAgent.invoke({
|
|
124
|
-
* input:
|
|
125
|
-
* "What is the sum of the current temperature in San Francisco, New York, and Tokyo?",
|
|
126
|
-
* });
|
|
127
|
-
*
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
export class OpenAIToolsAgentOutputParser extends AgentMultiActionOutputParser {
|
|
131
|
-
constructor() {
|
|
132
|
-
super(...arguments);
|
|
133
|
-
Object.defineProperty(this, "lc_namespace", {
|
|
134
|
-
enumerable: true,
|
|
135
|
-
configurable: true,
|
|
136
|
-
writable: true,
|
|
137
|
-
value: ["langchain", "agents", "openai"]
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
static lc_name() {
|
|
141
|
-
return "OpenAIToolsAgentOutputParser";
|
|
142
|
-
}
|
|
143
|
-
async parse(text) {
|
|
144
|
-
throw new Error(`OpenAIFunctionsAgentOutputParser can only parse messages.\nPassed input: ${text}`);
|
|
145
|
-
}
|
|
146
|
-
async parseResult(generations) {
|
|
147
|
-
if ("message" in generations[0] && isBaseMessage(generations[0].message)) {
|
|
148
|
-
return this.parseAIMessage(generations[0].message);
|
|
149
|
-
}
|
|
150
|
-
throw new Error("parseResult on OpenAIFunctionsAgentOutputParser only works on ChatGeneration output");
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Parses the output message into a ToolsAgentAction[] or AgentFinish
|
|
154
|
-
* object.
|
|
155
|
-
* @param message The BaseMessage to parse.
|
|
156
|
-
* @returns A ToolsAgentAction[] or AgentFinish object.
|
|
157
|
-
*/
|
|
158
|
-
parseAIMessage(message) {
|
|
159
|
-
if (message.content && typeof message.content !== "string") {
|
|
160
|
-
throw new Error("This agent cannot parse non-string model responses.");
|
|
161
|
-
}
|
|
162
|
-
if (message.additional_kwargs.tool_calls) {
|
|
163
|
-
const toolCalls = message.additional_kwargs.tool_calls;
|
|
164
|
-
try {
|
|
165
|
-
return toolCalls.map((toolCall, i) => {
|
|
166
|
-
const toolInput = toolCall.function.arguments
|
|
167
|
-
? JSON.parse(toolCall.function.arguments)
|
|
168
|
-
: {};
|
|
169
|
-
const messageLog = i === 0 ? [message] : [];
|
|
170
|
-
return {
|
|
171
|
-
tool: toolCall.function.name,
|
|
172
|
-
toolInput,
|
|
173
|
-
toolCallId: toolCall.id,
|
|
174
|
-
log: `Invoking "${toolCall.function.name}" with ${toolCall.function.arguments ?? "{}"}\n${message.content}`,
|
|
175
|
-
messageLog,
|
|
176
|
-
};
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
catch (error) {
|
|
180
|
-
throw new OutputParserException(`Failed to parse tool arguments from chat model response. Text: "${JSON.stringify(toolCalls)}". ${error}`);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
return {
|
|
185
|
-
returnValues: { output: message.content },
|
|
186
|
-
log: message.content,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
getFormatInstructions() {
|
|
191
|
-
throw new Error("getFormatInstructions not implemented inside OpenAIToolsAgentOutputParser.");
|
|
192
|
-
}
|
|
193
|
-
}
|
|
1
|
+
// console.warn([
|
|
2
|
+
// `[WARNING]: The root "langchain/agents/openai/output_parser" entrypoint is deprecated.`,
|
|
3
|
+
// `Please use either "langchain/agents/openai/output_parser" specific entrypoint instead.`
|
|
4
|
+
// ].join("\n"));
|
|
5
|
+
export * from "../openai_functions/output_parser.js";
|
|
6
|
+
export * from "../openai_tools/output_parser.js";
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OpenAIAgent = exports._formatIntermediateSteps = void 0;
|
|
3
|
+
exports.createOpenAIFunctionsAgent = exports.OpenAIAgent = exports._formatIntermediateSteps = void 0;
|
|
4
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
4
5
|
const index_js_1 = require("../../schema/index.cjs");
|
|
5
6
|
const agent_js_1 = require("../agent.cjs");
|
|
6
7
|
const prompt_js_1 = require("./prompt.cjs");
|
|
7
8
|
const chat_js_1 = require("../../prompts/chat.cjs");
|
|
8
9
|
const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
|
|
9
|
-
const output_parser_js_1 = require("
|
|
10
|
+
const output_parser_js_1 = require("../openai/output_parser.cjs");
|
|
10
11
|
const convert_to_openai_js_1 = require("../../tools/convert_to_openai.cjs");
|
|
12
|
+
const openai_functions_js_1 = require("../format_scratchpad/openai_functions.cjs");
|
|
11
13
|
/**
|
|
12
14
|
* Checks if the given action is a FunctionsAgentAction.
|
|
13
15
|
* @param action The action to check.
|
|
@@ -160,3 +162,77 @@ class OpenAIAgent extends agent_js_1.Agent {
|
|
|
160
162
|
}
|
|
161
163
|
}
|
|
162
164
|
exports.OpenAIAgent = OpenAIAgent;
|
|
165
|
+
/**
|
|
166
|
+
* Create an agent that uses OpenAI-style function calling.
|
|
167
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
168
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
169
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
170
|
+
* AgentAction or AgentFinish.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
|
|
175
|
+
* import { pull } from "langchain/hub";
|
|
176
|
+
* import type { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
177
|
+
* import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
|
178
|
+
*
|
|
179
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
180
|
+
*
|
|
181
|
+
* // Define the tools the agent will have access to.
|
|
182
|
+
* const tools = [...];
|
|
183
|
+
*
|
|
184
|
+
* // Get the prompt to use - you can modify this!
|
|
185
|
+
* const prompt = await pull<ChatPromptTemplate>(
|
|
186
|
+
* "hwchase17/openai-functions-agent"
|
|
187
|
+
* );
|
|
188
|
+
*
|
|
189
|
+
* const llm = new ChatOpenAI({
|
|
190
|
+
* temperature: 0,
|
|
191
|
+
* });
|
|
192
|
+
*
|
|
193
|
+
* const agent = await createOpenAIFunctionsAgent({
|
|
194
|
+
* llm,
|
|
195
|
+
* tools,
|
|
196
|
+
* prompt,
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* const agentExecutor = new AgentExecutor({
|
|
200
|
+
* agent,
|
|
201
|
+
* tools,
|
|
202
|
+
* });
|
|
203
|
+
*
|
|
204
|
+
* const result = await agentExecutor.invoke({
|
|
205
|
+
* input: "what is LangChain?",
|
|
206
|
+
* });
|
|
207
|
+
*
|
|
208
|
+
* // With chat history
|
|
209
|
+
* const result2 = await agentExecutor.invoke({
|
|
210
|
+
* input: "what's my name?",
|
|
211
|
+
* chat_history: [
|
|
212
|
+
* new HumanMessage("hi! my name is cob"),
|
|
213
|
+
* new AIMessage("Hello Cob! How can I assist you today?"),
|
|
214
|
+
* ],
|
|
215
|
+
* });
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
async function createOpenAIFunctionsAgent({ llm, tools, prompt, }) {
|
|
219
|
+
if (!prompt.inputVariables.includes("agent_scratchpad")) {
|
|
220
|
+
throw new Error([
|
|
221
|
+
`Prompt must have an input variable named "agent_scratchpad".`,
|
|
222
|
+
`Found ${JSON.stringify(prompt.inputVariables)} instead.`,
|
|
223
|
+
].join("\n"));
|
|
224
|
+
}
|
|
225
|
+
const llmWithTools = llm.bind({
|
|
226
|
+
functions: tools.map(convert_to_openai_js_1.formatToOpenAIFunction),
|
|
227
|
+
});
|
|
228
|
+
const agent = runnables_1.RunnableSequence.from([
|
|
229
|
+
runnables_1.RunnablePassthrough.assign({
|
|
230
|
+
agent_scratchpad: (input) => (0, openai_functions_js_1.formatToOpenAIFunctionMessages)(input.steps),
|
|
231
|
+
}),
|
|
232
|
+
prompt,
|
|
233
|
+
llmWithTools,
|
|
234
|
+
new output_parser_js_1.OpenAIFunctionsAgentOutputParser(),
|
|
235
|
+
]);
|
|
236
|
+
return agent;
|
|
237
|
+
}
|
|
238
|
+
exports.createOpenAIFunctionsAgent = createOpenAIFunctionsAgent;
|