From 1cbbb8b655131e6adf1a218b73cb69d4d260dde5 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 19 Dec 2024 09:54:13 +0100 Subject: [PATCH 1/5] Add kwargs to convert_tool function --- autogen/interop/crewai/crewai.py | 4 +++- autogen/interop/interoperability.py | 4 ++-- autogen/interop/interoperable.py | 2 +- autogen/interop/langchain/langchain.py | 4 +++- autogen/interop/pydantic_ai/pydantic_ai.py | 2 +- test/interop/test_interoperability.py | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/autogen/interop/crewai/crewai.py b/autogen/interop/crewai/crewai.py index c391b22888..240ec47960 100644 --- a/autogen/interop/crewai/crewai.py +++ b/autogen/interop/crewai/crewai.py @@ -18,9 +18,11 @@ def _sanitize_name(s: str) -> str: class CrewAIInteroperability(Interoperable): - def convert_tool(self, tool: Any) -> Tool: + def convert_tool(self, tool: Any, **kwargs: Any) -> Tool: if not isinstance(tool, CrewAITool): raise ValueError(f"Expected an instance of `crewai.tools.BaseTool`, got {type(tool)}") + if kwargs: + raise ValueError(f"The CrewAIInteroperability does not support any additional arguments, got {kwargs}") # needed for type checking crewai_tool: CrewAITool = tool # type: ignore[no-any-unimported] diff --git a/autogen/interop/interoperability.py b/autogen/interop/interoperability.py index 2c9ed1ffbf..68190fbf04 100644 --- a/autogen/interop/interoperability.py +++ b/autogen/interop/interoperability.py @@ -16,10 +16,10 @@ class Interoperability: def __init__(self) -> None: pass - def convert_tool(self, *, tool: Any, type: str) -> Tool: + def convert_tool(self, *, tool: Any, type: str, **kwargs: Any) -> Tool: interop_cls = self.get_interoperability_class(type) interop = interop_cls() - return interop.convert_tool(tool) + return interop.convert_tool(tool, **kwargs) @classmethod def get_interoperability_class(cls, type: str) -> Type[Interoperable]: diff --git a/autogen/interop/interoperable.py b/autogen/interop/interoperable.py index dfe0f82500..1b9bed1405 100644 --- a/autogen/interop/interoperable.py +++ b/autogen/interop/interoperable.py @@ -11,4 +11,4 @@ @runtime_checkable class Interoperable(Protocol): - def convert_tool(self, tool: Any) -> Tool: ... + def convert_tool(self, tool: Any, **kwargs: Any) -> Tool: ... diff --git a/autogen/interop/langchain/langchain.py b/autogen/interop/langchain/langchain.py index b3f4713c63..1507796c5d 100644 --- a/autogen/interop/langchain/langchain.py +++ b/autogen/interop/langchain/langchain.py @@ -13,9 +13,11 @@ class LangchainInteroperability(Interoperable): - def convert_tool(self, tool: Any) -> Tool: + def convert_tool(self, tool: Any, **kwargs: Any) -> Tool: if not isinstance(tool, LangchainTool): raise ValueError(f"Expected an instance of `langchain_core.tools.BaseTool`, got {type(tool)}") + if kwargs: + raise ValueError(f"The LangchainInteroperability does not support any additional arguments, got {kwargs}") # needed for type checking langchain_tool: LangchainTool = tool # type: ignore[no-any-unimported] diff --git a/autogen/interop/pydantic_ai/pydantic_ai.py b/autogen/interop/pydantic_ai/pydantic_ai.py index 27a146704c..0d4e464579 100644 --- a/autogen/interop/pydantic_ai/pydantic_ai.py +++ b/autogen/interop/pydantic_ai/pydantic_ai.py @@ -54,7 +54,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any: return wrapper - def convert_tool(self, tool: Any, deps: Any = None) -> AG2PydanticAITool: + def convert_tool(self, tool: Any, deps: Any = None, **kwargs: Any) -> AG2PydanticAITool: if not isinstance(tool, PydanticAITool): raise ValueError(f"Expected an instance of `pydantic_ai.tools.Tool`, got {type(tool)}") diff --git a/test/interop/test_interoperability.py b/test/interop/test_interoperability.py index 8dba1c763c..df7789dd13 100644 --- a/test/interop/test_interoperability.py +++ b/test/interop/test_interoperability.py @@ -33,7 +33,7 @@ def test_register_interoperability_class(self) -> None: try: class MyInteroperability: - def convert_tool(self, tool: Any) -> Tool: + def convert_tool(self, tool: Any, **kwargs: Any) -> Tool: return Tool(name="test", description="test description", func=tool) Interoperability.register_interoperability_class("my_interop", MyInteroperability) From 5507bf57ac68a35e71ae284aab6df5c22b359816 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 19 Dec 2024 10:28:21 +0100 Subject: [PATCH 2/5] Raise exception if tool expects context but deps param isn't provided --- autogen/interop/pydantic_ai/pydantic_ai.py | 11 ++++++++++- test/interop/pydantic_ai/test_pydantic_ai.py | 8 ++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/autogen/interop/pydantic_ai/pydantic_ai.py b/autogen/interop/pydantic_ai/pydantic_ai.py index 0d4e464579..fe778ea904 100644 --- a/autogen/interop/pydantic_ai/pydantic_ai.py +++ b/autogen/interop/pydantic_ai/pydantic_ai.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 +import warnings from functools import wraps from inspect import signature from typing import Any, Callable, Optional @@ -61,7 +62,15 @@ def convert_tool(self, tool: Any, deps: Any = None, **kwargs: Any) -> AG2Pydanti # needed for type checking pydantic_ai_tool: PydanticAITool = tool # type: ignore[no-any-unimported] - if deps is not None: + if tool.takes_ctx and deps is None: + raise ValueError("If the tool takes a context, the `deps` argument must be provided") + if not tool.takes_ctx and deps is not None: + warnings.warn( + "The `deps` argument is provided but will be ignored because the tool does not take a context.", + UserWarning, + ) + + if tool.takes_ctx: ctx = RunContext( deps=deps, retry=0, diff --git a/test/interop/pydantic_ai/test_pydantic_ai.py b/test/interop/pydantic_ai/test_pydantic_ai.py index fc7b7b463f..6b5dae9590 100644 --- a/test/interop/pydantic_ai/test_pydantic_ai.py +++ b/test/interop/pydantic_ai/test_pydantic_ai.py @@ -163,9 +163,13 @@ def get_player(ctx: RunContext[Player], additional_info: Optional[str] = None) - return f"Name: {ctx.deps.name}, Age: {ctx.deps.age}, Additional info: {additional_info}" # type: ignore[attr-defined] self.pydantic_ai_interop = PydanticAIInteroperability() - pydantic_ai_tool = PydanticAITool(get_player, takes_ctx=True) + self.pydantic_ai_tool = PydanticAITool(get_player, takes_ctx=True) player = Player(name="Luka", age=25) - self.tool = self.pydantic_ai_interop.convert_tool(tool=pydantic_ai_tool, deps=player) + self.tool = self.pydantic_ai_interop.convert_tool(tool=self.pydantic_ai_tool, deps=player) + + def test_convert_tool_raises_error_if_take_ctx_is_true_and_deps_is_none(self) -> None: + with pytest.raises(ValueError, match="If the tool takes a context, the `deps` argument must be provided"): + self.pydantic_ai_interop.convert_tool(tool=self.pydantic_ai_tool, deps=None) def test_expected_tools(self) -> None: config_list = [{"model": "gpt-4o", "api_key": "abc"}] From c731506a29cef6696ec0fe2e5a212704a2957b78 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 19 Dec 2024 11:45:06 +0100 Subject: [PATCH 3/5] Add tools_interoperability.ipynb --- notebook/tools_crewai_tools_integration.ipynb | 2 +- notebook/tools_interoperability.ipynb | 418 ++++++++++++++++++ 2 files changed, 419 insertions(+), 1 deletion(-) create mode 100644 notebook/tools_interoperability.ipynb diff --git a/notebook/tools_crewai_tools_integration.ipynb b/notebook/tools_crewai_tools_integration.ipynb index 07bdd0e341..553025a43f 100644 --- a/notebook/tools_crewai_tools_integration.ipynb +++ b/notebook/tools_crewai_tools_integration.ipynb @@ -151,7 +151,7 @@ "ag2_tool.register_for_execution(user_proxy)\n", "ag2_tool.register_for_llm(chatbot)\n", "\n", - "message = \"Scape the website https://ag2.ai/\"\n", + "message = \"Scrape the website https://ag2.ai/\"\n", "\n", "chat_result = user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)" ] diff --git a/notebook/tools_interoperability.ipynb b/notebook/tools_interoperability.ipynb new file mode 100644 index 0000000000..00469be0d4 --- /dev/null +++ b/notebook/tools_interoperability.ipynb @@ -0,0 +1,418 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Cross-Framework LLM Tool Integration with AG2\n", + "\n", + "In this tutorial, we demonstrate how to integrate LLM tools from various frameworks—including [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 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." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## LangChain Tools Integration\n", + "\n", + "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](https://github.com/langchain-ai/langchain/tree/master/libs/community/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.\n", + "\n", + "### Installation\n", + "To integrate LangChain tools into the AG2 framework, install the required dependencies:\n", + "\n", + "```bash\n", + "pip install ag2[interop-langchain]\n", + "```\n", + "\n", + "Additionally, this notebook uses LangChain's [Wikipedia Tool](https://api.python.langchain.com/en/latest/tools/langchain_community.tools.wikipedia.tool.WikipediaQueryRun.html), which requires the `wikipedia` package. Install it with:\n", + "\n", + "```bash\n", + "pip install wikipedia\n", + "```\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Imports\n", + "\n", + "Import necessary modules and tools.\n", + "- `WikipediaQueryRun` and `WikipediaAPIWrapper`: Tools for querying Wikipedia.\n", + "- `AssistantAgent` and `UserProxyAgent`: Agents that facilitate communication in the AG2 framework.\n", + "- `Interoperability`: This module acts as a bridge, making it easier to integrate LangChain tools with AG2’s architecture." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "from langchain_community.tools import WikipediaQueryRun\n", + "from langchain_community.utilities import WikipediaAPIWrapper\n", + "\n", + "from autogen import AssistantAgent, UserProxyAgent\n", + "from autogen.interop import Interoperability" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Agent Configuration\n", + "\n", + "Configure the agents for the interaction.\n", + "- `config_list` defines the LLM configurations, including the model and API key.\n", + "- `UserProxyAgent` simulates user inputs without requiring actual human interaction (set to `NEVER`).\n", + "- `AssistantAgent` represents the AI agent, configured with the LLM settings." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "config_list = [{\"model\": \"gpt-4o\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}]\n", + "user_proxy = UserProxyAgent(\n", + " name=\"User\",\n", + " human_input_mode=\"NEVER\",\n", + ")\n", + "\n", + "chatbot = AssistantAgent(\n", + " name=\"chatbot\",\n", + " llm_config={\"config_list\": config_list},\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tool Integration\n", + "\n", + "- Initialize and register the LangChain tool with AG2.\n", + "- `WikipediaAPIWrapper`: Configured to fetch the top 1 result from Wikipedia with a maximum of 1000 characters per document.\n", + "- `WikipediaQueryRun`: A LangChain tool that executes Wikipedia queries.\n", + "- `LangchainInteroperability`: Converts the LangChain tool into a format compatible with the AG2 framework.\n", + "- `ag2_tool.register_for_execution(user_proxy)`: Registers the tool for use by the user_proxy agent.\n", + "- `ag2_tool.register_for_llm(chatbot)`: Registers the tool for integration with the chatbot agent.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=1000)\n", + "langchain_tool = WikipediaQueryRun(api_wrapper=api_wrapper)\n", + "\n", + "interop = Interoperability()\n", + "ag2_tool = interop.convert_tool(tool=langchain_tool, type=\"langchain\")\n", + "\n", + "ag2_tool.register_for_execution(user_proxy)\n", + "ag2_tool.register_for_llm(chatbot)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "message = \"Tell me about the history of the United States\"\n", + "user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## CrewAI Tools Integration\n", + "\n", + "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](https://github.com/crewAIInc/crewAI-tools/tree/main) repository.\n", + "\n", + "### Installation\n", + "Install the required packages for integrating CrewAI tools into the AG2 framework.\n", + "This ensures all dependencies for both frameworks are installed.\n", + "\n", + "```bash\n", + "pip install ag2[interop-crewai]\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Imports\n", + "\n", + "Import necessary modules and tools.\n", + "- `ScrapeWebsiteTool` are the CrewAI tools for web scraping\n", + "- `AssistantAgent` and `UserProxyAgent` are core AG2 classes.\n", + "- `Interoperability`: This module acts as a bridge, making it easier to integrate CrewAI tools with AG2’s architecture." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "from crewai_tools import ScrapeWebsiteTool\n", + "\n", + "from autogen import AssistantAgent, UserProxyAgent\n", + "from autogen.interop import Interoperability" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Agent Configuration\n", + "\n", + "Configure the agents for the interaction.\n", + "- `config_list` defines the LLM configurations, including the model and API key.\n", + "- `UserProxyAgent` simulates user inputs without requiring actual human interaction (set to `NEVER`).\n", + "- `AssistantAgent` represents the AI agent, configured with the LLM settings." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "config_list = [{\"model\": \"gpt-4o\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}]\n", + "user_proxy = UserProxyAgent(\n", + " name=\"User\",\n", + " human_input_mode=\"NEVER\",\n", + ")\n", + "\n", + "chatbot = AssistantAgent(\n", + " name=\"chatbot\",\n", + " llm_config={\"config_list\": config_list},\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tool Integration\n", + "\n", + "Initialize and register the CrewAI tool with AG2.\n", + "- `crewai_tool` is an instance of the `ScrapeWebsiteTool` from CrewAI.\n", + "- `Interoperability` converts the CrewAI tool to make it usable in AG2.\n", + "- `register_for_execution` and `register_for_llm` allow the tool to work with the UserProxyAgent and AssistantAgent." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "interop = Interoperability()\n", + "crewai_tool = ScrapeWebsiteTool()\n", + "ag2_tool = interop.convert_tool(tool=crewai_tool, type=\"crewai\")\n", + "\n", + "ag2_tool.register_for_execution(user_proxy)\n", + "ag2_tool.register_for_llm(chatbot)\n", + "\n", + "message = \"Scrape the website https://ag2.ai/\"\n", + "\n", + "chat_result = user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(chat_result.summary)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## PydanticAI Tools Integration\n", + "\n", + "[PydanticAI](https://ai.pydantic.dev/) 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.\n", + "\n", + "### Installation\n", + "To integrate LangChain tools into the AG2 framework, install the required dependencies:\n", + "\n", + "```bash\n", + "pip install ag2[interop-pydantic-ai]\n", + "```\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Imports\n", + "\n", + "Import necessary modules and tools.\n", + "- `BaseModel`: Used to define data structures for tool inputs and outputs.\n", + "- `RunContext`: Provides context during the execution of tools.\n", + "- `PydanticAITool`: Represents a tool in the PydanticAI framework.\n", + "- `AssistantAgent` and `UserProxyAgent`: Agents that facilitate communication in the AG2 framework.\n", + "- `Interoperability`: This module acts as a bridge, making it easier to integrate PydanticAI tools with AG2’s architecture." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from typing import Optional\n", + "\n", + "from pydantic import BaseModel\n", + "from pydantic_ai import RunContext\n", + "from pydantic_ai.tools import Tool as PydanticAITool\n", + "\n", + "from autogen import AssistantAgent, UserProxyAgent\n", + "from autogen.interop import Interoperability" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Agent Configuration\n", + "\n", + "Configure the agents for the interaction.\n", + "- `config_list` defines the LLM configurations, including the model and API key.\n", + "- `UserProxyAgent` simulates user inputs without requiring actual human interaction (set to `NEVER`).\n", + "- `AssistantAgent` represents the AI agent, configured with the LLM settings." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "config_list = [{\"model\": \"gpt-4o\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}]\n", + "user_proxy = UserProxyAgent(\n", + " name=\"User\",\n", + " human_input_mode=\"NEVER\",\n", + ")\n", + "\n", + "chatbot = AssistantAgent(\n", + " name=\"chatbot\",\n", + " llm_config={\"config_list\": config_list},\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tool Integration\n", + "\n", + "Integrate the PydanticAI tool with AG2.\n", + "\n", + "- Define a `Player` model using `BaseModel` to structure the input data.\n", + "- Use `RunContext` to securely inject dependencies (like the `Player` instance) into the tool function without exposing them to the LLM.\n", + "- Implement `get_player` to define the tool's functionality, accessing `ctx.deps` for injected data.\n", + "- Convert the tool to an AG2-compatible format with `Interoperability` and register it for execution and LLM communication.\n", + "- Convert the PydanticAI tool into an AG2-compatible format using `convert_tool`.\n", + "- Register the tool for both execution and communication with the LLM by associating it with the `user_proxy` and `chatbot`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "class Player(BaseModel):\n", + " name: str\n", + " age: int\n", + "\n", + "\n", + "def get_player(ctx: RunContext[Player], additional_info: Optional[str] = None) -> str: # type: ignore[valid-type]\n", + " \"\"\"Get the player's name.\n", + "\n", + " Args:\n", + " additional_info: Additional information which can be used.\n", + " \"\"\"\n", + " return f\"Name: {ctx.deps.name}, Age: {ctx.deps.age}, Additional info: {additional_info}\" # type: ignore[attr-defined]\n", + "\n", + "\n", + "interop = Interoperability()\n", + "pydantic_ai_tool = PydanticAITool(get_player, takes_ctx=True)\n", + "\n", + "# player will be injected as a dependency\n", + "player = Player(name=\"Luka\", age=25)\n", + "ag2_tool = interop.convert_tool(tool=pydantic_ai_tool, type=\"pydanticai\", deps=player)\n", + "\n", + "ag2_tool.register_for_execution(user_proxy)\n", + "ag2_tool.register_for_llm(chatbot)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Initiate a conversation between the `UserProxyAgent` and the `AssistantAgent`.\n", + "\n", + "- Use the `initiate_chat` method to send a message from the `user_proxy` to the `chatbot`.\n", + "- In this example, the user requests the chatbot to retrieve player information, providing \"goal keeper\" as additional context.\n", + "- The `Player` instance is securely injected into the tool using `RunContext`, ensuring the chatbot can retrieve and use this data during the interaction." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "user_proxy.initiate_chat(\n", + " recipient=chatbot, message=\"Get player, for additional information use 'goal keeper'\", max_turns=3\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.16" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 659ca04752992bc1a56ba2d6570f1296c1fbba90 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 19 Dec 2024 12:19:48 +0100 Subject: [PATCH 4/5] Add docstrings --- autogen/interop/crewai/crewai.py | 25 ++++++++++ autogen/interop/interoperability.py | 55 ++++++++++++++++++++++ autogen/interop/interoperable.py | 22 ++++++++- autogen/interop/langchain/langchain.py | 27 +++++++++++ autogen/interop/pydantic_ai/pydantic_ai.py | 47 ++++++++++++++++++ autogen/tools/pydantic_ai_tool.py | 33 +++++++++++++ autogen/tools/tool.py | 30 ++++++++++++ 7 files changed, 238 insertions(+), 1 deletion(-) diff --git a/autogen/interop/crewai/crewai.py b/autogen/interop/crewai/crewai.py index 240ec47960..97abda9e77 100644 --- a/autogen/interop/crewai/crewai.py +++ b/autogen/interop/crewai/crewai.py @@ -18,7 +18,32 @@ def _sanitize_name(s: str) -> str: class CrewAIInteroperability(Interoperable): + """ + A class implementing the `Interoperable` protocol for converting CrewAI tools + to a general `Tool` format. + + This class takes a `CrewAITool` and converts it into a standard `Tool` object. + """ + def convert_tool(self, tool: Any, **kwargs: Any) -> Tool: + """ + Converts a given CrewAI tool into a general `Tool` format. + + This method ensures that the provided tool is a valid `CrewAITool`, sanitizes + the tool's name, processes its description, and prepares a function to interact + with the tool's arguments. It then returns a standardized `Tool` object. + + Args: + tool (Any): The tool to convert, expected to be an instance of `CrewAITool`. + **kwargs (Any): Additional arguments, which are not supported by this method. + + Returns: + Tool: A standardized `Tool` object converted from the CrewAI tool. + + Raises: + ValueError: If the provided tool is not an instance of `CrewAITool`, or if + any additional arguments are passed. + """ if not isinstance(tool, CrewAITool): raise ValueError(f"Expected an instance of `crewai.tools.BaseTool`, got {type(tool)}") if kwargs: diff --git a/autogen/interop/interoperability.py b/autogen/interop/interoperability.py index 68190fbf04..27df3cd9c7 100644 --- a/autogen/interop/interoperability.py +++ b/autogen/interop/interoperability.py @@ -11,28 +11,83 @@ class Interoperability: + """ + A class to handle interoperability between different tool types. + + This class allows the conversion of tools to various interoperability classes and provides functionality + for retrieving and registering interoperability classes. + """ + _interoperability_classes: Dict[str, Type[Interoperable]] = get_all_interoperability_classes() def __init__(self) -> None: + """ + Initializes an instance of the Interoperability class. + + This constructor does not perform any specific actions as the class is primarily used for its class + methods to manage interoperability classes. + """ pass def convert_tool(self, *, tool: Any, type: str, **kwargs: Any) -> Tool: + """ + Converts a given tool to an instance of a specified interoperability type. + + Args: + tool (Any): The tool object to be converted. + type (str): The type of interoperability to convert the tool to. + **kwargs (Any): Additional arguments to be passed during conversion. + + Returns: + Tool: The converted tool. + + Raises: + ValueError: If the interoperability class for the provided type is not found. + """ interop_cls = self.get_interoperability_class(type) interop = interop_cls() return interop.convert_tool(tool, **kwargs) @classmethod def get_interoperability_class(cls, type: str) -> Type[Interoperable]: + """ + Retrieves the interoperability class corresponding to the specified type. + + Args: + type (str): The type of the interoperability class to retrieve. + + Returns: + Type[Interoperable]: The interoperability class type. + + Raises: + ValueError: If no interoperability class is found for the provided type. + """ if type not in cls._interoperability_classes: raise ValueError(f"Interoperability class {type} not found") return cls._interoperability_classes[type] @classmethod def supported_types(cls) -> List[str]: + """ + Returns a sorted list of all supported interoperability types. + + Returns: + List[str]: A sorted list of strings representing the supported interoperability types. + """ return sorted(cls._interoperability_classes.keys()) @classmethod def register_interoperability_class(cls, name: str, interoperability_class: Type[Interoperable]) -> None: + """ + Registers a new interoperability class with the given name. + + Args: + name (str): The name to associate with the interoperability class. + interoperability_class (Type[Interoperable]): The class implementing the Interoperable protocol. + + Raises: + ValueError: If the provided class does not implement the Interoperable protocol. + """ if not issubclass(interoperability_class, Interoperable): raise ValueError( f"Expected a class implementing `Interoperable` protocol, got {type(interoperability_class)}" diff --git a/autogen/interop/interoperable.py b/autogen/interop/interoperable.py index 1b9bed1405..75aefaaf25 100644 --- a/autogen/interop/interoperable.py +++ b/autogen/interop/interoperable.py @@ -11,4 +11,24 @@ @runtime_checkable class Interoperable(Protocol): - def convert_tool(self, tool: Any, **kwargs: Any) -> Tool: ... + """ + A Protocol defining the interoperability interface for tool conversion. + + This protocol ensures that any class implementing it provides the method + `convert_tool` to convert a given tool into a desired format or type. + """ + + def convert_tool(self, tool: Any, **kwargs: Any) -> Tool: + """ + Converts a given tool to a desired format or type. + + This method should be implemented by any class adhering to the `Interoperable` protocol. + + Args: + tool (Any): The tool object to be converted. + **kwargs (Any): Additional parameters to pass during the conversion process. + + Returns: + Tool: The converted tool in the desired format or type. + """ + ... diff --git a/autogen/interop/langchain/langchain.py b/autogen/interop/langchain/langchain.py index 1507796c5d..925e00431a 100644 --- a/autogen/interop/langchain/langchain.py +++ b/autogen/interop/langchain/langchain.py @@ -13,7 +13,34 @@ class LangchainInteroperability(Interoperable): + """ + A class implementing the `Interoperable` protocol for converting Langchain tools + into a general `Tool` format. + + This class takes a `LangchainTool` and converts it into a standard `Tool` object, + ensuring compatibility between Langchain tools and other systems that expect + the `Tool` format. + """ + def convert_tool(self, tool: Any, **kwargs: Any) -> Tool: + """ + Converts a given Langchain tool into a general `Tool` format. + + This method verifies that the provided tool is a valid `LangchainTool`, + processes the tool's input and description, and returns a standardized + `Tool` object. + + Args: + tool (Any): The tool to convert, expected to be an instance of `LangchainTool`. + **kwargs (Any): Additional arguments, which are not supported by this method. + + Returns: + Tool: A standardized `Tool` object converted from the Langchain tool. + + Raises: + ValueError: If the provided tool is not an instance of `LangchainTool`, or if + any additional arguments are passed. + """ if not isinstance(tool, LangchainTool): raise ValueError(f"Expected an instance of `langchain_core.tools.BaseTool`, got {type(tool)}") if kwargs: diff --git a/autogen/interop/pydantic_ai/pydantic_ai.py b/autogen/interop/pydantic_ai/pydantic_ai.py index fe778ea904..b170ccf501 100644 --- a/autogen/interop/pydantic_ai/pydantic_ai.py +++ b/autogen/interop/pydantic_ai/pydantic_ai.py @@ -18,11 +18,38 @@ class PydanticAIInteroperability(Interoperable): + """ + A class implementing the `Interoperable` protocol for converting Pydantic AI tools + into a general `Tool` format. + + This class takes a `PydanticAITool` and converts it into a standard `Tool` object, + ensuring compatibility between Pydantic AI tools and other systems that expect + the `Tool` format. It also provides a mechanism for injecting context parameters + into the tool's function. + """ + @staticmethod def inject_params( # type: ignore[no-any-unimported] ctx: Optional[RunContext[Any]], tool: PydanticAITool, ) -> Callable[..., Any]: + """ + Wraps the tool's function to inject context parameters and handle retries. + + This method ensures that context parameters are properly passed to the tool + when invoked and that retries are managed according to the tool's settings. + + Args: + ctx (Optional[RunContext[Any]]): The run context, which may include dependencies + and retry information. + tool (PydanticAITool): The Pydantic AI tool whose function is to be wrapped. + + Returns: + Callable[..., Any]: A wrapped function that includes context injection and retry handling. + + Raises: + ValueError: If the tool fails after the maximum number of retries. + """ max_retries = tool.max_retries if tool.max_retries is not None else 1 f = tool.function @@ -56,6 +83,26 @@ def wrapper(*args: Any, **kwargs: Any) -> Any: return wrapper def convert_tool(self, tool: Any, deps: Any = None, **kwargs: Any) -> AG2PydanticAITool: + """ + Converts a given Pydantic AI tool into a general `Tool` format. + + This method verifies that the provided tool is a valid `PydanticAITool`, + handles context dependencies if necessary, and returns a standardized `Tool` object. + + Args: + tool (Any): The tool to convert, expected to be an instance of `PydanticAITool`. + deps (Any, optional): The dependencies to inject into the context, required if + the tool takes a context. Defaults to None. + **kwargs (Any): Additional arguments that are not used in this method. + + Returns: + AG2PydanticAITool: A standardized `Tool` object converted from the Pydantic AI tool. + + Raises: + ValueError: If the provided tool is not an instance of `PydanticAITool`, or if + dependencies are missing for tools that require a context. + UserWarning: If the `deps` argument is provided for a tool that does not take a context. + """ if not isinstance(tool, PydanticAITool): raise ValueError(f"Expected an instance of `pydantic_ai.tools.Tool`, got {type(tool)}") diff --git a/autogen/tools/pydantic_ai_tool.py b/autogen/tools/pydantic_ai_tool.py index 5c999eba3a..a106cd4b70 100644 --- a/autogen/tools/pydantic_ai_tool.py +++ b/autogen/tools/pydantic_ai_tool.py @@ -12,9 +12,33 @@ class PydanticAITool(Tool): + """ + A class representing a Pydantic AI Tool that extends the general Tool functionality + with additional functionality specific to Pydantic AI tools. + + This class inherits from the Tool class and adds functionality for registering + tools with a ConversableAgent, along with providing additional schema information + specific to Pydantic AI tools, such as parameters and function signatures. + + Attributes: + parameters_json_schema (Dict[str, Any]): A schema describing the parameters + that the tool's function expects. + """ + def __init__( self, name: str, description: str, func: Callable[..., Any], parameters_json_schema: Dict[str, Any] ) -> None: + """ + Initializes a PydanticAITool object with the provided name, description, + function, and parameter schema. + + Args: + name (str): The name of the tool. + description (str): A description of what the tool does. + func (Callable[..., Any]): The function that is executed when the tool is called. + parameters_json_schema (Dict[str, Any]): A schema describing the parameters + that the function accepts. + """ super().__init__(name, description, func) self._func_schema = { "type": "function", @@ -26,4 +50,13 @@ def __init__( } def register_for_llm(self, agent: ConversableAgent) -> None: + """ + Registers the tool with the ConversableAgent for use with a language model (LLM). + + This method updates the agent's tool signature to include the function schema, + allowing the agent to invoke the tool correctly during interactions with the LLM. + + Args: + agent (ConversableAgent): The agent with which the tool will be registered. + """ agent.update_tool_signature(self._func_schema, is_remove=False) diff --git a/autogen/tools/tool.py b/autogen/tools/tool.py index b01367235c..c0e615f37c 100644 --- a/autogen/tools/tool.py +++ b/autogen/tools/tool.py @@ -16,6 +16,18 @@ class Tool: + """ + A class representing a Tool that can be used by an agent for various tasks. + + This class encapsulates a tool with a name, description, and an executable function. + The tool can be registered with a ConversableAgent for use either with an LLM or for direct execution. + + Attributes: + name (str): The name of the tool. + description (str): A brief description of the tool's purpose or function. + func (Callable[..., Any]): The function to be executed when the tool is called. + """ + def __init__(self, name: str, description: str, func: Callable[..., Any]) -> None: """Create a new Tool object. @@ -41,7 +53,25 @@ def func(self) -> Callable[..., Any]: return self._func def register_for_llm(self, agent: ConversableAgent) -> None: + """ + Registers the tool for use with a ConversableAgent's language model (LLM). + + This method registers the tool so that it can be invoked by the agent during + interactions with the language model. + + Args: + agent (ConversableAgent): The agent to which the tool will be registered. + """ agent.register_for_llm(name=self._name, description=self._description)(self._func) def register_for_execution(self, agent: ConversableAgent) -> None: + """ + Registers the tool for direct execution by a ConversableAgent. + + This method registers the tool so that it can be executed by the agent, + typically outside of the context of an LLM interaction. + + Args: + agent (ConversableAgent): The agent to which the tool will be registered. + """ agent.register_for_execution(name=self._name)(self._func) From b41328fac28d461c2e853b280f94401997728016 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 19 Dec 2024 12:27:31 +0100 Subject: [PATCH 5/5] Remove notebooks --- notebook/tools_crewai_tools_integration.ipynb | 190 ------------------ .../tools_langchain_tools_integration.ipynb | 168 ---------------- .../tools_pydantic_ai_tools_integration.ipynb | 183 ----------------- 3 files changed, 541 deletions(-) delete mode 100644 notebook/tools_crewai_tools_integration.ipynb delete mode 100644 notebook/tools_langchain_tools_integration.ipynb delete mode 100644 notebook/tools_pydantic_ai_tools_integration.ipynb diff --git a/notebook/tools_crewai_tools_integration.ipynb b/notebook/tools_crewai_tools_integration.ipynb deleted file mode 100644 index 553025a43f..0000000000 --- a/notebook/tools_crewai_tools_integration.ipynb +++ /dev/null @@ -1,190 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Integrating CrewAI Tools with the AG2 Framework\n", - "\n", - "In this tutorial, we demonstrate how to integrate [CrewAI Tools](https://github.com/crewAIInc/crewAI-tools/tree/main) into the AG2 framework. This process enables smooth interoperability between the two systems, allowing developers to leverage CrewAI's powerful tools within AG2's flexible agent-based architecture. By the end of this guide, you will understand how to configure agents, convert CrewAI tools for use in AG2, and validate the integration with a practical example.\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Installation\n", - "Install the required packages for integrating CrewAI tools into the AG2 framework.\n", - "This ensures all dependencies for both frameworks are installed.\n", - "\n", - "```bash\n", - "pip install ag2[interop-crewai]\n", - "```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Imports\n", - "\n", - "Import necessary modules and tools.\n", - "- `os` is used to access environment variables.\n", - "- `Path` helps in handling file paths.\n", - "- `TemporaryDirectory` is used for creating a temporary workspace.\n", - "- `FileWriterTool` and `ScrapeWebsiteTool` are the CrewAI tools we will integrate.\n", - "- `AssistantAgent` and `UserProxyAgent` are core AG2 classes.\n", - "- `CrewAIInteroperability` facilitates the interoperability between AG2 and CrewAI." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "from pathlib import Path\n", - "from tempfile import TemporaryDirectory\n", - "\n", - "from crewai_tools import FileWriterTool, ScrapeWebsiteTool\n", - "\n", - "from autogen import AssistantAgent, UserProxyAgent\n", - "from autogen.interop.crewai import CrewAIInteroperability" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Agent Configuration\n", - "\n", - "Configure the agents for the interaction.\n", - "- `config_list` defines the LLM configurations, including the model and API key.\n", - "- `UserProxyAgent` simulates user inputs without requiring actual human interaction (set to `NEVER`).\n", - "- `AssistantAgent` represents the AI agent, configured with the LLM settings." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "config_list = [{\"model\": \"gpt-4o-mini\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}]\n", - "user_proxy = UserProxyAgent(\n", - " name=\"User\",\n", - " human_input_mode=\"NEVER\",\n", - ")\n", - "\n", - "chatbot = AssistantAgent(\n", - " name=\"chatbot\",\n", - " llm_config={\"config_list\": config_list},\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Tool Integration\n", - "\n", - "Initialize and register the CrewAI tool with AG2.\n", - "- `crewai_tool` is an instance of the `FileWriterTool` from CrewAI.\n", - "- `CrewAIInteroperability` converts the CrewAI tool to make it usable in AG2.\n", - "- `register_for_execution` and `register_for_llm` allow the tool to work with the UserProxyAgent and AssistantAgent." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "crewai_tool = FileWriterTool()\n", - "crewai_interop = CrewAIInteroperability()\n", - "ag2_tool = crewai_interop.convert_tool(crewai_tool)\n", - "\n", - "ag2_tool.register_for_execution(user_proxy)\n", - "ag2_tool.register_for_llm(chatbot)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## File creation\n", - "\n", - "Demonstrate the integration by writing to a file using the converted CrewAI tool.\n", - "- A temporary directory is created to simulate a file operation environment.\n", - "- The `message` instructs the chatbot to use the tool to write a specific string into a file.\n", - "- `user_proxy.initiate_chat` starts the interaction, with the chatbot processing the request and using the tool.\n", - "- Finally, the output file is verified to ensure the integration works correctly." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "with TemporaryDirectory() as tmpdirname:\n", - " filename = \"tool_result.txt\"\n", - " message = f\"\"\"Write 'Easy Migration :)' into {filename}.\n", - "Use {tmpdirname} dir.\n", - "\"\"\"\n", - "\n", - " user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)\n", - "\n", - " assert Path(tmpdirname, filename).read_text() == \"Easy Migration :)\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "crewai_scrape_tool = ScrapeWebsiteTool()\n", - "ag2_tool = crewai_interop.convert_tool(crewai_scrape_tool)\n", - "\n", - "ag2_tool.register_for_execution(user_proxy)\n", - "ag2_tool.register_for_llm(chatbot)\n", - "\n", - "message = \"Scrape the website https://ag2.ai/\"\n", - "\n", - "chat_result = user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(chat_result.summary)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".venv", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.16" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/notebook/tools_langchain_tools_integration.ipynb b/notebook/tools_langchain_tools_integration.ipynb deleted file mode 100644 index 48a1d0986a..0000000000 --- a/notebook/tools_langchain_tools_integration.ipynb +++ /dev/null @@ -1,168 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Integrating LangChain Tools with the AG2 Framework\n", - "\n", - "In this tutorial, we demonstrate how to integrate [LangChain Tools](https://python.langchain.com/v0.1/docs/modules/tools) into the AG2 framework. This process enables smooth interoperability between the two systems, allowing developers to leverage LangChain's powerful tools within AG2's flexible agent-based architecture. By the end of this guide, you will understand how to configure agents, convert LangChain tools for use in AG2, and validate the integration with a practical example.\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Installation\n", - "To integrate LangChain tools into the AG2 framework, install the required dependencies:\n", - "\n", - "```bash\n", - "pip install ag2[interop-langchain]\n", - "```\n", - "\n", - "Additionally, this notebook uses LangChain's [Wikipedia Tool](https://api.python.langchain.com/en/latest/tools/langchain_community.tools.wikipedia.tool.WikipediaQueryRun.html), which requires the `wikipedia` package. Install it with:\n", - "\n", - "```bash\n", - "pip install wikipedia\n", - "```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Imports\n", - "\n", - "Import necessary modules and tools.\n", - "- `os`: For accessing environment variables.\n", - "- `WikipediaQueryRun` and `WikipediaAPIWrapper`: Tools for querying Wikipedia.\n", - "- `AssistantAgent` and `UserProxyAgent`: Agents that facilitate communication in the AG2 framework.\n", - "- `LangchainInteroperability`: A bridge for integrating LangChain tools with the AG2 framework." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "\n", - "from langchain_community.tools import WikipediaQueryRun\n", - "from langchain_community.utilities import WikipediaAPIWrapper\n", - "\n", - "from autogen import AssistantAgent, UserProxyAgent\n", - "from autogen.interop.langchain import LangchainInteroperability" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Agent Configuration\n", - "\n", - "Configure the agents for the interaction.\n", - "- `config_list` defines the LLM configurations, including the model and API key.\n", - "- `UserProxyAgent` simulates user inputs without requiring actual human interaction (set to `NEVER`).\n", - "- `AssistantAgent` represents the AI agent, configured with the LLM settings." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "config_list = [{\"model\": \"gpt-4o\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}]\n", - "user_proxy = UserProxyAgent(\n", - " name=\"User\",\n", - " human_input_mode=\"NEVER\",\n", - ")\n", - "\n", - "chatbot = AssistantAgent(\n", - " name=\"chatbot\",\n", - " llm_config={\"config_list\": config_list},\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Tool Integration\n", - "\n", - "- Initialize and register the LangChain tool with AG2.\n", - "- `WikipediaAPIWrapper`: Configured to fetch the top 1 result from Wikipedia with a maximum of 1000 characters per document.\n", - "- `WikipediaQueryRun`: A LangChain tool that executes Wikipedia queries.\n", - "- `LangchainInteroperability`: Converts the LangChain tool into a format compatible with the AG2 framework.\n", - "- `ag2_tool.register_for_execution(user_proxy)`: Registers the tool for use by the user_proxy agent.\n", - "- `ag2_tool.register_for_llm(chatbot)`: Registers the tool for integration with the chatbot agent.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=1000)\n", - "langchain_tool = WikipediaQueryRun(api_wrapper=api_wrapper)\n", - "\n", - "langchain_interop = LangchainInteroperability()\n", - "ag2_tool = langchain_interop.convert_tool(langchain_tool)\n", - "\n", - "ag2_tool.register_for_execution(user_proxy)\n", - "ag2_tool.register_for_llm(chatbot)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Wikipedia Browsing\n", - "\n", - "- `user_proxy` queries the `chatbot`, which uses a Wikipedia tool to retrieve information.\n", - "- The `chatbot` identifies the query's intent and fetches a summary from Wikipedia.\n", - "- Tool execution returns a concise response from the relevant Wikipedia page." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "message = \"Tell me about the history of the United States\"\n", - "user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".venv", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.16" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/notebook/tools_pydantic_ai_tools_integration.ipynb b/notebook/tools_pydantic_ai_tools_integration.ipynb deleted file mode 100644 index edf2170135..0000000000 --- a/notebook/tools_pydantic_ai_tools_integration.ipynb +++ /dev/null @@ -1,183 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Integrating PydanticAI Tools with the AG2 Framework\n", - "\n", - "In this tutorial, we demonstrate how to integrate [PydanticAI Tools](https://ai.pydantic.dev/tools/) into the AG2 framework. This process enables smooth interoperability between the two systems, allowing developers to leverage PydanticAI's powerful tools within AG2's flexible agent-based architecture. By the end of this guide, you will understand how to configure agents, convert PydanticAI tools for use in AG2, and validate the integration with a practical example.\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Installation\n", - "To integrate LangChain tools into the AG2 framework, install the required dependencies:\n", - "\n", - "```bash\n", - "pip install ag2[interop-pydantic-ai]\n", - "```\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Imports\n", - "\n", - "Import necessary modules and tools.\n", - "- `BaseModel`: Used to define data structures for tool inputs and outputs.\n", - "- `RunContext`: Provides context during the execution of tools.\n", - "- `PydanticAITool`: Represents a tool in the PydanticAI framework.\n", - "- `AssistantAgent` and `UserProxyAgent`: Agents that facilitate communication in the AG2 framework.\n", - "- `PydanticAIInteroperability`: A bridge for integrating PydanticAI tools with the AG2 framework." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "from typing import Optional\n", - "\n", - "from pydantic import BaseModel\n", - "from pydantic_ai import RunContext\n", - "from pydantic_ai.tools import Tool as PydanticAITool\n", - "\n", - "from autogen import AssistantAgent, UserProxyAgent\n", - "from autogen.interop.pydantic_ai import PydanticAIInteroperability" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Agent Configuration\n", - "\n", - "Configure the agents for the interaction.\n", - "- `config_list` defines the LLM configurations, including the model and API key.\n", - "- `UserProxyAgent` simulates user inputs without requiring actual human interaction (set to `NEVER`).\n", - "- `AssistantAgent` represents the AI agent, configured with the LLM settings." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "config_list = [{\"model\": \"gpt-4o\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}]\n", - "user_proxy = UserProxyAgent(\n", - " name=\"User\",\n", - " human_input_mode=\"NEVER\",\n", - ")\n", - "\n", - "chatbot = AssistantAgent(\n", - " name=\"chatbot\",\n", - " llm_config={\"config_list\": config_list},\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Tool Integration\n", - "\n", - "Integrate the PydanticAI tool with AG2.\n", - "\n", - "- Define a `Player` model using `BaseModel` to structure the input data.\n", - "- Use `RunContext` to securely inject dependencies (like the `Player` instance) into the tool function without exposing them to the LLM.\n", - "- Implement `get_player` to define the tool's functionality, accessing `ctx.deps` for injected data.\n", - "- Convert the tool to an AG2-compatible format with `PydanticAIInteroperability` and register it for execution and LLM communication.\n", - "- Convert the PydanticAI tool into an AG2-compatible format using `convert_tool`.\n", - "- Register the tool for both execution and communication with the LLM by associating it with the `user_proxy` and `chatbot`." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "class Player(BaseModel):\n", - " name: str\n", - " age: int\n", - "\n", - "\n", - "def get_player(ctx: RunContext[Player], additional_info: Optional[str] = None) -> str: # type: ignore[valid-type]\n", - " \"\"\"Get the player's name.\n", - "\n", - " Args:\n", - " additional_info: Additional information which can be used.\n", - " \"\"\"\n", - " return f\"Name: {ctx.deps.name}, Age: {ctx.deps.age}, Additional info: {additional_info}\" # type: ignore[attr-defined]\n", - "\n", - "\n", - "pydantic_ai_interop = PydanticAIInteroperability()\n", - "pydantic_ai_tool = PydanticAITool(get_player, takes_ctx=True)\n", - "\n", - "# player will be injected as a dependency\n", - "player = Player(name=\"Luka\", age=25)\n", - "ag2_tool = pydantic_ai_interop.convert_tool(tool=pydantic_ai_tool, deps=player)\n", - "\n", - "ag2_tool.register_for_execution(user_proxy)\n", - "ag2_tool.register_for_llm(chatbot)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Initiate a conversation between the `UserProxyAgent` and the `AssistantAgent`.\n", - "\n", - "- Use the `initiate_chat` method to send a message from the `user_proxy` to the `chatbot`.\n", - "- In this example, the user requests the chatbot to retrieve player information, providing \"goal keeper\" as additional context.\n", - "- The `Player` instance is securely injected into the tool using `RunContext`, ensuring the chatbot can retrieve and use this data during the interaction." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "user_proxy.initiate_chat(\n", - " recipient=chatbot, message=\"Get player, for additional information use 'goal keeper'\", max_turns=3\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".venv", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.16" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}