diff --git a/agentops/partners/autogen_logger.py b/agentops/partners/autogen_logger.py index 4b6088a91..e35b04c80 100644 --- a/agentops/partners/autogen_logger.py +++ b/agentops/partners/autogen_logger.py @@ -92,7 +92,7 @@ def log_function_use(self, source: Union[str, Agent], function: F, args: Dict[st event.function = function # TODO: this is not a parameter event.params = args event.returns = returns - event.name = getattr(function, "_name") + event.name = getattr(function, "__name__") agentops.record(event) def log_new_wrapper( diff --git a/examples/autogen_examples/MathAgent.ipynb b/examples/autogen_examples/MathAgent.ipynb index 124017bc2..c934e65e1 100644 --- a/examples/autogen_examples/MathAgent.ipynb +++ b/examples/autogen_examples/MathAgent.ipynb @@ -1,237 +1,236 @@ { - "cells": [ - { - "attachments": {}, - "cell_type": "markdown", - "id": "bb6538d8-2a5d-4a99-b2c1-7130963e4f7b", - "metadata": {}, - "source": [ - "# AG2 Tool Example\n", - "\n", - "\n", - "To get started, you'll need to install the AgentOps package and [set an API key](app.agentops.ai).\n", - "\n", - "AgentOps automatically configures itself when it's initialized meaning your agent run data will be tracked and logged to your AgentOps account right away." - ] - }, - { - "cell_type": "markdown", - "id": "083244fa", - "metadata": {}, - "source": [ - "First let's install the required packages" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9c8104ad", - "metadata": {}, - "outputs": [], - "source": [ - "%pip install -U pyautogen\n", - "%pip install -U agentops\n", - "%pip install -U python-dotenv" - ] - }, - { - "cell_type": "markdown", - "id": "cc44e459", - "metadata": {}, - "source": [ - "Then import them" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7672f591", - "metadata": {}, - "outputs": [], - "source": [ - "from autogen import ConversableAgent\n", - "from typing import Annotated, Literal\n", - "from autogen import ConversableAgent, register_function\n", - "import agentops\n", - "import os\n", - "from dotenv import load_dotenv\n", - "from IPython.core.error import (\n", - " StdinNotImplementedError,\n", - ") # only needed by AgentOps testing automation" - ] - }, - { - "cell_type": "markdown", - "id": "24f8bd70", - "metadata": {}, - "source": [ - "Next, we'll set our API keys. There are several ways to do this, the code below is just the most foolproof way for the purposes of this notebook. It accounts for both users who use environment variables and those who just want to set the API Key here in this notebook.\n", - "\n", - "[Get an AgentOps API key](https://agentops.ai/settings/projects)\n", - "\n", - "1. Create an environment variable in a .env file or other method. By default, the AgentOps `init()` function will look for an environment variable named `AGENTOPS_API_KEY`. Or...\n", - "\n", - "2. Replace `` below and pass in the optional `api_key` parameter to the AgentOps `init(api_key=...)` function. Remember not to commit your API key to a public repo!" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9eeaef34", - "metadata": {}, - "outputs": [], - "source": [ - "load_dotenv()\n", - "OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") or \"\"\n", - "AGENTOPS_API_KEY = os.getenv(\"AGENTOPS_API_KEY\") or \"\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d93f2339-7b99-4cf1-9232-c24faba49c7b", - "metadata": {}, - "outputs": [], - "source": [ - "agentops.init(AGENTOPS_API_KEY, default_tags=[\"autogen-tool-example\"])\n", - "\n", - "print(\"AgentOps is now running. You can view your session in the link above\")" - ] - }, - { - "cell_type": "markdown", - "id": "7858f0f6-9aca-4cdb-a514-9fbf7e353d50", - "metadata": {}, - "source": [ - "AG2 will now start automatically tracking\n", - "\n", - "* LLM prompts and completions\n", - "* Token usage and costs\n", - "* Agent names and actions\n", - "* Correspondence between agents\n", - "* Tool usage\n", - "* Errors" - ] - }, - { - "cell_type": "markdown", - "id": "dc592637", - "metadata": {}, - "source": [ - "# Tool Example\n", - "AgentOps tracks when AG2 agents use tools. You can find more information on this example in [tool-use.ipynb](https://docs.ag2.ai/docs/tutorial/tool-use#tool-use)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9e4dfe37-85e0-4035-a314-3459c6e378c4", - "metadata": {}, - "outputs": [], - "source": [ - "# Define model, openai api key, tags, etc in the agent configuration\n", - "config_list = [\n", - " {\n", - " \"model\": \"gpt-4-turbo\",\n", - " \"api_key\": OPENAI_API_KEY,\n", - " \"tags\": [\"mathagent-example\", \"tool\"],\n", - " }\n", - "]\n", - "\n", - "Operator = Literal[\"+\", \"-\", \"*\", \"/\"]\n", - "\n", - "\n", - "def calculator(a: int, b: int, operator: Annotated[Operator, \"operator\"]) -> int:\n", - " if operator == \"+\":\n", - " return a + b\n", - " elif operator == \"-\":\n", - " return a - b\n", - " elif operator == \"*\":\n", - " return a * b\n", - " elif operator == \"/\":\n", - " return int(a / b)\n", - " else:\n", - " raise ValueError(\"Invalid operator\")\n", - "\n", - "\n", - "# Create the agent that uses the LLM.\n", - "assistant = ConversableAgent(\n", - " name=\"Assistant\",\n", - " system_message=\"You are a helpful AI assistant. \"\n", - " \"You can help with simple calculations. \"\n", - " \"Return 'TERMINATE' when the task is done.\",\n", - " llm_config={\"config_list\": config_list},\n", - ")\n", - "\n", - "# The user proxy agent is used for interacting with the assistant agent\n", - "# and executes tool calls.\n", - "user_proxy = ConversableAgent(\n", - " name=\"User\",\n", - " llm_config=False,\n", - " is_termination_msg=lambda msg: msg.get(\"content\") is not None\n", - " and \"TERMINATE\" in msg[\"content\"],\n", - " human_input_mode=\"NEVER\",\n", - ")\n", - "\n", - "assistant.register_for_llm(name=\"calculator\", description=\"A simple calculator\")(\n", - " calculator\n", - ")\n", - "user_proxy.register_for_execution(name=\"calculator\")(calculator)\n", - "\n", - "# Register the calculator function to the two agents.\n", - "register_function(\n", - " calculator,\n", - " caller=assistant, # The assistant agent can suggest calls to the calculator.\n", - " executor=user_proxy, # The user proxy agent can execute the calculator calls.\n", - " name=\"calculator\", # By default, the function name is used as the tool name.\n", - " description=\"A simple calculator\", # A description of the tool.\n", - ")\n", - "\n", - "# Let the assistant start the conversation. It will end when the user types \"exit\".\n", - "try:\n", - " user_proxy.initiate_chat(\n", - " assistant, message=\"What is (1423 - 123) / 3 + (32 + 23) * 5?\"\n", - " )\n", - "except StdinNotImplementedError:\n", - " # This is only necessary for AgentOps testing automation which is headless and will not have user input\n", - " print(\"Stdin not implemented. Skipping initiate_chat\")\n", - " agentops.end_session(\"Indeterminate\")\n", - "\n", - "agentops.end_session(\"Success\")" - ] - }, - { - "cell_type": "markdown", - "id": "f67b0305-1247-489e-b1b0-829127af76d3", - "metadata": {}, - "source": [ - "You can see your run in action at [app.agentops.ai](app.agentops.ai). In this example, the AgentOps dashboard will show:\n", - "\n", - "* Agents talking to each other\n", - "* Each use of the `calculator` tool\n", - "* Each call to OpenAI for LLM use" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.11.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 - } + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "id": "bb6538d8-2a5d-4a99-b2c1-7130963e4f7b", + "metadata": {}, + "source": [ + "# AG2 Tool Example\n", + "\n", + "\n", + "To get started, you'll need to install the AgentOps package and [set an API key](app.agentops.ai).\n", + "\n", + "AgentOps automatically configures itself when it's initialized meaning your agent run data will be tracked and logged to your AgentOps account right away." + ] + }, + { + "cell_type": "markdown", + "id": "083244fa", + "metadata": {}, + "source": [ + "First let's install the required packages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9c8104ad", + "metadata": {}, + "outputs": [], + "source": [ + "%pip install -U pyautogen\n", + "%pip install -U agentops\n", + "%pip install -U python-dotenv" + ] + }, + { + "cell_type": "markdown", + "id": "cc44e459", + "metadata": {}, + "source": [ + "Then import them" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7672f591", + "metadata": {}, + "outputs": [], + "source": [ + "from typing import Annotated, Literal\n", + "from autogen import ConversableAgent, register_function\n", + "import agentops\n", + "import os\n", + "from dotenv import load_dotenv\n", + "from IPython.core.error import (\n", + " StdinNotImplementedError,\n", + ") # only needed by AgentOps testing automation" + ] + }, + { + "cell_type": "markdown", + "id": "24f8bd70", + "metadata": {}, + "source": [ + "Next, we'll set our API keys. There are several ways to do this, the code below is just the most foolproof way for the purposes of this notebook. It accounts for both users who use environment variables and those who just want to set the API Key here in this notebook.\n", + "\n", + "[Get an AgentOps API key](https://agentops.ai/settings/projects)\n", + "\n", + "1. Create an environment variable in a .env file or other method. By default, the AgentOps `init()` function will look for an environment variable named `AGENTOPS_API_KEY`. Or...\n", + "\n", + "2. Replace `` below and pass in the optional `api_key` parameter to the AgentOps `init(api_key=...)` function. Remember not to commit your API key to a public repo!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9eeaef34", + "metadata": {}, + "outputs": [], + "source": [ + "load_dotenv()\n", + "OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") or \"\"\n", + "AGENTOPS_API_KEY = os.getenv(\"AGENTOPS_API_KEY\") or \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d93f2339-7b99-4cf1-9232-c24faba49c7b", + "metadata": {}, + "outputs": [], + "source": [ + "agentops.init(AGENTOPS_API_KEY, default_tags=[\"autogen-tool-example\"])\n", + "\n", + "print(\"AgentOps is now running. You can view your session in the link above\")" + ] + }, + { + "cell_type": "markdown", + "id": "7858f0f6-9aca-4cdb-a514-9fbf7e353d50", + "metadata": {}, + "source": [ + "AG2 will now start automatically tracking\n", + "\n", + "* LLM prompts and completions\n", + "* Token usage and costs\n", + "* Agent names and actions\n", + "* Correspondence between agents\n", + "* Tool usage\n", + "* Errors" + ] + }, + { + "cell_type": "markdown", + "id": "dc592637", + "metadata": {}, + "source": [ + "# Tool Example\n", + "AgentOps tracks when AG2 agents use tools. You can find more information on this example in [tool-use.ipynb](https://docs.ag2.ai/docs/tutorial/tool-use#tool-use)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e4dfe37-85e0-4035-a314-3459c6e378c4", + "metadata": {}, + "outputs": [], + "source": [ + "# Define model, openai api key, tags, etc in the agent configuration\n", + "config_list = [\n", + " {\n", + " \"model\": \"gpt-4-turbo\",\n", + " \"api_key\": OPENAI_API_KEY,\n", + " \"tags\": [\"mathagent-example\", \"tool\"],\n", + " }\n", + "]\n", + "\n", + "Operator = Literal[\"+\", \"-\", \"*\", \"/\"]\n", + "\n", + "\n", + "def calculator(a: int, b: int, operator: Annotated[Operator, \"operator\"]) -> int:\n", + " if operator == \"+\":\n", + " return a + b\n", + " elif operator == \"-\":\n", + " return a - b\n", + " elif operator == \"*\":\n", + " return a * b\n", + " elif operator == \"/\":\n", + " return int(a / b)\n", + " else:\n", + " raise ValueError(\"Invalid operator\")\n", + "\n", + "\n", + "# Create the agent that uses the LLM.\n", + "assistant = ConversableAgent(\n", + " name=\"Assistant\",\n", + " system_message=\"You are a helpful AI assistant. \"\n", + " \"You can help with simple calculations. \"\n", + " \"Return 'TERMINATE' when the task is done.\",\n", + " llm_config={\"config_list\": config_list},\n", + ")\n", + "\n", + "# The user proxy agent is used for interacting with the assistant agent\n", + "# and executes tool calls.\n", + "user_proxy = ConversableAgent(\n", + " name=\"User\",\n", + " llm_config=False,\n", + " is_termination_msg=lambda msg: msg.get(\"content\") is not None\n", + " and \"TERMINATE\" in msg[\"content\"],\n", + " human_input_mode=\"NEVER\",\n", + ")\n", + "\n", + "assistant.register_for_llm(name=\"calculator\", description=\"A simple calculator\")(\n", + " calculator\n", + ")\n", + "user_proxy.register_for_execution(name=\"calculator\")(calculator)\n", + "\n", + "# Register the calculator function to the two agents.\n", + "register_function(\n", + " calculator,\n", + " caller=assistant, # The assistant agent can suggest calls to the calculator.\n", + " executor=user_proxy, # The user proxy agent can execute the calculator calls.\n", + " name=\"calculator\", # By default, the function name is used as the tool name.\n", + " description=\"A simple calculator\", # A description of the tool.\n", + ")\n", + "\n", + "# Let the assistant start the conversation. It will end when the user types \"exit\".\n", + "try:\n", + " user_proxy.initiate_chat(\n", + " assistant, message=\"What is (1423 - 123) / 3 + (32 + 23) * 5?\"\n", + " )\n", + "except StdinNotImplementedError:\n", + " # This is only necessary for AgentOps testing automation which is headless and will not have user input\n", + " print(\"Stdin not implemented. Skipping initiate_chat\")\n", + " agentops.end_session(\"Indeterminate\")\n", + "\n", + "agentops.end_session(\"Success\")" + ] + }, + { + "cell_type": "markdown", + "id": "f67b0305-1247-489e-b1b0-829127af76d3", + "metadata": {}, + "source": [ + "You can see your run in action at [app.agentops.ai](app.agentops.ai). In this example, the AgentOps dashboard will show:\n", + "\n", + "* Agents talking to each other\n", + "* Each use of the `calculator` tool\n", + "* Each call to OpenAI for LLM use" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}