Agent#
This notebook covers how to create a custom agent for a chat model. It will utilize chat specific prompts.
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain.chains import LLMChain
from langchain.utilities import SerpAPIWrapper
search = SerpAPIWrapper()
tools = [
Tool(
name = "Search",
func=search.run,
description="useful for when you need to answer questions about current events"
)
]
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"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=[]
)
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
AIMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage
)
messages = [
SystemMessagePromptTemplate(prompt=prompt),
HumanMessagePromptTemplate.from_template("{input}\n\nThis was your previous work "
f"(but I haven't seen any of it! I only see what "
"you return as final answer):\n{agent_scratchpad}")
]
prompt = ChatPromptTemplate.from_messages(messages)
llm_chain = LLMChain(llm=ChatOpenAI(temperature=0), prompt=prompt)
tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
agent_executor.run("How many people live in canada as of 2023?")
> Entering new AgentExecutor chain...
Arrr, ye be in luck, matey! I'll find ye the answer to yer question.
Thought: I need to search for the current population of Canada.
Action: Search
Action Input: "current population of Canada 2023"
Observation: The current population of Canada is 38,623,091 as of Saturday, March 4, 2023, based on Worldometer elaboration of the latest United Nations data.
Thought:Ahoy, me hearties! I've found the answer to yer question.
Final Answer: As of March 4, 2023, the population of Canada be 38,623,091. Arrr!
> Finished chain.
'As of March 4, 2023, the population of Canada be 38,623,091. Arrr!'