|
1 | 1 | from __future__ import annotations |
2 | | -from agents import Agent, RunContextWrapper, Runner, function_tool |
3 | | -from agents.exceptions import AgentsException |
| 2 | + |
4 | 3 | import asyncio |
| 4 | +from agents import Agent, Runner, function_tool |
| 5 | +from agents.exceptions import AgentsException |
5 | 6 |
|
6 | 7 |
|
7 | 8 | """ |
|
12 | 13 | - Adding two numbers using the `sum_numbers` tool. |
13 | 14 |
|
14 | 15 | The agent is instructed to use only one tool per execution cycle and can switch to another tool in subsequent cycles. |
15 | | -The example sets a `max_turns=1` limit to intentionally restrict the agent to a single turn, which may trigger a `MaxTurnsExceeded` error if the agent attempts multiple tool calls. |
| 16 | +The example sets a `max_turns=1` limit to intentionally restrict the agent to a single turn, which may trigger a `MaxTurnsExceeded` error. |
16 | 17 |
|
17 | | -Error handling is implemented with `AgentsException`, which is the base class for all SDK-related exceptions, including: |
18 | | -- `MaxTurnsExceeded`: Raised when the run exceeds the `max_turns` specified in the run methods. |
19 | | -- `ModelBehaviorError`: Raised when the model produces invalid outputs, e.g., malformed JSON or using non-existent tools. |
20 | | -- `UserError`: Raised when the SDK user makes an error in code implementation. |
21 | | -- `InputGuardrailTripwireTriggered`: Raised when an input guardrail is violated (e.g., invalid or off-topic input). |
22 | | -- `OutputGuardrailTripwireTriggered`: Raised when an output guardrail is violated (e.g., invalid tool output). |
23 | | -
|
24 | | -Although this example does not include explicit guardrails, the structure supports adding input/output guardrails to validate user inputs or tool outputs. The `AgentsException` catch block ensures all SDK-related errors are handled gracefully. |
| 18 | +All exceptions are caught via `AgentsException`, the base class for SDK errors. |
25 | 19 | """ |
26 | 20 |
|
27 | | - |
| 21 | +# Define tools |
28 | 22 |
|
29 | 23 | @function_tool |
30 | | -def get_weather(city: str) -> str: |
| 24 | +async def get_weather(city: str) -> str: |
31 | 25 | """Returns weather info for the specified city.""" |
32 | | - return f"The weather in {city} is sunny" |
| 26 | + return f"The weather in {city} is sunny." |
33 | 27 |
|
34 | 28 |
|
35 | 29 | @function_tool |
36 | | -def sum_numbers(a: int, b: int) -> int: |
| 30 | +async def sum_numbers(a: int, b: int) -> str: |
37 | 31 | """Adds two numbers.""" |
38 | | - return a + b |
| 32 | + result = a + b |
| 33 | + return f"The sum of {a} and {b} is {result}." |
39 | 34 |
|
40 | 35 |
|
41 | 36 | agent = Agent( |
42 | 37 | name="Triage Agent", |
43 | | - instructions="Get weather or sum numbers. You can use one tool at a time, switching to another tool in subsequent turns.", |
44 | | - tools=[sum_numbers, get_weather], |
| 38 | + instructions="Get weather or sum numbers. Use only one tool per turn.", |
| 39 | + tools=[get_weather, sum_numbers], |
45 | 40 | ) |
46 | 41 |
|
47 | 42 |
|
48 | 43 | async def main(): |
49 | 44 | try: |
50 | 45 | user_input = input("Enter a message: ") |
51 | | - |
52 | 46 | result = await Runner.run(agent, user_input, max_turns=1) |
53 | | - print(result.final_output) |
| 47 | + print("✅ Final Output:", result.final_output) |
54 | 48 | except AgentsException as e: |
55 | | - print(f"Caught AgentsException: {e}") |
| 49 | + print(f"❌ Caught {e.__class__.__name__}: {e}") |
56 | 50 |
|
57 | 51 |
|
58 | 52 | if __name__ == "__main__": |
|
0 commit comments