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 -}