-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.ts
240 lines (226 loc) Β· 6.96 KB
/
index.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
236
237
238
239
240
import type {
BaseLanguageModel,
BaseLanguageModelInterface,
} from "@langchain/core/language_models/base";
import type { ToolInterface } from "@langchain/core/tools";
import { RunnablePassthrough } from "@langchain/core/runnables";
import type { BasePromptTemplate } from "@langchain/core/prompts";
import { AgentStep, AgentAction, AgentFinish } from "@langchain/core/agents";
import { ChainValues } from "@langchain/core/utils/types";
import {
AIMessagePromptTemplate,
ChatPromptTemplate,
HumanMessagePromptTemplate,
} from "@langchain/core/prompts";
import { CallbackManager } from "@langchain/core/callbacks/manager";
import { LLMChain } from "../../chains/llm_chain.js";
import {
AgentArgs,
AgentRunnableSequence,
BaseSingleActionAgent,
} from "../agent.js";
import { AGENT_INSTRUCTIONS } from "./prompt.js";
import { XMLAgentOutputParser } from "./output_parser.js";
import { renderTextDescription } from "../../tools/render.js";
import { formatXml } from "../format_scratchpad/xml.js";
/**
* Interface for the input to the XMLAgent class.
*/
export interface XMLAgentInput {
tools: ToolInterface[];
llmChain: LLMChain;
}
/**
* Class that represents an agent that uses XML tags.
*
* @deprecated Use the {@link https://api.js.langchain.com/functions/langchain_agents.createXmlAgent.html | createXmlAgent method instead}.
*/
export class XMLAgent extends BaseSingleActionAgent implements XMLAgentInput {
static lc_name() {
return "XMLAgent";
}
lc_namespace = ["langchain", "agents", "xml"];
tools: ToolInterface[];
llmChain: LLMChain;
outputParser: XMLAgentOutputParser = new XMLAgentOutputParser();
_agentType() {
return "xml" as const;
}
constructor(fields: XMLAgentInput) {
super(fields);
this.tools = fields.tools;
this.llmChain = fields.llmChain;
}
get inputKeys() {
return ["input"];
}
static createPrompt() {
return ChatPromptTemplate.fromMessages([
HumanMessagePromptTemplate.fromTemplate(AGENT_INSTRUCTIONS),
AIMessagePromptTemplate.fromTemplate("{intermediate_steps}"),
]);
}
/**
* Plans the next action or finish state of the agent based on the
* provided steps, inputs, and optional callback manager.
* @param steps The steps to consider in planning.
* @param inputs The inputs to consider in planning.
* @param callbackManager Optional CallbackManager to use in planning.
* @returns A Promise that resolves to an AgentAction or AgentFinish object representing the planned action or finish state.
*/
async plan(
steps: AgentStep[],
inputs: ChainValues,
callbackManager?: CallbackManager
): Promise<AgentAction | AgentFinish> {
let log = "";
for (const { action, observation } of steps) {
log += `<tool>${action.tool}</tool><tool_input>${action.toolInput}</tool_input><observation>${observation}</observation>`;
}
let tools = "";
for (const tool of this.tools) {
tools += `${tool.name}: ${tool.description}\n`;
}
const _inputs = {
intermediate_steps: log,
tools,
question: inputs.input,
stop: ["</tool_input>", "</final_answer>"],
};
const response = await this.llmChain.call(_inputs, callbackManager);
return this.outputParser.parse(response[this.llmChain.outputKey]);
}
/**
* Creates an XMLAgent from a BaseLanguageModel and a list of tools.
* @param llm The BaseLanguageModel to use.
* @param tools The tools to be used by the agent.
* @param args Optional arguments for creating the agent.
* @returns An instance of XMLAgent.
*/
static fromLLMAndTools(
llm: BaseLanguageModelInterface,
tools: ToolInterface[],
args?: XMLAgentInput & Pick<AgentArgs, "callbacks">
) {
const prompt = XMLAgent.createPrompt();
const chain = new LLMChain({
prompt,
llm,
callbacks: args?.callbacks,
});
return new XMLAgent({
llmChain: chain,
tools,
});
}
}
/**
* Params used by the createXmlAgent function.
*/
export type CreateXmlAgentParams = {
/** LLM to use for the agent. */
llm: BaseLanguageModelInterface;
/** Tools this agent has access to. */
tools: ToolInterface[];
/**
* The prompt to use. Must have input keys for
* `tools` and `agent_scratchpad`.
*/
prompt: BasePromptTemplate;
/**
* Whether to invoke the underlying model in streaming mode,
* allowing streaming of intermediate steps. Defaults to true.
*/
streamRunnable?: boolean;
};
/**
* Create an agent that uses XML to format its logic.
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
* @returns A runnable sequence representing an agent. It takes as input all the same input
* variables as the prompt passed in does. It returns as output either an
* AgentAction or AgentFinish.
*
* @example
* ```typescript
* import { AgentExecutor, createXmlAgent } from "langchain/agents";
* import { pull } from "langchain/hub";
* import type { PromptTemplate } from "@langchain/core/prompts";
*
* import { ChatAnthropic } from "@langchain/anthropic";
*
* // Define the tools the agent will have access to.
* const tools = [...];
*
* // Get the prompt to use - you can modify this!
* // If you want to see the prompt in full, you can at:
* // https://smith.langchain.com/hub/hwchase17/xml-agent-convo
* const prompt = await pull<PromptTemplate>("hwchase17/xml-agent-convo");
*
* const llm = new ChatAnthropic({
* temperature: 0,
* });
*
* const agent = await createXmlAgent({
* llm,
* tools,
* prompt,
* });
*
* const agentExecutor = new AgentExecutor({
* agent,
* tools,
* });
*
* const result = await agentExecutor.invoke({
* input: "what is LangChain?",
* });
*
* // With chat history
* const result2 = await agentExecutor.invoke({
* input: "what's my name?",
* // Notice that chat_history is a string, since this prompt is aimed at LLMs, not chat models
* chat_history: "Human: Hi! My name is Cob\nAI: Hello Cob! Nice to meet you",
* });
* ```
*/
export async function createXmlAgent({
llm,
tools,
prompt,
streamRunnable,
}: CreateXmlAgentParams) {
const missingVariables = ["tools", "agent_scratchpad"].filter(
(v) => !prompt.inputVariables.includes(v)
);
if (missingVariables.length > 0) {
throw new Error(
`Provided prompt is missing required input variables: ${JSON.stringify(
missingVariables
)}`
);
}
const partialedPrompt = await prompt.partial({
tools: renderTextDescription(tools),
});
// TODO: Add .bind to core runnable interface.
const llmWithStop = (llm as BaseLanguageModel).bind({
stop: ["</tool_input>", "</final_answer>"],
});
const agent = AgentRunnableSequence.fromRunnables(
[
RunnablePassthrough.assign({
agent_scratchpad: (input: { steps: AgentStep[] }) =>
formatXml(input.steps),
}),
partialedPrompt,
llmWithStop,
new XMLAgentOutputParser(),
],
{
name: "XMLAgent",
streamRunnable,
singleAction: true,
}
);
return agent;
}