Closed rhlarora84 closed 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
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?
@rhlarora84 Has this problem been solved now? How was it resolved? Online waiting
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.
Checked other resources
Example Code
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