langchain-ai / langchain

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

Langchain Tools: TypeError: BaseTool.invoke() takes from 2 to 3 positional arguments but 4 were given #23533

Open Jeomon opened 3 months ago

Jeomon commented 3 months ago

Checked other resources

Example Code

class Shell(BaseModel): command: str = Field(..., description="The shell command to be executed.") script_args: Optional[List[str]] = Field(default=None, description="Optional arguments for the shell command.") inputs: Optional[str] = Field(default=None, description="User inputs for the command, e.g., for Python scripts.")

@tool("shell_tool",args_schema=Shell) def shell_tool(command, script_args,inputs) -> str: """ Execute the given shell command and return its output. example with shell args: shell_tool('python foo.py',["Hello World","Welcome"],None) example with user inputs (When the script has input("Enter a nnumber")): shell_tool('python add.py',None,'5\n6\n') example for simple case: shell_tool('python foo.py',None,None) """ try: safe_command = shlex.split(command) if script_args: safe_command.extend(script_args) result = subprocess.Popen(safe_command,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True) stdout,stderr=result.communicate(input=inputs) if result.returncode != 0: return f"Error: {stderr}" return stdout except Exception as e: return f"Exception occurred: {str(e)}"

shell_tool.invoke('python sum.py',None,'5\n')

Error Message and Stack Trace (if applicable)


TypeError Traceback (most recent call last) Cell In[15], line 1 ----> 1 shell_tool.invoke('python sum.py',None,'5\n')

TypeError: BaseTool.invoke() takes from 2 to 3 positional arguments but 4 were given

Description

I executed this tool. but I get this error. But when I remove the decorator, the @tool decorator (now its a normal function) it works but once the decorator is on it's a tool and when I invoke it gives me this error. The same error (incomplete output/response) is seen when I use this tool inside an agent. Can anyone help me

System Info

langchain==0.2.5 langchain-community==0.2.5 langchain-core==0.2.9 langchain-experimental==0.0.61 langchain-groq==0.1.5 langchain-text-splitters==0.2.1 python 3.12.4 Windows 11 system

mackong commented 3 months ago

@Jeomon You should call invoke like this

shell_tool.invoke(input={"command": 'python sum.py', "script_args": None, "inputs": '5\n'})
Jeomon commented 3 months ago

[0:tasks] Starting step 0 with 1 task:

Entering new Python Code Agent chain... Question: Write a Python program to find the sum of two numbers and save the file in the current directory with the name sum.py. Thought: The program should take user input for the two numbers. Action:

{
"action": "Write Tool",
"action_input": {
"directory": "D:\\Personal Projects\\Python-Assistant",
"file_name": "sum.py",
"content": "num1 = int(input('Enter first number: '))\nnum2 = int(input('Enter second number: '))\nsum = num1 + num2\nprint('The sum of the two numbers is:', sum)"   
}
}
```The sum.py successfully saved to D:\Personal Projects\Python-Assistant\sum.pyAction:

{ "action": "Final Answer", "action_input": "The Python program to find the sum of two numbers has been successfully written and saved as sum.py in the current directory. You can run the program by executing python sum.py in the command line." }


Finished chain.
[1:writes] Finished step 1 with writes to 2 channels:
- input -> ('Writer Python program to Find the sum of two numbers and save this file in '
'the current directory with the name sum.py and this program must take user '
'input.')
- messages -> [HumanMessage(content='The Python program to find the sum of two numbers has been successfully written and saved as sum.py in the current directory. You can run the program by executing `python sum.py` in the command line.', name='Python Code Agent')]
[2:tasks] Starting step 2 with 1 task:
- executor -> {'input': 'Writer Python program to Find the sum of two numbers and save this '
'file in the current directory with the name sum.py and this program '
'must take user input.',
'messages': [HumanMessage(content='The Python program to find the sum of two numbers has been successfully written and saved as sum.py in the current directory. You can run the program by executing `python sum.py` in the command line.', name='Python Code Agent')]}

Entering new Python Executor Agent chain...
Could not parse LLM output: Action:

{ "action": "shell_tool", "action_input": { "command": "python sum.py", "script_args": None, "inputs": "2\n3\n" } }

The method you mentioned worked when I invoked it (in standalone without any llm the earlier case) but when I used it as tool for a react agent (executor agent) it is giving Invalid or incomplete responseCould not parse LLM output and then looping around this until the iteration limit is reached but the shell tool isn't invoked at all in that iterations.