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>
5.8 KiB
5.8 KiB
In [1]:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAIIn [2]:
llm = OpenAI(temperature=0, model_name='text-davinci-002')
tools = load_tools(["serpapi", "llm-math"], llm=llm)In [3]:
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True, return_intermediate_steps=True)In [5]:
response = agent({"input":"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?"})[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I should look up who Leo DiCaprio is dating Action: Search Action Input: "Leo DiCaprio girlfriend"[0m Observation: [36;1m[1;3mCamila Morrone[0m Thought:[32;1m[1;3m I should look up how old Camila Morrone is Action: Search Action Input: "Camila Morrone age"[0m Observation: [36;1m[1;3m25 years[0m Thought:[32;1m[1;3m I should calculate what 25 years raised to the 0.43 power is Action: Calculator Action Input: 25^0.43[0m Observation: [33;1m[1;3mAnswer: 3.991298452658078 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and she is 3.991298452658078 years old.[0m [1m> Finished chain.[0m
In [6]:
# The actual return type is a NamedTuple for the agent action, and then an observation
print(response["intermediate_steps"])[(AgentAction(tool='Search', tool_input='Leo DiCaprio girlfriend', log=' I should look up who Leo DiCaprio is dating\nAction: Search\nAction Input: "Leo DiCaprio girlfriend"'), 'Camila Morrone'), (AgentAction(tool='Search', tool_input='Camila Morrone age', log=' I should look up how old Camila Morrone is\nAction: Search\nAction Input: "Camila Morrone age"'), '25 years'), (AgentAction(tool='Calculator', tool_input='25^0.43', log=' I should calculate what 25 years raised to the 0.43 power is\nAction: Calculator\nAction Input: 25^0.43'), 'Answer: 3.991298452658078\n')]
In [7]:
import json
print(json.dumps(response["intermediate_steps"], indent=2))[
[
[
"Search",
"Leo DiCaprio girlfriend",
" I should look up who Leo DiCaprio is dating\nAction: Search\nAction Input: \"Leo DiCaprio girlfriend\""
],
"Camila Morrone"
],
[
[
"Search",
"Camila Morrone age",
" I should look up how old Camila Morrone is\nAction: Search\nAction Input: \"Camila Morrone age\""
],
"25 years"
],
[
[
"Calculator",
"25^0.43",
" I should calculate what 25 years raised to the 0.43 power is\nAction: Calculator\nAction Input: 25^0.43"
],
"Answer: 3.991298452658078\n"
]
]
In [ ]:
In [ ]: