Skip to main content

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 ag2, all you need to do is upgrade it using:
or
as 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.

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 to NEVER).
  • 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 and chatbot.

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: