Home > Technology peripherals > AI > Setting up Custom Tools and Agents in LangChain

Setting up Custom Tools and Agents in LangChain

William Shakespeare
Release: 2025-03-20 10:19:12
Original
925 people have browsed it

This tutorial demonstrates building a versatile conversational AI agent using LangChain, a powerful framework that integrates Large Language Models (LLMs) with external tools and APIs. This agent can perform diverse tasks, from generating random numbers and offering philosophical musings to dynamically retrieving and processing information from webpages. The combination of pre-built and custom tools enables real-time, context-aware, and informative responses.

Key Learning Outcomes

  • Master LangChain's integration with LLMs and external resources.
  • Develop and implement custom tools for specialized functions within your conversational agent.
  • Efficiently fetch and process live web data for accurate responses.
  • Build a conversational agent that retains context for coherent interactions.

*This article is part of the***Data Science Blogathon.

Table of Contents

  • Why Combine LangChain, OpenAI, and DuckDuckGo?
  • Installing Necessary Packages
  • Configuring API Access
  • Connecting LangChain to OpenAI Models
  • Integrating a Web Search Tool
  • Creating Custom Functions
  • Building the Conversational Agent with Custom Tools
  • Utilizing the Tool Class for Web Scraping
  • Conclusion
  • Frequently Asked Questions

Why Combine LangChain, OpenAI, and DuckDuckGo?

The synergy of LangChain, OpenAI, and DuckDuckGo allows for sophisticated conversational AI. OpenAI's LLMs provide natural language processing, while DuckDuckGo offers a privacy-focused search API. This combination enables the AI to generate contextually relevant responses and retrieve real-time data, enhancing its adaptability and accuracy. This powerful toolkit is ideal for creating intelligent chatbots or virtual assistants capable of handling diverse user inquiries.

Installing Necessary Packages

Begin by installing required Python packages using pip:

<code>!pip -q install langchain==0.3.4 openai
pip install langchain
!pip -q install duckduckgo-search</code>
Copy after login

Verify LangChain's installation:

<code>!pip show langchain</code>
Copy after login

Setting up Custom Tools and Agents in LangChain

Configuring API Access

Obtain your OpenAI API key and set it as an environment variable:

<code>import os

os.environ["OPENAI_API_KEY"] = "your_openai_key_here"</code>
Copy after login

Replace "your_openai_key_here" with your actual key. This is crucial for interacting with the GPT-3.5-turbo model.

Connecting LangChain to OpenAI Models

Establish a connection to OpenAI's model using LangChain:

<code>from langchain import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.chains.conversation.memory import ConversationBufferWindowMemory

# Configure the GPT-4o LLM
turbo_llm = ChatOpenAI(
    temperature=0,
    model_name='gpt-4o'
)</code>
Copy after login

A low temperature (temperature=0) ensures consistent responses.

Integrating a Web Search Tool

Enhance your agent's capabilities by adding the DuckDuckGo search tool:

<code>from langchain.tools import DuckDuckGoSearchTool
from langchain.agents import Tool
from langchain.tools import BaseTool

search = DuckDuckGoSearchTool()

# Define the tool
tools = [
    Tool(
        name = "search",
        func=search.run,
        description="Best for questions about current events.  Use precise queries."
    )
]</code>
Copy after login

This tool, described as ideal for current events, is added to the agent's toolkit.

Creating Custom Functions

Extend your agent's functionality with custom tools:

Custom Tool: Meaning of Life

This function provides a playful response to the question of life's meaning:

<code>def meaning_of_life(input=""):
    return 'The meaning of life is 42 (approximately!)'

life_tool = Tool(
    name='Meaning of Life',
    func= meaning_of_life,
    description="Use for questions about the meaning of life. Input: 'MOL'"
)</code>
Copy after login

Custom Tool: Random Number Generator

This tool generates random integers between 0 and 5:

<code>import random

def random_num(input=""):
    return random.randint(0,5)

random_tool = Tool(
    name='Random number',
    func= random_num,
    description="Use to get a random number. Input: 'random'"
)</code>
Copy after login

Building the Conversational Agent with Custom Tools

Creating a conversational agent with custom tools allows for highly tailored interactions.

Agent Initialization

Import initialize_agent and define the tools:

<code>from langchain.agents import initialize_agent

tools = [search, random_tool, life_tool]</code>
Copy after login

Agent Memory

Implement memory using ConversationBufferWindowMemory:

<code>from langchain.chains.conversation.memory import ConversationBufferWindowMemory

memory = ConversationBufferWindowMemory(
    memory_key='chat_history',
    k=3,
    return_messages=True
)</code>
Copy after login

This allows the agent to recall recent conversation turns (up to 3).

Agent Construction

Initialize the agent:

<code>conversational_agent = initialize_agent(
    agent='chat-conversational-react-description',
    tools=tools,
    llm=turbo_llm,
    verbose=True,
    max_iterations=3,
    early_stopping_method='generate',
    memory=memory
)</code>
Copy after login

The parameters specify the agent type, tools, LLM, verbosity, iteration limit, early stopping, and memory.

Agent Testing

Interact with the agent:

<code>conversational_agent("What time is it in London?")
conversational_agent("Can you give me a random number?")
conversational_agent("What is the meaning of life?")</code>
Copy after login

Setting up Custom Tools and Agents in LangChain Setting up Custom Tools and Agents in LangChain Setting up Custom Tools and Agents in LangChain

Customizing the System Prompt

Refine the agent's behavior by adjusting the system prompt:

<code># system prompt
conversational_agent.agent.llm_chain.prompt.messages[0].prompt.template</code>
Copy after login

Setting up Custom Tools and Agents in LangChain

<code>fixed_prompt = '''Assistant is a large language model... [modified prompt instructing the agent to use tools appropriately]'''</code>
Copy after login

Apply the modified prompt:

<code>conversational_agent.agent.llm_chain.prompt.messages[0].prompt.template = fixed_prompt</code>
Copy after login

Retest the agent.

Setting up Custom Tools and Agents in LangChain Setting up Custom Tools and Agents in LangChain

Utilizing the Tool Class for Web Scraping

Create a custom tool to extract plain text from webpages:

<code>from bs4 import BeautifulSoup
import requests
from langchain.agents import Tool

def stripped_webpage(webpage):
    # ... (function to fetch and clean webpage text) ...

web_scraper_tool = Tool(
    name='Web Scraper',
    func=stripped_webpage,
    description="Fetches and cleans webpage text (limited to 4000 characters)."
)</code>
Copy after login

Integrate this tool into your agent.

Creating a WebPageTool Class

A more robust solution involves creating a custom WebPageTool class:

from langchain.tools import BaseTool
from bs4 import BeautifulSoup
import requests

class WebPageTool(BaseTool):
    # ... (class definition as in original response) ...
Copy after login

Reinitialize the agent with the new tool and updated system prompt. Test with examples like:

conversational_agent.run("Is there an article about Clubhouse on https://techcrunch.com/? today")
conversational_agent.run("What are the top stories on www.cbsnews.com/?")
Copy after login

Setting up Custom Tools and Agents in LangChain Setting up Custom Tools and Agents in LangChain Setting up Custom Tools and Agents in LangChain

Conclusion

This tutorial demonstrates building a highly adaptable conversational agent using LangChain. The modular design allows for easy expansion and customization. This agent showcases the power of combining AI with real-time data access.

Key Takeaways

  • LangChain enables modular agent construction.
  • Web scraping and search tools provide up-to-date information.
  • Custom tools tailor the agent to specific needs.
  • Memory features maintain conversational context.

Frequently Asked Questions

(Same FAQs as in the original response, reworded for better flow and conciseness.)

The above is the detailed content of Setting up Custom Tools and Agents in LangChain. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template