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:

pip install ag2[openai,interop-langchain]

If you have been using autogen or pyautogen, all you need to do is upgrade it using:

pip install -U autogen[openai,interop-langchain]

or

pip install -U pyautogen[openai,interop-langchain]

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:

pip install wikipedia

Imports

Now, let’s import the necessary modules and tools.

import os

from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper

from autogen import AssistantAgent, UserProxyAgent
from autogen.interop import Interoperability

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.
config_list = [{"api_type": "openai", "model": "gpt-4o", "api_key": os.environ["OPENAI_API_KEY"]}]
user_proxy = UserProxyAgent(
    name="User",
    human_input_mode="NEVER",
)

chatbot = AssistantAgent(
    name="chatbot",
    llm_config={"config_list": config_list},
)

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.
api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=1000)
langchain_tool = WikipediaQueryRun(api_wrapper=api_wrapper)

interop = Interoperability()
ag2_tool = interop.convert_tool(tool=langchain_tool, type="langchain")

ag2_tool.register_for_execution(user_proxy)
ag2_tool.register_for_llm(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.

message = "Tell me about the history of the United States"
user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)

Output

When the chat is initiated, here’s the output you’ll see:

User (to chatbot):

Tell me about the history of the United States

--------------------------------------------------------------------------------
chatbot (to User):

***** Suggested tool call (call_hhy2G43ymytUFmJlDsK9J0tk): wikipedia *****
Arguments:
{"tool_input":{"query":"history of the United States"}}
**************************************************************************

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING FUNCTION wikipedia...
User (to chatbot):

***** Response from calling tool (call_hhy2G43ymytUFmJlDsK9J0tk) *****
Page: History of the United States
Summary: The history of the lands that became the United States began with the arrival of the first people in the Americas around 15,000 BC. After European colonization of North America began in the late 15th century, wars and epidemics decimated Indigenous societies. By the 1760s, the thirteen British colonies were established. The Southern Colonies built an agricultural system on slave labor, enslaving millions from Africa. After defeating France, the British Parliament imposed a series of taxes; resistance to these taxes, especially the Boston Tea Party in 1773, led to Parliament issuing the Intolerable Acts designed to end self-government.
In 1776, the United States declared its independence. Led by General George Washington, it won the Revolutionary War in 1783. The Constitution was adopted in 1789, and a Bill of Rights was added in 1791 to guarantee inalienable rights. Washington, the first president, and his adviser Alexander Hamilton created a
**********************************************************************

--------------------------------------------------------------------------------
chatbot (to User):

The history of the United States begins with the arrival of the first peoples in the Americas around 15,000 BC. This pre-Columbian era was followed by European colonization, beginning in the late 15th century, which dramatically altered the indigenous societies through wars and epidemics.

By the 1760s, thirteen British colonies were established along the Atlantic seaboard. In the Southern Colonies, an agricultural economy heavily reliant on enslaved labor from Africa was developed. The British victory over France in the Seven Years' War led Parliament to impose various taxes on the colonies. Resistance to these taxes, exemplified by the Boston Tea Party in 1773, prompted the Parliament to enact the Intolerable Acts, seeking to curtail colonial self-governance.

The United States declared independence in 1776. Under the leadership of General George Washington, the American Revolutionary War concluded successfully in 1783. Subsequently, the U.S. Constitution was adopted in 1789, with the Bill of Rights added in 1791 to ensure inalienable rights. During this early period, President George Washington and his advisor Alexander Hamilton played significant roles in forming the young nation's governmental and economic foundations.

This overview covers the early formation and foundational moments of what became the United States, setting the stage for the country's subsequent expansion and development. TERMINATE

--------------------------------------------------------------------------------