-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.ts
178 lines (161 loc) Β· 5.77 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
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type { ToolInterface } from "@langchain/core/tools";
import {
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
} from "@langchain/core/prompts";
import type { AgentStep } from "@langchain/core/agents";
import { LLMChain } from "../../chains/llm_chain.js";
import { Optional } from "../../types/type-utils.js";
import { Agent, AgentArgs, OutputParserArgs } from "../agent.js";
import { AgentInput } from "../types.js";
import { ChatAgentOutputParser } from "./outputParser.js";
import { FORMAT_INSTRUCTIONS, PREFIX, SUFFIX } from "./prompt.js";
const DEFAULT_HUMAN_MESSAGE_TEMPLATE = "{input}\n\n{agent_scratchpad}";
/**
* Interface for arguments used to create a chat prompt.
* @deprecated
*/
export interface ChatCreatePromptArgs {
/** String to put after the list of tools. */
suffix?: string;
/** String to put before the list of tools. */
prefix?: string;
/** String to use directly as the human message template. */
humanMessageTemplate?: string;
/** Formattable string to use as the instructions template. */
formatInstructions?: string;
/** List of input variables the final prompt will expect. */
inputVariables?: string[];
}
/**
* Type for input data for creating a ChatAgent, extending AgentInput with
* optional 'outputParser'.
*
* @deprecated
*/
export type ChatAgentInput = 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 ChatAgent extends Agent {
static lc_name() {
return "ChatAgent";
}
lc_namespace = ["langchain", "agents", "chat"];
declare ToolType: ToolInterface;
constructor(input: ChatAgentInput) {
const outputParser =
input?.outputParser ?? ChatAgent.getDefaultOutputParser();
super({ ...input, outputParser });
}
_agentType() {
return "chat-zero-shot-react-description" as const;
}
observationPrefix() {
return "Observation: ";
}
llmPrefix() {
return "Thought:";
}
_stop(): string[] {
return ["Observation:"];
}
/**
* Validates that all tools have descriptions. Throws an error if a tool
* without a description is found.
* @param tools Array of Tool instances to validate.
* @returns void
*/
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);
}
}
/**
* Returns a default output parser for the ChatAgent.
* @param _fields Optional OutputParserArgs to customize the output parser.
* @returns ChatAgentOutputParser instance
*/
static getDefaultOutputParser(_fields?: OutputParserArgs) {
return new ChatAgentOutputParser();
}
/**
* Constructs the agent's scratchpad, which is a string representation of
* the agent's previous steps.
* @param steps Array of AgentStep instances representing the agent's previous steps.
* @returns Promise resolving to a string representing the agent's scratchpad.
*/
async constructScratchPad(steps: AgentStep[]): Promise<string> {
const agentScratchpad = await super.constructScratchPad(steps);
if (agentScratchpad) {
return `This was your previous work (but I haven't seen any of it! I only see what you return as final answer):\n${agentScratchpad}`;
}
return agentScratchpad;
}
/**
* Create prompt in the style of the zero shot agent.
*
* @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.suffix - String to put after the list of tools.
* @param args.prefix - String to put before the list of tools.
* @param args.humanMessageTemplate - String to use directly as the human message template
* @param args.formatInstructions - Formattable string to use as the instructions template
*/
static createPrompt(tools: ToolInterface[], args?: ChatCreatePromptArgs) {
const {
prefix = PREFIX,
suffix = SUFFIX,
humanMessageTemplate = DEFAULT_HUMAN_MESSAGE_TEMPLATE,
formatInstructions = FORMAT_INSTRUCTIONS,
} = args ?? {};
const toolStrings = tools
.map((tool) => `${tool.name}: ${tool.description}`)
.join("\n");
const template = [prefix, toolStrings, formatInstructions, suffix].join(
"\n\n"
);
const messages = [
SystemMessagePromptTemplate.fromTemplate(template),
HumanMessagePromptTemplate.fromTemplate(humanMessageTemplate),
];
return ChatPromptTemplate.fromMessages(messages);
}
/**
* Creates a ChatAgent instance using a language model, tools, and
* optional arguments.
* @param llm BaseLanguageModelInterface instance to use in the agent.
* @param tools Array of Tool instances to include in the agent.
* @param args Optional arguments to customize the agent and prompt.
* @returns ChatAgent instance
*/
static fromLLMAndTools(
llm: BaseLanguageModelInterface,
tools: ToolInterface[],
args?: ChatCreatePromptArgs & AgentArgs
) {
ChatAgent.validateTools(tools);
const prompt = ChatAgent.createPrompt(tools, args);
const chain = new LLMChain({
prompt,
llm,
callbacks: args?.callbacks ?? args?.callbackManager,
});
const outputParser =
args?.outputParser ?? ChatAgent.getDefaultOutputParser();
return new ChatAgent({
llmChain: chain,
outputParser,
allowedTools: tools.map((t) => t.name),
});
}
}