{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tool Use\n", "\n", "The `AgentChat` api provides a `ToolUseAssistantAgent` with presets for adding tools that the agent can call as part of it's response. \n", "\n", ":::{note}\n", "\n", "The example presented here is a work in progress 🚧. Also, tool uses here assumed the `model_client` used by the agent supports tool calling. \n", "::: " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from autogen_agentchat.agents import ToolUseAssistantAgent\n", "from autogen_agentchat.teams import RoundRobinGroupChat, StopMessageTermination\n", "from autogen_core.components.models import OpenAIChatCompletionClient\n", "from autogen_core.components.tools import FunctionTool" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In AgentChat, a Tool is a function wrapped in the `FunctionTool` class exported from `autogen_core.components.tools`. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "async def get_weather(city: str) -> str:\n", " return f\"The weather in {city} is 72 degrees and Sunny.\"\n", "\n", "\n", "get_weather_tool = FunctionTool(get_weather, description=\"Get the weather for a city\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, agents that use tools are defined in the following manner. \n", "\n", "- An agent is instantiated based on the `ToolUseAssistantAgent` class in AgentChat. The agent is aware of the tools it can use by passing a `tools_schema` attribute to the class, which is passed to the `model_client` when the agent generates a response.\n", "- An agent Team is defined that takes a list of `tools`. Effectively, the `ToolUseAssistantAgent` can generate messages that call tools, and the team is responsible executing those tool calls and returning the results." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "--------------------------------------------------------------------------- \n", "\u001b[91m[2024-10-08T20:34:31.935149]:\u001b[0m\n", "\n", "What's the weather in New York?" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "--------------------------------------------------------------------------- \n", "\u001b[91m[2024-10-08T20:34:33.080494], Weather_Assistant:\u001b[0m\n", "\n", "The weather in New York is 72 degrees and sunny. \n", "\n", "TERMINATE" ] } ], "source": [ "assistant = ToolUseAssistantAgent(\n", " \"Weather_Assistant\",\n", " model_client=OpenAIChatCompletionClient(model=\"gpt-4o-mini\"),\n", " registered_tools=[get_weather_tool],\n", ")\n", "team = RoundRobinGroupChat([assistant])\n", "result = await team.run(\"What's the weather in New York?\", termination_condition=StopMessageTermination())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using Langchain Tools \n", "\n", "AutoGen also provides direct support for tools from LangChain via the `autogen_ext` package.\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# pip install langchain, langchain-community, wikipedia , autogen-ext\n", "\n", "import wikipedia\n", "from autogen_ext.tools.langchain import LangChainToolAdapter\n", "from langchain.tools import WikipediaQueryRun\n", "from langchain_community.utilities import WikipediaAPIWrapper\n", "\n", "api_wrapper = WikipediaAPIWrapper(wiki_client=wikipedia, top_k_results=1, doc_content_chars_max=100)\n", "tool = WikipediaQueryRun(api_wrapper=api_wrapper)\n", "\n", "langchain_wikipedia_tool = LangChainToolAdapter(tool)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "--------------------------------------------------------------------------- \n", "\u001b[91m[2024-10-08T20:44:08.218758]:\u001b[0m\n", "\n", "Who was the first president of the United States?\n", "--------------------------------------------------------------------------- \n", "\u001b[91m[2024-10-08T20:44:11.240067], WikiPedia_Assistant:\u001b[0m\n", "\n", "The first president of the United States was George Washington, who served from April 30, 1789, to March 4, 1797. \n", "\n", "TERMINATE" ] } ], "source": [ "wikipedia_assistant = ToolUseAssistantAgent(\n", " \"WikiPedia_Assistant\",\n", " model_client=OpenAIChatCompletionClient(model=\"gpt-4o-mini\"),\n", " registered_tools=[langchain_wikipedia_tool],\n", ")\n", "team = RoundRobinGroupChat([wikipedia_assistant])\n", "result = await team.run(\n", " \"Who was the first president of the United States?\", termination_condition=StopMessageTermination()\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.12.5" } }, "nbformat": 4, "nbformat_minor": 2 }