Add ToolException that a tool can throw. (#5050)

# Add ToolException that a tool can throw
This is an optional exception that tool throws when execution error
occurs.
When this exception is thrown, the agent will not stop working,but will
handle the exception according to the handle_tool_error variable of the
tool,and the processing result will be returned to the agent as
observation,and printed in pink on the console.It can be used like this:
```python 
from langchain.schema import ToolException
from langchain import LLMMathChain, SerpAPIWrapper, OpenAI
from langchain.agents import AgentType, initialize_agent
from langchain.chat_models import ChatOpenAI
from langchain.tools import BaseTool, StructuredTool, Tool, tool
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(temperature=0)
llm_math_chain = LLMMathChain(llm=llm, verbose=True)

class Error_tool:
    def run(self, s: str):
        raise ToolException('The current search tool is not available.')
    
def handle_tool_error(error) -> str:
    return "The following errors occurred during tool execution:"+str(error)

search_tool1 = Error_tool()
search_tool2 = SerpAPIWrapper()
tools = [
    Tool.from_function(
        func=search_tool1.run,
        name="Search_tool1",
        description="useful for when you need to answer questions about current events.You should give priority to using it.",
        handle_tool_error=handle_tool_error,
    ),
    Tool.from_function(
        func=search_tool2.run,
        name="Search_tool2",
        description="useful for when you need to answer questions about current events",
        return_direct=True,
    )
]
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True,
                         handle_tool_errors=handle_tool_error)
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")
```

![image](https://github.com/hwchase17/langchain/assets/32786500/51930410-b26e-4f85-a1e1-e6a6fb450ada)

## Who can review?
- @vowelparrot

---------

Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
This commit is contained in:
小铭
2023-05-30 04:05:58 +08:00
committed by GitHub
parent cce731c3c2
commit cf5803e44c
4 changed files with 272 additions and 9 deletions

View File

@@ -839,6 +839,127 @@
"source": [
"agent.run(\"whats 2**.12\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "f1da459d",
"metadata": {},
"source": [
"## Handling Tool Errors \n",
"When a tool encounters an error and the exception is not caught, the agent will stop executing. If you want the agent to continue execution, you can raise a `ToolException` and set `handle_tool_error` accordingly. \n",
"\n",
"When `ToolException` is thrown, the agent will not stop working, but will handle the exception according to the `handle_tool_error` variable of the tool, and the processing result will be returned to the agent as observation, and printed in red.\n",
"\n",
"You can set `handle_tool_error` to `True`, set it a unified string value, or set it as a function. If it's set as a function, the function should take a `ToolException` as a parameter and return a `str` value.\n",
"\n",
"Please note that only raising a `ToolException` won't be effective. You need to first set the `handle_tool_error` of the tool because its default value is `False`."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "ad16fbcf",
"metadata": {},
"outputs": [],
"source": [
"from langchain.schema import ToolException\n",
"\n",
"from langchain import SerpAPIWrapper\n",
"from langchain.agents import AgentType, initialize_agent\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.tools import Tool\n",
"\n",
"from langchain.chat_models import ChatOpenAI\n",
"\n",
"def _handle_error(error:ToolException) -> str:\n",
" return \"The following errors occurred during tool execution:\" + error.args[0]+ \"Please try another tool.\"\n",
"def search_tool1(s: str):raise ToolException(\"The search tool1 is not available.\")\n",
"def search_tool2(s: str):raise ToolException(\"The search tool2 is not available.\")\n",
"search_tool3 = SerpAPIWrapper()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c05aa75b",
"metadata": {},
"outputs": [],
"source": [
"description=\"useful for when you need to answer questions about current events.You should give priority to using it.\"\n",
"tools = [\n",
" Tool.from_function(\n",
" func=search_tool1,\n",
" name=\"Search_tool1\",\n",
" description=description,\n",
" handle_tool_error=True,\n",
" ),\n",
" Tool.from_function(\n",
" func=search_tool2,\n",
" name=\"Search_tool2\",\n",
" description=description,\n",
" handle_tool_error=_handle_error,\n",
" ),\n",
" Tool.from_function(\n",
" func=search_tool3.run,\n",
" name=\"Search_tool3\",\n",
" description=\"useful for when you need to answer questions about current events\",\n",
" ),\n",
"]\n",
"\n",
"agent = initialize_agent(\n",
" tools,\n",
" ChatOpenAI(temperature=0),\n",
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
" verbose=True,\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "cff8b4b5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3mI should use Search_tool1 to find recent news articles about Leo DiCaprio's personal life.\n",
"Action: Search_tool1\n",
"Action Input: \"Leo DiCaprio girlfriend\"\u001b[0m\n",
"Observation: \u001b[31;1m\u001b[1;3mThe search tool1 is not available.\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3mI should try using Search_tool2 instead.\n",
"Action: Search_tool2\n",
"Action Input: \"Leo DiCaprio girlfriend\"\u001b[0m\n",
"Observation: \u001b[31;1m\u001b[1;3mThe following errors occurred during tool execution:The search tool2 is not available.Please try another tool.\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3mI should try using Search_tool3 as a last resort.\n",
"Action: Search_tool3\n",
"Action Input: \"Leo DiCaprio girlfriend\"\u001b[0m\n",
"Observation: \u001b[38;5;200m\u001b[1;3mLeonardo DiCaprio and Gigi Hadid were recently spotted at a pre-Oscars party, sparking interest once again in their rumored romance. The Revenant actor and the model first made headlines when they were spotted together at a New York Fashion Week afterparty in September 2022.\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3mBased on the information from Search_tool3, it seems that Gigi Hadid is currently rumored to be Leo DiCaprio's girlfriend.\n",
"Final Answer: Gigi Hadid is currently rumored to be Leo DiCaprio's girlfriend.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"\"Gigi Hadid is currently rumored to be Leo DiCaprio's girlfriend.\""
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.run(\"Who is Leo DiCaprio's girlfriend?\")"
]
}
],
"metadata": {
@@ -857,7 +978,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
"version": "3.11.3"
},
"vscode": {
"interpreter": {