Skip to content

Commit

Permalink
Merge pull request #205 from rjambrecic/add-tool-imports-upstream
Browse files Browse the repository at this point in the history
Add tool imports upstream
  • Loading branch information
davorrunje authored Dec 13, 2024
2 parents f112397 + 6e34fbf commit fc631c3
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 4 deletions.
9 changes: 5 additions & 4 deletions autogen/tools/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
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()

__all__ = ["Tool"]


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]

Expand All @@ -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)
151 changes: 151 additions & 0 deletions notebook/tools_from_crewai_tool.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# ! pip install 'crewai[tools]'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"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",
"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": 3,
"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": 4,
"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/tmpv3px4peq dir.\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mchatbot\u001b[0m (to User):\n",
"\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/tmpv3px4peq\",\n",
" \"overwrite\": \"yes\",\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_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",
"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",
"--------------------------------------------------------------------------------\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"
]
},
{
"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
}

0 comments on commit fc631c3

Please sign in to comment.