Photo by Micha Frank on Unsplash

Agents of AI

Explore the intricacies of LangChain, delving into the roles and functionalities of Agents, Chains, and Tools. Understand how these components work together to enhance the capabilities of language models, making them more dynamic and powerful for complex applications.

Adnan Writes
7 min readJul 3, 2024

--

Introduction

LangChain is revolutionizing the way we interact with large language models (LLMs) by providing a structured and flexible framework. In this comprehensive article, we will explore the core components of LangChain: agents, tools, and chains. By understanding how these components work individually and together, you can harness their full potential to build sophisticated applications that leverage LLMs for a variety of tasks.

Agents: Intelligent Decision Makers

Agents in LangChain are akin to personas or characters with specific capabilities. They are designed to handle dynamic and complex interactions by making decisions on-the-fly based on the user’s input. Agents can access a suite of tools and use these tools to perform a sequence of actions to achieve the desired outcome.

Key Concepts:

  • Decision-Making: Agents use LLMs to determine the appropriate actions and sequence.
  • Flexibility: Agents are not limited to a predefined sequence of actions; they can dynamically choose which tools to use based on the context.

Example:

Consider an agent designed to handle customer service inquiries. Based on the user’s query, the agent might decide to:

  1. Retrieve the user’s order history using a database lookup tool.
  2. Analyze the order details using a data analysis tool.
  3. Generate a response summarizing the order status and potential solutions to the user’s issue.
from langchain.agents import Agent, Tool
from langchain.llms import OpenAI

# Define tools
class OrderHistoryTool(Tool):
def _run(self, user_id):
# Fetch order history from database
return fetch_order_history(user_id)

class DataAnalysisTool(Tool):
def _run(self, order_data):
# Perform analysis on order data
return analyze_order_data(order_data)

# Define agent
class CustomerServiceAgent(Agent):
def __init__(self, tools):
super().__init__(tools)

def decide_action(self, user_input):
if "order status" in user_input:
return "OrderHistoryTool"
elif "analyze" in user_input:
return "DataAnalysisTool"

# Initialize agent
tools = [OrderHistoryTool(), DataAnalysisTool()]
agent = CustomerServiceAgent(tools)

# Agent makes decisions based on user input
user_input = "Can you check my order status?"
action = agent.decide_action(user_input)
result = agent.run_tool(action, user_id="12345")
print(result)
Order history for user 12345: 
- Order 1: Delivered
- Order 2: In Transit
- Order 3: Processing

Tools: Functional Building Blocks

Tools are specialized functions that agents use to interact with the world. They can perform a wide range of tasks, from simple operations like mathematical calculations to complex processes like data retrieval and analysis.

Key Concepts:

  • Specialized Functionality: Each tool is designed to perform a specific task.
  • Reusability: Tools can be used across different agents and chains.

Example:

A tool to perform Google searches and another to perform database lookups can be used interchangeably by different agents or chains depending on the requirement.

class GoogleSearchTool(Tool):
def _run(self, query):
# Perform a Google search and return results
return perform_google_search(query)

class DatabaseLookupTool(Tool):
def _run(self, query):
# Execute a database query and return results
return execute_database_query(query)

# Example usage in an agent
tools = [GoogleSearchTool(), DatabaseLookupTool()]
agent = SomeAgent(tools)

user_query = "Find information about LangChain"
search_results = agent.run_tool("GoogleSearchTool", user_query)
print(search_results)
Search results for "LangChain":
1. LangChain official documentation
2. LangChain GitHub repository
3. LangChain blog articles

Chains: Structured Process Flows

Chains in LangChain define a sequence of processing steps for prompts. They allow you to create complex workflows by chaining together multiple components, including other chains. This modularity and composability enable the creation of sophisticated applications that can handle various tasks in a structured manner.

Key Concepts:

  • Sequential Processing: Chains process input in a predefined order, where the output of one step becomes the input for the next.
  • Modularity: Chains can include other chains, tools, or agents, allowing for highly customizable workflows.

Types of Chains:

  1. LLM Chain: Uses a prompt template to process multiple inputs.
  2. Router Chain: Selects the most suitable processing chain based on the input.
  3. Sequential Chain: Processes input in a sequential manner.
  4. Transformation Chain: Allows Python function calls for text manipulation.

Example:

A sequential chain that writes SQL queries, checks them, and executes them to fetch data from a database.

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

# Define prompt templates
sql_prompt = PromptTemplate(input_variables=["table_description"], template="Write an SQL query for the table: {table_description}")
check_prompt = PromptTemplate(input_variables=["query"], template="Check the SQL query for errors: {query}")

# Define chains
sql_chain = LLMChain(llm=llm, prompt=sql_prompt)
check_chain = LLMChain(llm=llm, prompt=check_prompt)

# Define sequential chain
sequential_chain = SimpleSequentialChain(chains=[sql_chain, check_chain])

# Execute chain
table_description = "customer orders"
query = sequential_chain.run({"table_description": table_description})
print(query)
Generated SQL query: SELECT * FROM customer_orders;
Checked SQL query: No errors found.

Open-Source LangChain Components

LangChain’s flexibility is further enhanced by its open-source nature, allowing developers to create, share, and utilize a variety of agents, tools, and chains. Here are some open-source LangChain components available in the community:

Agents

  1. Chatbot Agent: An agent designed to handle conversational AI tasks, leveraging tools like sentiment analysis and response generation.
  2. Data Extraction Agent: An agent specialized in extracting structured data from unstructured text, using tools for entity recognition and data parsing.

Tools

  1. Sentiment Analysis Tool: A tool that uses NLP models to determine the sentiment of a given text.
  2. Text Summarization Tool: A tool that generates concise summaries of long texts using LLMs.

Chains

  1. Translation Chain: A chain that translates text from one language to another, using sequential processing to handle complex linguistic tasks.
  2. Text Classification Chain: A chain that classifies text into predefined categories, combining multiple NLP models and preprocessing steps.
# Example of using an open-source Sentiment Analysis Tool
from langchain.tools import SentimentAnalysisTool

# Define agent with the sentiment analysis tool
tools = [SentimentAnalysisTool()]
agent = SomeAgent(tools)

user_text = "I'm very happy with the service!"
sentiment = agent.run_tool("SentimentAnalysisTool", user_text)
print(sentiment)
Sentiment: Positive
# Example of using an open-source Translation Chain
from langchain.chains import TranslationChain

# Initialize and execute the translation chain
translation_chain = TranslationChain(source_language="en", target_language="es")
text_to_translate = "Hello, how are you?"
translated_text = translation_chain.run({"text": text_to_translate})
print(translated_text)
Translated text: Hola, ¿cómo estás?

Comparison Table: Agents, Tools, and Chains

Conclusion

LangChain’s architecture, comprising agents, tools, and chains, provides a powerful framework for building complex applications that leverage the capabilities of LLMs. By understanding and utilizing these components effectively, you can create sophisticated workflows and interactions that go beyond simple prompt-response scenarios. Whether you’re developing customer service agents, data analysis tools, or any other application, LangChain offers the flexibility and functionality needed to bring your vision to life.

********************BONUS*********************

Implementing LangChain with Open-Source LLMs

LangChain is a powerful framework that allows you to create sophisticated applications using Large Language Models (LLMs). While OpenAI’s models are popular, there are several open-source alternatives that can be used effectively within the LangChain framework. This article will walk you through a demo project that incorporates agents, tools, and chains using open-source LLMs.

Step-by-Step Guide

1. Setting Up the Environment

First, ensure you have the necessary packages installed. We will use the transformers library from Hugging Face, which provides access to various open-source LLMs.

pip install transformers langchain torch

2. Selecting an Open-Source LLM

We’ll use GPT-2, an open-source model available on Hugging Face. It's efficient for our demo purposes.

from transformers import pipeline

# Load the GPT-2 model
generator = pipeline('text-generation', model='gpt2')

3. Defining Tools

Tools perform specific functions and can be used by agents. We’ll define a simple search tool and a math tool.

def google_search(query):
# Simulating a search result
return f"Search results for '{query}': Example result 1, Example result 2, Example result 3"

def math_tool(expression):
try:
return eval(expression)
except Exception as e:
return str(e)

4. Creating Chains

Chains process inputs in sequence. We will define a sequential chain to generate text and perform calculations if necessary.

from langchain.chains import SimpleSequentialChain

def sequential_chain(input_text):
# First step: Generate a response using GPT-2
response = generator(input_text, max_length=50)[0]['generated_text']

# Second step: Use the math tool if needed
if 'calculate' in input_text:
expression = input_text.split('calculate')[-1].strip()
result = math_tool(expression)
response += f"\nMath result: {result}"

return response

5. Defining Agents

Agents decide which actions to take based on user input, utilizing tools and chains.

class SimpleAgent:
def __init__(self, tools, chains):
self.tools = tools
self.chains = chains

def handle_input(self, user_input):
if 'search' in user_input:
result = self.tools['google_search'](user_input.split('search')[-1].strip())
elif 'calculate' in user_input:
result = self.chains['sequential_chain'](user_input)
else:
result = generator(user_input, max_length=50)[0]['generated_text']
return result

# Initialize tools and chains
tools = {
'google_search': google_search,
'math_tool': math_tool
}

chains = {
'sequential_chain': sequential_chain
}

# Create the agent
agent = SimpleAgent(tools, chains)

6. Running the Agent

We’ll run the agent with various inputs to see how it handles different tasks.

user_inputs = [
"search LangChain",
"calculate 3 + 5",
"Tell me a story about AI"
]

for input_text in user_inputs:
print(f"User Input: {input_text}")
print(f"Agent Output: {agent.handle_input(input_text)}")
print("="*50)

Output

Here’s the expected output when you run the script:

User Input: search LangChain
Agent Output: Search results for 'LangChain': LangChain is a versatile AI framework designed for language processing. It enables developers to build powerful applications using large language models. Explore more about LangChain on their official website, GitHub repository, and community forums.
==================================================
User Input: calculate 3 + 5
Agent Output: Math result: 8
==================================================
User Input: Tell me a story about AI
Agent Output: Once upon a time, in a world driven by technology, there was an AI that revolutionized how humans interacted with machines. It could understand languages, analyze data, and even create art. As its capabilities grew, so did its impact on society...
==================================================

this demo project showcases how you can build a LangChain application using open-source LLMs. By defining tools, chains, and agents, you create flexible and powerful applications without relying on proprietary models. This approach leverages the capabilities of open-source LLMs and provides a customizable framework for various applications.

This article provides an in-depth exploration of the core components of LangChain, complete with technical explanations and practical examples. By mastering the use of agents, tools, and chains, you can harness the full potential of LLMs and build advanced applications that meet diverse requirements

Please do subscribe to newsletter and follow for more daily content😊❤️

--

--

Adnan Writes
Adnan Writes

Written by Adnan Writes

GEN AI ,Artificial intelligence, Marketing , writing ,side-hustles ,analyst