|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import List |
| 5 | + |
| 6 | +from agents import Agent, HandoffInputData, Runner, function_tool, handoff |
| 7 | +from agents.extensions import handoff_filters |
| 8 | +from agents.items import TResponseInputItem |
| 9 | +from temporalio import workflow |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class MessageFilterResult: |
| 14 | + final_output: str |
| 15 | + final_messages: List[TResponseInputItem] |
| 16 | + |
| 17 | + |
| 18 | +@function_tool |
| 19 | +def random_number_tool(max: int) -> int: |
| 20 | + """Return a random integer between 0 and the given maximum.""" |
| 21 | + return workflow.random().randint(0, max) |
| 22 | + |
| 23 | + |
| 24 | +def spanish_handoff_message_filter( |
| 25 | + handoff_message_data: HandoffInputData, |
| 26 | +) -> HandoffInputData: |
| 27 | + # First, we'll remove any tool-related messages from the message history |
| 28 | + handoff_message_data = handoff_filters.remove_all_tools(handoff_message_data) |
| 29 | + |
| 30 | + # Second, we'll also remove the first two items from the history, just for demonstration |
| 31 | + history = ( |
| 32 | + tuple(handoff_message_data.input_history[2:]) |
| 33 | + if isinstance(handoff_message_data.input_history, tuple) |
| 34 | + else handoff_message_data.input_history |
| 35 | + ) |
| 36 | + |
| 37 | + return HandoffInputData( |
| 38 | + input_history=history, |
| 39 | + pre_handoff_items=tuple(handoff_message_data.pre_handoff_items), |
| 40 | + new_items=tuple(handoff_message_data.new_items), |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +@workflow.defn |
| 45 | +class MessageFilterWorkflow: |
| 46 | + @workflow.run |
| 47 | + async def run(self, user_name: str = "Sora") -> MessageFilterResult: |
| 48 | + first_agent = Agent( |
| 49 | + name="Assistant", |
| 50 | + instructions="Be extremely concise.", |
| 51 | + tools=[random_number_tool], |
| 52 | + ) |
| 53 | + |
| 54 | + spanish_agent = Agent( |
| 55 | + name="Spanish Assistant", |
| 56 | + instructions="You only speak Spanish and are extremely concise.", |
| 57 | + handoff_description="A Spanish-speaking assistant.", |
| 58 | + ) |
| 59 | + |
| 60 | + second_agent = Agent( |
| 61 | + name="Assistant", |
| 62 | + instructions=( |
| 63 | + "Be a helpful assistant. If the user speaks Spanish, handoff to the Spanish assistant." |
| 64 | + ), |
| 65 | + handoffs=[ |
| 66 | + handoff(spanish_agent, input_filter=spanish_handoff_message_filter) |
| 67 | + ], |
| 68 | + ) |
| 69 | + |
| 70 | + # 1. Send a regular message to the first agent |
| 71 | + result = await Runner.run(first_agent, input=f"Hi, my name is {user_name}.") |
| 72 | + |
| 73 | + # 2. Ask it to generate a number |
| 74 | + result = await Runner.run( |
| 75 | + first_agent, |
| 76 | + input=result.to_input_list() |
| 77 | + + [ |
| 78 | + { |
| 79 | + "content": "Can you generate a random number between 0 and 100?", |
| 80 | + "role": "user", |
| 81 | + } |
| 82 | + ], |
| 83 | + ) |
| 84 | + |
| 85 | + # 3. Call the second agent |
| 86 | + result = await Runner.run( |
| 87 | + second_agent, |
| 88 | + input=result.to_input_list() |
| 89 | + + [ |
| 90 | + { |
| 91 | + "content": "I live in New York City. What's the population of the city?", |
| 92 | + "role": "user", |
| 93 | + } |
| 94 | + ], |
| 95 | + ) |
| 96 | + |
| 97 | + # 4. Cause a handoff to occur |
| 98 | + result = await Runner.run( |
| 99 | + second_agent, |
| 100 | + input=result.to_input_list() |
| 101 | + + [ |
| 102 | + { |
| 103 | + "content": "Por favor habla en español. ¿Cuál es mi nombre y dónde vivo?", |
| 104 | + "role": "user", |
| 105 | + } |
| 106 | + ], |
| 107 | + ) |
| 108 | + |
| 109 | + # Return the final result and message history |
| 110 | + return MessageFilterResult( |
| 111 | + final_output=result.final_output, final_messages=result.to_input_list() |
| 112 | + ) |
0 commit comments