LangChain

How to Build a Custom Multi-Action Agent in LangChain?

LangChain provides the dependencies for building language models that can be trained on the data set of natural language text. The trained model can understand the queries given by the user in the human language and it becomes able to build a conversation with humans. The chat model answers questions by getting multiple observations from different sources and finding the correct answer to display on the screen.

Quick Outline

This post will demonstrate the following:

How to Built Custom Multi-Action Agent in LangChain

Agent is the tool that automates the steps and actions to complete the task of extracting information using the input string. Agents can perform multiple actions at a time like adding a signature with the displayed answer or writing some text with the answer. To learn the process of building the custom multi-action agent in LangChain, simply go through the listed steps:

Step 1: Installing Frameworks

Get started with the process of customizing the multi-action agent by installing the LangChain framework:

pip install langchain

Also, install the openai google-search-results to build the language model for getting the answers from the internet:

pip install openai google-search-results

Step 2: Setting up Environments

Set up the environment for the agent using the API keys from the OpenAI and SerpAPi accounts:

import os
import getpass

os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
os.environ["SERPAPI_API_KEY"] = getpass.getpass("Serpapi API Key:")

Step 3: Importing Libraries

Once the environments are set up, import the libraries using the dependencies from the LangChain framework:

from langchain.agents import Tool
from langchain.agents import AgentExecutor
from langchain.agents import BaseMultiActionAgent
from langchain.llms import OpenAI
#import library to get the google search results
from langchain.utilities import SerpAPIWrapper

Step 4: Configuring Random Action

Now, add an action that will be executed with the main action that is extracting answers to the questions given by the user:

def random_word(query: str) -> str:
    print("\nNow I'm doing this!")
    return "foo"

Step 5: Setting up Tools

Also, configure the tools for building the multi-action agent to fetch the final answers with the functions defined in the previous step:

search = SerpAPIWrapper()
tools = [
    Tool(
        name="Search",
        func=search.run,
        description="useful to asking queries with search tool",
    ),
    Tool(
        name="RandomWord",
        func=random_word,
        description="call this to get a random word",
    ),
]

Step 6: Building Multi-Action Agent

After setting up the tools, simply build the agent by configuring the FakeAgent() method with the BaseMultiActionAgent parameter. Also, configure the steps to perform in order to understand the questions to extract the correct answer from the internet:

from typing import List, Tuple, Any, Union
from langchain.schema import AgentAction, AgentFinish
#building the agent class to explain its key responsibilities and tasks after importing the required libraries

class FakeAgent(BaseMultiActionAgent):
    """Fake Custom Agent."""
#set the input keys to building the structure of the customized agent to get started with the process
    @property
    def input_keys(self):
        return ["input"]
#defining plan() method to make sure that it works to perform all the steps to get the required answer
    def plan(
        self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any
    ) -> Union[List[AgentAction], AgentFinish]:
        """ Given input, decided what to do.
#use multi-line comments to set the pathway of the steps
        Args:
            intermediate_steps: Tasks the Language model has performed till now,
                With its observations
            **kwargs: User inputs
#setting the agent when to stop generating text once the correct answer is found
        Returns:
            Action specifying what tool to use
        "
""
        if len(intermediate_steps) == 0:
            return [
                AgentAction(tool="Search", tool_input=kwargs["input"], log=""),
                AgentAction(tool="RandomWord", tool_input=kwargs["input"], log=""),
            ]
        else:
            return AgentFinish(return_values={"output": "bar"}, log="")
#usage of the objects used by the agents to tell their current status to find where the agent stands and define the asynchronous method.
    async def aplan(
        self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any
    ) -> Union[List[AgentAction], AgentFinish]:
        """ Given input, decided what to do
#use multi-line comments to set the pathway of the steps
        Args:
            intermediate_steps: Tasks the Language model has performed till now,
                With its observations
            **kwargs: User inputs
#setting the agent when to stop generating text once the correct answer is found
        Returns:
            Action specifying what tool to use
        "
""
        if len(intermediate_steps) == 0:
            return [
                AgentAction(tool="Search", tool_input=kwargs["input"], log=""),
                AgentAction(tool="RandomWord", tool_input=kwargs["input"], log=""),
            ]
        else:
            return AgentFinish(return_values={"output": "bar"}, log="")

Step 7: Designing Agent Executor

After configuring the agent, define the agent variable using the FakeAgent() method containing all the configurations:

agent = FakeAgent()

Design the AgentExecutor to run the agent using the agent, tools, and verbose arguments:

agent_executor = AgentExecutor.from_agent_and_tools(
    agent=agent, tools=tools, verbose=True
)

Step 8: Testing the Agent

Simply test the agent to check if it performs both tasks like answering the following question by printing the lines defined in the random_word() method:

agent_executor.run("What is the population of Brazil as of 2023")

The following screenshot displays that the agent has extracted the answer and prints the line by calling the random_work() method:

That’s all about building the custom multi-action agent in LangChain.

Conclusion

To build the custom multi-action agent in LangChain, simply install the modules to get the dependencies for importing the libraries. Use the libraries to set up the tools for searching data from the internet and calling the random_word() function at the same time. After that, simply test the agent by defining the AgentExecutor() method to run the multi-action agent in LangChain. This guide has elaborated on the process of building the custom multi-action agent in LangChain.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.