|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import os |
| 5 | + |
| 6 | +from agents import Agent, Runner, function_tool, set_tracing_disabled |
| 7 | +from agents.extensions.models.any_llm_model import AnyLLMModel |
| 8 | + |
| 9 | +"""This example uses the AnyLLMModel directly. |
| 10 | +
|
| 11 | +You can run it like this: |
| 12 | +uv run examples/model_providers/any_llm_provider.py --model openrouter/openai/gpt-5.4-mini |
| 13 | +or |
| 14 | +uv run examples/model_providers/any_llm_provider.py --model openrouter/anthropic/claude-4.5-sonnet |
| 15 | +""" |
| 16 | + |
| 17 | +set_tracing_disabled(disabled=True) |
| 18 | + |
| 19 | + |
| 20 | +@function_tool |
| 21 | +def get_weather(city: str): |
| 22 | + print(f"[debug] getting weather for {city}") |
| 23 | + return f"The weather in {city} is sunny." |
| 24 | + |
| 25 | + |
| 26 | +async def main(model: str, api_key: str): |
| 27 | + if api_key == "dummy": |
| 28 | + print("Skipping run because no valid OPENROUTER_API_KEY was provided.") |
| 29 | + return |
| 30 | + |
| 31 | + agent = Agent( |
| 32 | + name="Assistant", |
| 33 | + instructions="You only respond in haikus.", |
| 34 | + model=AnyLLMModel(model=model, api_key=api_key), |
| 35 | + tools=[get_weather], |
| 36 | + ) |
| 37 | + |
| 38 | + result = await Runner.run(agent, "What's the weather in Tokyo?") |
| 39 | + print(result.final_output) |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + import argparse |
| 44 | + |
| 45 | + parser = argparse.ArgumentParser() |
| 46 | + parser.add_argument("--model", type=str, required=False) |
| 47 | + parser.add_argument("--api-key", type=str, required=False) |
| 48 | + args = parser.parse_args() |
| 49 | + |
| 50 | + model = args.model or os.environ.get("ANY_LLM_MODEL", "openrouter/openai/gpt-5.4-mini") |
| 51 | + api_key = args.api_key or os.environ.get("OPENROUTER_API_KEY", "dummy") |
| 52 | + |
| 53 | + if not args.model: |
| 54 | + print(f"Using default model: {model}") |
| 55 | + if not args.api_key: |
| 56 | + print("Using OPENROUTER_API_KEY from environment (or dummy placeholder).") |
| 57 | + |
| 58 | + asyncio.run(main(model, api_key)) |
0 commit comments