LangChain

What is an Agent in LangChain?

LangChain framework is used to develop apps that utilize language models. LLMs give general responses, they don’t target any specific field while LangChain has the most powerful attribute it provides to create chains in which users can combine multiple components together and make a single coherent application. LangChain has many modules, data connections, chains, agents, memory, and callback.

In this article, we will discuss agents in LangChain from all possible aspects

What is an Agent in LangChain?

Some applications require not just predetermined chains but they require an unknown chain that depends on the user’s input. For such case, there is an “agent” who access the tool and decide which tool is required according to the user’s input and what he or she is asking for. A toolkit is basically a set of tools that is needed to do a specific objective and there are 3-5 tools in a toolkit.

Types of LangChain Agents

There are two main agents:

  • Action Agents
  • Plan-and-Execute Agents

Action Agents: These agents decide actions to take step by step evaluate each step and then execute it and move to the next if we discuss the pseudo-code of the agent which involves a few steps

  • Input is received from the user.
  • The agent decides the tool and what type of tool is required.
  • That tool is called with input tool and observation is recorded.
  • The history tool, observation tool, and input tool are passed back to the agent.
  • Repeat the process until the agent decides to quit this tool.

Plan-and-Execute Agents: These agents first decide on an action to take, and then execute all those actions.

  • User input is received.
  • The agent lists out all the steps to execute.
  • The executor goes through the list of steps, executing them.

Setting up Agent

Before setting up the agent you need to install the latest version of Python according to your operating system.

Step 1: Installing Packages
Firstly, we have to establish an environment for this we have to install LangChain, google-search-results, and openai through the “pip” command:

!pip install langchain
!pip install google-search-results
!pip install openai

Importing required libraries:

from langchain.schema import SystemMessage
from langchain.agents import OpenAIFunctionsAgent, AgentExecutor
from langchain.agents import tool
from langchain.chat_models import ChatOpenAI
import re
from getpass import getpass

Step 2: Get your Secret API
After setting up an environment, now you have to get secret API keys from the OpenAI Platform:

openai_api_key = getpass()
llm = ChatOpenAI(openai_api_key=openai_api_key,temperature=0)

Step 3: Initializing Tool
Next let’s define a tool, writing simple Python code to get the length of a string.

@tool
def get_word_string(word: str) -> int:
    """give me the length of a string."""
    return len(word)

tools = [get_word_string]

Step 4: Create a Prompt Template
After Defining the tool, set up a Prompt Template for this use the “OpenAIFunctionsAgent.create_prompt()” helper function which will create the template automatically.

system_message = SystemMessage(content="You are very powerful assistant, but bad at calculating lengths of string.")
prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)

Step 5: Creating Agent
Now we can conclude all the pieces and create an agent by using a function called “OpenAIFunctionsAgent()”.

agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)

Step 6: Setting Up Runtime
If you have created an agent successfully then create a runtime for the agent, for this ”AgentExecutor” is used as a runtime for the agent.

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

Step 7: Agent Testing
After creating Runtime, now it’s time to test the agent.

agent_executor.run("How many words does this string have?")

If you have inserted to correct API key in Step 2, you will get a response.

Conclusion

This article has been illustrated from many aspects, firstly it demonstrates what is LangChain, and how it works, then it moves to agents in LangChain, and discusses the purpose of the agents in LangChain and contains information about the two major types of agents “Action Agents” and “Plan-and-Execute Agents” used in LangChain and at the end code execution has been to establish an agent in LangChain

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.