AI Agents: A Deep Dive into LangChain’s Agent Framework

Sahitya Arya Last Updated : 22 Oct, 2024
6 min read

LangChain has become a potent toolset for creating complex AI applications in the rapidly developing field of artificial intelligence. One of its most intriguing aspects is the agent architecture, which enables programmers to design intelligent systems that can reason, make decisions, and take independent action. As we look closer into this exciting realm, discover how LangChain agents and tools transform AI development.

LangChain's Agent Framework | language model

Overview

  • LangChain’s Agent Framework allows developers to create intelligent systems that can reason, make decisions, and take autonomous actions based on a language model (LLM).
  • The key components of an Agent include a Language Model (the cognitive center), Tools (for interacting with external systems), and an Agent Executor (the runtime environment).
  • LangChain offers a variety of pre-built tools, such as Wikipedia, Calculator, and Search engines, and also allows developers to create custom tools.
  • The framework supports the creation of simple and complex agents, from basic search assistants to sophisticated AI systems that interact with multiple data sources and APIs.
  • Agents can be customized by creating and integrating custom tools, allowing for flexibility in addressing various real-world problems.

What is an Agent?

An agent is a system that determines and performs a series of actions based on a Large Language Model (LLM). The LLM functions as a reasoning engine by choosing what to do and when to do it. Following these actions, the Agent receives feedback, enabling it to assess whether additional actions are required or the task is finished.

 Key Components of an Agent:

  • Language Model: The Agent’s cognitive center, which handles logic and judgment.
  • Tools: Features an Agent can use to communicate with the outside world and carry out particular activities.
  • Agent Executor: The runtime environment that oversees the functionality of the agent.

Understanding Tools

The interfaces known as tools provide communication between Agents, chains, chat models, and other external systems and data sources. If an LLM is given a list of available tools and a prompt, it can ask to have one or more tools invoked with the proper inputs.

LangChain offers a wide variety of prebuilt tools, including:

  1.  Wikipedia
  2.  Calculator
  3.  Search engines (e.g., DuckDuckGo, Google)
  4.  SQL databases
  5.  Arxiv
  6.  And many more!

Developers can also create custom tools, modify existing ones, and connect them to LLMs as needed.

Also read: A Comprehensive Guide to Building Agentic RAG Systems with LangGraph

Building Agents with LangChain

LangChain provides a flexible framework for creating Agents. Let’s explore how to build a simple Agent using the OpenAI Functions API and the Tavily search tool.

Step 1: Setup and Dependencies

First, install the necessary libraries:

!pip install --upgrade langchain-openai
!pip install --upgrade tavily-python
!pip install langchainhub
!pip install langchain
!pip install langchain-community

Step 2: Configure API Keys

Set up your API keys for OpenAI and Tavily:

import os
os.environ['OPENAI_API_KEY']=OPENAI_KEY
os.environ['TAVILY_API_KEY']=TAVILY_API_KEY

Step 3: Import Required Modules

from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_openai import ChatOpenAI
from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper

Step 4: Create Tools and Agent

#creating tool
tools = [TavilySearchResults(max_results=1)]
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/openai-functions-agent")
# Choose the LLM that will drive the agent
llm = ChatOpenAI(model="gpt-3.5-turbo-1106")
# Construct the OpenAI Functions agent
agent = create_openai_functions_agent(llm, tools, prompt)

Step 5: Use the Agent

Now you can use your Agent to perform tasks:

results=agent_executor.invoke({"input": "What is Analytics Vidhya?"})

Output

> Entering new AgentExecutor chain...
Invoking: `tavily_search_results_json` with `{'query': 'Analytics Vidhya'}`


[{'url': 'https://www.analyticsvidhya.com/', 'content': 'Analytics Vidhya is
the ultimate place for learning and exploring Generative AI, Data Science
and Data Engineering. It offers in-depth blogs, courses, expert sessions,
podcasts, guides and a thriving community.'}]Analytics Vidhya is the
ultimate place for learning and exploring Generative AI, Data Science, and
Data Engineering. It offers in-depth blogs, courses, expert sessions,
podcasts, guides, and a thriving community. You can find more information on
their website: [Analytics Vidhya](https://www.analyticsvidhya.com/)

> Finished chain.

results['output']

Output

Analytics Vidhya is the ultimate place for learning and exploring Generative
AI, Data Science, and Data Engineering. It offers in-depth blogs, courses,
expert sessions, podcasts, guides, and a thriving community. You can find
more information on their website: [Analytics Vidhya]
(https://www.analyticsvidhya.com/)

Customizing Your Agent

One of the most powerful aspects of LangChain’s Agent framework is its flexibility. You can easily create custom tools and integrate them into your Agent. Here’s an example of how to create custom tools:

# tool calling with LLM
from langchain_core.tools import tool
@tool
def addition(x:int,y:int)->int:
 """Addition"""
 return x+y
@tool
def search_web(query: str)->list:
 """Search the web for a query -"""
 tavily_tool=TavilySearchResults(
             max_results=2,
             description='This is tavily tool it queries the tavily search apis and retrun result in json'
                            )
 results=tavily_tool.invoke(query)
 for result in results:
   print(result['content'])
tools=[addition,search_web,]

You can then use these custom tools with a more advanced LLM:

chatgpt=ChatOpenAI(model='gpt-4o')

Binding tools with LLM or chatgpt:

chatgpt_with_tools=chatgpt.bind_tools(tools)
prompt="""Given only the tools at your disposal , mention tool calls for the following tasks:
       1. what is 2+3
       2.Can you tell me about the analytics vidhya
       """
results=chatgpt_with_tools.invoke(prompt)
results

Output

AIMessage(content='', additional_kwargs={'tool_calls': [{'id':
'call_dymFl7lu1gpj4aAwFmKNsvhH', 'function': {'arguments': '{"x": 2, "y":
3}', 'name': 'addition'}, 'type': 'function'}, {'id':
'call_rG3TM01OXRRuFiy6bRpXhxEO', 'function': {'arguments': '{"query":
"Analytics Vidhya"}', 'name': 'search_web'}, 'type': 'function'}]},
response_metadata={'token_usage': {'completion_tokens': 51, 'prompt_tokens':
106, 'total_tokens': 157}, 'model_name': 'gpt-4o-2024-05-13',
'system_fingerprint': 'fp_d33f7b429e', 'finish_reason': 'tool_calls',
'logprobs': None}, id='run-27dafe88-89d6-40d9-bb43-c0cf8450b264-0',
tool_calls=[{'name': 'addition', 'args': {'x': 2, 'y': 3}, 'id':
'call_dymFl7lu1gpj4aAwFmKNsvhH'}, {'name': 'search_web', 'args': {'query':
'Analytics Vidhya'}, 'id': 'call_rG3TM01OXRRuFiy6bRpXhxEO'}],
usage_metadata={'input_tokens': 106, 'output_tokens': 51, 'total_tokens':
157})
results.tool_calls

Output

[{'name': 'addition',
'args': {'x': 2, 'y': 3},
'id': 'call_dymFl7lu1gpj4aAwFmKNsvhH'},
{'name': 'search_web',
'args': {'query': 'Analytics Vidhya'},
'id': 'call_rG3TM01OXRRuFiy6bRpXhxEO'}]

Adding Tools to the Toolkit

#Process and execute tool calls
toolkit={'addition':addition,
        'search_web':search_web}
for tool_call in results.tool_calls:
 print(tool_call)
 selected_tool=toolkit[tool_call['name']]
 print('Tool Description : ',selected_tool)
 print(selected_tool.description)
 print('Calling Tool.. :: Result : ')
 print(selected_tool.invoke(tool_call['args']))
 print()

Output

{'name': 'addition', 'args': {'x': 2, 'y': 3}, 'id':
'call_dymFl7lu1gpj4aAwFmKNsvhH'}
Tool Description : name='addition' description='Addition' args_schema=<class
'pydantic.v1.main.additionSchema'> func=<function addition at
0x7a3596197ac0>
Addition
Calling Tool :: Result :
5
{'name': 'search_web', 'args': {'query': 'Analytics Vidhya'}, 'id':
'call_rG3TM01OXRRuFiy6bRpXhxEO'}
Tool Description : name='search_web' description='Search the web for a
query' args_schema=<class 'pydantic.v1.main.search_webSchema'> func=<function
search_web at 0x7a3594c6d990>
Search the web for a query
Calling Tool :: Result :
Analytics Vidhya is the leading community of Analytics, Data Science and AI
professionals. We are building the next generation of AI professionals. Get
the latest data science, machine learning, and AI courses, news, blogs,
tutorials, and resources.
Learn from industry experts and become an AI and ML Blackbelt with Analytics
Vidhya. Explore free and comprehensive courses, projects, ebooks and
mentoring sessions in data science, NLP, computer vision and more.
None

Explanation of Code

Here we dissect the code:

Addition Tool

The agent’s mental process is demonstrated here. The ‘addition’ tool was needed for the work, as it accurately indicated. After that, the agent summoned the tool and prepared the arguments (x: 2, y: 3). Five was the anticipated outcome. This shows the agent’s comprehension of basic mathematical queries and ability to apply the right tool for the job.

Search_web Tool

The agent saw that web-based information retrieval was necessary for this task. It chose to use the’ search_web’ tool and typed “Analytics Vidhya” as its query. The end product was a thorough overview of Analytics Vidhya’s products and its place in the data science community.

Conclusion

The Agent framework from LangChain opens up a world of possibilities for AI developers. Developers may design intelligent systems that can reason, make decisions, and act independently by combining the power of big language models with specialized tools and adaptable execution environments.

Your ideas may become a reality using LangChain’s Agent framework. This framework offers the building blocks required to develop anything from a basic search assistant to a sophisticated AI system communicating with several data sources and APIs.

Remember that creating the ideal mix of tools, cues, and execution tactics is essential to success as you delve deeper into the world of AI agents. Using LangChain, you can build genuinely clever and flexible AI systems that can solve various real-world problems.

Frequently Asked Questions

Q1. What is the primary function of an Agent in LangChain?

Ans. An Agent in LangChain determines and performs a series of actions based on a language model, choosing what to do and when to do it. It receives feedback to assess whether additional actions are needed or if the task is complete.

Q2. Can developers create custom tools for their Agents?

Ans. Yes, developers can create custom tools, modify existing ones, and connect them to LLMs as needed, allowing for greater flexibility and customization of Agent capabilities.

Q3. How does the Agent interact with external systems or data sources?

Ans. Agents interact with external systems and data sources through Tools, which serve as interfaces between the Agent and these external resources.

Q4. What is the role of the Agent Executor?

Ans. The Agent Executor is the runtime environment that oversees the agent’s functionality, managing the execution of actions and the overall workflow.

Q5. Can LangChain Agents be used with different language models?

Ans. Yes, LangChain supports various language models. The document specifically mentions using OpenAI’s GPT models, but the framework is designed to be flexible with different LLMs.

I'm Sahitya Arya, a seasoned Deep Learning Engineer with one year of hands-on experience in both Deep Learning and Machine Learning. Throughout my career, I've authored more than three research papers and have gained a profound understanding of Deep Learning techniques. Additionally, I possess expertise in Large Language Models (LLMs), contributing to my comprehensive skill set in cutting-edge technologies for artificial intelligence.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details