Files
langchain/docs/modules/agents/tools/multi_input_tool.ipynb
T
Gerard Hernandez cc50a4579e Fix spelling and grammar in multi_input_tool.ipynb (#2337)
Changes:
- Corrected the title to use hyphens instead of spaces.
- Fixed a typo in the second paragraph where "therefor" was changed to
"Therefore".
- Added a hyphen between "comma" and "separated" in the last paragraph.

File link:
[multi_input_tool.ipynb](https://github.com/hwchase17/langchain/blob/master/docs/modules/agents/tools/multi_input_tool.ipynb)
2023-04-03 14:13:48 -07:00

4.0 KiB

Multi-Input Tools

This notebook shows how to use a tool that requires multiple inputs with an agent.

The difficulty in doing so comes from the fact that an agent decides its next step from a language model, which outputs a string. So if that step requires multiple inputs, they need to be parsed from that. Therefore, the currently supported way to do this is to write a smaller wrapper function that parses a string into multiple inputs.

For a concrete example, let's work on giving an agent access to a multiplication function, which takes as input two integers. In order to use this, we will tell the agent to generate the "Action Input" as a comma-separated list of length two. We will then write a thin wrapper that takes a string, splits it into two around a comma, and passes both parsed sides as integers to the multiplication function.

In [1]:
from langchain.llms import OpenAI
from langchain.agents import initialize_agent, Tool

Here is the multiplication function, as well as a wrapper to parse a string as input.

In [2]:
def multiplier(a, b):
    return a * b

def parsing_multiplier(string):
    a, b = string.split(",")
    return multiplier(int(a), int(b))
In [3]:
llm = OpenAI(temperature=0)
tools = [
    Tool(
        name = "Multiplier",
        func=parsing_multiplier,
        description="useful for when you need to multiply two numbers together. The input to this tool should be a comma separated list of numbers of length two, representing the two numbers you want to multiply together. For example, `1,2` would be the input if you wanted to multiply 1 by 2."
    )
]
mrkl = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
In [4]:
mrkl.run("What is 3 times 4")
Out [4]:

> Entering new AgentExecutor chain...
 I need to multiply two numbers
Action: Multiplier
Action Input: 3,4
Observation: 12
Thought: I now know the final answer
Final Answer: 3 times 4 is 12

> Finished chain.
'3 times 4 is 12'
In [ ]: