mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-10 03:45:17 +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>
11 KiB
11 KiB
In [23]:
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain import OpenAI, SerpAPIWrapper, LLMChainIn [24]:
search = SerpAPIWrapper()
tools = [
Tool(
name = "Search",
func=search.run,
description="useful for when you need to answer questions about current events"
)
]In [25]:
prefix = """Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:"""
suffix = """Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Args"
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "agent_scratchpad"]
)In [26]:
print(prompt.template)Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:
Search: useful for when you need to answer questions about current events
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [Search]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Args"
Question: {input}
{agent_scratchpad}
In [27]:
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)In [28]:
tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)In [29]:
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)In [31]:
agent_executor.run("How many people live in canada as of 2023?")Out [31]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3mThought: I need to find out the population of Canada Action: Search Action Input: Population of Canada 2023[0m Observation: [36;1m[1;3mThe current population of Canada is 38,610,447 as of Saturday, February 18, 2023, based on Worldometer elaboration of the latest United Nations data. Canada 2020 population is estimated at 37,742,154 people at mid year according to UN data.[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Arrr, Canada be havin' 38,610,447 scallywags livin' there as of 2023![0m [1m> Finished chain.[0m
"Arrr, Canada be havin' 38,610,447 scallywags livin' there as of 2023!"
In [32]:
prefix = """Answer the following questions as best you can. You have access to the following tools:"""
suffix = """When answering, you MUST speak in the following language: {language}.
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "language", "agent_scratchpad"]
)In [33]:
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)In [34]:
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)In [35]:
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)In [36]:
agent_executor.run(input="How many people live in canada as of 2023?", language="italian")Out [36]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3mThought: I need to find out the population of Canada in 2023. Action: Search Action Input: Population of Canada in 2023[0m Observation: [36;1m[1;3mThe current population of Canada is 38,610,447 as of Saturday, February 18, 2023, based on Worldometer elaboration of the latest United Nations data. Canada 2020 population is estimated at 37,742,154 people at mid year according to UN data.[0m Thought:[32;1m[1;3m I now know the final answer. Final Answer: La popolazione del Canada nel 2023 è stimata in 38.610.447 persone.[0m [1m> Finished chain.[0m
'La popolazione del Canada nel 2023 è stimata in 38.610.447 persone.'
In [ ]: