## 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](https://python.langchain.com/v0.1/docs/modules/tools): Useful for tasks like API querying and web scraping.
- [CrewAI Tools](https://github.com/crewAIInc/crewAI-tools/tree/main): Offers a variety of tools for web scraping, search, and more.
- [PydanticAI Tools](https://ai.pydantic.dev/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](https://python.langchain.com/v0.1/docs/modules/tools), [CrewAI Tools](https://github.com/crewAIInc/crewAI-tools/tree/main), and [PydanticAI Tools](https://ai.pydantic.dev/tools/)—into AG2.

Because, really, the magic happens when you combine them all.

### 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](https://python.langchain.com/docs/integrations/tools/wikipedia/), which needs the wikipedia package. Install it like this:
```
pip install wikipedia
```

### Imports
Now, let’s import the necessary modules and tools.
- [WikipediaQueryRun](https://api.python.langchain.com/en/latest/tools/langchain_community.tools.wikipedia.tool.WikipediaQueryRun.html) and [WikipediaAPIWrapper](https://python.langchain.com/api_reference/community/utilities/langchain_community.utilities.wikipedia.WikipediaAPIWrapper.html) are the tools for querying Wikipedia.
- [`AssistantAgent`](https://docs.ag2.ai/docs/api-reference/autogen/AssistantAgent) and [`UserProxyAgent`](https://docs.ag2.ai/docs/api-reference/autogen/UserProxyAgent) are the agents for interaction within AG2.
- [`Interoperability`](https://docs.ag2.ai/docs/api-reference/autogen/interop/overview) is what helps connect LangChain tools with AG2.
```python
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.
```python
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.  
- First, we set up [WikipediaAPIWrapper](https://python.langchain.com/api_reference/community/utilities/langchain_community.utilities.wikipedia.WikipediaAPIWrapper.html), which fetches the top Wikipedia result (with a character limit).  
- Then, we use [WikipediaQueryRun](https://api.python.langchain.com/en/latest/tools/langchain_community.tools.wikipedia.tool.WikipediaQueryRun.html) to perform Wikipedia queries.  
- [`Interoperability`](https://docs.ag2.ai/docs/reference/interop/interoperability) helps convert the LangChain tool to AG2’s format.  
- Finally, we register the tool for use with both the `user_proxy` and `chatbot`.
```python
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.
```python
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.  
```python
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.
In 1776, the United States declared its independence.
...
```

### CrewAI 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.

### 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.
```
pip install ag2[openai,interop-crewai]
```

### Agent Configuration
Configure the agents for the interaction.
```python
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
Integrate the CrewAI tool with AG2.
```python
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/docs/api-reference/autogen/UserProxyAgent) and the [`AssistantAgent`](https://docs.ag2.ai/docs/api-reference/autogen/AssistantAgent).
```python
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:
```python
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...

User (to chatbot):

***** Response from calling tool (call_ZStuwmexfN7j56uJKOi6BCid) *****

...
```

### PydanticAI Integration
[PydanticAI](https://ai.pydantic.dev/) is a newer framework that brings powerful features for working with LLMs. 
### Installation
To get PydanticAI tools working with AG2, install the necessary dependencies:
```
pip install ag2[openai,interop-pydantic-ai]
```

### Outputs
```python
User (to chatbot):

Get player, for additional information use 'goal keeper'

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

***** Suggested tool call (call_lPXIohFiJfnjmgwDnNFPQCzc): get_player *****
Arguments:
{"additional_info":"goal keeper"}
***************************************************************************

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

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

***** Response from calling tool (call_lPXIohFiJfnjmgwDnNFPQCzc) *****
Name: Luka, Age: 25, Additional info: goal keeper

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

The player's name is Luka, who is a 25-year-old goalkeeper.
```
