Closed jiangying000 closed 5 months ago
Found same issue when using the math calculator tool. This issues happen randomly and sometimes it failed and I found the action format is a bit strange. Success action should look like: Action:
{
"action": "math_calculator",
"action_input": "2.40 + 1.00"
}
Failed action looks like below: Action:
{
"action": "math_calculator",
"action_input": {"type": "string", "tool_input": "2.40 + 1.00"}
}
I encountered the exact same problem. Is there any good solution?
OAI functions agent doesn't get this class of error
I also encountered the problem.
Yeah, this seems to happen when the LLM decides that the input to a tool should be multiple arguments. I found that putting something in the description along the lines of "Input to this tool must be a SINGLE JSON STRING" helps on occasion. It still goes off the rails, though, and will occasionally send multi-argument inputs.
I think this could be better handled on the langchain side of things, where an interpreter for the tool arguments can be built to handle pretty much anything that is thrown at it.
Also facing this same issue randomly. Any update or solution?
I had some success with the following
def weather(action_input: str) -> str:
print(action_input)
input_obj = json.loads(action_input)
where = input_obj["location"]
when = input_obj["date"]
# call api
def setup_tool():
weather_request_format = '{{"date":"date","location":"location"}}'
weather_description = f'''
Helps to retrieve weather forecast.
Input should be a single string strictly in the following JSON format: {weather_request_format}
'''
return Tool(
name="Weather",
func=weather,
description=weather_description
)
Always meet this issue when agent=AgentType.OPENAI_MULTI_FUNCTIONS
, is that have different input shcema between OPENAI_MULTI_FUNCTIONS
and OPENAI_FUNCTIONS
?
I defined a handle_tool_error function and used this code snippet:
""" Handles tool exceptions. """
if error==JSONDecodeError:
return "Reformat in JSON and try again"
elif error.args[0].startswith("Too many arguments to single-input tool"):
return "Format in a SINGLE JSON STRING. DO NOT USE MULTI-ARGUMENTS INPUT."
return (
"The following errors occurred during tool execution:"
+ error.args[0]
+ "Please try another tool.")
Here's an explanation on how to use it: https://python.langchain.com/docs/modules/agents/tools/custom_tools#handling-tool-errors
I had the same issues and I solved it changing the tool definition form myTool = Tool(... to myTool = StructuredTool.from_function(...
Building off on what @antfin mentioned... to anyone still trying to figure this out you can do something similar to:
from langchain.tools.base import StructuredTool
from langchain_core.pydantic_v1 import BaseModel, Field
class Sum(BaseModel):
n: int = Field()
k: int =Field()
pass
def calculate_sum(n: int, k: int) -> int:
return str(n+k)
calculate_sum=StructuredTool(
name='calculate_sum',
func=calculate_sum,
description="Used to calculate sum",
args_schema=Sum,
)
I've found that older tools work with initialize_agent
and create_react_agent
, but no longer with create_openai_tools_agent
. Here's a fix:
from langchain.agents import Tool, load_tools
from langchain_core.tools import StructuredTool
def convert_to_structured_tool(tool):
return StructuredTool.from_function(tool.func, name=tool.name, description=tool.description)
tools = load_tools(['serpapi'])
tools = [convert_to_structured_tool(tool) for tool in tools]
With this change, tools
will be compatible with create_openai_tools_agent
. I believe the LangChain team should ensure backward compatibility, as many existing demos, like https://www.gradio.app/guides/agents-and-tool-usage#a-real-example-using-langchain-agents, are now broken, causing significant issues for users.
@taoari you should make a new issue on https://github.com/langchain-ai/langchain/issues this issue you're commenting on has low visibility to the LangChain team unless you tag them. You may make a new issue and tag them there, also reference this current issue for background info.
System Info
0.200
Who can help?
No response
Information
Related Components
Reproduction
run docs/modules/agents/agents/examples/openai_functions_agent.ipynb at line
mrkl.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")
then throw err:
ValueError: Too many arguments to single-input tool Calculator. Args: ['age ^ 0.43', {'age': 26}]
Expected behavior
should output value