Introduction to Agents in LangChain to supercharge LLMs

Agents in LangChain

In the world of AI, large language models have demonstrated remarkable capabilities. However, there are certain tasks that even the simplest computer programs can handle better than these sophisticated models. In order to overcome it, we have Agents in Langchain. This blog will explore the concept of Agents and demonstrate how the LangChain library can utilize them to enhance the capabilities of large language models.

What are Agents in LangChain?

Tasks such as logic, calculations, and search pose challenges for LLMs, where more rudimentary programs excel. While computers have been solving complex calculations for decades, it’s intriguing to witness how a basic calculator program can outperform even the most advanced AI program available to us. Even GPT-4 have limitations in their knowledge cutoff and lack connection to the outside world.

Agents enable large language models to perform tasks beyond their standalone capabilities, unlocking an almost unlimited potential for what they can achieve.

Agents in LangChain serve as enabling tools for large language models, similar to how humans use calculators for math or perform Google searches for information. These agents empower large language models to perform various tasks, including writing and executing Python code, conducting Google searches, and even executing SQL queries.

To utilize agents in LangChain, you need three key components: a large language model or multiple models, an interaction tool, and an agent that controls the interaction. Combining these elements significantly expands the potential of large language models.

LangChain Agent Initialization

Let us try to better understand it by using agents in langchain library itself.


!pip install -qU langchain openai

To start harnessing the power of agents, you need to install and initialize the LangChain library. You can accomplish this by executing the command “pip install langchain” to ensure that the required models, such as OpenAI’s large language models, are available.

Once you initialize the large language model, the next step is for you to create a tool that the agent will utilize. Now let us try to understand it using LLMMath chain


from langchain import OpenAI

llm = OpenAI(
    openai_api_key="OPENAI_API_KEY", 
    temperature=0,
    model_name="text-davinci-003"
)

next we start with LLM math chain

from langchain.chains import LLMMathChain
from langchain.agents import Tool

llm_math = LLMMathChain(llm=llm)

# initialize the math tool
math_tool = Tool(
    name='Calculator',
    func=llm_math.run,
    description='Useful for when you need to answer questions about math.'
)
# when giving tools to LLM, we must pass as list of tools
tools = [math_tool]

If you know about Chains in LangChain, you might be familiar with how LLMBash chain functions. Similarly, LLM Math chain is used to generate responses to mathematical questions. LLM Math chain generates Python code capable of solving specific math problems, and users execute this code in the Python terminal to determine the answer to the question.

On examining the tool, we observe that several parameters are passing. Let’s review each parameter individually.

  • The name parameter is used to identify the tool in the chain run.
  • func: it is basically the functionality to call while running a chain
  • The description parameter, although seemingly evident and straightforward, is passed into the prompt along with the tool. This allows the chain to make a decision on whether or not to utilize this particular tool.

so now let us see this chain in action and understand it further. TO use this tool we need a base model. You can think of it as a developer (in out case a base model) which has several languages with which it can develop various application (in this case the tools it has).


from langchain.agents import initialize_agent

zero_shot_agent = initialize_agent(
	agent="zero-shot-react-description",
	tools=tools,
	llm=llm,
	verbose=True,
	max_iterations=3
)

Now, let’s explore the detailed process of initializing the Zero-Shot-React agent. In this case zero shot means that the agent does not have any memory component attached to it. It answers the current query based solely on the input prompt without utilizing any history. React is an agent execution mechanism where the agent receives a task and attempts to create a task for it. Once the agent creates a task, it actively searches for a suitable tool to generate specific input and output, enabling effective resolution of the smaller task.

The max iteration parameter ensures that the agent interrupts the model chain after three iterations. The agent breaks tasks into logical questions and reasoning, preventing the possibility of infinite chains without this limitation.

let us execute this and see it for ourselves:


zero_shot_agent("what is (3.14 * 2.71)^(1/6)")
     
> Entering new AgentExecutor chain...
 I need to calculate the sixth root of the product of 3.14 and 2.71
Action: Calculator
Action Input: (3.14 * 2.71)^(1/6)
Observation: Answer: 1.4288385419166971
Thought: I now know the final answer
Final Answer: 1.4288385419166971

> Finished chain.
{'input': 'what is (3.14 * 2.71)^(1/6)', 'output': '1.4288385419166971'

Looking at the executed chain, we observe that the agent initially reasons what calculations are required. Based on this reasoning, it selects the calculator tool and generates the corresponding output. Now what if we ask chain a question which it can not answer using calculator tool then?

zero_shot_agent("who is the president of the united states?")
> Entering new AgentExecutor chain...
 I need to find out who the president is
Action: None
Action Input: N/A
Observation: None is not a valid tool, try another one.
Thought:
I need to find out who the president is
Action: Google
Action Input: "who is the president of the united states"
Observation: Google is not a valid tool, try another one.
Thought: I need to find out who the president is
Action: Wikipedia
Action Input: "who is the president of the united states"
Observation: Wikipedia is not a valid tool, try another one.
Thought:

> Finished chain.
{'input': 'who is the president of the united states?',
 'output': 'Agent stopped due to iteration limit or time limit.'

The chain fails to generate output without relevant tools. Let’s create a new tool to solve this problem.

from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

prompt = PromptTemplate(
    input_variables=["query"],
    template="{query}"
)

llm_chain = LLMChain(llm=llm, prompt=prompt)

# initialize the LLM tool
llm_tool = Tool(
    name='Language Model',
    func=llm_chain.run,
    description='use this tool for general purpose queries and logic'
)
     
tools.append(llm_tool)
zero_shot_agent = initialize_agent(
    agent="zero-shot-react-description",
    tools=tools,
    llm=llm,
    verbose=True,
    max_iterations=3
)

now we created a Language model tool which will help us answer the basic question

> Entering new AgentExecutor chain...
LLM could be an acronym or a name
Action: Language Model
Action Input: What is LLM?
Observation: 

LLM stands for "Master of Laws," which is a postgraduate degree in law. It is typically a one-year program that focuses on a specific area of law, such as international law, tax law, or corporate law. LLM programs are often offered by law schools and universities, and they are designed to provide students with a deeper understanding of the legal system and the skills needed to practice law.
Thought: I now know the final answer
Final Answer: LLM stands for "Master of Laws," which is a postgraduate degree in law. It is typically a one-year program that focuses on a specific area of law, such as international law, tax law, or corporate law. LLM programs are often offered by law schools and universities, and they are designed to provide students with a deeper understanding of the legal system and the skills needed to practice law.

> Finished chain.
{'input': 'what is LLM?',
 'output': 'LLM stands for "Master of Laws," which is a postgraduate degree in law. It is typically a one-year program that focuses on a specific area of law, such as international law, tax law, or corporate law. LLM programs are often offered by law schools and universities, and they are designed to provide students with a deeper understanding of the legal system and the skills needed to practice law.'}

This shows us how we can create a variety of tools to help us in creating a powerful language model and solving a variety of our use cases.

Takeaways

Now you know the basics of Agents in LangChain and how to initialize Agents. The main thing is to understand how different components of an Agent work together. While Chains have a defined sequence of tasks, Agents use language model to decide what actions to perform.

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like