langchain/tests/unit_tests/tools
小铭 cf5803e44c
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>
2023-05-29 20:05:58 +00:00
..
file_management Add Other File Utilities (#3209) 2023-04-28 10:53:37 -07:00
openapi Add Request body (#2507) 2023-04-06 13:02:42 -07:00
powerbi power bi api wrapper integration tests & bug fix (#4983) 2023-05-19 11:25:52 -04:00
python Improved query, print & exception handling in REPL Tool (#4997) 2023-05-22 13:43:44 +00:00
requests tools refactor (#2961) 2023-04-17 21:35:29 -07:00
shell Accept str or list[str] for shell (#4060) 2023-05-03 21:11:06 -07:00
__init__.py
test_base.py Add ToolException that a tool can throw. (#5050) 2023-05-29 20:05:58 +00:00
test_exported.py Update some Tools Docs (#3913) 2023-05-01 19:07:26 -07:00
test_json.py
test_public_api.py Add AzureCognitiveServicesToolkit to call Azure Cognitive Services API (#5012) 2023-05-23 06:45:48 -07:00
test_signatures.py Pass parsed inputs through to tool _run (#4309) 2023-05-08 09:13:05 -07:00
test_zapier.py Allow custom base Zapier prompt (#4213) 2023-05-13 21:08:18 -07:00