LangChain

Getting Started With Agents in LangChain?

LangChain is the framework to solve Natural Language Processing to build applications or software that can interact and converse with humans. The chatbots or Large Language Models(LLMs) are designed to create an environment that can act as an interface for chat/conversation. These chats are conducted in human languages called natural languages like English, etc. between humans and an AI model.

Quick Outline

This post will demonstrate the following:

What are Agents in Natural Language Processing(NLP)

Getting Started With Agents in LangChain

Conclusion

What are Agents in Natural Language Processing(NLP)?

Agents are the vital components of the natural language application and they use the Natural Language Understanding (NLU) to understand queries. These agents are programs that act like a conversational template for having an interaction with humans using the sequence of tasks. Agents use multiple tools that can be called by the agent to perform multiple actions or specify the next task to perform.

Getting Started With Agents in LangChain

Start the process of building the agents to have a conversation with humans by extracting output using the agents in LangChain. To learn the process of getting started with the agents in LangChain, simply follow the listed steps below:

Step 1: Installing Frameworks

First, get started with the process of installing the LangChain framework using the “pip” command to get the required dependencies for using agents:

pip install langchain

Install the OpenAI module for building the LLM and use it to configure the agents in LangChain:

pip install openai

Set up the environment for the OpenAI module using its API key from the account by running the following code:

import os
import getpass

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

Step 2: Configure Chat Model

Import the ChatOpenAI module from the LangChain to build the LLM using its function:

from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(temperature=0)

Import tools for the agent to configure the tasks or actions needed to be performed by the agent. The following code uses the get_word_length() method to get the length of the word provided by the user:

from langchain.agents import tool

@tool

def get_word_length(word: str) -> int:

   """getting the word's length"""

   return len(word)

tools = [get_word_length]

Configure the template or structure for the chat model to create an interface for having a chat:

from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder

prompt = ChatPromptTemplate.from_messages([

   ("system", "your assistant is quite amazing, but need improvement at calculating lengths"),

  ("user", "{input}"),

  MessagesPlaceholder(variable_name="agent_scratchpad"),

])

Step 3: Building Agent

Import the tools library for building the LLM with tools using the OpenAI functions from the LangChain module:

from langchain.tools.render import format_tool_to_openai_function

llm_with_tools = llm.bind(

  functions=[format_tool_to_openai_function(t) for t in tools]

)

Configure the agent using the OpenAI function agent to use the output parser to set the actions/tasks sequences:

from langchain.agents.format_scratchpad import format_to_openai_functions

from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser

agent = {

   "input": lambda x: x["input"],

   "agent_scratchpad": lambda x: format_to_openai_functions(x['intermediate_steps'])

} | prompt | llm_with_tools | OpenAIFunctionsAgentOutputParser()

Step 4: Invoking the Agent

The next step uses the invoke() function to call the agent using the input and intermediate_steps arguments:

agent.invoke({

  "input": "how many letters in the word good",

  "intermediate_steps": []

})

Step 5: Configure Agent Tools

After that, simply import the AgentFinish library to configure the intermediate_steps by integrating all the steps in a sequence to complete the activity:

from langchain.schema.agent import AgentFinish
intermediate_steps = []
while True:
    output = agent.invoke({
        "input": " letters in good",
        "intermediate_steps": intermediate_steps
    })
    if isinstance(output, AgentFinish):
        final_result = output.return_values["output"]
        break
    else:
        print(output.tool, output.tool_input)
        tool = {
            "get_word_length": get_word_length
        }[output.tool]
        observation = tool.run(output.tool_input)
        intermediate_steps.append((output, observation))
print(final_result)

Step 6: Testing the Agent

Now, execute the agent by calling the AgentExecutor() method after importing its library from the LangChain:

from langchain.agents import AgentExecutor

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

At the end, invoke the agent_executor with the input argument to enter the query for the agent:

agent_executor.invoke({"input": "how many letters in the word good"})

The agent has displayed the answer to the question provided in the input argument after finishing the chain:

That’s all about getting started with the agents in the LangChain framework.

Conclusion

To get started with the agents in LangChain, simply install the modules required to set up the environment using the OpenAI API key. After that, configure the chat model by setting the prompt template for building the agent with the sequence of intermediate steps. Once the agent is configured, simply build the tools by specifying the tasks after giving the input string to the user. This blog has demonstrated the process of using the agents 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.