
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.
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:Imports
Now, let’s import the necessary modules and tools.- WikipediaQueryRun and WikipediaAPIWrapper are the tools for querying Wikipedia.
AssistantAgentandUserProxyAgentare the agents for interaction within AG2.Interoperabilityis what helps connect LangChain tools with AG2.
Agent Configuration
Let’s set up the agents for interaction.config_listis where you define the LLM configuration, like the model and API key.UserProxyAgentsimulates user inputs without requiring actual human interaction (set toNEVER).AssistantAgentrepresents 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.
Interoperabilityhelps convert the LangChain tool to AG2’s format.- Finally, we register the tool for use with both the
user_proxyandchatbot.
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.
AssistantAgentandUserProxyAgent: 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_listdefines the LLM configurations, including the model and API key.UserProxyAgentsimulates user inputs without requiring actual human interaction (set toNEVER).AssistantAgentrepresents the AI agent, configured with the LLM settings.
Tool Integration
Integrate the CrewAI tool with AG2.Interoperabilityconverts 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 theUserProxyAgent and the AssistantAgent to utilize the CrewAI tool.
Output
Thechatbot provides results based on the web scraping operation:
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.
AssistantAgentandUserProxyAgent: 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_listdefines the LLM configurations, including the model and API key.UserProxyAgentsimulates user inputs without requiring actual human interaction (set toNEVER).AssistantAgentrepresents the AI agent, configured with the LLM settings.
Tool Integration
To integrate a PydanticAI tool into AG2:- First, define a
Playermodel using BaseModel to structure the input data. - Use RunContext to inject dependencies (like the
Playerinstance) securely into the tool. - The
get_playerfunction 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_proxyandchatbot.
Initiating the chat
Now that everything is set up, you can initiate a chat between theUserProxyAgent and the AssistantAgent:
- The
user_proxysends a message to thechatbot. - The user requests player information, and includes “goal keeper” as additional context.
- The
Playerdata 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.
