|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +from agents import Agent, Runner, function_tool, set_tracing_disabled |
| 6 | +from agents.extensions.models.litellm_model import LitellmModel |
| 7 | + |
| 8 | +"""This example uses the LitellmModel directly, to hit any model provider. |
| 9 | +You can run it like this: |
| 10 | +uv run examples/model_providers/litellm_provider.py --model anthropic/claude-3-5-sonnet-20240620 |
| 11 | +or |
| 12 | +uv run examples/model_providers/litellm_provider.py --model gemini/gemini-2.0-flash |
| 13 | +
|
| 14 | +Find more providers here: https://docs.litellm.ai/docs/providers |
| 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 | + agent = Agent( |
| 28 | + name="Assistant", |
| 29 | + instructions="You only respond in haikus.", |
| 30 | + model=LitellmModel(model=model, api_key=api_key), |
| 31 | + tools=[get_weather], |
| 32 | + ) |
| 33 | + |
| 34 | + result = await Runner.run(agent, "What's the weather in Tokyo?") |
| 35 | + print(result.final_output) |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == "__main__": |
| 39 | + # First try to get model/api key from args |
| 40 | + import argparse |
| 41 | + |
| 42 | + parser = argparse.ArgumentParser() |
| 43 | + parser.add_argument("--model", type=str, required=False) |
| 44 | + parser.add_argument("--api-key", type=str, required=False) |
| 45 | + args = parser.parse_args() |
| 46 | + |
| 47 | + model = args.model |
| 48 | + if not model: |
| 49 | + model = input("Enter a model name for Litellm: ") |
| 50 | + |
| 51 | + api_key = args.api_key |
| 52 | + if not api_key: |
| 53 | + api_key = input("Enter an API key for Litellm: ") |
| 54 | + |
| 55 | + asyncio.run(main(model, api_key)) |
0 commit comments