|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "# Install dependencies. \n", |
| 10 | + "!pip install -U agentops openai-agents" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "# Set the API keys for your AgentOps and OpenAI accounts.\n", |
| 20 | + "import os\n", |
| 21 | + "\n", |
| 22 | + "os.environ[\"OPENAI_API_KEY\"] = ''\n", |
| 23 | + "os.environ[\"AGENTOPS_API_KEY\"] = ''\n", |
| 24 | + "\n", |
| 25 | + "if os.environ[\"OPENAI_API_KEY\"] == '':\n", |
| 26 | + " raise ValueError(\"OPENAI_API_KEY is not set. You can get one at https://platform.openai.com/api-keys\")\n", |
| 27 | + "if os.environ[\"AGENTOPS_API_KEY\"] == '':\n", |
| 28 | + " raise ValueError(\"AGENTOPS_API_KEY is not set. You can get one at https://app.agentops.ai/\")\n" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "from __future__ import annotations as _annotations\n", |
| 38 | + "\n", |
| 39 | + "import random\n", |
| 40 | + "import uuid\n", |
| 41 | + "from pydantic import BaseModel\n", |
| 42 | + "import agentops\n", |
| 43 | + "\n", |
| 44 | + "# API key is automatically read from the environment variable\n", |
| 45 | + "agentops.init(tags=[\"customer-service-agent\", \"agentops-example\"])\n", |
| 46 | + "\n", |
| 47 | + "from agents import (\n", |
| 48 | + " Agent,\n", |
| 49 | + " HandoffOutputItem,\n", |
| 50 | + " ItemHelpers,\n", |
| 51 | + " MessageOutputItem,\n", |
| 52 | + " RunContextWrapper,\n", |
| 53 | + " Runner,\n", |
| 54 | + " ToolCallItem,\n", |
| 55 | + " ToolCallOutputItem,\n", |
| 56 | + " TResponseInputItem,\n", |
| 57 | + " function_tool,\n", |
| 58 | + " handoff,\n", |
| 59 | + " trace,\n", |
| 60 | + ")\n", |
| 61 | + "from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX\n", |
| 62 | + "\n", |
| 63 | + "### CONTEXT\n", |
| 64 | + "\n", |
| 65 | + "\n", |
| 66 | + "class AirlineAgentContext(BaseModel):\n", |
| 67 | + " passenger_name: str | None = None\n", |
| 68 | + " confirmation_number: str | None = None\n", |
| 69 | + " seat_number: str | None = None\n", |
| 70 | + " flight_number: str | None = None\n", |
| 71 | + "\n", |
| 72 | + "\n", |
| 73 | + "### TOOLS\n", |
| 74 | + "\n", |
| 75 | + "\n", |
| 76 | + "@function_tool(\n", |
| 77 | + " name_override=\"faq_lookup_tool\", description_override=\"Lookup frequently asked questions.\"\n", |
| 78 | + ")\n", |
| 79 | + "async def faq_lookup_tool(question: str) -> str:\n", |
| 80 | + " if \"bag\" in question or \"baggage\" in question:\n", |
| 81 | + " return (\n", |
| 82 | + " \"You are allowed to bring one bag on the plane. \"\n", |
| 83 | + " \"It must be under 50 pounds and 22 inches x 14 inches x 9 inches.\"\n", |
| 84 | + " )\n", |
| 85 | + " elif \"seats\" in question or \"plane\" in question:\n", |
| 86 | + " return (\n", |
| 87 | + " \"There are 120 seats on the plane. \"\n", |
| 88 | + " \"There are 22 business class seats and 98 economy seats. \"\n", |
| 89 | + " \"Exit rows are rows 4 and 16. \"\n", |
| 90 | + " \"Rows 5-8 are Economy Plus, with extra legroom. \"\n", |
| 91 | + " )\n", |
| 92 | + " elif \"wifi\" in question:\n", |
| 93 | + " return \"We have free wifi on the plane, join Airline-Wifi\"\n", |
| 94 | + " return \"I'm sorry, I don't know the answer to that question.\"\n", |
| 95 | + "\n", |
| 96 | + "\n", |
| 97 | + "@function_tool\n", |
| 98 | + "async def update_seat(\n", |
| 99 | + " context: RunContextWrapper[AirlineAgentContext], confirmation_number: str, new_seat: str\n", |
| 100 | + ") -> str:\n", |
| 101 | + " \"\"\"\n", |
| 102 | + " Update the seat for a given confirmation number.\n", |
| 103 | + "\n", |
| 104 | + " Args:\n", |
| 105 | + " confirmation_number: The confirmation number for the flight.\n", |
| 106 | + " new_seat: The new seat to update to.\n", |
| 107 | + " \"\"\"\n", |
| 108 | + " # Update the context based on the customer's input\n", |
| 109 | + " context.context.confirmation_number = confirmation_number\n", |
| 110 | + " context.context.seat_number = new_seat\n", |
| 111 | + " # Ensure that the flight number has been set by the incoming handoff\n", |
| 112 | + " assert context.context.flight_number is not None, \"Flight number is required\"\n", |
| 113 | + " return f\"Updated seat to {new_seat} for confirmation number {confirmation_number}\"\n", |
| 114 | + "\n", |
| 115 | + "\n", |
| 116 | + "### HOOKS\n", |
| 117 | + "\n", |
| 118 | + "\n", |
| 119 | + "async def on_seat_booking_handoff(context: RunContextWrapper[AirlineAgentContext]) -> None:\n", |
| 120 | + " flight_number = f\"FLT-{random.randint(100, 999)}\"\n", |
| 121 | + " context.context.flight_number = flight_number\n", |
| 122 | + "\n", |
| 123 | + "\n", |
| 124 | + "### AGENTS\n", |
| 125 | + "\n", |
| 126 | + "faq_agent = Agent[AirlineAgentContext](\n", |
| 127 | + " name=\"FAQ Agent\",\n", |
| 128 | + " handoff_description=\"A helpful agent that can answer questions about the airline.\",\n", |
| 129 | + " instructions=f\"\"\"{RECOMMENDED_PROMPT_PREFIX}\n", |
| 130 | + " You are an FAQ agent. If you are speaking to a customer, you probably were transferred to from the triage agent.\n", |
| 131 | + " Use the following routine to support the customer.\n", |
| 132 | + " # Routine\n", |
| 133 | + " 1. Identify the last question asked by the customer.\n", |
| 134 | + " 2. Use the faq lookup tool to answer the question. Do not rely on your own knowledge.\n", |
| 135 | + " 3. If you cannot answer the question, transfer back to the triage agent.\"\"\",\n", |
| 136 | + " tools=[faq_lookup_tool],\n", |
| 137 | + ")\n", |
| 138 | + "\n", |
| 139 | + "seat_booking_agent = Agent[AirlineAgentContext](\n", |
| 140 | + " name=\"Seat Booking Agent\",\n", |
| 141 | + " handoff_description=\"A helpful agent that can update a seat on a flight.\",\n", |
| 142 | + " instructions=f\"\"\"{RECOMMENDED_PROMPT_PREFIX}\n", |
| 143 | + " You are a seat booking agent. If you are speaking to a customer, you probably were transferred to from the triage agent.\n", |
| 144 | + " Use the following routine to support the customer.\n", |
| 145 | + " # Routine\n", |
| 146 | + " 1. Ask for their confirmation number.\n", |
| 147 | + " 2. Ask the customer what their desired seat number is.\n", |
| 148 | + " 3. Use the update seat tool to update the seat on the flight.\n", |
| 149 | + " If the customer asks a question that is not related to the routine, transfer back to the triage agent. \"\"\",\n", |
| 150 | + " tools=[update_seat],\n", |
| 151 | + ")\n", |
| 152 | + "\n", |
| 153 | + "triage_agent = Agent[AirlineAgentContext](\n", |
| 154 | + " name=\"Triage Agent\",\n", |
| 155 | + " handoff_description=\"A triage agent that can delegate a customer's request to the appropriate agent.\",\n", |
| 156 | + " instructions=(\n", |
| 157 | + " f\"{RECOMMENDED_PROMPT_PREFIX} \"\n", |
| 158 | + " \"You are a helpful triaging agent. You can use your tools to delegate questions to other appropriate agents.\"\n", |
| 159 | + " ),\n", |
| 160 | + " handoffs=[\n", |
| 161 | + " faq_agent,\n", |
| 162 | + " handoff(agent=seat_booking_agent, on_handoff=on_seat_booking_handoff),\n", |
| 163 | + " ],\n", |
| 164 | + ")\n", |
| 165 | + "\n", |
| 166 | + "faq_agent.handoffs.append(triage_agent)\n", |
| 167 | + "seat_booking_agent.handoffs.append(triage_agent)\n", |
| 168 | + "\n", |
| 169 | + "\n", |
| 170 | + "### RUN\n", |
| 171 | + "\n", |
| 172 | + "\n", |
| 173 | + "async def main():\n", |
| 174 | + " current_agent: Agent[AirlineAgentContext] = triage_agent\n", |
| 175 | + " input_items: list[TResponseInputItem] = []\n", |
| 176 | + " context = AirlineAgentContext()\n", |
| 177 | + "\n", |
| 178 | + " # Normally, each input from the user would be an API request to your app, and you can wrap the request in a trace()\n", |
| 179 | + " # Here, we'll just use a random UUID for the conversation ID\n", |
| 180 | + " conversation_id = uuid.uuid4().hex[:16]\n", |
| 181 | + "\n", |
| 182 | + " while True:\n", |
| 183 | + " user_input = input(\"Enter your message: \")\n", |
| 184 | + " with trace(\"Customer service\", group_id=conversation_id):\n", |
| 185 | + " input_items.append({\"content\": user_input, \"role\": \"user\"})\n", |
| 186 | + " result = await Runner.run(current_agent, input_items, context=context)\n", |
| 187 | + "\n", |
| 188 | + " for new_item in result.new_items:\n", |
| 189 | + " agent_name = new_item.agent.name\n", |
| 190 | + " if isinstance(new_item, MessageOutputItem):\n", |
| 191 | + " print(f\"{agent_name}: {ItemHelpers.text_message_output(new_item)}\")\n", |
| 192 | + " elif isinstance(new_item, HandoffOutputItem):\n", |
| 193 | + " print(\n", |
| 194 | + " f\"Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}\"\n", |
| 195 | + " )\n", |
| 196 | + " elif isinstance(new_item, ToolCallItem):\n", |
| 197 | + " print(f\"{agent_name}: Calling a tool\")\n", |
| 198 | + " elif isinstance(new_item, ToolCallOutputItem):\n", |
| 199 | + " print(f\"{agent_name}: Tool call output: {new_item.output}\")\n", |
| 200 | + " else:\n", |
| 201 | + " print(f\"{agent_name}: Skipping item: {new_item.__class__.__name__}\")\n", |
| 202 | + " input_items = result.to_input_list()\n", |
| 203 | + " current_agent = result.last_agent\n", |
| 204 | + "\n", |
| 205 | + "\n", |
| 206 | + "await main()" |
| 207 | + ] |
| 208 | + }, |
| 209 | + { |
| 210 | + "cell_type": "markdown", |
| 211 | + "metadata": {}, |
| 212 | + "source": [ |
| 213 | + "You can check the trace in the AgentOps UI: https://app.agentops.ai/traces" |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "code", |
| 218 | + "execution_count": null, |
| 219 | + "metadata": {}, |
| 220 | + "outputs": [], |
| 221 | + "source": [] |
| 222 | + } |
| 223 | + ], |
| 224 | + "metadata": { |
| 225 | + "kernelspec": { |
| 226 | + "display_name": "base", |
| 227 | + "language": "python", |
| 228 | + "name": "python3" |
| 229 | + }, |
| 230 | + "language_info": { |
| 231 | + "codemirror_mode": { |
| 232 | + "name": "ipython", |
| 233 | + "version": 3 |
| 234 | + }, |
| 235 | + "file_extension": ".py", |
| 236 | + "mimetype": "text/x-python", |
| 237 | + "name": "python", |
| 238 | + "nbconvert_exporter": "python", |
| 239 | + "pygments_lexer": "ipython3", |
| 240 | + "version": "3.12.7" |
| 241 | + } |
| 242 | + }, |
| 243 | + "nbformat": 4, |
| 244 | + "nbformat_minor": 2 |
| 245 | +} |
0 commit comments