An In-Depth Exploration of LangChain’s Framework for AI Agents

Devendra Parihar
4 min readNov 11, 2024

--

LangChain has emerged as a powerful framework for building advanced AI applications in the fast-evolving field of artificial intelligence. One of the most intriguing features it offers is the agent framework, which empowers developers to create intelligent systems capable of reasoning, decision-making, and even taking autonomous actions. In this post, we’ll dive deep into LangChain’s agent framework, exploring how it enables developers to craft intelligent agents that can engage with various data sources and APIs to solve real-world problems.

LangChain’s Agent Framework

Overview

LangChain’s Agent Framework is designed to help developers build AI systems that can reason, make decisions, and perform tasks autonomously using a language model (LLM). The main components of an agent include the following:

  1. Language Model (LLM): The brain of the agent, responsible for logic and decision-making.
  2. Tools: Interfaces that allow the agent to interact with the outside world and perform specific tasks.
  3. Agent Executor: The runtime environment that manages the agent’s execution.

LangChain provides a wide range of pre-built tools, including Wikipedia, Calculator, and Search engines, but also enables developers to create custom tools. This flexibility allows developers to build anything from a simple search assistant to complex AI systems interacting with multiple APIs and data sources.

What is an Agent?

An agent is essentially a system powered by an LLM that determines and executes a series of actions based on its reasoning. This system enables agents to assess their actions, take feedback, and decide whether further steps are required to complete a task.

Key Components of an Agent:

  • Language Model: The agent’s core, provides cognitive functions.
  • Tools: Methods the agent uses to connect with the outside world and carry out activities.
  • Agent Executor: The operational environment overseeing the agent’s performance.

Understanding Tools

Tools in LangChain act as interfaces connecting agents, chains, and models with external systems and data sources. When given a list of tools and a prompt, the LLM can select and request specific tools to perform tasks as needed.

Some of the pre-built tools available include:

  • Wikipedia
  • Calculator
  • Search engines (like DuckDuckGo and Google)
  • SQL databases
  • Arxiv

Developers can also create custom tools and link them to LLMs, providing even more adaptability to tackle diverse use cases.

Building Agents with LangChain

The LangChain framework is versatile, allowing developers to construct agents from simple to highly complex. Let’s go through the steps of building a basic agent using the OpenAI Functions API and the Tavily search tool.

Step-by-Step Guide to Building a LangChain Agent

Step 1: Setup and Dependencies

Install the required 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 your OpenAI and Tavily API keys:

import os
os.environ['OPENAI_API_KEY'] = 'your_openai_key'
os.environ['TAVILY_API_KEY'] = 'your_tavily_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

# Create a tool
tools = [TavilySearchResults(max_results=1)]

# Fetch the prompt to use
prompt = hub.pull("hwchase17/openai-functions-agent")

# Choose the language model for the agent
llm = ChatOpenAI(model="gpt-3.5-turbo-1106")

# Create the agent
agent = create_openai_functions_agent(llm, tools, prompt)

Step 5: Use the Agent

You can now instruct your agent to perform tasks:

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

Sample Output:

Entering new AgentExecutor chain… Invoking: tavily_search_results_json with {'query': 'Medium'}

[{‘url’: ‘https://medium.com/', ‘content’: ‘Medium is an online publishing platform where people can share their thoughts, ideas, and stories with a wide audience. It hosts a community of writers and readers across topics such as technology, culture, politics, personal development, and more. Medium offers a range of articles and insights from both emerging and established voices. You can explore content on Medium and even write your blog on their site.’}]

Finished chain.

Response:

“Medium is an online publishing platform where people can share their thoughts, ideas, and stories with a wide audience. It hosts a community of writers and readers across topics such as technology, culture, politics, personal development, and more. Medium offers a range of articles and insights from both emerging and established voices. You can explore content on Medium and even write your blog on their site: Medium

Customizing Your Agent

LangChain’s agent framework offers immense flexibility, allowing developers to create and add custom tools. Below is an example of creating custom tools:

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)
return tavily_tool.invoke(query)

tools = [addition, search_web]

Using a More Advanced LLM

With a more sophisticated LLM, you can bind these tools for complex operations:

chatgpt = ChatOpenAI(model='gpt-4o')
chatgpt_with_tools = chatgpt.bind_tools(tools)

prompt = """Using the tools provided, answer the following:
1. What is 9*8?
2. Can you tell me about Medium?
"""

results = chatgpt_with_tools.invoke(prompt)
results.tool_calls

Adding Tools to the Toolkit

To process and execute tool calls:

toolkit = {'addition': addition, 'search_web': search_web}
for tool_call in results.tool_calls:
selected_tool = toolkit[tool_call['name']]
print(selected_tool.invoke(tool_call['args']))

Explanation of Code

Addition Tool

The addition tool shows how the agent can use specific tools for arithmetic tasks. In this case, the agent identified the need for an addition function and called it with the appropriate arguments.

Search Web Tool

For web-based information retrieval, the agent chose the search_web tool to retrieve information about Medium. This approach exemplifies how agents can access real-time web data to supplement their responses.

Conclusion

LangChain’s Agent Framework is an exciting advancement for AI developers, offering the building blocks to create smart, autonomous systems. By combining LLMs with customized tools and adaptable execution environments, LangChain enables developers to bring sophisticated ideas to life.

With LangChain, the possibilities are boundless, allowing you to create anything from basic AI assistants to complex systems interacting across multiple data sources and APIs. As you explore the world of AI agents, remember that the key to success lies in combining the right tools, prompts, and execution strategies. With LangChain, you have the power to build truly intelligent AI solutions capable of addressing a range of real-world challenges.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Devendra Parihar
Devendra Parihar

No responses yet

Write a response