-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.ts
217 lines (202 loc) Β· 6.88 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
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type { ToolInterface } from "@langchain/core/tools";
import {
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
renderTemplate,
} from "@langchain/core/prompts";
import type { AgentStep } from "@langchain/core/agents";
import {
type BaseMessage,
HumanMessage,
AIMessage,
} from "@langchain/core/messages";
import { LLMChain } from "../../chains/llm_chain.js";
import { Optional } from "../../types/type-utils.js";
import { Agent, AgentArgs, OutputParserArgs } from "../agent.js";
import { AgentActionOutputParser, AgentInput } from "../types.js";
import { ChatConversationalAgentOutputParserWithRetries } from "./outputParser.js";
import {
PREFIX_END,
DEFAULT_PREFIX,
DEFAULT_SUFFIX,
TEMPLATE_TOOL_RESPONSE,
} from "./prompt.js";
/**
* Interface defining the structure of arguments used to create a prompt
* for the ChatConversationalAgent class.
*/
export interface ChatConversationalCreatePromptArgs {
/** String to put after the list of tools. */
systemMessage?: string;
/** String to put before the list of tools. */
humanMessage?: string;
/** List of input variables the final prompt will expect. */
inputVariables?: string[];
/** Output parser to use for formatting. */
outputParser?: AgentActionOutputParser;
}
/**
* Type that extends the AgentInput interface for the
* ChatConversationalAgent class, making the outputParser property
* optional.
*/
export type ChatConversationalAgentInput = Optional<AgentInput, "outputParser">;
/**
* Agent for the MRKL chain.
* @augments Agent
*
* @deprecated Use the {@link https://api.js.langchain.com/functions/langchain_agents.createStructuredChatAgent.html | createStructuredChatAgent method instead}.
*/
export class ChatConversationalAgent extends Agent {
static lc_name() {
return "ChatConversationalAgent";
}
lc_namespace = ["langchain", "agents", "chat_convo"];
declare ToolType: ToolInterface;
constructor(input: ChatConversationalAgentInput) {
const outputParser =
input.outputParser ?? ChatConversationalAgent.getDefaultOutputParser();
super({ ...input, outputParser });
}
_agentType() {
return "chat-conversational-react-description" as const;
}
observationPrefix() {
return "Observation: ";
}
llmPrefix() {
return "Thought:";
}
_stop(): string[] {
return ["Observation:"];
}
static validateTools(tools: ToolInterface[]) {
const descriptionlessTool = tools.find((tool) => !tool.description);
if (descriptionlessTool) {
const msg =
`Got a tool ${descriptionlessTool.name} without a description.` +
` This agent requires descriptions for all tools.`;
throw new Error(msg);
}
}
/**
* Constructs the agent scratchpad based on the agent steps. It returns an
* array of base messages representing the thoughts of the agent.
* @param steps The agent steps to construct the scratchpad from.
* @returns An array of base messages representing the thoughts of the agent.
*/
async constructScratchPad(steps: AgentStep[]): Promise<BaseMessage[]> {
const thoughts: BaseMessage[] = [];
for (const step of steps) {
thoughts.push(new AIMessage(step.action.log));
thoughts.push(
new HumanMessage(
renderTemplate(TEMPLATE_TOOL_RESPONSE, "f-string", {
observation: step.observation,
})
)
);
}
return thoughts;
}
/**
* Returns the default output parser for the ChatConversationalAgent
* class. It takes optional fields as arguments to customize the output
* parser.
* @param fields Optional fields to customize the output parser.
* @returns The default output parser for the ChatConversationalAgent class.
*/
static getDefaultOutputParser(
fields?: OutputParserArgs & {
toolNames: string[];
}
): AgentActionOutputParser {
if (fields?.llm) {
return ChatConversationalAgentOutputParserWithRetries.fromLLM(
fields.llm,
{
toolNames: fields.toolNames,
}
);
}
return new ChatConversationalAgentOutputParserWithRetries({
toolNames: fields?.toolNames,
});
}
/**
* Create prompt in the style of the ChatConversationAgent.
*
* @param tools - List of tools the agent will have access to, used to format the prompt.
* @param args - Arguments to create the prompt with.
* @param args.systemMessage - String to put before the list of tools.
* @param args.humanMessage - String to put after the list of tools.
* @param args.outputParser - Output parser to use for formatting.
*/
static createPrompt(
tools: ToolInterface[],
args?: ChatConversationalCreatePromptArgs
) {
const systemMessage = (args?.systemMessage ?? DEFAULT_PREFIX) + PREFIX_END;
const humanMessage = args?.humanMessage ?? DEFAULT_SUFFIX;
const toolStrings = tools
.map((tool) => `${tool.name}: ${tool.description}`)
.join("\n");
const toolNames = tools.map((tool) => tool.name);
const outputParser =
args?.outputParser ??
ChatConversationalAgent.getDefaultOutputParser({ toolNames });
const formatInstructions = outputParser.getFormatInstructions({
toolNames,
});
const renderedHumanMessage = renderTemplate(humanMessage, "f-string", {
format_instructions: formatInstructions,
tools: toolStrings,
});
const messages = [
SystemMessagePromptTemplate.fromTemplate(systemMessage),
new MessagesPlaceholder("chat_history"),
HumanMessagePromptTemplate.fromTemplate(renderedHumanMessage),
new MessagesPlaceholder("agent_scratchpad"),
];
return ChatPromptTemplate.fromMessages(messages);
}
/**
* Creates an instance of the ChatConversationalAgent class from a
* BaseLanguageModel and a set of tools. It takes optional arguments to
* customize the agent.
* @param llm The BaseLanguageModel to create the agent from.
* @param tools The set of tools to create the agent from.
* @param args Optional arguments to customize the agent.
* @returns An instance of the ChatConversationalAgent class.
*/
static fromLLMAndTools(
llm: BaseLanguageModelInterface,
tools: ToolInterface[],
args?: ChatConversationalCreatePromptArgs & AgentArgs
) {
ChatConversationalAgent.validateTools(tools);
const outputParser =
args?.outputParser ??
ChatConversationalAgent.getDefaultOutputParser({
llm,
toolNames: tools.map((tool) => tool.name),
});
const prompt = ChatConversationalAgent.createPrompt(tools, {
...args,
outputParser,
});
const chain = new LLMChain({
prompt,
llm,
callbacks: args?.callbacks ?? args?.callbackManager,
});
return new ChatConversationalAgent({
llmChain: chain,
outputParser,
allowedTools: tools.map((t) => t.name),
});
}
}