|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from typing import Any, Literal |
| 5 | + |
| 6 | +from pydantic import BaseModel |
| 7 | + |
| 8 | +from agents import ( |
| 9 | + Agent, |
| 10 | + FunctionToolResult, |
| 11 | + ModelSettings, |
| 12 | + RunContextWrapper, |
| 13 | + Runner, |
| 14 | + ToolsToFinalOutputFunction, |
| 15 | + ToolsToFinalOutputResult, |
| 16 | + function_tool, |
| 17 | +) |
| 18 | + |
| 19 | +""" |
| 20 | +This example shows how to force the agent to use a tool. It uses `ModelSettings(tool_choice="required")` |
| 21 | +to force the agent to use any tool. |
| 22 | +
|
| 23 | +You can run it with 3 options: |
| 24 | +1. `default`: The default behavior, which is to send the tool output to the LLM. In this case, |
| 25 | + `tool_choice` is not set, because otherwise it would result in an infinite loop - the LLM would |
| 26 | + call the tool, the tool would run and send the results to the LLM, and that would repeat |
| 27 | + (because the model is forced to use a tool every time.) |
| 28 | +2. `first_tool_result`: The first tool result is used as the final output. |
| 29 | +3. `custom`: A custom tool use behavior function is used. The custom function receives all the tool |
| 30 | + results, and chooses to use the first tool result to generate the final output. |
| 31 | +
|
| 32 | +Usage: |
| 33 | +python examples/agent_patterns/forcing_tool_use.py -t default |
| 34 | +python examples/agent_patterns/forcing_tool_use.py -t first_tool |
| 35 | +python examples/agent_patterns/forcing_tool_use.py -t custom |
| 36 | +""" |
| 37 | + |
| 38 | + |
| 39 | +class Weather(BaseModel): |
| 40 | + city: str |
| 41 | + temperature_range: str |
| 42 | + conditions: str |
| 43 | + |
| 44 | + |
| 45 | +@function_tool |
| 46 | +def get_weather(city: str) -> Weather: |
| 47 | + print("[debug] get_weather called") |
| 48 | + return Weather(city=city, temperature_range="14-20C", conditions="Sunny with wind") |
| 49 | + |
| 50 | + |
| 51 | +async def custom_tool_use_behavior( |
| 52 | + context: RunContextWrapper[Any], results: list[FunctionToolResult] |
| 53 | +) -> ToolsToFinalOutputResult: |
| 54 | + weather: Weather = results[0].output |
| 55 | + return ToolsToFinalOutputResult( |
| 56 | + is_final_output=True, final_output=f"{weather.city} is {weather.conditions}." |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +async def main(tool_use_behavior: Literal["default", "first_tool", "custom"] = "default"): |
| 61 | + if tool_use_behavior == "default": |
| 62 | + behavior: Literal["run_llm_again", "stop_on_first_tool"] | ToolsToFinalOutputFunction = ( |
| 63 | + "run_llm_again" |
| 64 | + ) |
| 65 | + elif tool_use_behavior == "first_tool": |
| 66 | + behavior = "stop_on_first_tool" |
| 67 | + elif tool_use_behavior == "custom": |
| 68 | + behavior = custom_tool_use_behavior |
| 69 | + |
| 70 | + agent = Agent( |
| 71 | + name="Weather agent", |
| 72 | + instructions="You are a helpful agent.", |
| 73 | + tools=[get_weather], |
| 74 | + tool_use_behavior=behavior, |
| 75 | + model_settings=ModelSettings( |
| 76 | + tool_choice="required" if tool_use_behavior != "default" else None |
| 77 | + ), |
| 78 | + ) |
| 79 | + |
| 80 | + result = await Runner.run(agent, input="What's the weather in Tokyo?") |
| 81 | + print(result.final_output) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + import argparse |
| 86 | + |
| 87 | + parser = argparse.ArgumentParser() |
| 88 | + parser.add_argument( |
| 89 | + "-t", |
| 90 | + "--tool-use-behavior", |
| 91 | + type=str, |
| 92 | + required=True, |
| 93 | + choices=["default", "first_tool", "custom"], |
| 94 | + help="The behavior to use for tool use. Default will cause tool outputs to be sent to the model. " |
| 95 | + "first_tool_result will cause the first tool result to be used as the final output. " |
| 96 | + "custom will use a custom tool use behavior function.", |
| 97 | + ) |
| 98 | + args = parser.parse_args() |
| 99 | + asyncio.run(main(args.tool_use_behavior)) |
0 commit comments