-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathinitialize.ts
235 lines (230 loc) Β· 7.95 KB
/
initialize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type {
StructuredToolInterface,
ToolInterface,
} from "@langchain/core/tools";
import { CallbackManager } from "@langchain/core/callbacks/manager";
import { BufferMemory } from "../memory/buffer_memory.js";
import { ChatAgent } from "./chat/index.js";
import { ChatConversationalAgent } from "./chat_convo/index.js";
import { StructuredChatAgent } from "./structured_chat/index.js";
import { AgentExecutor, AgentExecutorInput } from "./executor.js";
import { ZeroShotAgent } from "./mrkl/index.js";
import { OpenAIAgent } from "./openai_functions/index.js";
import { XMLAgent } from "./xml/index.js";
/**
* Represents the type of an agent in LangChain. It can be
* "zero-shot-react-description", "chat-zero-shot-react-description", or
* "chat-conversational-react-description".
*/
type AgentType =
| "zero-shot-react-description"
| "chat-zero-shot-react-description"
| "chat-conversational-react-description";
/**
* @deprecated See {@link https://js.langchain.com/docs/modules/agents/agent_types/ | new agent creation docs}.
*/
export const initializeAgentExecutor = async (
tools: ToolInterface[],
llm: BaseLanguageModelInterface,
_agentType?: AgentType,
_verbose?: boolean,
_callbackManager?: CallbackManager
): Promise<AgentExecutor> => {
const agentType = _agentType ?? "zero-shot-react-description";
const verbose = _verbose;
const callbackManager = _callbackManager;
switch (agentType) {
case "zero-shot-react-description":
return AgentExecutor.fromAgentAndTools({
agent: ZeroShotAgent.fromLLMAndTools(llm, tools),
tools,
returnIntermediateSteps: true,
verbose,
callbackManager,
});
case "chat-zero-shot-react-description":
return AgentExecutor.fromAgentAndTools({
agent: ChatAgent.fromLLMAndTools(llm, tools),
tools,
returnIntermediateSteps: true,
verbose,
callbackManager,
});
case "chat-conversational-react-description":
return AgentExecutor.fromAgentAndTools({
agent: ChatConversationalAgent.fromLLMAndTools(llm, tools),
tools,
verbose,
callbackManager,
});
default:
throw new Error("Unknown agent type");
}
};
/**
* @interface
*/
export type InitializeAgentExecutorOptions =
| ({
agentType: "zero-shot-react-description";
agentArgs?: Parameters<typeof ZeroShotAgent.fromLLMAndTools>[2];
memory?: never;
} & Omit<AgentExecutorInput, "agent" | "tools">)
| ({
agentType: "chat-zero-shot-react-description";
agentArgs?: Parameters<typeof ChatAgent.fromLLMAndTools>[2];
memory?: never;
} & Omit<AgentExecutorInput, "agent" | "tools">)
| ({
agentType: "chat-conversational-react-description";
agentArgs?: Parameters<typeof ChatConversationalAgent.fromLLMAndTools>[2];
} & Omit<AgentExecutorInput, "agent" | "tools">)
| ({
agentType: "xml";
agentArgs?: Parameters<typeof XMLAgent.fromLLMAndTools>[2];
} & Omit<AgentExecutorInput, "agent" | "tools">);
/**
* @interface
*/
export type InitializeAgentExecutorOptionsStructured =
| ({
agentType: "structured-chat-zero-shot-react-description";
agentArgs?: Parameters<typeof StructuredChatAgent.fromLLMAndTools>[2];
} & Omit<AgentExecutorInput, "agent" | "tools">)
| ({
agentType: "openai-functions";
agentArgs?: Parameters<typeof OpenAIAgent.fromLLMAndTools>[2];
} & Omit<AgentExecutorInput, "agent" | "tools">);
/**
* Initialize an agent executor with options.
* @deprecated See {@link https://js.langchain.com/docs/modules/agents/agent_types/ | new agent creation docs}.
* @param tools Array of tools to use in the agent
* @param llm LLM or ChatModel to use in the agent
* @param options Options for the agent, including agentType, agentArgs, and other options for AgentExecutor.fromAgentAndTools
* @returns AgentExecutor
*/
export async function initializeAgentExecutorWithOptions(
tools: StructuredToolInterface[],
llm: BaseLanguageModelInterface,
options: InitializeAgentExecutorOptionsStructured
): Promise<AgentExecutor>;
/** @deprecated See {@link https://js.langchain.com/docs/modules/agents/agent_types/ | new agent creation docs}. */
export async function initializeAgentExecutorWithOptions(
tools: ToolInterface[],
llm: BaseLanguageModelInterface,
options?: InitializeAgentExecutorOptions
): Promise<AgentExecutor>;
/** @deprecated See {@link https://js.langchain.com/docs/modules/agents/agent_types/ | new agent creation docs}. */
export async function initializeAgentExecutorWithOptions(
tools: StructuredToolInterface[] | ToolInterface[],
llm: BaseLanguageModelInterface,
options:
| InitializeAgentExecutorOptions
| InitializeAgentExecutorOptionsStructured = {
agentType:
llm._modelType() === "base_chat_model"
? "chat-zero-shot-react-description"
: "zero-shot-react-description",
}
): Promise<AgentExecutor> {
// Note this tools cast is safe as the overload signatures prevent
// the function from being called with a StructuredTool[] when
// the agentType is not in InitializeAgentExecutorOptionsStructured
switch (options.agentType) {
case "zero-shot-react-description": {
const { agentArgs, tags, ...rest } = options;
return AgentExecutor.fromAgentAndTools({
tags: [...(tags ?? []), "zero-shot-react-description"],
agent: ZeroShotAgent.fromLLMAndTools(
llm,
tools as ToolInterface[],
agentArgs
),
tools,
...rest,
});
}
case "chat-zero-shot-react-description": {
const { agentArgs, tags, ...rest } = options;
return AgentExecutor.fromAgentAndTools({
tags: [...(tags ?? []), "chat-zero-shot-react-description"],
agent: ChatAgent.fromLLMAndTools(
llm,
tools as ToolInterface[],
agentArgs
),
tools,
...rest,
});
}
case "chat-conversational-react-description": {
const { agentArgs, memory, tags, ...rest } = options;
const executor = AgentExecutor.fromAgentAndTools({
tags: [...(tags ?? []), "chat-conversational-react-description"],
agent: ChatConversationalAgent.fromLLMAndTools(
llm,
tools as ToolInterface[],
agentArgs
),
tools,
memory:
memory ??
new BufferMemory({
returnMessages: true,
memoryKey: "chat_history",
inputKey: "input",
outputKey: "output",
}),
...rest,
});
return executor;
}
case "xml": {
const { agentArgs, tags, ...rest } = options;
const executor = AgentExecutor.fromAgentAndTools({
tags: [...(tags ?? []), "xml"],
agent: XMLAgent.fromLLMAndTools(
llm,
tools as ToolInterface[],
agentArgs
),
tools,
...rest,
});
return executor;
}
case "structured-chat-zero-shot-react-description": {
const { agentArgs, memory, tags, ...rest } = options;
const executor = AgentExecutor.fromAgentAndTools({
tags: [...(tags ?? []), "structured-chat-zero-shot-react-description"],
agent: StructuredChatAgent.fromLLMAndTools(llm, tools, agentArgs),
tools,
memory,
...rest,
});
return executor;
}
case "openai-functions": {
const { agentArgs, memory, tags, ...rest } = options;
const executor = AgentExecutor.fromAgentAndTools({
tags: [...(tags ?? []), "openai-functions"],
agent: OpenAIAgent.fromLLMAndTools(llm, tools, agentArgs),
tools,
memory:
memory ??
new BufferMemory({
returnMessages: true,
memoryKey: "chat_history",
inputKey: "input",
outputKey: "output",
}),
...rest,
});
return executor;
}
default: {
throw new Error("Unknown agent type");
}
}
}