-
Notifications
You must be signed in to change notification settings - Fork 375
/
react.ts
413 lines (380 loc) · 10.7 KB
/
react.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { randomUUID, ReadableStream } from "@llamaindex/env";
import { getReACTAgentSystemHeader } from "../internal/prompt/react.js";
import {
isAsyncIterable,
stringifyJSONToMessageContent,
} from "../internal/utils.js";
import {
type ChatMessage,
type ChatResponse,
type ChatResponseChunk,
type LLM,
} from "../llm/index.js";
import { extractText } from "../llm/utils.js";
import { ObjectRetriever } from "../objects/index.js";
import { Settings } from "../Settings.js";
import type {
BaseTool,
BaseToolWithCall,
JSONObject,
JSONValue,
} from "../types.js";
import { AgentRunner, AgentWorker, type AgentParamsBase } from "./base.js";
import type { TaskHandler } from "./types.js";
import {
callTool,
consumeAsyncIterable,
createReadableStream,
} from "./utils.js";
type ReACTAgentParamsBase = AgentParamsBase<LLM>;
type ReACTAgentParamsWithTools = ReACTAgentParamsBase & {
tools: BaseToolWithCall[];
};
type ReACTAgentParamsWithToolRetriever = ReACTAgentParamsBase & {
toolRetriever: ObjectRetriever<BaseToolWithCall>;
};
export type ReACTAgentParams =
| ReACTAgentParamsWithTools
| ReACTAgentParamsWithToolRetriever;
type BaseReason = {
type: unknown;
};
type ObservationReason = BaseReason & {
type: "observation";
observation: JSONValue;
};
type ActionReason = BaseReason & {
type: "action";
thought: string;
action: string;
input: JSONObject;
};
type ResponseReason = BaseReason & {
type: "response";
thought: string;
response: ChatResponse;
};
type Reason = ObservationReason | ActionReason | ResponseReason;
function reasonFormatter(reason: Reason): string | Promise<string> {
switch (reason.type) {
case "observation":
return `Observation: ${stringifyJSONToMessageContent(reason.observation)}`;
case "action":
return `Thought: ${reason.thought}\nAction: ${reason.action}\nInput: ${stringifyJSONToMessageContent(
reason.input,
)}`;
case "response": {
return `Thought: ${reason.thought}\nAnswer: ${extractText(
reason.response.message.content,
)}`;
}
}
}
function extractJsonStr(text: string): string {
const pattern = /\{.*}/s;
const match = text.match(pattern);
if (!match) {
throw new SyntaxError(`Could not extract json string from output: ${text}`);
}
return match[0];
}
function extractFinalResponse(
inputText: string,
): [thought: string, answer: string] {
const pattern = /\s*Thought:(.*?)Answer:(.*?)$/s;
const match = inputText.match(pattern);
if (!match) {
throw new Error(
`Could not extract final answer from input text: ${inputText}`,
);
}
const thought = match[1].trim();
const answer = match[2].trim();
return [thought, answer];
}
function extractToolUse(
inputText: string,
): [thought: string, action: string, input: string] {
const pattern =
/\s*Thought: (.*?)\nAction: ([a-zA-Z0-9_]+).*?\.*Input: .*?(\{.*?})/s;
const match = inputText.match(pattern);
if (!match) {
throw new Error(
`Could not extract tool use from input text: "${inputText}"`,
);
}
const thought = match[1].trim();
const action = match[2].trim();
const actionInput = match[3].trim();
return [thought, action, actionInput];
}
function actionInputParser(jsonStr: string): JSONObject {
const processedString = jsonStr.replace(/(?<!\w)'|'(?!\w)/g, '"');
const pattern = /"(\w+)":\s*"([^"]*)"/g;
const matches = [...processedString.matchAll(pattern)];
return Object.fromEntries(matches);
}
type ReACTOutputParser = <Options extends object>(
output: ChatResponse<Options> | AsyncIterable<ChatResponseChunk<Options>>,
onResolveType: (
type: "action" | "thought" | "answer",
response:
| ChatResponse<Options>
| ReadableStream<ChatResponseChunk<Options>>,
) => void,
) => Promise<Reason>;
const reACTOutputParser: ReACTOutputParser = async (
output,
onResolveType,
): Promise<Reason> => {
let reason: Reason | null = null;
if (isAsyncIterable(output)) {
const [peakStream, finalStream] = createReadableStream(output).tee();
const reader = peakStream.getReader();
let type: "action" | "thought" | "answer" | null = null;
let content = "";
do {
const { done, value } = await reader.read();
if (done) {
break;
}
content += value.delta;
if (content.includes("Action:")) {
type = "action";
} else if (content.includes("Answer:")) {
type = "answer";
}
} while (true);
if (type === null) {
// `Thought:` is always present at the beginning of the output.
type = "thought";
}
reader.releaseLock();
if (!type) {
throw new Error("Could not determine type of output");
}
onResolveType(type, finalStream);
// step 2: do the parsing from content
switch (type) {
case "action": {
// have to consume the stream to get the full content
const response = await consumeAsyncIterable(peakStream, content);
const [thought, action, input] = extractToolUse(response.content);
const jsonStr = extractJsonStr(input);
let json: JSONObject;
try {
json = JSON.parse(jsonStr);
} catch (e) {
json = actionInputParser(jsonStr);
}
reason = {
type: "action",
thought,
action,
input: json,
};
break;
}
case "thought": {
const thought = "(Implicit) I can answer without any more tools!";
const response = await consumeAsyncIterable(peakStream, content);
reason = {
type: "response",
thought,
response: {
raw: peakStream,
message: response,
},
};
break;
}
case "answer": {
const response = await consumeAsyncIterable(peakStream, content);
const [thought, answer] = extractFinalResponse(response.content);
reason = {
type: "response",
thought,
response: {
raw: response,
message: {
role: "assistant",
content: answer,
},
},
};
break;
}
default: {
throw new Error(`Invalid type: ${type}`);
}
}
} else {
const content = extractText(output.message.content);
const type = content.includes("Answer:")
? "answer"
: content.includes("Action:")
? "action"
: // `Thought:` is always present at the beginning of the output.
"thought";
onResolveType(type, output);
// step 2: do the parsing from content
switch (type) {
case "action": {
const [thought, action, input] = extractToolUse(content);
const jsonStr = extractJsonStr(input);
let json: JSONObject;
try {
json = JSON.parse(jsonStr);
} catch (e) {
json = actionInputParser(jsonStr);
}
reason = {
type: "action",
thought,
action,
input: json,
};
break;
}
case "thought": {
const thought = "(Implicit) I can answer without any more tools!";
reason = {
type: "response",
thought,
response: {
raw: output,
message: {
role: "assistant",
content: extractText(output.message.content),
},
},
};
break;
}
case "answer": {
const [thought, answer] = extractFinalResponse(content);
reason = {
type: "response",
thought,
response: {
raw: output,
message: {
role: "assistant",
content: answer,
},
},
};
break;
}
default: {
throw new Error(`Invalid type: ${type}`);
}
}
}
if (reason === null) {
throw new TypeError("Reason is null");
}
return reason;
};
type ReACTAgentStore = {
reasons: Reason[];
};
type ChatFormatter = <Options extends object>(
tools: BaseTool[],
messages: ChatMessage<Options>[],
currentReasons: Reason[],
) => Promise<ChatMessage<Options>[]>;
const chatFormatter: ChatFormatter = async <Options extends object>(
tools: BaseTool[],
messages: ChatMessage<Options>[],
currentReasons: Reason[],
): Promise<ChatMessage<Options>[]> => {
const header = getReACTAgentSystemHeader(tools);
const reasonMessages: ChatMessage<Options>[] = [];
for (const reason of currentReasons) {
const response = await reasonFormatter(reason);
reasonMessages.push({
role: reason.type === "observation" ? "user" : "assistant",
content: response,
});
}
return [
{
role: "system",
content: header,
},
...messages,
...reasonMessages,
];
};
export class ReACTAgentWorker extends AgentWorker<LLM, ReACTAgentStore> {
taskHandler = ReActAgent.taskHandler;
}
export class ReActAgent extends AgentRunner<LLM, ReACTAgentStore> {
constructor(
params: ReACTAgentParamsWithTools | ReACTAgentParamsWithToolRetriever,
) {
super({
llm: params.llm ?? Settings.llm,
chatHistory: params.chatHistory ?? [],
runner: new ReACTAgentWorker(),
systemPrompt: params.systemPrompt ?? null,
tools:
"tools" in params
? params.tools
: params.toolRetriever.retrieve.bind(params.toolRetriever),
verbose: params.verbose ?? false,
});
}
createStore() {
return {
reasons: [],
};
}
static taskHandler: TaskHandler<LLM, ReACTAgentStore> = async (
step,
enqueueOutput,
) => {
const { llm, stream, getTools } = step.context;
const lastMessage = step.context.store.messages.at(-1)!.content;
const tools = await getTools(lastMessage);
const messages = await chatFormatter(
tools,
step.context.store.messages,
step.context.store.reasons,
);
const response = await llm.chat({
// @ts-expect-error
stream,
messages,
});
const reason = await reACTOutputParser(response, (type, response) => {
enqueueOutput({
taskStep: step,
output: response,
isLast: type !== "action",
});
});
step.context.logger.log("current reason: %O", reason);
step.context.store.reasons = [...step.context.store.reasons, reason];
if (reason.type === "action") {
const tool = tools.find((tool) => tool.metadata.name === reason.action);
const toolOutput = await callTool(
tool,
{
id: randomUUID(),
input: reason.input,
name: reason.action,
},
step.context.logger,
);
step.context.store.reasons = [
...step.context.store.reasons,
{
type: "observation",
observation: toolOutput.output,
},
];
}
};
}