In this tutorial, we demonstrate how to integrate LLM tools from various
frameworks—including LangChain
Tools, CrewAI
Tools, and
PydanticAI Tools into the AG2
framework. This process enables smooth interoperability between these
systems, allowing developers to leverage the unique capabilities of each
toolset within AG2’s flexible agent-based architecture. By the end of
this guide, you will understand how to configure agents, adapt these
tools for use in AG2, and validate the integration through practical
examples.
LangChain is a popular framework that offers a wide range of tools to
work with LLMs. LangChain has already implemented a variety of tools
that can be easily integrated into AG2. You can explore the available
tools in the LangChain Community
Tools
folder. These tools, such as those for querying APIs, web scraping, and
text generation, can be quickly incorporated into AG2, providing
powerful functionality for your agents.
Installation
To integrate LangChain tools into the AG2 framework, install the
required dependencies:
Additionally, this notebook uses LangChain’s Wikipedia
Tool,
which requires the wikipedia
package. Install it with:
Imports
Import necessary modules and tools.
- WikipediaQueryRun
and
WikipediaAPIWrapper:
Tools for querying Wikipedia.
AssistantAgent
and UserProxyAgent
: Agents that facilitate
communication in the AG2 framework.
Interoperability
: This module acts as a bridge, making it easier
to integrate LangChain tools with AG2’s architecture.
Agent Configuration
Configure the agents for the interaction.
config_list
defines the LLM configurations, including 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.
- Initialize and register the LangChain tool with AG2.
- WikipediaAPIWrapper:
Configured to fetch the top 1 result from Wikipedia with a maximum
of 1000 characters per document.
- WikipediaQueryRun:
A LangChain tool that executes Wikipedia queries.
Interoperability
: Converts the LangChain tool into a format
compatible with the AG2 framework.
ag2_tool.register_for_execution(user_proxy)
: Registers the tool
for use by the user_proxy agent.
ag2_tool.register_for_llm(chatbot)
: Registers the tool for
integration with the chatbot agent.
ChatResult(chat_id=None, chat_history=[{'content': 'Tell me about the history of the United States', 'role': 'assistant', 'name': 'User'}, {'tool_calls': [{'id': 'call_0x3F3lbGj9tth5xxuipsZk3Y', 'function': {'arguments': '{"tool_input":{"query":"history of the United States"}}', 'name': 'wikipedia'}, 'type': 'function'}], 'content': None, 'role': 'assistant'}, {'content': 'Page: History of the United States\nSummary: 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.\nIn 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', 'tool_responses': [{'tool_call_id': 'call_0x3F3lbGj9tth5xxuipsZk3Y', 'role': 'tool', 'content': 'Page: History of the United States\nSummary: 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.\nIn 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'}], 'role': 'tool', 'name': 'User'}, {'content': "The history of the United States began with the arrival of the first people in the Americas around 15,000 BC. European colonization commenced in the late 15th century, and this led to significant impacts on Indigenous societies, including wars and epidemics that decimated populations. By the 1760s, thirteen British colonies were firmly established. In the Southern Colonies, an agricultural economy heavily reliant on slave labor developed, involving the enslavement of millions of Africans.\n\nTensions arose in the 18th century between these colonies and Britain, primarily due to taxation without representation, which culminated in events such as the Boston Tea Party in 1773. These events prompted the British Parliament to pass the Intolerable Acts in an attempt to suppress self-governance in the colonies.\n\nIn 1776, the colonies declared their independence, forming the United States of America. Under the leadership of General George Washington, the colonies won the Revolutionary War against Britain, achieving victory in 1783. The United States adopted its Constitution in 1789, followed by the Bill of Rights in 1791, which enshrined fundamental rights.\n\nGeorge Washington became the first President, and along with his adviser Alexander Hamilton, began shaping the new nation's governmental and economic structures. This marked the early stages of the United States' journey to becoming an independent and unified country. TERMINATE", 'role': 'user', 'name': 'chatbot'}], summary="The history of the United States began with the arrival of the first people in the Americas around 15,000 BC. European colonization commenced in the late 15th century, and this led to significant impacts on Indigenous societies, including wars and epidemics that decimated populations. By the 1760s, thirteen British colonies were firmly established. In the Southern Colonies, an agricultural economy heavily reliant on slave labor developed, involving the enslavement of millions of Africans.\n\nTensions arose in the 18th century between these colonies and Britain, primarily due to taxation without representation, which culminated in events such as the Boston Tea Party in 1773. These events prompted the British Parliament to pass the Intolerable Acts in an attempt to suppress self-governance in the colonies.\n\nIn 1776, the colonies declared their independence, forming the United States of America. Under the leadership of General George Washington, the colonies won the Revolutionary War against Britain, achieving victory in 1783. The United States adopted its Constitution in 1789, followed by the Bill of Rights in 1791, which enshrined fundamental rights.\n\nGeorge Washington became the first President, and along with his adviser Alexander Hamilton, began shaping the new nation's governmental and economic structures. This marked the early stages of the United States' journey to becoming an independent and unified country. ", cost={'usage_including_cached_inference': {'total_cost': 0.00639, 'gpt-4o-2024-08-06': {'cost': 0.00639, 'prompt_tokens': 1356, 'completion_tokens': 300, 'total_tokens': 1656}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])
CrewAI provides a variety of powerful tools designed for tasks such as
web scraping, search, code interpretation, and more. These tools are
easy to integrate into the AG2 framework, allowing you to enhance your
agents with advanced capabilities. You can explore the full list of
available tools in the CrewAI
Tools repository.
Installation
Install the required packages for integrating CrewAI tools into the AG2
framework. This ensures all dependencies for both frameworks are
installed.
Imports
Import necessary modules and tools.
- ScrapeWebsiteTool
are the CrewAI tools for web scraping
AssistantAgent
and UserProxyAgent
are core AG2 classes.
Interoperability
: This module acts as a bridge, making it easier
to integrate CrewAI tools with AG2’s architecture.
Agent Configuration
Configure the agents for the interaction.
config_list
defines the LLM configurations, including 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.
Initialize and register the CrewAI tool with AG2.
crewai_tool
is an instance of the
ScrapeWebsiteTool
from CrewAI.
Interoperability
converts the CrewAI tool to make it usable in
AG2.
register_for_execution
and register_for_llm
allow the tool to
work with the UserProxyAgent and AssistantAgent.
PydanticAI is a newer framework that offers
powerful capabilities for working with LLMs. Although it currently does
not have a repository with pre-built tools, it provides features like
dependency injection, allowing you to inject a “Context” into a tool
for better execution without relying on LLMs. This context can be used
for passing parameters or managing state during the execution of a tool.
While the framework is still growing, you can integrate its tools into
AG2 to enhance agent capabilities, especially for tasks that involve
structured data and context-driven logic.
Installation
To integrate LangChain tools into the AG2 framework, install the
required dependencies:
Imports
Import necessary modules and tools.
- BaseModel: Used
to define data structures for tool inputs and outputs.
- RunContext:
Provides context during the execution of tools.
- PydanticAITool:
Represents a tool in the PydanticAI framework.
AssistantAgent
and UserProxyAgent
: Agents that facilitate
communication in the AG2 framework.
Interoperability
: This module acts as a bridge, making it easier
to integrate PydanticAI tools with AG2’s architecture.
Agent Configuration
Configure the agents for the interaction.
config_list
defines the LLM configurations, including 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.
Integrate the PydanticAI tool with AG2.
- Define a
Player
model using
BaseModel to
structure the input data.
- Use
RunContext
to securely inject dependencies (like the
Player
instance) into
the tool function without exposing them to the LLM.
- Implement
get_player
to define the tool’s functionality, accessing
ctx.deps
for injected data.
- Convert the tool to an AG2-compatible format with
Interoperability
and register it for execution and LLM communication.
- Convert the PydanticAI tool into an AG2-compatible format using
convert_tool
.
- Register the tool for both execution and communication with the LLM
by associating it with the
user_proxy
and chatbot
.
Initiate a conversation between the UserProxyAgent
and the
AssistantAgent
.
- Use the
initiate_chat
method to send a message from the
user_proxy
to the chatbot
.
- In this example, the user requests the chatbot to retrieve player
information, providing “goal keeper” as additional context.
- The
Player
instance is securely injected into the tool using
RunContext,
ensuring the chatbot can retrieve and use this data during the
interaction.
ChatResult(chat_id=None, chat_history=[{'content': "Get player, for additional information use 'goal keeper'", 'role': 'assistant', 'name': 'User'}, {'tool_calls': [{'id': 'call_6zAckrEDxxbffyNc4XtX5rIM', 'function': {'arguments': '{"additional_info":"goal keeper"}', 'name': 'get_player'}, 'type': 'function'}], 'content': None, 'role': 'assistant'}, {'content': 'Name: Luka, Age: 25, Additional info: goal keeper', 'tool_responses': [{'tool_call_id': 'call_6zAckrEDxxbffyNc4XtX5rIM', 'role': 'tool', 'content': 'Name: Luka, Age: 25, Additional info: goal keeper'}], 'role': 'tool', 'name': 'User'}, {'content': "The player's name is Luka, aged 25, and he is a goal keeper. TERMINATE", 'role': 'user', 'name': 'chatbot'}, {'content': '', 'role': 'assistant', 'name': 'User'}, {'content': "It looks like there isn't any additional input from your side. If you need further assistance, feel free to ask! TERMINATE", 'role': 'user', 'name': 'chatbot'}], summary="It looks like there isn't any additional input from your side. If you need further assistance, feel free to ask! ", cost={'usage_including_cached_inference': {'total_cost': 0.0048325, 'gpt-4o-2024-08-06': {'cost': 0.0048325, 'prompt_tokens': 1677, 'completion_tokens': 64, 'total_tokens': 1741}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])