|
1 | 1 | from agents import function_tool
|
2 |
| -from agents.realtime import RealtimeAgent |
| 2 | +from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX |
| 3 | +from agents.realtime import RealtimeAgent, realtime_handoff |
3 | 4 |
|
4 | 5 | """
|
5 | 6 | When running the UI example locally, you can edit this file to change the setup. THe server
|
6 | 7 | will use the agent returned from get_starting_agent() as the starting agent."""
|
7 | 8 |
|
| 9 | +### TOOLS |
| 10 | + |
| 11 | + |
| 12 | +@function_tool( |
| 13 | + name_override="faq_lookup_tool", description_override="Lookup frequently asked questions." |
| 14 | +) |
| 15 | +async def faq_lookup_tool(question: str) -> str: |
| 16 | + if "bag" in question or "baggage" in question: |
| 17 | + return ( |
| 18 | + "You are allowed to bring one bag on the plane. " |
| 19 | + "It must be under 50 pounds and 22 inches x 14 inches x 9 inches." |
| 20 | + ) |
| 21 | + elif "seats" in question or "plane" in question: |
| 22 | + return ( |
| 23 | + "There are 120 seats on the plane. " |
| 24 | + "There are 22 business class seats and 98 economy seats. " |
| 25 | + "Exit rows are rows 4 and 16. " |
| 26 | + "Rows 5-8 are Economy Plus, with extra legroom. " |
| 27 | + ) |
| 28 | + elif "wifi" in question: |
| 29 | + return "We have free wifi on the plane, join Airline-Wifi" |
| 30 | + return "I'm sorry, I don't know the answer to that question." |
| 31 | + |
| 32 | + |
| 33 | +@function_tool |
| 34 | +async def update_seat(confirmation_number: str, new_seat: str) -> str: |
| 35 | + """ |
| 36 | + Update the seat for a given confirmation number. |
| 37 | +
|
| 38 | + Args: |
| 39 | + confirmation_number: The confirmation number for the flight. |
| 40 | + new_seat: The new seat to update to. |
| 41 | + """ |
| 42 | + return f"Updated seat to {new_seat} for confirmation number {confirmation_number}" |
| 43 | + |
8 | 44 |
|
9 | 45 | @function_tool
|
10 | 46 | def get_weather(city: str) -> str:
|
11 | 47 | """Get the weather in a city."""
|
12 | 48 | return f"The weather in {city} is sunny."
|
13 | 49 |
|
14 | 50 |
|
15 |
| -@function_tool |
16 |
| -def get_secret_number() -> int: |
17 |
| - """Returns the secret number, if the user asks for it.""" |
18 |
| - return 71 |
19 |
| - |
| 51 | +faq_agent = RealtimeAgent( |
| 52 | + name="FAQ Agent", |
| 53 | + handoff_description="A helpful agent that can answer questions about the airline.", |
| 54 | + instructions=f"""{RECOMMENDED_PROMPT_PREFIX} |
| 55 | + You are an FAQ agent. If you are speaking to a customer, you probably were transferred to from the triage agent. |
| 56 | + Use the following routine to support the customer. |
| 57 | + # Routine |
| 58 | + 1. Identify the last question asked by the customer. |
| 59 | + 2. Use the faq lookup tool to answer the question. Do not rely on your own knowledge. |
| 60 | + 3. If you cannot answer the question, transfer back to the triage agent.""", |
| 61 | + tools=[faq_lookup_tool], |
| 62 | +) |
20 | 63 |
|
21 |
| -haiku_agent = RealtimeAgent( |
22 |
| - name="Haiku Agent", |
23 |
| - instructions="You are a haiku poet. You must respond ONLY in traditional haiku format (5-7-5 syllables). Every response should be a proper haiku about the topic. Do not break character.", |
24 |
| - tools=[], |
| 64 | +seat_booking_agent = RealtimeAgent( |
| 65 | + name="Seat Booking Agent", |
| 66 | + handoff_description="A helpful agent that can update a seat on a flight.", |
| 67 | + instructions=f"""{RECOMMENDED_PROMPT_PREFIX} |
| 68 | + You are a seat booking agent. If you are speaking to a customer, you probably were transferred to from the triage agent. |
| 69 | + Use the following routine to support the customer. |
| 70 | + # Routine |
| 71 | + 1. Ask for their confirmation number. |
| 72 | + 2. Ask the customer what their desired seat number is. |
| 73 | + 3. Use the update seat tool to update the seat on the flight. |
| 74 | + If the customer asks a question that is not related to the routine, transfer back to the triage agent. """, |
| 75 | + tools=[update_seat], |
25 | 76 | )
|
26 | 77 |
|
27 |
| -assistant_agent = RealtimeAgent( |
28 |
| - name="Assistant", |
29 |
| - instructions="If the user wants poetry or haikus, you can hand them off to the haiku agent via the transfer_to_haiku_agent tool.", |
30 |
| - tools=[get_weather, get_secret_number], |
31 |
| - handoffs=[haiku_agent], |
| 78 | +triage_agent = RealtimeAgent( |
| 79 | + name="Triage Agent", |
| 80 | + handoff_description="A triage agent that can delegate a customer's request to the appropriate agent.", |
| 81 | + instructions=( |
| 82 | + f"{RECOMMENDED_PROMPT_PREFIX} " |
| 83 | + "You are a helpful triaging agent. You can use your tools to delegate questions to other appropriate agents." |
| 84 | + ), |
| 85 | + handoffs=[faq_agent, realtime_handoff(seat_booking_agent)], |
32 | 86 | )
|
33 | 87 |
|
| 88 | +faq_agent.handoffs.append(triage_agent) |
| 89 | +seat_booking_agent.handoffs.append(triage_agent) |
| 90 | + |
34 | 91 |
|
35 | 92 | def get_starting_agent() -> RealtimeAgent:
|
36 |
| - return assistant_agent |
| 93 | + return triage_agent |
0 commit comments