LangChain Tools Integration
Introduction
LangChain is a popular framework with lots of tools for working with LLMs. It’s got a range of tools that can be easily integrated into AG2. If you want to see the full list, check out the LangChain Community Tools. You can quickly add things like API queries, web scraping, and text generation to your AG2 setup.
Installation
To get LangChain tools working with AG2, you’ll need to install a couple of dependencies:
If you have been using autogen
or pyautogen
, all you need to do is upgrade it using:
or
as pyautogen
, autogen
, and ag2
are aliases for the same PyPI package.
Also, we’ll use LangChain’s Wikipedia Tool, which needs the wikipedia package. Install it like this:
Imports
Now, let’s import the necessary modules and tools.
- WikipediaQueryRun and WikipediaAPIWrapper are the tools for querying Wikipedia.
AssistantAgent
andUserProxyAgent
are the agents for interaction within AG2.Interoperability
is what helps connect LangChain tools with AG2.
Agent Configuration
Let’s set up the agents for interaction.
config_list
is where you define the LLM configuration, like the model and API key.UserProxyAgent
simulates user inputs without requiring actual human interaction (set toNEVER
).AssistantAgent
represents the AI agent, configured with the LLM settings.
Tool Integration
Here’s where we connect everything.
- First, we set up WikipediaAPIWrapper, which fetches the top Wikipedia result (with a character limit).
- Then, we use WikipediaQueryRun to perform Wikipedia queries.
Interoperability
helps convert the LangChain tool to AG2’s format.- Finally, we register the tool for use with both the
user_proxy
andchatbot
.
Initiating the Chat
Once everything’s set up, we can send a message to the chatbot, and it’ll use the Wikipedia tool to fetch the relevant information.
Output
When the chat is initiated, here’s the output you’ll see: