-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.ts
119 lines (107 loc) · 3.32 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
import { Tool } from "../../tools/base.js";
import { LLMChain } from "../../chains/llm_chain.js";
import {
AgentStep,
AgentAction,
AgentFinish,
ChainValues,
} from "../../schema/index.js";
import {
AIMessagePromptTemplate,
ChatPromptTemplate,
HumanMessagePromptTemplate,
} from "../../prompts/chat.js";
import { AgentArgs, BaseSingleActionAgent } from "../agent.js";
import { AGENT_INSTRUCTIONS } from "./prompt.js";
import { CallbackManager } from "../../callbacks/manager.js";
import { BaseLanguageModel } from "../../base_language/index.js";
import { XMLAgentOutputParser } from "./output_parser.js";
/**
* Interface for the input to the XMLAgent class.
*/
export interface XMLAgentInput {
tools: Tool[];
llmChain: LLMChain;
}
/**
* Class that represents an agent that uses XML tags.
*/
export class XMLAgent extends BaseSingleActionAgent implements XMLAgentInput {
static lc_name() {
return "XMLAgent";
}
lc_namespace = ["langchain", "agents", "xml"];
tools: Tool[];
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: BaseLanguageModel,
tools: Tool[],
args?: XMLAgentInput & Pick<AgentArgs, "callbacks">
) {
const prompt = XMLAgent.createPrompt();
const chain = new LLMChain({
prompt,
llm,
callbacks: args?.callbacks,
});
return new XMLAgent({
llmChain: chain,
tools,
});
}
}