A Beginner's Tutorial on Langchain Agent Building

A Comprehensive How-To Guide

If you are here, I assume you have some basic knowledge about LLM and RAG (Retrieval Augmented Generation).
Here's my favorite blog that explains RAG if you want to learn more about it.
What Is Retrieval-Augmented Generation, aka RAG?

Don't worry I am scared too with the fast-moving world of AI, every time I open my phone there's some new paper 😅. I hope my shared insights from my learning will help you start with LLM Agents.

What is an Agent?

Agents are designed to perform precise tasks. As the Langchain documentation says

The core idea of agents is to use a language model to choose a sequence of actions to take.

Unlike normal generative/ retrieval responses, Agents use particular tools to carry out certain tasks.
For example, it uses an Essay writing tool when the user queries a description to write an Essay. It is like creating different tools for different actions that you want the Agent to carry out. You will understand this after going through the following example.

The introduction of agents has revolutionized the application of large language models (LLMs) in addressing intricate problems. Language Models (LLMs) have introduced a cutting-edge dimension with their enhanced Natural Language Understanding (NLU) capabilities.
Leveraging the potential of Large Language Models (LLMs) for practical applications proves immensely beneficial with the assistance of Agents. Agents are powered by LLM and a prompt.

The following are components of a Langchain Agent:

1) Tools :
Tools are functionalities that agents can employ to engage with the world. These tools may encompass general utilities (such as search), other chains, or even interactions with other agents.

2) User input: A Natural Language input from the user which is a high-level objective.

**3) Intermediate steps:
**In prior executions, specific actions involving tools were undertaken to fulfill user input. The outcome of these actions can be either the next set of actions (AgentActions) to be performed or the ultimate response intended for the user (AgentFinish). Each action comprises the selection of a tool and the provision of input for that tool.

You can build a custom agent as well, here are a few existing agent types(here).
Agents vary in their approaches to prompting styles for reasoning, encoding input information, and parsing output results.

How to create a Tool?

There are various prebuild tools offered by Langchain, such as duckduckgo (for internet access), Wikipedia, and many more. Explore all available tools here.
When creating your own agent, you'll have to furnish it with a set of tools it can utilize. Beyond just the invoked function, a tool comprises various components.

  • name (str), is required and must be unique within a set of tools provided to an agent

  • description (str), is optional but recommended, as it is used by an agent to determine tool use

  • return_direct (bool), defaults to False

  • args_schema (Pydantic BaseModel), is optional but recommended, can be used to provide more information (e.g., few-shot examples) or validation for expected parameters.

Refer to this page to create a custom tool.

How to create agents using Langchain

I will be using Langchain's duckduckgo tool. duckduckgo is a search engine, this tool gives agents internet access to search over duckduckgo search engine and retrieve the required information.

Install all required packages

%%capture
!pip install duckduckgo-search langchain openai

Import packages

from langchain.tools import Tool, DuckDuckGoSearchRun
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
search = DuckDuckGoSearchRun()

Set LLM. You can use any OpenAI model, or temperature. Add your openai api key.

llm = ChatOpenAI(model= "gpt-3.5-turbo", temperature=0.5, openai_api_key="sk-",)

Set your tool along with its description. Description is used by an agent to determine tool use.

tools = [
    Tool(
        name="Search",
        func=search.run,
        description="useful for when you need to search for most recent information"
    )] #You can add other tools here

# create an agent along with the tools, here we will be using only one tool
agent = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)
# Verbose = True, will show the thinking and steps that the agent is taking.

Define a prompt template/ instruction prompt that will be helpful for an Agent to follow some instructions and get a role.

prompt = """
You are a friendly assistant. 
Use search tool to find answers of the question which are from recent time.
input: {input}
"""

Here's the input query. As you know gpt's training data does not have any recent news so we can utilize the duckduckgo tool to search online to get an answer.

query = "Which LLM is better Llama2 or mistral?"

Run the agent with prompt and query

response = agent.run(prompt.format(input=query))

The following screenshot show how verbose=True will look like.

Here is Agent's response:

According to recent information, the Mistral 7B and Llama2 series have 
been benchmarked and compared. The detailed performance analysis shows
that the Mistral 7B and various Llama models have been evaluated across
a broad spectrum of benchmarks. However, specific details on which LLM 
is better between Llama2 and Mistral are not provided in the search 
results. It is recommended to refer to the detailed benchmarking blogs 
and performance analysis for a more comprehensive comparison.

Thank you for reading 😁.

If you like my work, you can support me here: Support my work

I do welcome constructive criticism and alternative viewpoints. If you have any thoughts or feedback on our analysis, please feel free to share them in the comments section below.

For more such content make sure to subscribe to my Newsletter here

Follow me on

Twitter

GitHub

Linkedin

Did you find this article valuable?

Support Kaushal Powar by becoming a sponsor. Any amount is appreciated!