langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.44k stars 15.26k forks source link

ToolException("Too many arguments to single-input tool Calculator.\n Consider using StructuredTool instead. Args: [['5*3'], {}]") #25353

Closed rhlarora84 closed 2 months ago

rhlarora84 commented 2 months ago

Checked other resources

Example Code

from datetime import datetime

from langchain.chains.llm_math.base import LLMMathChain
from langchain_community.chat_models import ChatOpenAI
from langchain_core.tools import tool, Tool
from langgraph.prebuilt import create_react_agent

model = ChatOpenAI(
    ...
)

@tool
def check_weather(location: str, at_time: datetime) -> str:
    """Return the weather forecast for the specified location."""
    return f"It's always sunny in {location}"

llm_math_chain = LLMMathChain.from_llm(llm=model,
                                       verbose=True)
tools = [check_weather, Tool(
    name="Calculator",
    func=llm_math_chain.run,
    description="Useful for when you need to answer questions about math. This tool is only for math "
                "questions and nothing else. Only input math expressions.",
)]
graph = create_react_agent(model, tools=tools)

if __name__ == "__main__":
    inputs = {"messages": [("user", "Calculate 5*3")]}
    for s in graph.stream(inputs, stream_mode="values"):
        message = s["messages"][-1]
        if isinstance(message, tuple):
            print(message)
        else:
            message.pretty_print()

Error Message and Stack Trace (if applicable)

================================ Human Message =================================

Calculate 53 ================================== Ai Message ================================== Tool Calls: Calculator (call_O5H3HHhTlul8kea3pvHj8FSN) Call ID: call_O5H3HHhTlul8kea3pvHj8FSN Args: args: ['53'] config: {} ================================= Tool Message ================================= Name: Calculator

Error: ToolException("Too many arguments to single-input tool Calculator.\n Consider using StructuredTool instead. Args: [['53'], {}]") Please fix your mistakes. ================================== Ai Message ================================== Tool Calls: Calculator (call_U0B84wfj0pwZuZCsP5rUtcsN) Call ID: call_U0B84wfj0pwZuZCsP5rUtcsN Args: args: ['53'] config: {} ================================= Tool Message ================================= Name: Calculator

Error: ToolException("Too many arguments to single-input tool Calculator.\n Consider using StructuredTool instead. Args: [['53'], {}]") Please fix your mistakes. ================================== Ai Message ================================== Tool Calls: Calculator (call_wF4MlZXTmnpRiauaHp1IZmZt) Call ID: call_wF4MlZXTmnpRiauaHp1IZmZt Args: args: ['53'] config: {} ================================= Tool Message ================================= Name: Calculator

Error: ToolException("Too many arguments to single-input tool Calculator.\n Consider using StructuredTool instead. Args: [['53'], {}]") Please fix your mistakes. ================================== Ai Message ================================== Tool Calls: Calculator (call_aBw92kXhfEJkrzFCwbEbaAFT) Call ID: call_aBw92kXhfEJkrzFCwbEbaAFT Args: args: ['53'] config: {} ================================= Tool Message ================================= Name: Calculator

Error: ToolException("Too many arguments to single-input tool Calculator.\n Consider using StructuredTool instead. Args: [['5*3'], {}]") Please fix your mistakes.

Description

The code works fine when using the check_weather tool, but does not work when using the Calculator. This may have to do with the RunnableConfig changes done for Tool calls !

I also believe that specifying Pydantic model using args_schema prevents this issue from happening. Issue is causing the legacy stuff to break !

System Info

langchain==0.2.12 langchain-anthropic==0.1.22 langchain-aws==0.1.16 langchain-community==0.2.11 langchain-core==0.2.29 langchain-experimental==0.0.64 langchain-ollama==0.1.0rc0 langchain-openai==0.1.21 langchain-text-splitters==0.2.1 langdetect==1.0.9 langgraph==0.2.3 langgraph-checkpoint==1.0.2 langserve==0.2.2 langsmith==0.1.84

ccurme commented 2 months ago

Instead of LLMMathChain, would it work to use a simple calculator tool? Here is one that uses similar code as LLMMathChain:

@tool
def calculator(expression: str) -> str:
    """Calculate expression using Python's numexpr library.

    Useful for when you need to answer questions about math.
    This tool is only for math questions and nothing else.
    Only input math expressions.

    Expression should be a single line mathematical expression
    that solves the problem.

    Examples:
        "37593 * 67" for "37593 times 67"
        "37593**(1/5)" for "37593^(1/5)"
    """
    import math
    import numexpr

    local_dict = {"pi": math.pi, "e": math.e}
    return str(
        numexpr.evaluate(
            expression.strip(),
            global_dict={},  # restrict access to globals
            local_dict=local_dict,  # add common mathematical functions
        )
    )

This works in your snippet for me, although I believe you need to import ChatOpenAI from langchain-openai to use its tool calling features:

pip install --upgrade langchain-openai
from langchain_openai import ChatOpenAI
rhlarora84 commented 2 months ago

Sorry I should have been more clear here. The snippet was an example to highlight the issue that extending from BaseTool does not work anymore without specifying args_schema. Is that model of specifying tool going away?

jinec commented 1 month ago

@rhlarora84 Has this problem been solved now? How was it resolved? Online waiting

rhlarora84 commented 1 month ago

I ended up moving to Langgraph based AgentExecutor where this wasn't an issue. The ticket was closed without an acknowledgement and the suggestion provided wasn't of any value ! To me, the original issue is likely a regression stemming from changes done to support RunnableConfig in the tool interface.