Open zacharyfmarion opened 2 months ago
Hi @zacharyfmarion! I'm here to help you with any issues you have. Let me know how I can assist you!
The handleToolEnd
callback is not being called when using createToolCallingAgent
with ChatAnthropic
and AgentExecutor
because the handleAgentAction
method is incorrectly incrementing toolStarts
instead of agentStarts
. This causes a mismatch in the expected counts for tool starts and ends.
In your code, the handleAgentAction
method should be implemented as follows:
async handleAgentAction(_action: AgentAction): Promise<void> {
this.starts += 1;
this.agentStarts += 1;
}
This discrepancy leads to the toolEnds
count not being incremented correctly, which is why the handleToolEnd
callback is not called as expected [1].
To continue talking to Dosu, mention @dosu.
@dosu that is a test file, the issue must be somewhere in the code where handleToolEnd is called for an agent
The handleToolEnd
function is called within the executor.invoke
method when using createToolCallingAgent
with ChatAnthropic
and AgentExecutor
. This is evident from the provided code snippet where executor.invoke
is called with an input and a set of callbacks, including handleToolEnd
[1].
Additionally, the handleToolEnd
method is called within the _call
method of the AgentExecutor
class. Here is the relevant code snippet:
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];
}
// Call handleToolEnd here
await this.handleToolEnd(actions, runManager);
}
The handleToolEnd
method is called within the while
loop in the _call
method, specifically after determining the actions
array [2].
To continue talking to Dosu, mention @dosu.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
No response
Description
When using
createToolCallingAgent
withChatAnthropic
and passing in callbacks through anAgentExecutor
thehandleToolEnd
callback does not get executed. Because of this I am not able to track token usage for the created agent.Current Behavior
When run only two log statements appear:
Expected Behavior
I would expect
handleToolEnd
to also be logged.System Info
Plaform: mac Node version:
v20.15.0
Yarn version:4.0.2