Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AgentExecutor.fromAgentAndTools can never resolved #5981

Open
5 tasks done
wulucxy opened this issue Jul 4, 2024 · 5 comments
Open
5 tasks done

AgentExecutor.fromAgentAndTools can never resolved #5981

wulucxy opened this issue Jul 4, 2024 · 5 comments
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@wulucxy
Copy link

wulucxy commented Jul 4, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

const prompt = await pull<ChatPromptTemplate>('hwchase17/openai-tools-agent');
  const agent = await createOpenAIToolsAgent({
    llm,
    tools,
    prompt,
  });
  const agentExecutor = AgentExecutor.fromAgentAndTools({
    agent,
    tools,
    verbose: true,
    // maxIterations: 2,
  });

  const result = await agentExecutor.invoke({
    input: task,
  });

Error Message and Stack Trace (if applicable)

No response

Description

if (message.additional_kwargs.tool_calls) {

 if (message.additional_kwargs.tool_calls) {
// ...

should be:

 if (message.additional_kwargs.tool_calls && message.additional_kwargs.tool_calls.length > 0) {

System Info

langchainjs: v0.2.6

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jul 4, 2024
Copy link

dosubot bot commented Jul 4, 2024

The issue with AgentExecutor.fromAgentAndTools never resolving could indeed be related to the condition at line 75 of the output_parser.ts file. Specifically, if the output from the LLM contains both a final answer and a parse-able action, the code throws an error and does not return a result, which could cause the method to hang indefinitely.

Here is the relevant condition:

if (actionMatch) {
  if (includesAnswer) {
    throw new Error(
      `${FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE}: ${text}`
    );
  }
  // ... rest of the code
}

If the LLM output includes both an action and a final answer, the error is thrown, and no result is returned, which could lead to the AgentExecutor never resolving [1][2].

Additionally, the ToolCallingAgentOutputParser class in output_parser.ts is designed to parse the output text from the agent and return either an AgentAction or AgentFinish object. If the text includes both </tool> and <final_answer>, it might not be handled correctly, leading to the unresolved state [3].

Moreover, the AgentExecutor class has a mechanism to handle errors raised by the agent's output parser. The handleParsingErrors property can be set to handle these errors in different ways, such as sending the error back to the LLM as an observation or using a custom function to handle the error. If this property is not set correctly, it could also contribute to the method never resolving [4].

To address the specific issue you mentioned, modifying the condition as follows could help:

if (message.additional_kwargs.tool_calls && message.additional_kwargs.tool_calls.length > 0) {
// ...
}

This ensures that the condition only checks for tool calls if they exist and have a length greater than zero, which might prevent the method from hanging indefinitely.

To continue talking to Dosu, mention @dosu.

@bracesproul
Copy link
Collaborator

For now, you should just use:

  const agentExecutor = new AgentExecutor({
    agent,
    tools,
    verbose: true,
    // maxIterations: 2,
  });

@wulucxy
Copy link
Author

wulucxy commented Jul 9, 2024

@bracesproul

if (message.additional_kwargs.tool_calls) {

I think the assertion of this line is not correct

if (message.additional_kwargs.tool_calls) {
 // ....
}

it should be

if (message.additional_kwargs.tool_calls && message.additional_kwargs.tool_calls.length > 0) {
// ...
}

@wulucxy
Copy link
Author

wulucxy commented Jul 9, 2024

For now, you should just use:

  const agentExecutor = new AgentExecutor({
    agent,
    tools,
    verbose: true,
    // maxIterations: 2,
  });

AgentExecutor

static fromAgentAndTools(fields: AgentExecutorInput): AgentExecutor {

these two methods call just the same

@djaffer
Copy link
Contributor

djaffer commented Jul 28, 2024

This agent never triggers the _call for the tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

3 participants