Incorrect argument count handling (#5543)

Throwing ToolException when incorrect arguments are passed to tools so
that that agent can course correct them.

# Incorrect argument count handling

I was facing an error where the agent passed incorrect arguments to
tools. As per the discussions going around, I started throwing
ToolException to allow the model to course correct.

## Before submitting

<!-- If you're adding a new integration, please include:

1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use


See contribution guidelines for more information on how to write tests,
lint
etc:


https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->

## Who can review?

Community members can review the PR once tests pass. Tag
maintainers/contributors who might be interested:

<!-- For a quicker response, figure out the right person to tag with @

  @hwchase17 - project lead

  Tracing / Callbacks
  - @agola11

  Async
  - @agola11

  DataLoaders
  - @eyurtsev

  Models
  - @hwchase17
  - @agola11

  Agents / Tools / Toolkits
  - @vowelparrot

  VectorStores / Retrievers / Memory
  - @dev2049

 -->

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
Swapnil Sharma 2023-06-20 01:06:20 -04:00 committed by GitHub
parent 3a58c4c3a0
commit dc4ffa8d9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 3 deletions

View File

@ -394,7 +394,7 @@ class Tool(BaseTool):
# For backwards compatibility. The tool must be run with a single input # For backwards compatibility. The tool must be run with a single input
all_args = list(args) + list(kwargs.values()) all_args = list(args) + list(kwargs.values())
if len(all_args) != 1: if len(all_args) != 1:
raise ValueError( raise ToolException(
f"Too many arguments to single-input tool {self.name}." f"Too many arguments to single-input tool {self.name}."
f" Args: {all_args}" f" Args: {all_args}"
) )

View File

@ -14,6 +14,7 @@ from langchain.agents.mrkl.base import ZeroShotAgent
from langchain.agents.react.base import ReActDocstoreAgent, ReActTextWorldAgent from langchain.agents.react.base import ReActDocstoreAgent, ReActTextWorldAgent
from langchain.agents.self_ask_with_search.base import SelfAskWithSearchAgent from langchain.agents.self_ask_with_search.base import SelfAskWithSearchAgent
from langchain.agents.tools import Tool, tool from langchain.agents.tools import Tool, tool
from langchain.tools.base import ToolException
from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler
@ -63,7 +64,7 @@ def test_tool_no_args_specified_assumes_str() -> None:
assert some_tool.args == expected_args assert some_tool.args == expected_args
assert some_tool.run("foobar") == "foobar" assert some_tool.run("foobar") == "foobar"
assert some_tool.run({"tool_input": "foobar"}) == "foobar" assert some_tool.run({"tool_input": "foobar"}) == "foobar"
with pytest.raises(ValueError, match="Too many arguments to single-input tool"): with pytest.raises(ToolException, match="Too many arguments to single-input tool"):
some_tool.run({"tool_input": "foobar", "other_input": "bar"}) some_tool.run({"tool_input": "foobar", "other_input": "bar"})

View File

@ -231,7 +231,7 @@ def test_structured_args_decorator_no_infer_schema() -> None:
assert isinstance(structured_tool_input, BaseTool) assert isinstance(structured_tool_input, BaseTool)
assert structured_tool_input.name == "structured_tool_input" assert structured_tool_input.name == "structured_tool_input"
args = {"arg1": 1, "arg2": 0.001, "opt_arg": {"foo": "bar"}} args = {"arg1": 1, "arg2": 0.001, "opt_arg": {"foo": "bar"}}
with pytest.raises(ValueError): with pytest.raises(ToolException):
assert structured_tool_input.run(args) assert structured_tool_input.run(args)