From 65b545222064e4beef3f37aca96c8c9a90ada68a Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 12 Dec 2024 17:20:34 +0100 Subject: [PATCH 1/2] Initial tools tutorial added --- autogen/tools/tool.py | 7 +- notebook/tools_from_crewai_tool.ipynb | 142 ++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 notebook/tools_from_crewai_tool.ipynb diff --git a/autogen/tools/tool.py b/autogen/tools/tool.py index aa6946c8a7..c23baf362b 100644 --- a/autogen/tools/tool.py +++ b/autogen/tools/tool.py @@ -16,13 +16,14 @@ class Tool: - def __init__(self, name: str, description: str, func: Callable[..., Any]): + def __init__(self, name: str, description: str, func: Callable[..., Any], kwargs: Dict[str, Any]): self._name = name self._description = description self._func = func + self._kwargs = kwargs @classmethod - def from_crewai_tool(self, tool: CrewAITool) -> "Tool": + def from_crewai_tool(cls, tool: CrewAITool) -> "Tool": name = tool.name.replace(" ", "_") description = tool.description.split("Tool Description: ")[-1] @@ -40,4 +41,4 @@ def register_for_llm(self, agent: ConversableAgent) -> None: agent.register_for_llm(name=self._name, description=self._description)(self._func) def register_for_execution(self, agent: ConversableAgent) -> None: - agent.register_for_execution(name=self._name, description=self._description)(self._func) + agent.register_for_execution(name=self._name)(self._func) diff --git a/notebook/tools_from_crewai_tool.ipynb b/notebook/tools_from_crewai_tool.ipynb new file mode 100644 index 0000000000..5de4c5d82f --- /dev/null +++ b/notebook/tools_from_crewai_tool.ipynb @@ -0,0 +1,142 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "# ! pip install 'crewai[tools]'" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import tempfile\n", + "from pathlib import Path\n", + "\n", + "from crewai_tools import FileWriterTool\n", + "\n", + "from autogen import AssistantAgent, UserProxyAgent\n", + "from autogen.tools import Tool" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "config_list = [{\"model\": \"gpt-4\", \"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": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mUser\u001b[0m (to chatbot):\n", + "\n", + "Write 'Easy Migration :)' into tool_result.txt.\n", + "Use /var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpplvoeszo dir.\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33mchatbot\u001b[0m (to User):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_8BpLgDeuKWZJz6W21U0LmNRq): File_Writer_Tool *****\u001b[0m\n", + "Arguments: \n", + "{\n", + " \"args\": {\n", + " \"filename\": \"tool_result.txt\",\n", + " \"directory\": \"/var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpplvoeszo\",\n", + " \"overwrite\": \"True\",\n", + " \"content\": \"Easy Migration :)\"\n", + " }\n", + "}\n", + "\u001b[32m*********************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION File_Writer_Tool...\u001b[0m\n", + "Using Tool: File Writer Tool\n", + "\u001b[33mUser\u001b[0m (to chatbot):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_8BpLgDeuKWZJz6W21U0LmNRq) *****\u001b[0m\n", + "Content successfully written to /var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpplvoeszo/tool_result.txt\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33mchatbot\u001b[0m (to User):\n", + "\n", + "I've successfully written the text 'Easy Migration :)' into the file named 'tool_result.txt' located in the directory '/var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpplvoeszo'. \n", + "\n", + "TERMINATE\n", + "\n", + "--------------------------------------------------------------------------------\n" + ] + } + ], + "source": [ + "with tempfile.TemporaryDirectory() as tmpdirname:\n", + " crewai_tool = FileWriterTool()\n", + " ag2_tool = Tool.from_crewai_tool(crewai_tool)\n", + "\n", + " ag2_tool.register_for_execution(user_proxy)\n", + " ag2_tool.register_for_llm(chatbot)\n", + "\n", + " filename = \"tool_result.txt\"\n", + " message = f\"\"\"Write 'Easy Migration :)' into {filename}.\n", + "Use {tmpdirname} dir.\n", + "\"\"\"\n", + " user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)\n", + "\n", + " assert Path(tmpdirname, filename).read_text() == \"Easy Migration :)\"\n" + ] + } + ], + "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 6e34fbf01a0c5d546d2af1e3907b0fedaceb434c Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 12 Dec 2024 17:30:11 +0100 Subject: [PATCH 2/2] Fix incorrect import from crewai --- autogen/tools/tool.py | 2 +- notebook/tools_from_crewai_tool.ipynb | 47 ++++++++++++++++----------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/autogen/tools/tool.py b/autogen/tools/tool.py index c23baf362b..d223aa0ce4 100644 --- a/autogen/tools/tool.py +++ b/autogen/tools/tool.py @@ -8,7 +8,7 @@ from autogen.agentchat.conversable_agent import ConversableAgent try: - from crewai.tools import Tool as CrewAITool + from crewai.tools import BaseTool as CrewAITool except ImportError: CrewAITool = MagicMock() diff --git a/notebook/tools_from_crewai_tool.ipynb b/notebook/tools_from_crewai_tool.ipynb index 5de4c5d82f..b583a8c0b3 100644 --- a/notebook/tools_from_crewai_tool.ipynb +++ b/notebook/tools_from_crewai_tool.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 10, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -11,9 +11,17 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-12-12 17:28:45,612 - 8269089792 - __init__.py-__init__:18 - WARNING: flaml.automl is not available. Please install flaml[automl] to enable AutoML functionalities.\n" + ] + } + ], "source": [ "import os\n", "import tempfile\n", @@ -27,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -45,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -55,25 +63,19 @@ "\u001b[33mUser\u001b[0m (to chatbot):\n", "\n", "Write 'Easy Migration :)' into tool_result.txt.\n", - "Use /var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpplvoeszo dir.\n", - "\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ + "Use /var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpv3px4peq dir.\n", + "\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mchatbot\u001b[0m (to User):\n", "\n", - "\u001b[32m***** Suggested tool call (call_8BpLgDeuKWZJz6W21U0LmNRq): File_Writer_Tool *****\u001b[0m\n", + "\u001b[32m***** Suggested tool call (call_or8EnSzoRxayagU27crpydfF): File_Writer_Tool *****\u001b[0m\n", "Arguments: \n", "{\n", " \"args\": {\n", " \"filename\": \"tool_result.txt\",\n", - " \"directory\": \"/var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpplvoeszo\",\n", - " \"overwrite\": \"True\",\n", + " \"directory\": \"/var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpv3px4peq\",\n", + " \"overwrite\": \"yes\",\n", " \"content\": \"Easy Migration :)\"\n", " }\n", "}\n", @@ -85,14 +87,14 @@ "Using Tool: File Writer Tool\n", "\u001b[33mUser\u001b[0m (to chatbot):\n", "\n", - "\u001b[32m***** Response from calling tool (call_8BpLgDeuKWZJz6W21U0LmNRq) *****\u001b[0m\n", - "Content successfully written to /var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpplvoeszo/tool_result.txt\n", + "\u001b[32m***** Response from calling tool (call_or8EnSzoRxayagU27crpydfF) *****\u001b[0m\n", + "Content successfully written to /var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpv3px4peq/tool_result.txt\n", "\u001b[32m**********************************************************************\u001b[0m\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mchatbot\u001b[0m (to User):\n", "\n", - "I've successfully written the text 'Easy Migration :)' into the file named 'tool_result.txt' located in the directory '/var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpplvoeszo'. \n", + "The content 'Easy Migration :)' has been successfully written to the file /var/folders/79/kmq3y0pj0rbgrrnf4xlq8q0c0000gn/T/tmpv3px4peq/tool_result.txt.\n", "\n", "TERMINATE\n", "\n", @@ -116,6 +118,13 @@ "\n", " assert Path(tmpdirname, filename).read_text() == \"Easy Migration :)\"\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {