|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +from agents import Agent, HandoffInputData, Runner, function_tool, handoff |
| 6 | +from agents.extensions import handoff_filters |
| 7 | +from agents.models import is_gpt_5_default |
| 8 | + |
| 9 | + |
| 10 | +def spanish_handoff_message_filter(handoff_message_data: HandoffInputData) -> HandoffInputData: |
| 11 | + if is_gpt_5_default(): |
| 12 | + print("gpt-5 is enabled, so we're not filtering the input history") |
| 13 | + # when using gpt-5, removing some of the items could break things, so we do this filtering only for other models |
| 14 | + return HandoffInputData( |
| 15 | + input_history=handoff_message_data.input_history, |
| 16 | + pre_handoff_items=tuple(handoff_message_data.pre_handoff_items), |
| 17 | + new_items=tuple(handoff_message_data.new_items), |
| 18 | + ) |
| 19 | + |
| 20 | + # First, we'll remove any tool-related messages from the message history |
| 21 | + handoff_message_data = handoff_filters.remove_all_tools(handoff_message_data) |
| 22 | + |
| 23 | + # Second, we'll also remove the first two items from the history, just for demonstration |
| 24 | + history = ( |
| 25 | + tuple(handoff_message_data.input_history[2:]) |
| 26 | + if isinstance(handoff_message_data.input_history, tuple) |
| 27 | + else handoff_message_data.input_history |
| 28 | + ) |
| 29 | + |
| 30 | + # or, you can use the HandoffInputData.clone(kwargs) method |
| 31 | + return HandoffInputData( |
| 32 | + input_history=history, |
| 33 | + pre_handoff_items=tuple(handoff_message_data.pre_handoff_items), |
| 34 | + new_items=tuple(handoff_message_data.new_items), |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def main(random_number_tool): |
| 39 | + first_agent = Agent( |
| 40 | + name="Assistant", |
| 41 | + instructions="Be extremely concise.", |
| 42 | + tools=[random_number_tool], |
| 43 | + ) |
| 44 | + |
| 45 | + spanish_agent = Agent( |
| 46 | + name="Spanish Assistant", |
| 47 | + instructions="You only speak Spanish and are extremely concise.", |
| 48 | + handoff_description="A Spanish-speaking assistant.", |
| 49 | + ) |
| 50 | + |
| 51 | + second_agent = Agent( |
| 52 | + name="Assistant", |
| 53 | + instructions=( |
| 54 | + "Be a helpful assistant. If the user speaks Spanish, handoff to the Spanish assistant." |
| 55 | + ), |
| 56 | + handoffs=[handoff(spanish_agent, input_filter=spanish_handoff_message_filter)], |
| 57 | + ) |
| 58 | + |
| 59 | + # 1. Send a regular message to the first agent |
| 60 | + result = Runner.run_sync(first_agent, input="Hi, my name is Sora.") |
| 61 | + |
| 62 | + print("Step 1 done") |
| 63 | + |
| 64 | + # 2. Ask it to generate a number |
| 65 | + result = Runner.run_sync( |
| 66 | + first_agent, |
| 67 | + input=result.to_input_list() |
| 68 | + + [{"content": "Can you generate a random number between 0 and 100?", "role": "user"}], |
| 69 | + ) |
| 70 | + |
| 71 | + print("Step 2 done") |
| 72 | + |
| 73 | + # 3. Call the second agent |
| 74 | + result = Runner.run_sync( |
| 75 | + second_agent, |
| 76 | + input=result.to_input_list() |
| 77 | + + [ |
| 78 | + { |
| 79 | + "content": "I live in New York City. Whats the population of the city?", |
| 80 | + "role": "user", |
| 81 | + } |
| 82 | + ], |
| 83 | + ) |
| 84 | + |
| 85 | + print("Step 3 done") |
| 86 | + |
| 87 | + # 4. Cause a handoff to occur |
| 88 | + result = Runner.run_sync( |
| 89 | + second_agent, |
| 90 | + input=result.to_input_list() |
| 91 | + + [ |
| 92 | + { |
| 93 | + "content": "Por favor habla en español. ¿Cuál es mi nombre y dónde vivo?", |
| 94 | + "role": "user", |
| 95 | + } |
| 96 | + ], |
| 97 | + ) |
| 98 | + |
| 99 | + print("Step 4 done") |
| 100 | + |
| 101 | + print("\n===Final messages===\n") |
| 102 | + |
| 103 | + # 5. That should have caused spanish_handoff_message_filter to be called, which means the |
| 104 | + # output should be missing the first two messages, and have no tool calls. |
| 105 | + # Let's print the messages to see what happened |
| 106 | + for message in result.to_input_list(): |
| 107 | + print(json.dumps(message, indent=2)) |
| 108 | + # tool_calls = message.tool_calls if isinstance(message, AssistantMessage) else None |
| 109 | + |
| 110 | + # print(f"{message.role}: {message.content}\n - Tool calls: {tool_calls or 'None'}") |
| 111 | + """ |
| 112 | + $python examples/handoffs/message_filter.py |
| 113 | + Step 1 done |
| 114 | + Step 2 done |
| 115 | + Step 3 done |
| 116 | + Step 4 done |
| 117 | +
|
| 118 | + ===Final messages=== |
| 119 | +
|
| 120 | + { |
| 121 | + "content": "Can you generate a random number between 0 and 100?", |
| 122 | + "role": "user" |
| 123 | + } |
| 124 | + { |
| 125 | + "id": "...", |
| 126 | + "content": [ |
| 127 | + { |
| 128 | + "annotations": [], |
| 129 | + "text": "Sure! Here's a random number between 0 and 100: **42**.", |
| 130 | + "type": "output_text" |
| 131 | + } |
| 132 | + ], |
| 133 | + "role": "assistant", |
| 134 | + "status": "completed", |
| 135 | + "type": "message" |
| 136 | + } |
| 137 | + { |
| 138 | + "content": "I live in New York City. Whats the population of the city?", |
| 139 | + "role": "user" |
| 140 | + } |
| 141 | + { |
| 142 | + "id": "...", |
| 143 | + "content": [ |
| 144 | + { |
| 145 | + "annotations": [], |
| 146 | + "text": "As of the most recent estimates, the population of New York City is approximately 8.6 million people. However, this number is constantly changing due to various factors such as migration and birth rates. For the latest and most accurate information, it's always a good idea to check the official data from sources like the U.S. Census Bureau.", |
| 147 | + "type": "output_text" |
| 148 | + } |
| 149 | + ], |
| 150 | + "role": "assistant", |
| 151 | + "status": "completed", |
| 152 | + "type": "message" |
| 153 | + } |
| 154 | + { |
| 155 | + "content": "Por favor habla en espa\u00f1ol. \u00bfCu\u00e1l es mi nombre y d\u00f3nde vivo?", |
| 156 | + "role": "user" |
| 157 | + } |
| 158 | + { |
| 159 | + "id": "...", |
| 160 | + "content": [ |
| 161 | + { |
| 162 | + "annotations": [], |
| 163 | + "text": "No tengo acceso a esa informaci\u00f3n personal, solo s\u00e9 lo que me has contado: vives en Nueva York.", |
| 164 | + "type": "output_text" |
| 165 | + } |
| 166 | + ], |
| 167 | + "role": "assistant", |
| 168 | + "status": "completed", |
| 169 | + "type": "message" |
| 170 | + } |
| 171 | + """ |
| 172 | + |
| 173 | + return result.final_output |
0 commit comments