langchain 0.0.212 → 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 +21 -3
- package/dist/agents/executor.d.ts +1 -0
- package/dist/agents/executor.js +21 -3
- 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/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,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;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
|
|
1
|
+
import type { BaseLanguageModelInterface, BaseFunctionCallOptions } from "@langchain/core/language_models/base";
|
|
2
2
|
import type { StructuredToolInterface } from "@langchain/core/tools";
|
|
3
|
+
import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
4
|
+
import { RunnableSequence } from "@langchain/core/runnables";
|
|
3
5
|
import { CallbackManager } from "../../callbacks/manager.js";
|
|
4
|
-
import { BasePromptTemplate } from "../../prompts/base.js";
|
|
6
|
+
import type { BasePromptTemplate } from "../../prompts/base.js";
|
|
5
7
|
import { AgentAction, AgentFinish, AgentStep, BaseMessage, ChainValues, SystemMessage } from "../../schema/index.js";
|
|
6
8
|
import { Agent, AgentArgs } from "../agent.js";
|
|
7
9
|
import { AgentInput } from "../types.js";
|
|
8
|
-
import {
|
|
10
|
+
import { ChatPromptTemplate } from "../../prompts/chat.js";
|
|
11
|
+
import { OpenAIFunctionsAgentOutputParser } from "../openai/output_parser.js";
|
|
9
12
|
export declare function _formatIntermediateSteps(intermediateSteps: AgentStep[]): BaseMessage[];
|
|
10
13
|
/**
|
|
11
14
|
* Interface for the input data required to create an OpenAIAgent.
|
|
@@ -68,3 +71,72 @@ export declare class OpenAIAgent extends Agent {
|
|
|
68
71
|
*/
|
|
69
72
|
plan(steps: Array<AgentStep>, inputs: ChainValues, callbackManager?: CallbackManager): Promise<AgentAction | AgentFinish>;
|
|
70
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Params used by the createOpenAIFunctionsAgent function.
|
|
76
|
+
*/
|
|
77
|
+
export type CreateOpenAIFunctionsAgentParams = {
|
|
78
|
+
/**
|
|
79
|
+
* LLM to use as the agent. Should work with OpenAI function calling,
|
|
80
|
+
* so must either be an OpenAI model that supports that or a wrapper of
|
|
81
|
+
* a different model that adds in equivalent support.
|
|
82
|
+
*/
|
|
83
|
+
llm: BaseChatModel<BaseFunctionCallOptions>;
|
|
84
|
+
/** Tools this agent has access to. */
|
|
85
|
+
tools: StructuredToolInterface[];
|
|
86
|
+
/** The prompt to use, must have an input key for `agent_scratchpad`. */
|
|
87
|
+
prompt: ChatPromptTemplate;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Create an agent that uses OpenAI-style function calling.
|
|
91
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
92
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
93
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
94
|
+
* AgentAction or AgentFinish.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
|
|
99
|
+
* import { pull } from "langchain/hub";
|
|
100
|
+
* import type { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
101
|
+
* import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
|
102
|
+
*
|
|
103
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
104
|
+
*
|
|
105
|
+
* // Define the tools the agent will have access to.
|
|
106
|
+
* const tools = [...];
|
|
107
|
+
*
|
|
108
|
+
* // Get the prompt to use - you can modify this!
|
|
109
|
+
* const prompt = await pull<ChatPromptTemplate>(
|
|
110
|
+
* "hwchase17/openai-functions-agent"
|
|
111
|
+
* );
|
|
112
|
+
*
|
|
113
|
+
* const llm = new ChatOpenAI({
|
|
114
|
+
* temperature: 0,
|
|
115
|
+
* });
|
|
116
|
+
*
|
|
117
|
+
* const agent = await createOpenAIFunctionsAgent({
|
|
118
|
+
* llm,
|
|
119
|
+
* tools,
|
|
120
|
+
* prompt,
|
|
121
|
+
* });
|
|
122
|
+
*
|
|
123
|
+
* const agentExecutor = new AgentExecutor({
|
|
124
|
+
* agent,
|
|
125
|
+
* tools,
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* const result = await agentExecutor.invoke({
|
|
129
|
+
* input: "what is LangChain?",
|
|
130
|
+
* });
|
|
131
|
+
*
|
|
132
|
+
* // With chat history
|
|
133
|
+
* const result2 = await agentExecutor.invoke({
|
|
134
|
+
* input: "what's my name?",
|
|
135
|
+
* chat_history: [
|
|
136
|
+
* new HumanMessage("hi! my name is cob"),
|
|
137
|
+
* new AIMessage("Hello Cob! How can I assist you today?"),
|
|
138
|
+
* ],
|
|
139
|
+
* });
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export declare function createOpenAIFunctionsAgent({ llm, tools, prompt, }: CreateOpenAIFunctionsAgentParams): Promise<RunnableSequence<Record<string, unknown>, AgentAction | AgentFinish>>;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
|
|
1
2
|
import { AIMessage, FunctionMessage, } from "../../schema/index.js";
|
|
2
3
|
import { Agent } from "../agent.js";
|
|
3
4
|
import { PREFIX } from "./prompt.js";
|
|
4
5
|
import { ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, } from "../../prompts/chat.js";
|
|
5
6
|
import { LLMChain } from "../../chains/llm_chain.js";
|
|
6
|
-
import { OpenAIFunctionsAgentOutputParser, } from "
|
|
7
|
+
import { OpenAIFunctionsAgentOutputParser, } from "../openai/output_parser.js";
|
|
7
8
|
import { formatToOpenAIFunction } from "../../tools/convert_to_openai.js";
|
|
9
|
+
import { formatToOpenAIFunctionMessages } from "../format_scratchpad/openai_functions.js";
|
|
8
10
|
/**
|
|
9
11
|
* Checks if the given action is a FunctionsAgentAction.
|
|
10
12
|
* @param action The action to check.
|
|
@@ -155,3 +157,76 @@ export class OpenAIAgent extends Agent {
|
|
|
155
157
|
return this.outputParser.parseAIMessage(message);
|
|
156
158
|
}
|
|
157
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* Create an agent that uses OpenAI-style function calling.
|
|
162
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
163
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
164
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
165
|
+
* AgentAction or AgentFinish.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
|
|
170
|
+
* import { pull } from "langchain/hub";
|
|
171
|
+
* import type { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
172
|
+
* import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
|
173
|
+
*
|
|
174
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
175
|
+
*
|
|
176
|
+
* // Define the tools the agent will have access to.
|
|
177
|
+
* const tools = [...];
|
|
178
|
+
*
|
|
179
|
+
* // Get the prompt to use - you can modify this!
|
|
180
|
+
* const prompt = await pull<ChatPromptTemplate>(
|
|
181
|
+
* "hwchase17/openai-functions-agent"
|
|
182
|
+
* );
|
|
183
|
+
*
|
|
184
|
+
* const llm = new ChatOpenAI({
|
|
185
|
+
* temperature: 0,
|
|
186
|
+
* });
|
|
187
|
+
*
|
|
188
|
+
* const agent = await createOpenAIFunctionsAgent({
|
|
189
|
+
* llm,
|
|
190
|
+
* tools,
|
|
191
|
+
* prompt,
|
|
192
|
+
* });
|
|
193
|
+
*
|
|
194
|
+
* const agentExecutor = new AgentExecutor({
|
|
195
|
+
* agent,
|
|
196
|
+
* tools,
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* const result = await agentExecutor.invoke({
|
|
200
|
+
* input: "what is LangChain?",
|
|
201
|
+
* });
|
|
202
|
+
*
|
|
203
|
+
* // With chat history
|
|
204
|
+
* const result2 = await agentExecutor.invoke({
|
|
205
|
+
* input: "what's my name?",
|
|
206
|
+
* chat_history: [
|
|
207
|
+
* new HumanMessage("hi! my name is cob"),
|
|
208
|
+
* new AIMessage("Hello Cob! How can I assist you today?"),
|
|
209
|
+
* ],
|
|
210
|
+
* });
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
export async function createOpenAIFunctionsAgent({ llm, tools, prompt, }) {
|
|
214
|
+
if (!prompt.inputVariables.includes("agent_scratchpad")) {
|
|
215
|
+
throw new Error([
|
|
216
|
+
`Prompt must have an input variable named "agent_scratchpad".`,
|
|
217
|
+
`Found ${JSON.stringify(prompt.inputVariables)} instead.`,
|
|
218
|
+
].join("\n"));
|
|
219
|
+
}
|
|
220
|
+
const llmWithTools = llm.bind({
|
|
221
|
+
functions: tools.map(formatToOpenAIFunction),
|
|
222
|
+
});
|
|
223
|
+
const agent = RunnableSequence.from([
|
|
224
|
+
RunnablePassthrough.assign({
|
|
225
|
+
agent_scratchpad: (input) => formatToOpenAIFunctionMessages(input.steps),
|
|
226
|
+
}),
|
|
227
|
+
prompt,
|
|
228
|
+
llmWithTools,
|
|
229
|
+
new OpenAIFunctionsAgentOutputParser(),
|
|
230
|
+
]);
|
|
231
|
+
return agent;
|
|
232
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAIFunctionsAgentOutputParser = void 0;
|
|
4
|
+
const index_js_1 = require("../../schema/index.cjs");
|
|
5
|
+
const types_js_1 = require("../types.cjs");
|
|
6
|
+
const output_parser_js_1 = require("../../schema/output_parser.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* @example
|
|
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;
|