mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 01:45:27 +03:00
10 KiB
10 KiB
In [1]:
from langchain.agents import ZeroShotAgent, Tool
from langchain.chains.conversation.memory import ConversationBufferMemory
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 = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
suffix = """Begin!"
{chat_history}
Question: {input}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "chat_history"]
)
memory = ConversationBufferMemory(memory_key="chat_history")In [4]:
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt, memory=memory)
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)In [5]:
agent.run("How many people live in canada?")Out [5]:
[1m> Entering new chain...[0m How many people live in canada? Thought:[32;1m[1;3m I should look up how many people live in canada 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: The current population of Canada is 38,533,678 as of Friday, November 25, 2022, based on Worldometer elaboration of the latest United Nations data.[0m [1m> Finished chain.[0m
'The current population of Canada is 38,533,678 as of Friday, November 25, 2022, based on Worldometer elaboration of the latest United Nations data.'
In [6]:
agent.run("what is their national anthem called?")Out [6]:
[1m> Entering new chain...[0m what is their national anthem called? Thought:[32;1m[1;3m AI: I should look up the name of Canada's national anthem Action: Search Action Input: "What is the name of Canada's national anthem?"[0m Observation: [36;1m[1;3mAfter 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa ...[0m Thought:[32;1m[1;3m AI: I now know the final answer Final Answer: After 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa Lavallée.[0m [1m> Finished chain.[0m
"After 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa Lavallée."
In [7]:
prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
suffix = """Begin!"
Question: {input}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input"]
)
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
agent_without_memory = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)In [10]:
agent_without_memory.run("How many people live in canada?")Out [10]:
[1m> Entering new chain...[0m How many people live in canada? Thought:[32;1m[1;3m I should look up how many people live in canada 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: The current population of Canada is 38,533,678[0m [1m> Finished chain.[0m
'The current population of Canada is 38,533,678'
In [11]:
agent_without_memory.run("what is their national anthem called?")Out [11]:
[1m> Entering new chain...[0m what is their national anthem called? Thought:[32;1m[1;3m I should probably look this up Action: Search Action Input: "What is the national anthem of [country]"[0m Observation: [36;1m[1;3mMost nation states have an anthem, defined as "a song, as of praise, devotion, or patriotism"; most anthems are either marches or hymns in style.[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: The national anthem is called "the national anthem."[0m [1m> Finished chain.[0m
'The national anthem is called "the national anthem."'
In [ ]: