mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 18:05:25 +03:00
Follow-up of @hinthornw's PR: - Migrate the Tool abstraction to a separate file (`BaseTool`). - `Tool` implementation of `BaseTool` takes in function and coroutine to more easily maintain backwards compatibility - Add a Toolkit abstraction that can own the generation of tools around a shared concept or state --------- Co-authored-by: William FH <13333726+hinthornw@users.noreply.github.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Francisco Ingham <fpingham@gmail.com> Co-authored-by: Dhruv Anand <105786647+dhruv-anand-aintech@users.noreply.github.com> Co-authored-by: cragwolfe <cragcw@gmail.com> Co-authored-by: Anton Troynikov <atroyn@users.noreply.github.com> Co-authored-by: Oliver Klingefjord <oliver@klingefjord.com> Co-authored-by: William Fu-Hinthorn <whinthorn@Williams-MBP-3.attlocal.net> Co-authored-by: Bruno Bornsztein <bruno.bornsztein@gmail.com>
4.0 KiB
4.0 KiB
In [1]:
from langchain.llms import OpenAI
from langchain.agents import initialize_agent, ToolIn [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]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to multiply two numbers Action: Multiplier Action Input: 3,4[0m Observation: [36;1m[1;3m12[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: 3 times 4 is 12[0m [1m> Finished chain.[0m
'3 times 4 is 12'
In [ ]: