-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
executor.ts
808 lines (730 loc) Β· 23.2 KB
/
executor.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
import {
type StructuredToolInterface,
type ToolInterface,
ToolInputParsingException,
Tool,
} from "@langchain/core/tools";
import {
Runnable,
type RunnableConfig,
patchConfig,
} from "@langchain/core/runnables";
import { AgentAction, AgentFinish, AgentStep } from "@langchain/core/agents";
import { ChainValues } from "@langchain/core/utils/types";
import {
CallbackManager,
CallbackManagerForChainRun,
Callbacks,
} from "@langchain/core/callbacks/manager";
import { OutputParserException } from "@langchain/core/output_parsers";
import { Serializable } from "@langchain/core/load/serializable";
import { SerializedLLMChain } from "../chains/serde.js";
import { StoppingMethod } from "./types.js";
import {
AgentRunnableSequence,
BaseMultiActionAgent,
BaseSingleActionAgent,
RunnableMultiActionAgent,
RunnableSingleActionAgent,
isRunnableAgent,
} from "./agent.js";
import { BaseChain, ChainInputs } from "../chains/base.js";
interface AgentExecutorIteratorInput {
agentExecutor: AgentExecutor;
inputs: Record<string, string>;
config?: RunnableConfig;
/** @deprecated Use "config" */
callbacks?: Callbacks;
/** @deprecated Use "config" */
tags?: string[];
/** @deprecated Use "config" */
metadata?: Record<string, unknown>;
runName?: string;
runManager?: CallbackManagerForChainRun;
}
export class AgentExecutorIterator
extends Serializable
implements AgentExecutorIteratorInput
{
lc_namespace = ["langchain", "agents", "executor_iterator"];
agentExecutor: AgentExecutor;
inputs: Record<string, string>;
config?: RunnableConfig;
/** @deprecated Use "config" */
callbacks?: Callbacks;
/** @deprecated Use "config" */
tags: string[] | undefined;
/** @deprecated Use "config" */
metadata: Record<string, unknown> | undefined;
/** @deprecated Use "config" */
runName: string | undefined;
private _finalOutputs: Record<string, unknown> | undefined;
get finalOutputs(): Record<string, unknown> | undefined {
return this._finalOutputs;
}
/** Intended to be used as a setter method, needs to be async. */
async setFinalOutputs(value: Record<string, unknown> | undefined) {
this._finalOutputs = undefined;
if (value) {
const preparedOutputs: Record<string, unknown> =
await this.agentExecutor.prepOutputs(this.inputs, value, true);
this._finalOutputs = preparedOutputs;
}
}
runManager: CallbackManagerForChainRun | undefined;
intermediateSteps: AgentStep[] = [];
iterations = 0;
get nameToToolMap(): Record<string, ToolInterface> {
const toolMap = this.agentExecutor.tools.map((tool) => ({
[tool.name]: tool,
}));
return Object.assign({}, ...toolMap);
}
constructor(fields: AgentExecutorIteratorInput) {
super(fields);
this.agentExecutor = fields.agentExecutor;
this.inputs = fields.inputs;
this.callbacks = fields.callbacks;
this.tags = fields.tags;
this.metadata = fields.metadata;
this.runName = fields.runName;
this.runManager = fields.runManager;
this.config = fields.config;
}
/**
* Reset the iterator to its initial state, clearing intermediate steps,
* iterations, and the final output.
*/
reset(): void {
this.intermediateSteps = [];
this.iterations = 0;
this._finalOutputs = undefined;
}
updateIterations(): void {
this.iterations += 1;
}
async *streamIterator() {
this.reset();
// Loop to handle iteration
while (true) {
try {
if (this.iterations === 0) {
await this.onFirstStep();
}
const result = await this._callNext();
yield result;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
if (
"message" in e &&
e.message.startsWith("Final outputs already reached: ")
) {
if (!this.finalOutputs) {
throw e;
}
return this.finalOutputs;
}
if (this.runManager) {
await this.runManager.handleChainError(e);
}
throw e;
}
}
}
/**
* Perform any necessary setup for the first step
* of the asynchronous iterator.
*/
async onFirstStep(): Promise<void> {
if (this.iterations === 0) {
const callbackManager = await CallbackManager.configure(
this.callbacks ?? this.config?.callbacks,
this.agentExecutor.callbacks,
this.tags ?? this.config?.tags,
this.agentExecutor.tags,
this.metadata ?? this.config?.metadata,
this.agentExecutor.metadata,
{
verbose: this.agentExecutor.verbose,
}
);
this.runManager = await callbackManager?.handleChainStart(
this.agentExecutor.toJSON(),
this.inputs,
this.config?.runId,
undefined,
this.tags ?? this.config?.tags,
this.metadata ?? this.config?.metadata,
this.runName ?? this.config?.runName
);
if (this.config !== undefined) {
delete this.config.runId;
}
}
}
/**
* Execute the next step in the chain using the
* AgentExecutor's _takeNextStep method.
*/
async _executeNextStep(
runManager?: CallbackManagerForChainRun
): Promise<AgentFinish | AgentStep[]> {
return this.agentExecutor._takeNextStep(
this.nameToToolMap,
this.inputs,
this.intermediateSteps,
runManager,
this.config
);
}
/**
* Process the output of the next step,
* handling AgentFinish and tool return cases.
*/
async _processNextStepOutput(
nextStepOutput: AgentFinish | AgentStep[],
runManager?: CallbackManagerForChainRun
): Promise<Record<string, string | AgentStep[]>> {
if ("returnValues" in nextStepOutput) {
const output = await this.agentExecutor._return(
nextStepOutput as AgentFinish,
this.intermediateSteps,
runManager
);
if (this.runManager) {
await this.runManager.handleChainEnd(output);
}
await this.setFinalOutputs(output);
return output;
}
this.intermediateSteps = this.intermediateSteps.concat(
nextStepOutput as AgentStep[]
);
let output: Record<string, string | AgentStep[]> = {};
if (Array.isArray(nextStepOutput) && nextStepOutput.length === 1) {
const nextStep = nextStepOutput[0];
const toolReturn = await this.agentExecutor._getToolReturn(nextStep);
if (toolReturn) {
output = await this.agentExecutor._return(
toolReturn,
this.intermediateSteps,
runManager
);
await this.runManager?.handleChainEnd(output);
await this.setFinalOutputs(output);
}
}
output = { intermediateSteps: nextStepOutput as AgentStep[] };
return output;
}
async _stop(): Promise<Record<string, unknown>> {
const output = await this.agentExecutor.agent.returnStoppedResponse(
this.agentExecutor.earlyStoppingMethod,
this.intermediateSteps,
this.inputs
);
const returnedOutput = await this.agentExecutor._return(
output,
this.intermediateSteps,
this.runManager
);
await this.setFinalOutputs(returnedOutput);
await this.runManager?.handleChainEnd(returnedOutput);
return returnedOutput;
}
async _callNext(): Promise<Record<string, unknown>> {
// final output already reached: stopiteration (final output)
if (this.finalOutputs) {
throw new Error(
`Final outputs already reached: ${JSON.stringify(
this.finalOutputs,
null,
2
)}`
);
}
// timeout/max iterations: stopiteration (stopped response)
if (!this.agentExecutor.shouldContinueGetter(this.iterations)) {
return this._stop();
}
const nextStepOutput = await this._executeNextStep(this.runManager);
const output = await this._processNextStepOutput(
nextStepOutput,
this.runManager
);
this.updateIterations();
return output;
}
}
type ExtractToolType<T> = T extends { ToolType: infer ToolInterface }
? ToolInterface
: StructuredToolInterface;
/**
* Interface defining the structure of input data for creating an
* AgentExecutor. It extends ChainInputs and includes additional
* properties specific to agent execution.
*/
export interface AgentExecutorInput extends ChainInputs {
agent:
| BaseSingleActionAgent
| BaseMultiActionAgent
| Runnable<
ChainValues & { steps?: AgentStep[] },
AgentAction[] | AgentAction | AgentFinish
>;
tools: ExtractToolType<this["agent"]>[];
returnIntermediateSteps?: boolean;
maxIterations?: number;
earlyStoppingMethod?: StoppingMethod;
handleParsingErrors?:
| boolean
| string
| ((e: OutputParserException | ToolInputParsingException) => string);
handleToolRuntimeErrors?: (e: Error) => string;
}
// TODO: Type properly with { intermediateSteps?: AgentStep[] };
export type AgentExecutorOutput = ChainValues;
/**
* Tool that just returns the query.
* Used for exception tracking.
*/
export class ExceptionTool extends Tool {
name = "_Exception";
description = "Exception tool";
async _call(query: string) {
return query;
}
}
/**
* A chain managing an agent using tools.
* @augments BaseChain
* @example
* ```typescript
*
* const executor = AgentExecutor.fromAgentAndTools({
* agent: async () => loadAgentFromLangchainHub(),
* tools: [new SerpAPI(), new Calculator()],
* returnIntermediateSteps: true,
* });
*
* const result = await executor.invoke({
* input: `Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?`,
* });
*
* ```
*/
export class AgentExecutor extends BaseChain<ChainValues, AgentExecutorOutput> {
static lc_name() {
return "AgentExecutor";
}
get lc_namespace() {
return ["langchain", "agents", "executor"];
}
agent: BaseSingleActionAgent | BaseMultiActionAgent;
tools: this["agent"]["ToolType"][];
returnIntermediateSteps = false;
maxIterations?: number = 15;
earlyStoppingMethod: StoppingMethod = "force";
// TODO: Update BaseChain implementation on breaking change to include this
returnOnlyOutputs = true;
/**
* How to handle errors raised by the agent's output parser.
Defaults to `False`, which raises the error.
If `true`, the error will be sent back to the LLM as an observation.
If a string, the string itself will be sent to the LLM as an observation.
If a callable function, the function will be called with the exception
as an argument, and the result of that function will be passed to the agent
as an observation.
*/
handleParsingErrors:
| boolean
| string
| ((e: OutputParserException | ToolInputParsingException) => string) =
false;
handleToolRuntimeErrors?: (e: Error) => string;
get inputKeys() {
return this.agent.inputKeys;
}
get outputKeys() {
return this.agent.returnValues;
}
constructor(input: AgentExecutorInput) {
let agent: BaseSingleActionAgent | BaseMultiActionAgent;
let returnOnlyOutputs = true;
if (Runnable.isRunnable(input.agent)) {
if (AgentRunnableSequence.isAgentRunnableSequence(input.agent)) {
if (input.agent.singleAction) {
agent = new RunnableSingleActionAgent({
runnable: input.agent,
streamRunnable: input.agent.streamRunnable,
});
} else {
agent = new RunnableMultiActionAgent({
runnable: input.agent,
streamRunnable: input.agent.streamRunnable,
});
}
} else {
agent = new RunnableMultiActionAgent({ runnable: input.agent });
}
// TODO: Update BaseChain implementation on breaking change
returnOnlyOutputs = false;
} else {
if (isRunnableAgent(input.agent)) {
returnOnlyOutputs = false;
}
agent = input.agent;
}
super(input);
this.agent = agent;
this.tools = input.tools;
this.handleParsingErrors =
input.handleParsingErrors ?? this.handleParsingErrors;
this.handleToolRuntimeErrors = input.handleToolRuntimeErrors;
this.returnOnlyOutputs = returnOnlyOutputs;
if (this.agent._agentActionType() === "multi") {
for (const tool of this.tools) {
if (tool.returnDirect) {
throw new Error(
`Tool with return direct ${tool.name} not supported for multi-action agent.`
);
}
}
}
this.returnIntermediateSteps =
input.returnIntermediateSteps ?? this.returnIntermediateSteps;
this.maxIterations = input.maxIterations ?? this.maxIterations;
this.earlyStoppingMethod =
input.earlyStoppingMethod ?? this.earlyStoppingMethod;
}
/** Create from agent and a list of tools. */
static fromAgentAndTools(fields: AgentExecutorInput): AgentExecutor {
return new AgentExecutor(fields);
}
get shouldContinueGetter() {
return this.shouldContinue.bind(this);
}
/**
* Method that checks if the agent execution should continue based on the
* number of iterations.
* @param iterations The current number of iterations.
* @returns A boolean indicating whether the agent execution should continue.
*/
private shouldContinue(iterations: number): boolean {
return this.maxIterations === undefined || iterations < this.maxIterations;
}
/** @ignore */
async _call(
inputs: ChainValues,
runManager?: CallbackManagerForChainRun,
config?: RunnableConfig
): Promise<AgentExecutorOutput> {
const toolsByName = Object.fromEntries(
this.tools.map((t) => [t.name.toLowerCase(), t])
);
const steps: AgentStep[] = [];
let iterations = 0;
const getOutput = async (
finishStep: AgentFinish
): Promise<AgentExecutorOutput> => {
const { returnValues } = finishStep;
const additional = await this.agent.prepareForOutput(returnValues, steps);
await runManager?.handleAgentEnd(finishStep);
let response;
if (this.returnIntermediateSteps) {
response = { ...returnValues, intermediateSteps: steps, ...additional };
} else {
response = { ...returnValues, ...additional };
}
if (!this.returnOnlyOutputs) {
response = { ...inputs, ...response };
}
return response;
};
while (this.shouldContinue(iterations)) {
let output;
try {
output = await this.agent.plan(
steps,
inputs,
runManager?.getChild(),
config
);
} catch (e) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (e instanceof OutputParserException) {
let observation;
let text = e.message;
if (this.handleParsingErrors === true) {
if (e.sendToLLM) {
observation = e.observation;
text = e.llmOutput ?? "";
} else {
observation = "Invalid or incomplete response";
}
} else if (typeof this.handleParsingErrors === "string") {
observation = this.handleParsingErrors;
} else if (typeof this.handleParsingErrors === "function") {
observation = this.handleParsingErrors(e);
} else {
throw e;
}
output = {
tool: "_Exception",
toolInput: observation,
log: text,
} as AgentAction;
} else {
throw e;
}
}
// Check if the agent has finished
if ("returnValues" in output) {
return getOutput(output);
}
let actions: AgentAction[];
if (Array.isArray(output)) {
actions = output as AgentAction[];
} else {
actions = [output as AgentAction];
}
const newSteps = await Promise.all(
actions.map(async (action) => {
await runManager?.handleAgentAction(action);
const tool =
action.tool === "_Exception"
? new ExceptionTool()
: toolsByName[action.tool?.toLowerCase()];
let observation;
try {
observation = tool
? await tool.invoke(
action.toolInput,
patchConfig(config, { callbacks: runManager?.getChild() })
)
: `${action.tool} is not a valid tool, try another one.`;
if (typeof observation !== "string") {
throw new Error(
"Received unsupported non-string response from tool call."
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (e instanceof ToolInputParsingException) {
if (this.handleParsingErrors === true) {
observation =
"Invalid or incomplete tool input. Please try again.";
} else if (typeof this.handleParsingErrors === "string") {
observation = this.handleParsingErrors;
} else if (typeof this.handleParsingErrors === "function") {
observation = this.handleParsingErrors(e);
} else {
throw e;
}
observation = await new ExceptionTool().call(
observation,
runManager?.getChild()
);
return { action, observation: observation ?? "" };
} else if (this.handleToolRuntimeErrors !== undefined) {
observation = this.handleToolRuntimeErrors(e);
}
}
return { action, observation: observation ?? "" };
})
);
steps.push(...newSteps);
const lastStep = steps[steps.length - 1];
const lastTool = toolsByName[lastStep.action.tool?.toLowerCase()];
if (lastTool?.returnDirect) {
return getOutput({
returnValues: { [this.agent.returnValues[0]]: lastStep.observation },
log: "",
});
}
iterations += 1;
}
const finish = await this.agent.returnStoppedResponse(
this.earlyStoppingMethod,
steps,
inputs
);
return getOutput(finish);
}
async _takeNextStep(
nameToolMap: Record<string, ToolInterface>,
inputs: ChainValues,
intermediateSteps: AgentStep[],
runManager?: CallbackManagerForChainRun,
config?: RunnableConfig
): Promise<AgentFinish | AgentStep[]> {
let output;
try {
output = await this.agent.plan(
intermediateSteps,
inputs,
runManager?.getChild(),
config
);
} catch (e) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (e instanceof OutputParserException) {
let observation;
let text = e.message;
if (this.handleParsingErrors === true) {
if (e.sendToLLM) {
observation = e.observation;
text = e.llmOutput ?? "";
} else {
observation = "Invalid or incomplete response";
}
} else if (typeof this.handleParsingErrors === "string") {
observation = this.handleParsingErrors;
} else if (typeof this.handleParsingErrors === "function") {
observation = this.handleParsingErrors(e);
} else {
throw e;
}
output = {
tool: "_Exception",
toolInput: observation,
log: text,
} as AgentAction;
} else {
throw e;
}
}
if ("returnValues" in output) {
return output;
}
let actions: AgentAction[];
if (Array.isArray(output)) {
actions = output as AgentAction[];
} else {
actions = [output as AgentAction];
}
const result: AgentStep[] = [];
for (const agentAction of actions) {
let observation = "";
if (runManager) {
await runManager?.handleAgentAction(agentAction);
}
if (agentAction.tool in nameToolMap) {
const tool = nameToolMap[agentAction.tool];
try {
observation = await tool.call(
agentAction.toolInput,
runManager?.getChild()
);
if (typeof observation !== "string") {
throw new Error(
"Received unsupported non-string response from tool call."
);
}
} catch (e) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (e instanceof ToolInputParsingException) {
if (this.handleParsingErrors === true) {
observation =
"Invalid or incomplete tool input. Please try again.";
} else if (typeof this.handleParsingErrors === "string") {
observation = this.handleParsingErrors;
} else if (typeof this.handleParsingErrors === "function") {
observation = this.handleParsingErrors(e);
} else {
throw e;
}
observation = await new ExceptionTool().call(
observation,
runManager?.getChild()
);
}
}
} else {
observation = `${
agentAction.tool
} is not a valid tool, try another available tool: ${Object.keys(
nameToolMap
).join(", ")}`;
}
result.push({
action: agentAction,
observation,
});
}
return result;
}
async _return(
output: AgentFinish,
intermediateSteps: AgentStep[],
runManager?: CallbackManagerForChainRun
): Promise<AgentExecutorOutput> {
if (runManager) {
await runManager.handleAgentEnd(output);
}
const finalOutput: Record<string, unknown> = output.returnValues;
if (this.returnIntermediateSteps) {
finalOutput.intermediateSteps = intermediateSteps;
}
return finalOutput;
}
async _getToolReturn(nextStepOutput: AgentStep): Promise<AgentFinish | null> {
const { action, observation } = nextStepOutput;
const nameToolMap = Object.fromEntries(
this.tools.map((t) => [t.name.toLowerCase(), t])
);
const [returnValueKey = "output"] = this.agent.returnValues;
// Invalid tools won't be in the map, so we return False.
if (action.tool in nameToolMap) {
if (nameToolMap[action.tool].returnDirect) {
return {
returnValues: { [returnValueKey]: observation },
log: "",
};
}
}
return null;
}
_returnStoppedResponse(earlyStoppingMethod: StoppingMethod) {
if (earlyStoppingMethod === "force") {
return {
returnValues: {
output: "Agent stopped due to iteration limit or time limit.",
},
log: "",
} as AgentFinish;
}
throw new Error(
`Got unsupported early_stopping_method: ${earlyStoppingMethod}`
);
}
async *_streamIterator(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inputs: Record<string, any>,
options?: Partial<RunnableConfig>
): AsyncGenerator<ChainValues> {
const agentExecutorIterator = new AgentExecutorIterator({
inputs,
agentExecutor: this,
config: options,
// TODO: Deprecate these other parameters
metadata: options?.metadata,
tags: options?.tags,
callbacks: options?.callbacks,
});
const iterator = agentExecutorIterator.streamIterator();
for await (const step of iterator) {
if (!step) {
continue;
}
yield step;
}
}
_chainType() {
return "agent_executor" as const;
}
serialize(): SerializedLLMChain {
throw new Error("Cannot serialize an AgentExecutor");
}
}