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
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAIToolsAgentOutputParser = 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 runnableAgent = RunnableSequence.from([
|
|
18
|
+
* {
|
|
19
|
+
* input: (i: { input: string; steps: ToolsAgentStep[] }) => i.input,
|
|
20
|
+
* agent_scratchpad: (i: { input: string; steps: ToolsAgentStep[] }) =>
|
|
21
|
+
* formatToOpenAIToolMessages(i.steps),
|
|
22
|
+
* },
|
|
23
|
+
* prompt,
|
|
24
|
+
* new ChatOpenAI({
|
|
25
|
+
* modelName: "gpt-3.5-turbo-1106",
|
|
26
|
+
* temperature: 0,
|
|
27
|
+
* }).bind({ tools: tools.map(formatToOpenAITool) }),
|
|
28
|
+
* new OpenAIToolsAgentOutputParser(),
|
|
29
|
+
* ]).withConfig({ runName: "OpenAIToolsAgent" });
|
|
30
|
+
*
|
|
31
|
+
* const result = await runnableAgent.invoke({
|
|
32
|
+
* input:
|
|
33
|
+
* "What is the sum of the current temperature in San Francisco, New York, and Tokyo?",
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
class OpenAIToolsAgentOutputParser extends types_js_1.AgentMultiActionOutputParser {
|
|
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 "OpenAIToolsAgentOutputParser";
|
|
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] && (0, index_js_1.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 ToolsAgentAction[] or AgentFinish
|
|
62
|
+
* object.
|
|
63
|
+
* @param message The BaseMessage to parse.
|
|
64
|
+
* @returns A ToolsAgentAction[] 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.tool_calls) {
|
|
71
|
+
const toolCalls = message.additional_kwargs.tool_calls;
|
|
72
|
+
try {
|
|
73
|
+
return toolCalls.map((toolCall, i) => {
|
|
74
|
+
const toolInput = toolCall.function.arguments
|
|
75
|
+
? JSON.parse(toolCall.function.arguments)
|
|
76
|
+
: {};
|
|
77
|
+
const messageLog = i === 0 ? [message] : [];
|
|
78
|
+
return {
|
|
79
|
+
tool: toolCall.function.name,
|
|
80
|
+
toolInput,
|
|
81
|
+
toolCallId: toolCall.id,
|
|
82
|
+
log: `Invoking "${toolCall.function.name}" with ${toolCall.function.arguments ?? "{}"}\n${message.content}`,
|
|
83
|
+
messageLog,
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
throw new output_parser_js_1.OutputParserException(`Failed to parse tool arguments from chat model response. Text: "${JSON.stringify(toolCalls)}". ${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 OpenAIToolsAgentOutputParser.");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.OpenAIToolsAgentOutputParser = OpenAIToolsAgentOutputParser;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { AgentAction, AgentFinish, AgentStep, BaseMessage, ChatGeneration } from "../../schema/index.js";
|
|
2
|
+
import { AgentMultiActionOutputParser } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Type that represents an agent action with an optional message log.
|
|
5
|
+
*/
|
|
6
|
+
export type ToolsAgentAction = AgentAction & {
|
|
7
|
+
toolCallId: string;
|
|
8
|
+
messageLog?: BaseMessage[];
|
|
9
|
+
};
|
|
10
|
+
export type ToolsAgentStep = AgentStep & {
|
|
11
|
+
action: ToolsAgentAction;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
*
|
|
17
|
+
* const prompt = ChatPromptTemplate.fromMessages([
|
|
18
|
+
* ["ai", "You are a helpful assistant"],
|
|
19
|
+
* ["human", "{input}"],
|
|
20
|
+
* new MessagesPlaceholder("agent_scratchpad"),
|
|
21
|
+
* ]);
|
|
22
|
+
*
|
|
23
|
+
* const runnableAgent = RunnableSequence.from([
|
|
24
|
+
* {
|
|
25
|
+
* input: (i: { input: string; steps: ToolsAgentStep[] }) => i.input,
|
|
26
|
+
* agent_scratchpad: (i: { input: string; steps: ToolsAgentStep[] }) =>
|
|
27
|
+
* formatToOpenAIToolMessages(i.steps),
|
|
28
|
+
* },
|
|
29
|
+
* prompt,
|
|
30
|
+
* new ChatOpenAI({
|
|
31
|
+
* modelName: "gpt-3.5-turbo-1106",
|
|
32
|
+
* temperature: 0,
|
|
33
|
+
* }).bind({ tools: tools.map(formatToOpenAITool) }),
|
|
34
|
+
* new OpenAIToolsAgentOutputParser(),
|
|
35
|
+
* ]).withConfig({ runName: "OpenAIToolsAgent" });
|
|
36
|
+
*
|
|
37
|
+
* const result = await runnableAgent.invoke({
|
|
38
|
+
* input:
|
|
39
|
+
* "What is the sum of the current temperature in San Francisco, New York, and Tokyo?",
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare class OpenAIToolsAgentOutputParser extends AgentMultiActionOutputParser {
|
|
45
|
+
lc_namespace: string[];
|
|
46
|
+
static lc_name(): string;
|
|
47
|
+
parse(text: string): Promise<AgentAction[] | AgentFinish>;
|
|
48
|
+
parseResult(generations: ChatGeneration[]): Promise<AgentFinish | ToolsAgentAction[]>;
|
|
49
|
+
/**
|
|
50
|
+
* Parses the output message into a ToolsAgentAction[] or AgentFinish
|
|
51
|
+
* object.
|
|
52
|
+
* @param message The BaseMessage to parse.
|
|
53
|
+
* @returns A ToolsAgentAction[] or AgentFinish object.
|
|
54
|
+
*/
|
|
55
|
+
parseAIMessage(message: BaseMessage): ToolsAgentAction[] | AgentFinish;
|
|
56
|
+
getFormatInstructions(): string;
|
|
57
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { isBaseMessage, } from "../../schema/index.js";
|
|
2
|
+
import { AgentMultiActionOutputParser } from "../types.js";
|
|
3
|
+
import { OutputParserException } from "../../schema/output_parser.js";
|
|
4
|
+
/**
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
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 runnableAgent = RunnableSequence.from([
|
|
15
|
+
* {
|
|
16
|
+
* input: (i: { input: string; steps: ToolsAgentStep[] }) => i.input,
|
|
17
|
+
* agent_scratchpad: (i: { input: string; steps: ToolsAgentStep[] }) =>
|
|
18
|
+
* formatToOpenAIToolMessages(i.steps),
|
|
19
|
+
* },
|
|
20
|
+
* prompt,
|
|
21
|
+
* new ChatOpenAI({
|
|
22
|
+
* modelName: "gpt-3.5-turbo-1106",
|
|
23
|
+
* temperature: 0,
|
|
24
|
+
* }).bind({ tools: tools.map(formatToOpenAITool) }),
|
|
25
|
+
* new OpenAIToolsAgentOutputParser(),
|
|
26
|
+
* ]).withConfig({ runName: "OpenAIToolsAgent" });
|
|
27
|
+
*
|
|
28
|
+
* const result = await runnableAgent.invoke({
|
|
29
|
+
* input:
|
|
30
|
+
* "What is the sum of the current temperature in San Francisco, New York, and Tokyo?",
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class OpenAIToolsAgentOutputParser extends AgentMultiActionOutputParser {
|
|
36
|
+
constructor() {
|
|
37
|
+
super(...arguments);
|
|
38
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: ["langchain", "agents", "openai"]
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
static lc_name() {
|
|
46
|
+
return "OpenAIToolsAgentOutputParser";
|
|
47
|
+
}
|
|
48
|
+
async parse(text) {
|
|
49
|
+
throw new Error(`OpenAIFunctionsAgentOutputParser can only parse messages.\nPassed input: ${text}`);
|
|
50
|
+
}
|
|
51
|
+
async parseResult(generations) {
|
|
52
|
+
if ("message" in generations[0] && isBaseMessage(generations[0].message)) {
|
|
53
|
+
return this.parseAIMessage(generations[0].message);
|
|
54
|
+
}
|
|
55
|
+
throw new Error("parseResult on OpenAIFunctionsAgentOutputParser only works on ChatGeneration output");
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Parses the output message into a ToolsAgentAction[] or AgentFinish
|
|
59
|
+
* object.
|
|
60
|
+
* @param message The BaseMessage to parse.
|
|
61
|
+
* @returns A ToolsAgentAction[] or AgentFinish object.
|
|
62
|
+
*/
|
|
63
|
+
parseAIMessage(message) {
|
|
64
|
+
if (message.content && typeof message.content !== "string") {
|
|
65
|
+
throw new Error("This agent cannot parse non-string model responses.");
|
|
66
|
+
}
|
|
67
|
+
if (message.additional_kwargs.tool_calls) {
|
|
68
|
+
const toolCalls = message.additional_kwargs.tool_calls;
|
|
69
|
+
try {
|
|
70
|
+
return toolCalls.map((toolCall, i) => {
|
|
71
|
+
const toolInput = toolCall.function.arguments
|
|
72
|
+
? JSON.parse(toolCall.function.arguments)
|
|
73
|
+
: {};
|
|
74
|
+
const messageLog = i === 0 ? [message] : [];
|
|
75
|
+
return {
|
|
76
|
+
tool: toolCall.function.name,
|
|
77
|
+
toolInput,
|
|
78
|
+
toolCallId: toolCall.id,
|
|
79
|
+
log: `Invoking "${toolCall.function.name}" with ${toolCall.function.arguments ?? "{}"}\n${message.content}`,
|
|
80
|
+
messageLog,
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
throw new OutputParserException(`Failed to parse tool arguments from chat model response. Text: "${JSON.stringify(toolCalls)}". ${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 OpenAIToolsAgentOutputParser.");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createReactAgent = void 0;
|
|
4
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
5
|
+
const render_js_1 = require("../../tools/render.cjs");
|
|
6
|
+
const log_js_1 = require("../format_scratchpad/log.cjs");
|
|
7
|
+
const output_parser_js_1 = require("./output_parser.cjs");
|
|
8
|
+
/**
|
|
9
|
+
* Create an agent that uses ReAct prompting.
|
|
10
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
11
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
12
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
13
|
+
* AgentAction or AgentFinish.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { AgentExecutor, createReactAgent } from "langchain/agents";
|
|
18
|
+
* import { pull } from "langchain/hub";
|
|
19
|
+
* import type { PromptTemplate } from "@langchain/core/prompts";
|
|
20
|
+
*
|
|
21
|
+
* import { OpenAI } from "@langchain/openai";
|
|
22
|
+
*
|
|
23
|
+
* // Define the tools the agent will have access to.
|
|
24
|
+
* const tools = [...];
|
|
25
|
+
*
|
|
26
|
+
* // Get the prompt to use - you can modify this!
|
|
27
|
+
* const prompt = await pull<PromptTemplate>("hwchase17/react");
|
|
28
|
+
*
|
|
29
|
+
* const llm = new OpenAI({
|
|
30
|
+
* temperature: 0,
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* const agent = await createReactAgent({
|
|
34
|
+
* llm,
|
|
35
|
+
* tools,
|
|
36
|
+
* prompt,
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* const agentExecutor = new AgentExecutor({
|
|
40
|
+
* agent,
|
|
41
|
+
* tools,
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* const result = await agentExecutor.invoke({
|
|
45
|
+
* input: "what is LangChain?",
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
async function createReactAgent({ llm, tools, prompt, }) {
|
|
50
|
+
const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
51
|
+
if (missingVariables.length > 0) {
|
|
52
|
+
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
53
|
+
}
|
|
54
|
+
const toolNames = tools.map((tool) => tool.name);
|
|
55
|
+
const partialedPrompt = await prompt.partial({
|
|
56
|
+
tools: (0, render_js_1.renderTextDescription)(tools),
|
|
57
|
+
tool_names: toolNames.join(", "),
|
|
58
|
+
});
|
|
59
|
+
// TODO: Add .bind to core runnable interface.
|
|
60
|
+
const llmWithStop = llm.bind({
|
|
61
|
+
stop: ["\nObservation:"],
|
|
62
|
+
});
|
|
63
|
+
const agent = runnables_1.RunnableSequence.from([
|
|
64
|
+
runnables_1.RunnablePassthrough.assign({
|
|
65
|
+
agent_scratchpad: (input) => (0, log_js_1.formatLogToString)(input.steps),
|
|
66
|
+
}),
|
|
67
|
+
partialedPrompt,
|
|
68
|
+
llmWithStop,
|
|
69
|
+
new output_parser_js_1.ReActSingleInputOutputParser({
|
|
70
|
+
toolNames,
|
|
71
|
+
}),
|
|
72
|
+
]);
|
|
73
|
+
return agent;
|
|
74
|
+
}
|
|
75
|
+
exports.createReactAgent = createReactAgent;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ToolInterface } from "@langchain/core/tools";
|
|
2
|
+
import { BasePromptTemplate } from "@langchain/core/prompts";
|
|
3
|
+
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
|
|
4
|
+
import { RunnableSequence } from "@langchain/core/runnables";
|
|
5
|
+
/**
|
|
6
|
+
* Params used by the createXmlAgent function.
|
|
7
|
+
*/
|
|
8
|
+
export type CreateReactAgentParams = {
|
|
9
|
+
/** LLM to use for the agent. */
|
|
10
|
+
llm: BaseLanguageModelInterface;
|
|
11
|
+
/** Tools this agent has access to. */
|
|
12
|
+
tools: ToolInterface[];
|
|
13
|
+
/**
|
|
14
|
+
* The prompt to use. Must have input keys for
|
|
15
|
+
* `tools`, `tool_names`, and `agent_scratchpad`.
|
|
16
|
+
*/
|
|
17
|
+
prompt: BasePromptTemplate;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Create an agent that uses ReAct prompting.
|
|
21
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
22
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
23
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
24
|
+
* AgentAction or AgentFinish.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { AgentExecutor, createReactAgent } from "langchain/agents";
|
|
29
|
+
* import { pull } from "langchain/hub";
|
|
30
|
+
* import type { PromptTemplate } from "@langchain/core/prompts";
|
|
31
|
+
*
|
|
32
|
+
* import { OpenAI } from "@langchain/openai";
|
|
33
|
+
*
|
|
34
|
+
* // Define the tools the agent will have access to.
|
|
35
|
+
* const tools = [...];
|
|
36
|
+
*
|
|
37
|
+
* // Get the prompt to use - you can modify this!
|
|
38
|
+
* const prompt = await pull<PromptTemplate>("hwchase17/react");
|
|
39
|
+
*
|
|
40
|
+
* const llm = new OpenAI({
|
|
41
|
+
* temperature: 0,
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* const agent = await createReactAgent({
|
|
45
|
+
* llm,
|
|
46
|
+
* tools,
|
|
47
|
+
* prompt,
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* const agentExecutor = new AgentExecutor({
|
|
51
|
+
* agent,
|
|
52
|
+
* tools,
|
|
53
|
+
* });
|
|
54
|
+
*
|
|
55
|
+
* const result = await agentExecutor.invoke({
|
|
56
|
+
* input: "what is LangChain?",
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function createReactAgent({ llm, tools, prompt, }: CreateReactAgentParams): Promise<RunnableSequence<Record<string, unknown>, import("@langchain/core/agents").AgentAction | import("@langchain/core/agents").AgentFinish>>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
|
|
2
|
+
import { renderTextDescription } from "../../tools/render.js";
|
|
3
|
+
import { formatLogToString } from "../format_scratchpad/log.js";
|
|
4
|
+
import { ReActSingleInputOutputParser } from "./output_parser.js";
|
|
5
|
+
/**
|
|
6
|
+
* Create an agent that uses ReAct prompting.
|
|
7
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
8
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
9
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
10
|
+
* AgentAction or AgentFinish.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { AgentExecutor, createReactAgent } from "langchain/agents";
|
|
15
|
+
* import { pull } from "langchain/hub";
|
|
16
|
+
* import type { PromptTemplate } from "@langchain/core/prompts";
|
|
17
|
+
*
|
|
18
|
+
* import { OpenAI } from "@langchain/openai";
|
|
19
|
+
*
|
|
20
|
+
* // Define the tools the agent will have access to.
|
|
21
|
+
* const tools = [...];
|
|
22
|
+
*
|
|
23
|
+
* // Get the prompt to use - you can modify this!
|
|
24
|
+
* const prompt = await pull<PromptTemplate>("hwchase17/react");
|
|
25
|
+
*
|
|
26
|
+
* const llm = new OpenAI({
|
|
27
|
+
* temperature: 0,
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* const agent = await createReactAgent({
|
|
31
|
+
* llm,
|
|
32
|
+
* tools,
|
|
33
|
+
* prompt,
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* const agentExecutor = new AgentExecutor({
|
|
37
|
+
* agent,
|
|
38
|
+
* tools,
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* const result = await agentExecutor.invoke({
|
|
42
|
+
* input: "what is LangChain?",
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export async function createReactAgent({ llm, tools, prompt, }) {
|
|
47
|
+
const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
48
|
+
if (missingVariables.length > 0) {
|
|
49
|
+
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
50
|
+
}
|
|
51
|
+
const toolNames = tools.map((tool) => tool.name);
|
|
52
|
+
const partialedPrompt = await prompt.partial({
|
|
53
|
+
tools: renderTextDescription(tools),
|
|
54
|
+
tool_names: toolNames.join(", "),
|
|
55
|
+
});
|
|
56
|
+
// TODO: Add .bind to core runnable interface.
|
|
57
|
+
const llmWithStop = llm.bind({
|
|
58
|
+
stop: ["\nObservation:"],
|
|
59
|
+
});
|
|
60
|
+
const agent = RunnableSequence.from([
|
|
61
|
+
RunnablePassthrough.assign({
|
|
62
|
+
agent_scratchpad: (input) => formatLogToString(input.steps),
|
|
63
|
+
}),
|
|
64
|
+
partialedPrompt,
|
|
65
|
+
llmWithStop,
|
|
66
|
+
new ReActSingleInputOutputParser({
|
|
67
|
+
toolNames,
|
|
68
|
+
}),
|
|
69
|
+
]);
|
|
70
|
+
return agent;
|
|
71
|
+
}
|
|
@@ -43,7 +43,6 @@ const FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE = "Parsing LLM output produ
|
|
|
43
43
|
* const result = await agent.invoke({
|
|
44
44
|
* input: "whats the weather in pomfret?",
|
|
45
45
|
* });
|
|
46
|
-
*
|
|
47
46
|
* ```
|
|
48
47
|
*/
|
|
49
48
|
class ReActSingleInputOutputParser extends types_js_1.AgentActionOutputParser {
|
|
@@ -37,7 +37,6 @@ import { AgentAction, AgentFinish } from "../../schema/index.js";
|
|
|
37
37
|
* const result = await agent.invoke({
|
|
38
38
|
* input: "whats the weather in pomfret?",
|
|
39
39
|
* });
|
|
40
|
-
*
|
|
41
40
|
* ```
|
|
42
41
|
*/
|
|
43
42
|
export declare class ReActSingleInputOutputParser extends AgentActionOutputParser {
|
|
@@ -40,7 +40,6 @@ const FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE = "Parsing LLM output produ
|
|
|
40
40
|
* const result = await agent.invoke({
|
|
41
41
|
* input: "whats the weather in pomfret?",
|
|
42
42
|
* });
|
|
43
|
-
*
|
|
44
43
|
* ```
|
|
45
44
|
*/
|
|
46
45
|
export class ReActSingleInputOutputParser extends AgentActionOutputParser {
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StructuredChatAgent = void 0;
|
|
3
|
+
exports.createStructuredChatAgent = exports.StructuredChatAgent = void 0;
|
|
4
4
|
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
5
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
5
6
|
const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
|
|
6
7
|
const prompt_js_1 = require("../../prompts/prompt.cjs");
|
|
7
8
|
const chat_js_1 = require("../../prompts/chat.cjs");
|
|
8
9
|
const agent_js_1 = require("../agent.cjs");
|
|
9
10
|
const outputParser_js_1 = require("./outputParser.cjs");
|
|
10
11
|
const prompt_js_2 = require("./prompt.cjs");
|
|
12
|
+
const render_js_1 = require("../../tools/render.cjs");
|
|
13
|
+
const log_js_1 = require("../format_scratchpad/log.cjs");
|
|
11
14
|
/**
|
|
12
15
|
* Agent that interoperates with Structured Tools using React logic.
|
|
13
16
|
* @augments Agent
|
|
@@ -151,3 +154,84 @@ class StructuredChatAgent extends agent_js_1.Agent {
|
|
|
151
154
|
}
|
|
152
155
|
}
|
|
153
156
|
exports.StructuredChatAgent = StructuredChatAgent;
|
|
157
|
+
/**
|
|
158
|
+
* Create an agent aimed at supporting tools with multiple inputs.
|
|
159
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
160
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
161
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
162
|
+
* AgentAction or AgentFinish.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* import { AgentExecutor, createStructuredChatAgent } from "langchain/agents";
|
|
167
|
+
* import { pull } from "langchain/hub";
|
|
168
|
+
* import type { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
169
|
+
* import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
|
170
|
+
*
|
|
171
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
172
|
+
*
|
|
173
|
+
* // Define the tools the agent will have access to.
|
|
174
|
+
* const tools = [...];
|
|
175
|
+
*
|
|
176
|
+
* // Get the prompt to use - you can modify this!
|
|
177
|
+
* const prompt = await pull<ChatPromptTemplate>(
|
|
178
|
+
* "hwchase17/structured-chat-agent"
|
|
179
|
+
* );
|
|
180
|
+
*
|
|
181
|
+
* const llm = new ChatOpenAI({
|
|
182
|
+
* temperature: 0,
|
|
183
|
+
* modelName: "gpt-3.5-turbo-1106",
|
|
184
|
+
* });
|
|
185
|
+
*
|
|
186
|
+
* const agent = await createStructuredChatAgent({
|
|
187
|
+
* llm,
|
|
188
|
+
* tools,
|
|
189
|
+
* prompt,
|
|
190
|
+
* });
|
|
191
|
+
*
|
|
192
|
+
* const agentExecutor = new AgentExecutor({
|
|
193
|
+
* agent,
|
|
194
|
+
* tools,
|
|
195
|
+
* });
|
|
196
|
+
*
|
|
197
|
+
* const result = await agentExecutor.invoke({
|
|
198
|
+
* input: "what is LangChain?",
|
|
199
|
+
* });
|
|
200
|
+
*
|
|
201
|
+
* // With chat history
|
|
202
|
+
* const result2 = await agentExecutor.invoke({
|
|
203
|
+
* input: "what's my name?",
|
|
204
|
+
* chat_history: [
|
|
205
|
+
* new HumanMessage("hi! my name is cob"),
|
|
206
|
+
* new AIMessage("Hello Cob! How can I assist you today?"),
|
|
207
|
+
* ],
|
|
208
|
+
* });
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
async function createStructuredChatAgent({ llm, tools, prompt, }) {
|
|
212
|
+
const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
213
|
+
if (missingVariables.length > 0) {
|
|
214
|
+
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
215
|
+
}
|
|
216
|
+
const toolNames = tools.map((tool) => tool.name);
|
|
217
|
+
const partialedPrompt = await prompt.partial({
|
|
218
|
+
tools: (0, render_js_1.renderTextDescriptionAndArgs)(tools),
|
|
219
|
+
tool_names: toolNames.join(", "),
|
|
220
|
+
});
|
|
221
|
+
// TODO: Add .bind to core runnable interface.
|
|
222
|
+
const llmWithStop = llm.bind({
|
|
223
|
+
stop: ["Observation"],
|
|
224
|
+
});
|
|
225
|
+
const agent = runnables_1.RunnableSequence.from([
|
|
226
|
+
runnables_1.RunnablePassthrough.assign({
|
|
227
|
+
agent_scratchpad: (input) => (0, log_js_1.formatLogToString)(input.steps),
|
|
228
|
+
}),
|
|
229
|
+
partialedPrompt,
|
|
230
|
+
llmWithStop,
|
|
231
|
+
outputParser_js_1.StructuredChatOutputParserWithRetries.fromLLM(llm, {
|
|
232
|
+
toolNames,
|
|
233
|
+
}),
|
|
234
|
+
]);
|
|
235
|
+
return agent;
|
|
236
|
+
}
|
|
237
|
+
exports.createStructuredChatAgent = createStructuredChatAgent;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { StructuredToolInterface } from "@langchain/core/tools";
|
|
2
2
|
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
|
|
3
|
+
import { RunnableSequence } from "@langchain/core/runnables";
|
|
4
|
+
import type { BasePromptTemplate } from "@langchain/core/prompts";
|
|
3
5
|
import { BaseMessagePromptTemplate, ChatPromptTemplate } from "../../prompts/chat.js";
|
|
4
6
|
import { AgentStep } from "../../schema/index.js";
|
|
5
7
|
import { Optional } from "../../types/type-utils.js";
|
|
@@ -90,3 +92,72 @@ export declare class StructuredChatAgent extends Agent {
|
|
|
90
92
|
*/
|
|
91
93
|
static fromLLMAndTools(llm: BaseLanguageModelInterface, tools: StructuredToolInterface[], args?: StructuredChatCreatePromptArgs & AgentArgs): StructuredChatAgent;
|
|
92
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Params used by the createStructuredChatAgent function.
|
|
97
|
+
*/
|
|
98
|
+
export type CreateStructuredChatAgentParams = {
|
|
99
|
+
/** LLM to use as the agent. */
|
|
100
|
+
llm: BaseLanguageModelInterface;
|
|
101
|
+
/** Tools this agent has access to. */
|
|
102
|
+
tools: StructuredToolInterface[];
|
|
103
|
+
/**
|
|
104
|
+
* The prompt to use. Must have input keys for
|
|
105
|
+
* `tools`, `tool_names`, and `agent_scratchpad`.
|
|
106
|
+
*/
|
|
107
|
+
prompt: BasePromptTemplate;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Create an agent aimed at supporting tools with multiple inputs.
|
|
111
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
112
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
113
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
114
|
+
* AgentAction or AgentFinish.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* import { AgentExecutor, createStructuredChatAgent } from "langchain/agents";
|
|
119
|
+
* import { pull } from "langchain/hub";
|
|
120
|
+
* import type { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
121
|
+
* import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
|
122
|
+
*
|
|
123
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
124
|
+
*
|
|
125
|
+
* // Define the tools the agent will have access to.
|
|
126
|
+
* const tools = [...];
|
|
127
|
+
*
|
|
128
|
+
* // Get the prompt to use - you can modify this!
|
|
129
|
+
* const prompt = await pull<ChatPromptTemplate>(
|
|
130
|
+
* "hwchase17/structured-chat-agent"
|
|
131
|
+
* );
|
|
132
|
+
*
|
|
133
|
+
* const llm = new ChatOpenAI({
|
|
134
|
+
* temperature: 0,
|
|
135
|
+
* modelName: "gpt-3.5-turbo-1106",
|
|
136
|
+
* });
|
|
137
|
+
*
|
|
138
|
+
* const agent = await createStructuredChatAgent({
|
|
139
|
+
* llm,
|
|
140
|
+
* tools,
|
|
141
|
+
* prompt,
|
|
142
|
+
* });
|
|
143
|
+
*
|
|
144
|
+
* const agentExecutor = new AgentExecutor({
|
|
145
|
+
* agent,
|
|
146
|
+
* tools,
|
|
147
|
+
* });
|
|
148
|
+
*
|
|
149
|
+
* const result = await agentExecutor.invoke({
|
|
150
|
+
* input: "what is LangChain?",
|
|
151
|
+
* });
|
|
152
|
+
*
|
|
153
|
+
* // With chat history
|
|
154
|
+
* const result2 = await agentExecutor.invoke({
|
|
155
|
+
* input: "what's my name?",
|
|
156
|
+
* chat_history: [
|
|
157
|
+
* new HumanMessage("hi! my name is cob"),
|
|
158
|
+
* new AIMessage("Hello Cob! How can I assist you today?"),
|
|
159
|
+
* ],
|
|
160
|
+
* });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export declare function createStructuredChatAgent({ llm, tools, prompt, }: CreateStructuredChatAgentParams): Promise<RunnableSequence<Record<string, unknown>, import("../../schema/index.js").AgentAction | import("../../schema/index.js").AgentFinish>>;
|