Machine Learning Spot

LangChain Agent: New Force Connecting LLM and the World

Let’s talk about LangChain Agent today. We’ve explored the capabilities of LangChain Chains, how to craft prompts for chained LLMs, and even built a LangChain RAG to connect our LLM to new information through documents.

But the power of LangChain goes beyond that! It even has the power of LangChain Agent, which can access various APIs to connect your application with the real world. And the best part? They’re not hardcoded; they work on their own!

LangChain Agent provides you with multiple agent types and tools, giving you flexibility to choose the right framework based on your needs.

On completing this article you will learn:

  1. How to use a LangChain agent?
  2. How to Use Fireworks LLM?
  3. On what basis can we choose a langChain agent?
  4. Why are there so many LangChain agents?
  5. How to use a tool in a agent?
  6. How to pull a prompt from LangChain Hub?

Installing Every Required Package

%pip install --upgrade --quiet langchain langchain-openai 
%pip install --upgrade --quiet  duckduckgo-search
%pip install fireworks-ai  --quiet
%pip install langchainhub   --quiet


Importing Every Required Module

from langchain_openai import ChatOpenAI

from langchain.agents import AgentExecutor, create_openai_tools_agent, create_react_agent, create_self_ask_with_search_agent
 

from langchain_community.llms import Fireworks

from langchain_community.tools import DuckDuckGoSearchRun

from langchain import hub

import os

The reason to write everything I have imported together here is just to avoid any error by missing any module or package while using the blog. I will keep mentioning the installation and import again in the blog so that you can know why we did it.

How to Use a LangChain Agent

The general steps to create a LangChain agent are as follows:

  1. Installing and importing the required packages and modules.
  2. Choose right tools
  3. Initialize the right tools
  4. Craft a prompt
  5. Initialize a LLM
  6. Initialize or Create an Agent
  7. Use the Agent

These were the simple 7 steps on how to use an agent, so now let’s look at 3 different examples to see how I created an agent using three different agent types.

Setting up Environment For LLM’s That we will be Using

To set the environment for the below Langchain Agent examples, you need to have API key of Firework and OpenAI.

os.environ["OPENAI_API_KEY"] = "My API" #replace My API with your API

os.environ["FIREWORKS_API_KEY"] = "My API" #replace My API with your API

3 Examples of Creating LangChain Agent

To keep explanation simple and easy to understand for all three examples, I have used same Duck Duck Go search engine as a tool this one does not require any API key you must have already installed it at the beginning of the blog but for explanation purpose I am mentioning here again.
Don’t forget to install and import other modules and packages mentioned in the beginning of the blog like Langchain and chat_openAI and Fireworks and make sure your environment is set.

%pip install --upgrade --quiet  duckduckgo-search

Now import

from langchain_community.tools import DuckDuckGoSearchRun

For prompts, instead of crafting, we will pull others prompts from LangChain hub so installing it.

pip install langchainhub

Now importing common modules used in each examples.

from langchain import hub
from langchain.agents import AgentExecutor

Apart from tool that we have selected, the hub and agent executor are the two packages that will remain common in the examples below. To create each agent, another module according to that agent will be imported.

1. OpenAI Tools Agent

This Agent specializes in multiple tools usage and it is more generalized and supports a wider range of models.

                   # Importing Modules to Create the Agent 

from langchain.agents import create_openai_tools_agent #Module for this agent type.
from langchain_openai import ChatOpenAI

                   # Initializing Our Tool Duck Duck go

tools = [DuckDuckGoSearchRun()] #Any number of tools can be added not limited to on

                   # Pulling an already crafted prompt from langchain hub

prompt = hub.pull("hwchase17/openai-tools-agent")

                   # Initializing an LLM

llm = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0)

                   # Creating An Agent With Our Tool and LLM

agent = create_openai_tools_agent(llm, tools, prompt)

                   # Using The Agent

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

agent_executor.invoke({"input": "who is current president of pakistan ?"})

The Complete Output of Above LangChain Agent with Verbose as True:

> Entering new AgentExecutor chain...

Invoking: `duckduckgo_search` with `{'query': 'current president of Pakistan'}`


ISLAMABAD (AP) — Pakistan's lawmakers elected Asif Ali Zardari on Saturday as the country's president for the second time. He is the widower of assassinated former premier Benazir Bhutto and the father of former foreign minister Bilawal Bhutto-Zardari. Zardari secured 411 votes from national and provincial lawmakers. 9 Mar 2024. Save articles to read later and create your own reading list. Pakistan Peoples Party's co-chairperson Asif Ali Zardari has won a second term as Pakistan's president, supported by ... Asif Ali Zardari (born July 26, 1955, Karachi, Pakistan) is the president of Pakistan (2008-13; 2024- ) and is the first to have completed a full term in office. Since the December 2007 assassination of former prime minister Benazir Bhutto, his wife and the leader of the Pakistan People's Party (PPP), he has been the de facto leader of the PPP. His son, Bilawal Bhutto Zardari, also plays ... Sat 9 Mar 2024 10.13 EST. Last modified on Sat 9 Mar 2024 11.05 EST. Pakistan's lawmakers have elected Asif Ali Zardari as the country's president for the second time. Zardari is the widower ... Asif Ali Zardari (Urdu: آصف علی زرداری; Sindhi: آصف علي زرداري ‎; born 26 July 1955) is a Pakistani politician serving as the 14th president of Pakistan since 10 March 2024. He is the president of Pakistan People's Party Parliamentarians and was the co-chairperson of Pakistan People's Party.. He earlier served as the 11th president of Pakistan from 2008 to 2013, the ...The current President of Pakistan is Asif Ali Zardari, who has been in office since March 10, 2024.

> Finished chain.
{'input': 'who is current president of pakistan ?',
 'output': 'The current President of Pakistan is Asif Ali Zardari, who has been in office since March 10, 2024.'}

If verbose is not set true then only the original output along with input is printed which is only a single line answer. As you can see the LLM we used is not updated but our agent interacted with the real world to bring information of current world scenario.

2. ReAct Agent

This Agent is usually used for simple tasks. The agent’s actions are determined by a series of “ReAct” steps, which are actions, observations, and thoughts.

               # Importing Module to create our LangChain Agent

from langchain.agents import create_react_agent
from langchain_openai import ChatOpenAI
               
                # Initializing Our Tool Duck Duck go

tools = [DuckDuckGoSearchRun()]
                
                # Pulling an Already Crafted Prompt from langchain hub

prompt = hub.pull("hwchase17/react")

                # Initializing an LLM

llm = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0)

               # Creating An Agent With Our Tool and LLM

agent = create_react_agent(llm, tools, prompt)

                # Using The Agent

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

agent_executor.invoke({"input": "who is current president of Pakistanand when was he elected"})

Output:

> Entering new AgentExecutor chain...
I should use the duckduckgo_search tool to find the current president of Pakistan and when he was elected.
Action: duckduckgo_search
Action Input: "current president of Pakistan and date of election"ISLAMABAD (AP) — Pakistan's lawmakers elected Asif Ali Zardari on Saturday as the country's president for the second time. He is the widower of assassinated former premier Benazir Bhutto ........... His grandfather Zulfikar Ali Bhutto and mother Benazir Bhutto ruled the country as prime ministers, and his father Asif Ali Zardari was Pakistan's president from 2008 to 2013. Seats won in 2018: ...I now know the final answer
Final Answer: The current president of Pakistan is Asif Ali Zardari, and he was elected on March 9, 2024.

> Finished chain.
{'input': 'who is current president of Pakistanand when was he elected',
 'output': 'The current president of Pakistan is Asif Ali Zardari, and he was elected on March 9, 2024.'}

I purposefully trimmed the above output but it was as large as previous output only the way of interpreting was different as you can see in the bold letters it follows the pattern of observation, action and thoughts if verbose is not set to true then only the answer would be visible

3. Self Ask With Search

This one works in a unique way whenever a query is given, it asks itself follow-up question, which uses the tool to search and keep questioning itself by breaking down the query until the final answer is found for this i had to use Fireworks’ LLM but will definitely share with you if I try it with another alternative.

So don’t forget to install Fireworks for this one

pip install fireworks-ai #Written just to explain you must have already installed 


             # Importing Module to create our LangChain Agent
from langchain_community.llms import Fireworks
from langchain.agents import create_self_ask_with_search_agent

                # Initializing Our Tool Duck Duck go

tools = [DuckDuckGoSearchRun(name="Intermediate Answer")] #In this agent tool expects a name
                
                # Pulling an Already Crafted Prompt from langchain hub

prompt = hub.pull("hwchase17/self-ask-with-search")

                # Initializing an LLM
llm = Fireworks()
                
                # Creating Agent

agent = create_self_ask_with_search_agent(llm, tools, prompt)

                 # Using The Agent

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

agent_executor.invoke(
    {"input": "which country got independent earlier Pakistan or USA"}
)

Output of this langchain agent:

> Entering new AgentExecutor chain...
Yes. 
Follow up: When did Pakistan gain independence?On 14 August 1947 Pakistan gained independence. India gained independence the following day. The two provinces of British India: Punjab and Bengal were divided along religious lines by the Radcliffe Commission... partition of India, division of British  ... Pakistan emerged as an independent country on 14th August 1947,  ...Follow up: When did the United States gain independence? .... On July 4, 1776, it was adopted unanimously by the 56 delegates to the Second Continental Congress, who had convened at the Pennsylvania State House, later renamed Independence ... The Declaration of Independence is the foundational document of the United States of America. Written primarily by Thomas Jefferson, it explains why the Thirteen Colonies decided to separate from Great Britain during the American Revolution (1765-1789). It was adopted by the Second Continental Congress on 4 July 1776, the anniversary of which ... Declaration of Independence, document approved by the Continental Congress on July 4, 1776, .. fought between 1775 and 1783 through which 13 of Great Britain's North American colonies threw off British rule to establish the sovereign United States of America, founded with the Declaration of Independence in 1776. British attempts to assert greater control over colonial affairs after a long period ... Date: 1765 to 1783: Location: Thirteen Colonies (1765-1775) United Colonies (1775-1781) United States (1781-1783) Outcome: Independence of the United States of America from the  ...So the final answer is: The United States gained independence earlier than Pakistan.

> Finished chain.
{'input': 'which countery got independent earlier pakistan or usa',
 'output': 'The United States gained independence earlier than Pakistan.'}

LangChain Agent: Why So Many Types of LangChain Agents?

There are multiple agent types that LangChain provides, each with their own benefits and functionalities and based on the language model you have selected, you choose the LangChain agent. For example, if a model does not support parallel function calling, then there is no need to use that agent.

Similarly, With the use of right agent, performance can be optimized and decision about the right agent can also be made based on what functionality we want If we don’t want chat history, then there is no need to use a LangChain agent with chat host support. If we have to deal with JSON, then use an agent that specializes in JSON format. If we are dealing with XML, then use a LangChain agent that specializes in XML usage.

Further, you can even create your own custom LangChain agent according to your needs.

Tools Available in LangChain Agents

When it comes to tools, there are various options available to be integrated in any type of langChain agent, from Wikipedia, to google search API, Google Drive, Google Lens, google jobs, Python Repl to LLM-math There is a tool available for everything you name it. You can see entire list here.
But just like LangChain Agent you can even build your own custom tool that can be used in LangChain.

Conclusion:

Congratulations! Now you have learnt how to use a LangChain Agent if you have any feedback feel free to contact. Furthermore, I highly encourage you read my other blogs on LangChain and you can also learn how to craft a prompt template and how to use Wikipedia tool from my previous blogs on LangChain.

Liked the Post?  You can share as well

Facebook
Twitter
LinkedIn

More From Machine Learning Spot

Get The Latest AI News and Insights

Directly To Your Inbox
Subscribe

Signup ML Spot Newsletter

What Will You Get?

Bonus

Get A Free Workshop on
AI Development