-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathagent.int.test.ts
374 lines (330 loc) Β· 11 KB
/
agent.int.test.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* eslint-disable no-process-env */
import { expect, test } from "@jest/globals";
import { OpenAI, OpenAIEmbeddings, ChatOpenAI } from "@langchain/openai";
import { Tool } from "@langchain/core/tools";
import { RunnableSequence } from "@langchain/core/runnables";
import { OutputParserException } from "@langchain/core/output_parsers";
import { AIMessage } from "@langchain/core/messages";
import { AgentStep } from "@langchain/core/agents";
import { ChatMessageHistory } from "../../stores/message/in_memory.js";
import { AgentExecutor, ZeroShotAgent } from "../index.js";
import { SerpAPI } from "../../util/testing/tools/serpapi.js";
import { Calculator } from "../../util/testing/tools/calculator.js";
import { initializeAgentExecutorWithOptions } from "../initialize.js";
import { WebBrowser } from "../../tools/webbrowser.js";
import { BufferMemory } from "../../memory/buffer_memory.js";
test("Pass runnable to agent executor", async () => {
const model = new ChatOpenAI({ temperature: 0, modelName: "gpt-3.5-turbo" });
const tools: Tool[] = [
new SerpAPI(undefined, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];
const prompt = ZeroShotAgent.createPrompt(tools);
const outputParser = ZeroShotAgent.getDefaultOutputParser();
const runnable = RunnableSequence.from([
{
input: (i: { input: string }) => i.input,
agent_scratchpad: (i: { input: string }) => i.input,
},
prompt,
model,
outputParser,
]);
const executor = AgentExecutor.fromAgentAndTools({
agent: runnable,
tools,
});
const res = await executor.invoke({
input:
"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
});
// console.log(
// {
// res,
// },
// "Pass runnable to agent executor"
// );
expect(res.output).not.toEqual("");
expect(res.output).not.toEqual("Agent stopped due to max iterations.");
});
test("Custom output parser", async () => {
const model = new ChatOpenAI({ temperature: 0, modelName: "gpt-3.5-turbo" });
const tools: Tool[] = [
new SerpAPI(undefined, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];
const parser = (output: AIMessage) => {
const text = output.content;
if (typeof text !== "string") {
throw new Error("Cannot parse non-string output.");
}
if (text.includes("Final Answer:")) {
return {
returnValues: {
output: "We did it!",
},
log: text,
};
}
const match = /Action:([\s\S]*?)(?:\nAction Input:([\s\S]*?))?$/.exec(text);
if (!match) {
throw new OutputParserException(`Could not parse LLM output: ${text}`);
}
return {
tool: match[1].trim(),
toolInput: match[2]
? match[2].trim().replace(/^("+)(.*?)(\1)$/, "$2")
: "",
log: text,
};
};
const prompt = ZeroShotAgent.createPrompt(tools);
const runnable = RunnableSequence.from([
{
input: (i: { input: string }) => i.input,
agent_scratchpad: (i: { input: string }) => i.input,
},
prompt,
model,
parser,
]);
const executor = AgentExecutor.fromAgentAndTools({
agent: runnable,
tools,
});
const res = await executor.invoke({
input:
"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
});
// console.log(
// {
// res,
// },
// "Custom output parser"
// );
expect(res.output).toEqual("We did it!");
});
test("Add a fallback method", async () => {
// Model should always fail since the model name passed does not exist.
const modelBase = new ChatOpenAI({
modelName: "fake-model",
temperature: 10,
});
const modelLarge = new ChatOpenAI({
modelName: "gpt-3.5-turbo-16k",
temperature: 0.6,
});
const model = modelBase.withFallbacks({
fallbacks: [modelLarge],
});
const prompt = ZeroShotAgent.createPrompt([]);
const outputParser = ZeroShotAgent.getDefaultOutputParser();
const runnable = RunnableSequence.from([
{
input: (i: { input: string }) => i.input,
agent_scratchpad: (i: { input: string }) => i.input,
},
prompt,
model,
outputParser,
]);
const executor = AgentExecutor.fromAgentAndTools({
agent: runnable,
tools: [],
});
const res = await executor.invoke({
input: "Is the sky blue? Response with a concise answer",
});
// console.log(
// {
// res,
// },
// "Pass runnable to agent executor"
// );
expect(res.output).not.toEqual("");
expect(res.output).not.toEqual("Agent stopped due to max iterations.");
});
test("Run agent with an abort signal", async () => {
const model = new OpenAI({ temperature: 0, modelName: "text-babbage-001" });
const tools = [new Calculator()];
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: "zero-shot-react-description",
});
// console.log("Loaded agent.");
const input = `What is 3 to the fourth power?`;
// console.log(`Executing with input "${input}"...`);
const controller = new AbortController();
await expect(() => {
const result = executor.call({ input, signal: controller.signal });
controller.abort();
return result;
}).rejects.toThrow();
});
test("Run agent with incorrect api key should throw error", async () => {
const model = new OpenAI({
temperature: 0,
modelName: "text-babbage-001",
openAIApiKey: "invalid",
});
const tools = [
new SerpAPI(undefined, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: "zero-shot-react-description",
});
// console.log("Loaded agent.");
const input = `Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?`;
let error;
// Test that the model throws an error
await expect(async () => {
try {
await model.invoke(input);
} catch (e) {
error = e;
throw e;
}
}).rejects.toThrowError();
// Test that the agent throws the same error
await expect(() => executor.call({ input })).rejects.toThrowError(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error as any).message
);
}, 10000);
test("Run tool web-browser", async () => {
const model = new OpenAI({ temperature: 0 });
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
new WebBrowser({ model, embeddings: new OpenAIEmbeddings() }),
];
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: "zero-shot-react-description",
returnIntermediateSteps: true,
});
// console.log("Loaded agent.");
const input = `What is the word of the day on merriam webster`;
// console.log(`Executing with input "${input}"...`);
const result = await executor.call({ input });
// console.log(
// {
// result,
// },
// "Run tool web-browser"
// );
expect(result.intermediateSteps.length).toBeGreaterThanOrEqual(1);
expect(result.intermediateSteps[0].action.tool).toEqual("search");
expect(result.intermediateSteps[1].action.tool).toEqual("web-browser");
expect(result.output).not.toEqual("");
expect(result.output).not.toEqual("Agent stopped due to max iterations.");
});
test("Agent can stream", async () => {
const model = new ChatOpenAI({
temperature: 0,
modelName: "gpt-4-1106-preview",
streaming: true,
});
const tools = [
new Calculator(),
new WebBrowser({ model, embeddings: new OpenAIEmbeddings() }),
];
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: "zero-shot-react-description",
returnIntermediateSteps: false,
});
// console.log("Loaded agent.");
const input = `What is the word of the day on merriam webster`;
// console.log(`Executing with input "${input}"...`);
const result = await executor.stream({ input });
let streamIters = 0;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const finalResponse: any = [];
for await (const item of result) {
streamIters += 1;
// console.log("Stream item:", item);
// each stream does NOT contain the previous steps,
// because returnIntermediateSteps is false so we
// push each new stream item to the array.
finalResponse.push(item);
}
// The last item should contain "output"
expect("output" in finalResponse[finalResponse.length - 1]).toBeTruthy();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const intermediateSteps = finalResponse.flatMap((item: any) => {
if ("intermediateSteps" in item) {
return item.intermediateSteps;
}
return [];
});
expect(streamIters).toBeGreaterThan(1);
const toolsUsed: Array<string> = intermediateSteps.map(
(step: AgentStep) => step.action.tool
);
// the last tool used should be the web-browser
expect(toolsUsed?.[toolsUsed.length - 1]).toEqual("web-browser");
});
test("Agent can stream with chat messages", async () => {
const model = new ChatOpenAI({
temperature: 0,
modelName: "gpt-4-1106-preview",
streaming: true,
});
const tools = [
new Calculator(),
new WebBrowser({ model, embeddings: new OpenAIEmbeddings() }),
];
const memory = new BufferMemory({
chatHistory: new ChatMessageHistory([]),
memoryKey: "chat_history",
inputKey: "input",
outputKey: "output",
returnMessages: true,
});
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: "chat-conversational-react-description",
returnIntermediateSteps: true,
memory,
});
// console.log("Loaded agent.");
const input = `What is the word of the day on merriam webster, and what is the sum of all letter indices (relative to the english alphabet) in the word?`;
// console.log(`Executing with input "${input}"...`);
const result = await executor.stream({ input, chat_history: [] });
let streamIters = 0;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let finalResponse: any;
for await (const item of result) {
streamIters += 1;
// console.log("Stream item:", item);
// each stream contains the previous steps
// because returnIntermediateSteps is true),
// so we can overwrite on each stream.
finalResponse = item;
}
// console.log("__finalResponse__", finalResponse);
expect("intermediateSteps" in finalResponse).toBeTruthy();
expect("output" in finalResponse).toBeTruthy();
expect(streamIters).toBeGreaterThan(1);
const toolsUsed: Array<string> = finalResponse.intermediateSteps.map(
(step: AgentStep) => step.action.tool
);
// the first tool used should be web-browser, and last should be calculator.
// This can be flaky so if the test is failing, inspect these conditions first.
expect(toolsUsed?.[toolsUsed.length - 1]).toEqual("calculator");
expect(toolsUsed?.[0]).toEqual("web-browser");
});