Cross-Framework LLM Tool Integration with AG2
TL;DR AG2 lets you bring in Tools from different frameworks like LangChain, CrewAI, and PydanticAI.
- LangChain Tools: Useful for tasks like API querying and web scraping.
- CrewAI Tools: Offers a variety of tools for web scraping, search, and more.
- PydanticAI Tools: Adds context-driven tools and structured data processing.
With AG2, you can combine these tools and enhance your agents’ capabilities.
In this post, we’ll walk through how to integrate tools from various frameworks—like LangChain Tools, CrewAI Tools, and PydanticAI Tools—into AG2.
Because, really, the magic happens when you combine them all. This allows you to use tools from different frameworks within AG2, giving your agents more power and flexibility. This blog builds upon the concepts covered in the Tool Integration notebook.
In this post, you will understand how to configure agents, adapt these tools for use in AG2, and validate the integration through practical examples.
LangChain Tools Integration
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:
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:
CrewAI Tools Integration
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: A CrewAI tool for web scraping.
AssistantAgent
andUserProxyAgent
: 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 toNEVER
).AssistantAgent
represents the AI agent, configured with the LLM settings.
Tool Integration
Integrate the CrewAI tool with AG2.
Interoperability
converts the CrewAI tool to a format compatible with AG2.- ScrapeWebsiteTool is used for web scraping tasks.
- Register the tool for both execution and interaction with LLMs.
Initiating the chat
Initiate the conversation between the UserProxyAgent
and the AssistantAgent
to utilize the CrewAI tool.
Output
The chatbot
provides results based on the web scraping operation:
You can also access a detailed summary of the interaction:
PydanticAI Tools Integration
PydanticAI is a newer framework that brings powerful features for working with LLMs. Although it doesn’t yet have a collection of pre-built tools like other frameworks, it offers useful capabilities such as dependency injection. This feature allows you to inject a “Context” into tools, which can help pass parameters or manage state without relying on LLMs. Though it’s still evolving, you can easily integrate PydanticAI tools into AG2 to boost agent capabilities, particularly for tasks that involve structured data and context-driven logic.
Installation
To get PydanticAI tools working with AG2, install the necessary 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
andUserProxyAgent
: 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 toNEVER
).AssistantAgent
represents the AI agent, configured with the LLM settings.
Tool Integration
To integrate a PydanticAI tool into AG2:
- First, define a
Player
model using BaseModel to structure the input data. - Use RunContext to inject dependencies (like the
Player
instance) securely into the tool. - The
get_player
function defines the tool’s functionality, retrieving injected data throughctx.deps
. - Then, convert the tool into an AG2-compatible format with
Interoperability
. - Register the tool for execution and interaction with both the
user_proxy
andchatbot
.
Initiating the chat
Now that everything is set up, you can initiate a chat between the UserProxyAgent
and the AssistantAgent
:
- The
user_proxy
sends a message to thechatbot
. - The user requests player information, and includes “goal keeper” as additional context.
- The
Player
data is securely injected into the tool, and the chatbot can access and use it during the chat.
Output
Summary
In this post, we’ve explored how to integrate tools from multiple frameworks (LangChain, CrewAI, and PydanticAI) into the AG2 framework, enabling cross-framework interoperability. By integrating these tools, you can enhance your agents with a variety of capabilities, such as API querying, web scraping, and structured data processing.
- LangChain offers a wide range of pre-built tools for working with APIs and web scraping, making it easy to extend AG2’s functionality.
- CrewAI brings diverse tools for search, web scraping, and more, allowing for robust agent interactions.
- PydanticAI introduces dependency injection and context-driven logic, enabling efficient data handling without relying on LLMs.
With AG2’s flexible architecture and the power of these frameworks, developers can create agents that are more capable and adaptable. By following the integration steps for each framework, you can enhance your agents’ performance, expand their capabilities, and create more dynamic interactions.
Now you should have a better understanding of how to integrate tools from different frameworks into AG2, and how to use these tools effectively within your own projects.