mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 01:45:27 +03:00
7.1 KiB
7.1 KiB
In [1]:
from langchain.agents import ZeroShotAgent, Tool
from langchain import OpenAI, SerpAPIWrapper, LLMChainIn [2]:
search = SerpAPIWrapper()
tools = [
Tool(
name = "Search",
func=search.run,
description="useful for when you need to answer questions about current events"
)
]In [3]:
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}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input"]
)In [4]:
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}
In [5]:
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)In [6]:
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)In [7]:
agent.run("How many people live in canada?")Out [7]:
[1m> Entering new chain...[0m How many people live in canada? Thought:[32;1m[1;3m I should look this up Action: Search Action Input: How many people live in canada[0m Observation: [36;1m[1;3mThe current population of Canada is 38,533,678 as of Friday, November 25, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada 2020 ...[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Arrr, there be 38,533,678 people in Canada[0m [1m> Finished chain.[0m
'Arrr, there be 38,533,678 people in Canada'
In [ ]: