Agents

In this lesson, you will learn how to create an agent.

Agents wrap a model and give it access to a set of tools. These tools may access additional data sources, APIs, or functionality. The model is used to determine which of the tools to use to complete a task.

The agent you will create will be able to chat about movies and search YouTube for movie trailers.

Tools

A tool is a specific abstraction around a function that makes it easy for a language model to interact with it. Langchain provides several tools out of the box, and you can create tools to extend the functionality of your agents.

Tools can be grouped into toolkits to solve a particular problem. For example, a toolkit that gets YouTube information - videos, thumbnails, transcripts, views, etc.

You will use the YouTubeSearchTool to search YouTube for movie trailers.

Movie trailer agent

Review the program below, before running it.

python
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.agents import AgentExecutor, create_react_agent
from langchain.tools import Tool
from langchain import hub

llm = ChatOpenAI(openai_api_key="sk-...")

prompt = PromptTemplate(
    template="""
    You are a movie expert. You find movies from a genre or plot.

    Chat History:{chat_history}
    Question:{input}
    """,
    input_variables=["chat_history", "input"],
)

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

chat_chain = LLMChain(llm=llm, prompt=prompt, memory=memory)

tools = [
    Tool.from_function(
        name="Movie Chat",
        description="For when you need to chat about movies. The question will be a string. Return a string.",
        func=chat_chain.run,
        return_direct=True,
    )
]

agent_prompt = hub.pull("hwchase17/react-chat")
agent = create_react_agent(llm, tools, agent_prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory)

while True:
    q = input("> ")
    response = agent_executor.invoke({"input": q})
    print(response["output"])
Click here to see a typical output from this program
[user] Find a movie where aliens land on earth.
[chat model] Sure, I can help you with that. One movie I would recommend where aliens land on Earth is "Arrival" (2016). It's a science fiction film directed by Denis Villeneuve. The story follows a linguist who is recruited by the military to help communicate with an alien species that has landed on Earth. It's a thought-provoking and visually stunning movie that explores themes of communication, time, and the human experience. I hope you enjoy it!

Based on your understanding from previous lessons, you should be able to identify the following:

  1. A chat model is being used to have a conversation about movies

  2. The prompt which sets the context for the LLM and the input variables

  3. That memory is used to store the conversation history

  4. A chain is created to link the chat model, prompt, and memory together

In addition to the above, the following is new:

  1. A tool is created using the chain:

    python
    tools = [
        Tool.from_function(
            name="Movie Chat",
            description="For when you need to chat about movies. The question will be a string. Return a string.",
            func=chat_chain.run,
            return_direct=True
        )
    ]
  2. An agent is created that uses the tool:

    python
    agent_prompt = hub.pull("hwchase17/react-chat")
    agent = create_react_agent(llm, tools, agent_prompt)
    agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory)

Creating tools

Tools are interfaces that an agent can interact with. You can create custom tools able to perform any functionality you want.

In this example, the Tool is created from a function. The function is the chat_chain.run method.

python
tools = [
    Tool.from_function(
        name="Movie Chat",
        description="For when you need to chat about movies. The question will be a string. Return a string.",
        func=chat_chain.run,
        return_direct=True
    )
]

The name and description help the LLM select which tool to use when presented with a question. The func parameter is the function that will be called when the tool is selected. The return_direct flag indicates that the tool will return the result directly.

Agents support multiple tools, so you pass them to the agent as a list (tools).

Initializing an agent

The following code creates the agent:

python
agent_prompt = hub.pull("hwchase17/react-chat")
agent = create_react_agent(llm, tools, agent_prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory)

There are different types of agents that you can create. This example creates a ReAct - Reasoning and Acting) agent type.

An agent requires a prompt. You could create a prompt, but in this example, the program pulls a pre-existing prompt from the Langsmith Hub.

The hwcase17/react-chat prompt instructs the model to provide an answer using the tools available in a specific format.

The create_react_agent function creates the agent and expects the following parameters:

  • The llm that will manage the interactions and decide which tool to use

  • The tools that the agent can use

  • The prompt that the agent will use

The AgentExecutor class runs the agent. It expects the following parameters:

  • The agent to run

  • The tools that the agent can use

  • The memory which will store the conversation history

AgentExecutor parameters

You may find the following additional parameters useful when initializing an agent:

  • max_iterations - the maximum number of iterations to run the LLM for. This is useful in preventing the LLM from running for too long or entering an infinite loop.

  • verbose - if True the agent will print out the LLM output and the tool output.

  • handle_parsing_errors - if True the agent will handle parsing errors and return a message to the user.

python
agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    memory=memory,
    max_interations=3,
    verbose=True,
    handle_parse_errors=True
)

Multiple tools

A key advantage of using an agent is that they can use multiple tools. Access to multiple tools allows you to create agents that can perform several specific tasks.

You can extend this example to allow it to search YouTube for movie trailers by adding the YouTubeSearchTool to the tools list.

Firsly, you will need to install the youtube-search package.

bash
pip install youtube-search

Import the YouTubeSearchTool and create a new tool.

python
from langchain_community.tools import YouTubeSearchTool

youtube = YouTubeSearchTool()

tools = [
    Tool.from_function(
        name="Movie Chat",
        description="For when you need to chat about movies. The question will be a string. Return a string.",
        func=chat_chain.run,
        return_direct=True
    ),
    Tool.from_function(
        name="Movie Trailer Search",
        description="Use when needing to find a movie trailer. The question will include the word 'trailer'. Return a link to a YouTube video.",
        func=youtube.run,
        return_direct=True
    )
]
Click here to reveal the complete program
python
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.agents import AgentExecutor, create_react_agent
from langchain.tools import Tool
from langchain import hub
from langchain_community.tools import YouTubeSearchTool

llm = ChatOpenAI(openai_api_key="sk-...")

prompt = PromptTemplate(
    template="""
    You are a movie expert. You find movies from a genre or plot.

    Chat History:{chat_history}
    Question:{input}
    """,
    input_variables=["chat_history", "input"],
)

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

chat_chain = LLMChain(llm=llm, prompt=prompt, memory=memory)

youtube = YouTubeSearchTool()

tools = [
    Tool.from_function(
        name="Movie Chat",
        description="For when you need to chat about movies. The question will be a string. Return a string.",
        func=chat_chain.run,
        return_direct=True,
    ),
    Tool.from_function(
        name="Movie Trailer Search",
        description="Use when needing to find a movie trailer. The question will include the word 'trailer'. Return a link to a YouTube video.",
        func=youtube.run,
        return_direct=True,
    ),
]

agent_prompt = hub.pull("hwchase17/react-chat")
agent = create_react_agent(llm, tools, agent_prompt)
agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    memory=memory,
    max_interations=3,
    verbose=True,
    handle_parse_errors=True,
)

while True:
    q = input("> ")
    response = agent_executor.invoke({"input": q})
    print(response["output"])

The model will then use the name and description for each tool to decide which tool to use.

When prompted to find a movie trailer, the model should use the YouTubeSearchTool tool.

[user] Find the movie trailer for the Matrix.
[agent] ['https://www.youtube.com/watch?v=vKQi3bBA1y8&pp=ygUUTWF0cml4IG1vdmllIHRyYWlsZXI%3D', 'https://www.youtube.com/watch?v=9ix7TUGVYIo&pp=ygUUTWF0cml4IG1vdmllIHRyYWlsZXI%3D']

However, when asked about movies, genres or plots, the model will use the chat_chain tool.

[user] Find a movie about the meaning of life
[agent] Certainly! One movie that explores the meaning of life is "The Tree of Life" directed by Terrence Malick. It follows the journey of a young boy as he grows up in the 1950s and reflects on his experiences and the meaning of existence. It's a visually stunning and thought-provoking film that delves into existential questions.

As the agent also uses the conversation memory you can refer back to the previous questions, such as finding a trailer for a movie it has recommended:

[user] Can you find the trailer
[agent] ['https://www.youtube.com/watch?v=RrAz1YLh8nY&pp=ygUeVGhlIFRyZWUgb2YgTGlmZSBtb3ZpZSB0cmFpbGVy', 'https://www.youtube.com/watch?v=OKqqboXuvyE&pp=ygUeVGhlIFRyZWUgb2YgTGlmZSBtb3ZpZSB0cmFpbGVy']

Agents and tools allow you to create more adaptable and flexible models to perform multiple tasks.

Check Your Understanding

Agents

Select all relating to agents that are True.

  • ✓ Agents give a model access to a set of tools

  • ✓ The model decides which tool to use

  • ❏ Memory is not accessible by an agent

  • ❏ Agents can only use tools provided by langchain

Hint

Agents are highly flexible and can used a variety of tools to accomplish their goals.

Solution

The correct answers are:

  • Agents give a model access to a set of tools

  • The model decides which tool to use

The others are false because:

  • Agents can have memory

  • You can create custom tools for agents to use

Summary

In this lesson, you learned how to create an agent to use multiple tools.

In the next lesson, you will learn how to use Langchain to connect to a Neo4j database.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?