Cross-Framework LLM Tool Integration with AG2 - AG2
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.
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 ag2, all you need to do is upgrade it using:
pip install -U autogen[openai,interop-langchain]
or
pip install -U ag2[openai,interop-langchain]
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:
pip install wikipedia
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.
import os
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from autogen import AssistantAgent, UserProxyAgent, LLMConfig
from autogen.interop import Interoperability
Agent Configuration
Let’s set up the agents for interaction.
llm_config = LLMConfig(api_type="openai", model="gpt-4o", api_key=os.environ["OPENAI_API_KEY"])
user_proxy = UserProxyAgent(
name="User",
human_input_mode="NEVER",
)
with llm_config:
chatbot = AssistantAgent(name="chatbot")
Tool Integration
Here’s where we connect everything.
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
--------------------------------------------------------------------------------
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
At the moment CrewAI tool work for python < 3.13, so to use them, please make sure you are using an appropriate version of python.
Install the required packages for integrating CrewAI tools into the AG2 framework. This ensures all dependencies for both frameworks are installed.
pip install ag2[openai,interop-crewai]
Imports
Import necessary modules and tools.
import os
from crewai_tools import ScrapeWebsiteTool
from autogen import AssistantAgent, UserProxyAgent, LLMConfig
from autogen.interop import Interoperability
Agent Configuration
Configure the agents for the interaction.
with llm_config: chatbot = AssistantAgent(name="chatbot")
### Tool Integration
Integrate the CrewAI tool with AG2.
interop = Interoperability() crewai_tool = ScrapeWebsiteTool() ag2_tool = interop.convert_tool(tool=crewai_tool, type="crewai")
ag2_tool.register_for_execution(user_proxy) ag2_tool.register_for_llm(chatbot)
### Initiating the chat
Initiate the conversation between the [`UserProxyAgent`](https://docs.ag2.ai/0.9.8/docs/api-reference/autogen/UserProxyAgent) and the [`AssistantAgent`](https://docs.ag2.ai/0.9.8/docs/api-reference/autogen/AssistantAgent) to utilize the CrewAI tool.
message = "Scrape the website https://ag2.ai/" chat_result = user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)
### Output
The `chatbot` provides results based on the web scraping operation:
User (to chatbot):
Scrape the website https://ag2.ai/
chatbot (to User):
***** Suggested tool call (call_ZStuwmexfN7j56uJKOi6BCid): Read_website_content ***** Arguments: {"args":{"website_url":"https://ag2.ai/"}}
EXECUTING FUNCTION Read_website_content... Using Tool: Read website content User (to chatbot):
***** Response from calling tool (call_ZStuwmexfN7j56uJKOi6BCid) *****
AgentOS Join our growing community of over 20,000 agent builders Join our growing community of over 20,000 agent builders The Open-Source AgentOS Build production-ready multi-agent systems in minutes, not months. Github Discord The End-to-End Platform for Multi-Agent Automation The End-to-End Platform for Multi-Agent Automation Flexible Agent Construction and Orchestration Create specialized agents that work together seamlessly. AG2 makes it easy to define roles, configure behaviors, and orchestrate collaboration - all through simple, intuitive code. → Assistant agents for problem-solving → Executor agents for taking action → Critic agents for validation → Group chat managers for coordination Built-in Conversation Patterns Built-in Conversation Patterns Stop wrestling with agent coordination. AG2 handles message routing, state management, and conversation flow automatically. → Two-agent conversations → Group chats with dynamic speaker selection → Sequential chats with context carryover → Nested conversations for modularity Seamless Human-AI collaboration Seamless Human-AI collaboration Seamlessly integrate human oversight and input into your agent workflows. → Configurable human input modes → Flexible intervention points → Optional human approval workflows → Interactive conversation interfaces → Context-aware human handoff Roadmap AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls Whether you're a solo founder prototyping the next big AI product, or an enterprise team deploying at scale we're building AG2 for you. This is AgentOS - making multi-agent development accessible to everyone. Github Join Our Growing Community Join Our Growing Community → 20,000+ active agent builders → Daily technical discussions → Weekly community calls → Open RFC process → Regular contributor events (Coming soon) Discord Problem Features Roadmap Community Documentation Problem Features Roadmap Community Documentation Problem Features Roadmap Community Documentation
chatbot (to User):
The website "https://ag2.ai/" promotes a platform named AgentOS, which is designed for building multi-agent systems efficiently. Key highlights from the website are:
Community: They have a growing community of over 20,000 agent builders.
End-to-End Platform: AG2 is described as an end-to-end platform for multi-agent automation. It supports flexible agent construction and orchestration, helping to define roles, configure behaviors, and orchestrate collaboration.
Agent Types: It includes assistant agents for problem-solving, executor agents for taking action, critic agents for validation, and group chat managers for coordination.
Built-in Conversation Patterns: AG2 offers capabilities for message routing, state management, and conversation flow management, supporting various conversation types like two-agent conversations, group chats, and nested conversations.
Human-AI Collaboration: The platform facilitates seamless integration of human oversight and input, with options for human intervention and approval workflows.
AG2 Studio: This feature provides visual agent system design, real-time testing, debugging, and one-click deployment, suited for prototyping and MVPs.
AG2 Marketplace: Provides a place to share, monetize agents, discover pre-built solution templates, and connect with other builders.
Scaling Tools: Includes guides for deployment, analytics, cost optimization, team collaboration features, and enterprise-ready security controls.
Community and Documentation: They encourage connecting through GitHub and Discord and have regular community calls and events planned.
This comprehensive platform seems to aim at both individual developers and enterprise teams looking to deploy multi-agent systems effectively and collaboratively.