From 5a43c888bf74d153fc87074ea20cefd770e14f7a Mon Sep 17 00:00:00 2001 From: Dwij Patel Date: Wed, 14 May 2025 20:30:22 +0530 Subject: [PATCH] Refactor notebook examples for linitng --- .../openai_agents/instrumentor.py | 7 +++--- .../customer_service_agent.ipynb | 24 +++++++------------ .../openai_agents_sdk/tool_usage_agent.ipynb | 19 ++++++++------- .../openai_agents_sdk/web_search_agent.ipynb | 14 ++++++----- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/agentops/instrumentation/openai_agents/instrumentor.py b/agentops/instrumentation/openai_agents/instrumentor.py index 60941dfa5..2684bb50d 100644 --- a/agentops/instrumentation/openai_agents/instrumentor.py +++ b/agentops/instrumentation/openai_agents/instrumentor.py @@ -14,11 +14,12 @@ from the `agents.tracing` module. TODO Calls to the OpenAI API are not available in this tracing context, so we may -need to monkey-patch the `openai` from here to get that data. While we do have -separate instrumentation for the OpenAI API, in order to get it to nest with the +need to monkey-patch the `openai` from here to get that data. While we do have +separate instrumentation for the OpenAI API, in order to get it to nest with the spans we create here, it's probably easier (or even required) that we incorporate -that here as well. +that here as well. """ + from typing import Collection from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore from agentops.logging import logger diff --git a/examples/openai_agents_sdk/customer_service_agent.ipynb b/examples/openai_agents_sdk/customer_service_agent.ipynb index 53b84b957..1d1a5f107 100644 --- a/examples/openai_agents_sdk/customer_service_agent.ipynb +++ b/examples/openai_agents_sdk/customer_service_agent.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Install dependencies. \n", + "# Install dependencies.\n", "!pip install -U agentops openai-agents" ] }, @@ -19,13 +19,13 @@ "# Set the API keys for your AgentOps and OpenAI accounts.\n", "import os\n", "\n", - "os.environ[\"OPENAI_API_KEY\"] = ''\n", - "os.environ[\"AGENTOPS_API_KEY\"] = ''\n", + "os.environ[\"OPENAI_API_KEY\"] = \"\"\n", + "os.environ[\"AGENTOPS_API_KEY\"] = \"\"\n", "\n", - "if os.environ[\"OPENAI_API_KEY\"] == '':\n", + "if os.environ[\"OPENAI_API_KEY\"] == \"\":\n", " raise ValueError(\"OPENAI_API_KEY is not set. You can get one at https://platform.openai.com/api-keys\")\n", - "if os.environ[\"AGENTOPS_API_KEY\"] == '':\n", - " raise ValueError(\"AGENTOPS_API_KEY is not set. You can get one at https://app.agentops.ai/\")\n" + "if os.environ[\"AGENTOPS_API_KEY\"] == \"\":\n", + " raise ValueError(\"AGENTOPS_API_KEY is not set. You can get one at https://app.agentops.ai/\")" ] }, { @@ -73,9 +73,7 @@ "### TOOLS\n", "\n", "\n", - "@function_tool(\n", - " name_override=\"faq_lookup_tool\", description_override=\"Lookup frequently asked questions.\"\n", - ")\n", + "@function_tool(name_override=\"faq_lookup_tool\", description_override=\"Lookup frequently asked questions.\")\n", "async def faq_lookup_tool(question: str) -> str:\n", " if \"bag\" in question or \"baggage\" in question:\n", " return (\n", @@ -95,9 +93,7 @@ "\n", "\n", "@function_tool\n", - "async def update_seat(\n", - " context: RunContextWrapper[AirlineAgentContext], confirmation_number: str, new_seat: str\n", - ") -> str:\n", + "async def update_seat(context: RunContextWrapper[AirlineAgentContext], confirmation_number: str, new_seat: str) -> str:\n", " \"\"\"\n", " Update the seat for a given confirmation number.\n", "\n", @@ -190,9 +186,7 @@ " if isinstance(new_item, MessageOutputItem):\n", " print(f\"{agent_name}: {ItemHelpers.text_message_output(new_item)}\")\n", " elif isinstance(new_item, HandoffOutputItem):\n", - " print(\n", - " f\"Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}\"\n", - " )\n", + " print(f\"Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}\")\n", " elif isinstance(new_item, ToolCallItem):\n", " print(f\"{agent_name}: Calling a tool\")\n", " elif isinstance(new_item, ToolCallOutputItem):\n", diff --git a/examples/openai_agents_sdk/tool_usage_agent.ipynb b/examples/openai_agents_sdk/tool_usage_agent.ipynb index 73a0f04c2..37c5ac272 100644 --- a/examples/openai_agents_sdk/tool_usage_agent.ipynb +++ b/examples/openai_agents_sdk/tool_usage_agent.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Install dependencies. \n", + "# Install dependencies.\n", "!pip install -U agentops openai-agents" ] }, @@ -19,13 +19,13 @@ "# Set the API keys for your AgentOps and OpenAI accounts.\n", "import os\n", "\n", - "os.environ[\"OPENAI_API_KEY\"] = ''\n", - "os.environ[\"AGENTOPS_API_KEY\"] = ''\n", + "os.environ[\"OPENAI_API_KEY\"] = \"\"\n", + "os.environ[\"AGENTOPS_API_KEY\"] = \"\"\n", "\n", - "if os.environ[\"OPENAI_API_KEY\"] == '':\n", + "if os.environ[\"OPENAI_API_KEY\"] == \"\":\n", " raise ValueError(\"OPENAI_API_KEY is not set. You can get one at https://platform.openai.com/api-keys\")\n", - "if os.environ[\"AGENTOPS_API_KEY\"] == '':\n", - " raise ValueError(\"AGENTOPS_API_KEY is not set. You can get one at https://app.agentops.ai/\")\n" + "if os.environ[\"AGENTOPS_API_KEY\"] == \"\":\n", + " raise ValueError(\"AGENTOPS_API_KEY is not set. You can get one at https://app.agentops.ai/\")" ] }, { @@ -40,6 +40,7 @@ "# AgentOps automatically reads the API key from the environment variable\n", "agentops.init(tags=[\"weather-checker\", \"agentops-example\"])\n", "\n", + "\n", "@function_tool\n", "def get_weather(city: str) -> str:\n", " return f\"The weather in {city} is sunny.\"\n", @@ -54,12 +55,12 @@ "\n", "async def main():\n", " print(\"Starting agent...\")\n", - " result = await Runner.run(agent, \n", - " input=\"What's the weather in San Francisco?\")\n", + " result = await Runner.run(agent, input=\"What's the weather in San Francisco?\")\n", " print(result.final_output)\n", " # The weather in San Francisco is sunny.\n", "\n", - "await main()\n" + "\n", + "await main()" ] }, { diff --git a/examples/openai_agents_sdk/web_search_agent.ipynb b/examples/openai_agents_sdk/web_search_agent.ipynb index 54d90e21a..be9528b13 100644 --- a/examples/openai_agents_sdk/web_search_agent.ipynb +++ b/examples/openai_agents_sdk/web_search_agent.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Install dependencies. \n", + "# Install dependencies.\n", "!pip install -U agentops openai-agents" ] }, @@ -19,13 +19,13 @@ "# Set the API keys for your AgentOps and OpenAI accounts.\n", "import os\n", "\n", - "os.environ[\"OPENAI_API_KEY\"] = ''\n", - "os.environ[\"AGENTOPS_API_KEY\"] = ''\n", + "os.environ[\"OPENAI_API_KEY\"] = \"\"\n", + "os.environ[\"AGENTOPS_API_KEY\"] = \"\"\n", "\n", - "if os.environ[\"OPENAI_API_KEY\"] == '':\n", + "if os.environ[\"OPENAI_API_KEY\"] == \"\":\n", " raise ValueError(\"OPENAI_API_KEY is not set. You can get one at https://platform.openai.com/api-keys\")\n", - "if os.environ[\"AGENTOPS_API_KEY\"] == '':\n", - " raise ValueError(\"AGENTOPS_API_KEY is not set. You can get one at https://app.agentops.ai/\")\n" + "if os.environ[\"AGENTOPS_API_KEY\"] == \"\":\n", + " raise ValueError(\"AGENTOPS_API_KEY is not set. You can get one at https://app.agentops.ai/\")" ] }, { @@ -40,6 +40,7 @@ "# AgentOps automatically reads the API key from the environment variable\n", "agentops.init(tags=[\"web-search-agent\", \"agentops-example\"])\n", "\n", + "\n", "async def main():\n", " agent = Agent(\n", " name=\"Web searcher\",\n", @@ -55,6 +56,7 @@ " print(result.final_output)\n", " # The New York Giants are reportedly pursuing quarterback Aaron Rodgers after his ...\n", "\n", + "\n", "await main()" ] },