Files
langchain/docs/examples/agents/custom_agent.ipynb

7.1 KiB

Custom Agent

This notebook goes through how to create your own custom agent.

An agent consists of three parts:

- Tools: The tools the agent has available to use.
- LLMChain: The LLMChain that produces the text that is parsed in a certain way to determine which action to take.
- The agent class itself: this parses the output of the LLMChain to determin which action to take.

In this notebook we walk through two types of custom agents. The first type shows how to create a custom LLMChain, but still use an existing agent class to parse the output. The second shows how to create a custom agent class.

Custom LLMChain

The first way to create a custom agent is to use an existing Agent class, but use a custom LLMChain. This is the simplest way to create a custom Agent. It is highly reccomended that you work with the ZeroShotAgent, as at the moment that is by far the most generalizable one.

Most of the work in creating the custom LLMChain comes down to the prompt. Because we are using an existing agent class to parse the output, it is very important that the prompt say to produce text in that format. However, besides those instructions, you can customize the prompt as you wish.

To ensure that the prompt contains the appropriate instructions, we will utilize a helper method on that class. The helper method for the ZeroShotAgent takes the following arguments:

  • tools: List of tools the agent will have access to, used to format the prompt.
  • prefix: String to put before the list of tools.
  • suffix: String to put after the list of tools.
  • input_variables: List of input variables the final prompt will expect.

For this exercise, we will give our agent access to Google Search, and we will customize it in that we will have it answer as a pirate.

In [1]:
from langchain.agents import ZeroShotAgent, Tool
from langchain import OpenAI, SerpAPIWrapper, LLMChain
In [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 case we are curious, we can now take a look at the final prompt template to see what it looks like when its all put together.

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]:

> Entering new chain...
How many people live in canada?
Thought: I should look this up
Action: Search
Action Input: How many people live in canada
Observation: 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. · Canada 2020 ...
Thought: I now know the final answer
Final Answer: Arrr, there be 38,533,678 people in Canada
> Finished chain.
'Arrr, there be 38,533,678 people in Canada'

Custom Agent Class

Coming soon.

In [ ]: