chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
# Model provider examples
|
||||
|
||||
The examples in this directory show how to route models through adapter layers such as LiteLLM and
|
||||
any-llm. The default examples all use OpenRouter so you only need one API key:
|
||||
|
||||
```bash
|
||||
export OPENROUTER_API_KEY="..."
|
||||
```
|
||||
|
||||
Run one of the adapter examples:
|
||||
|
||||
```bash
|
||||
uv run examples/model_providers/any_llm_provider.py
|
||||
uv run examples/model_providers/any_llm_auto.py
|
||||
uv run examples/model_providers/litellm_provider.py
|
||||
uv run examples/model_providers/litellm_auto.py
|
||||
```
|
||||
|
||||
Direct-model examples let you override the target model:
|
||||
|
||||
```bash
|
||||
uv run examples/model_providers/any_llm_provider.py --model openrouter/openai/gpt-5.4-mini
|
||||
uv run examples/model_providers/litellm_provider.py --model openrouter/openai/gpt-5.4-mini
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from agents import Agent, ModelSettings, Runner, function_tool, set_tracing_disabled
|
||||
|
||||
"""This example uses the built-in any-llm routing through OpenRouter.
|
||||
|
||||
Set OPENROUTER_API_KEY before running it.
|
||||
"""
|
||||
|
||||
set_tracing_disabled(disabled=True)
|
||||
|
||||
|
||||
@function_tool
|
||||
def get_weather(city: str):
|
||||
print(f"[debug] getting weather for {city}")
|
||||
return f"The weather in {city} is sunny."
|
||||
|
||||
|
||||
class Result(BaseModel):
|
||||
output_text: str
|
||||
tool_results: list[str]
|
||||
|
||||
|
||||
async def main():
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
instructions="You only respond in haikus.",
|
||||
model="any-llm/openrouter/openai/gpt-5.4-mini",
|
||||
tools=[get_weather],
|
||||
model_settings=ModelSettings(tool_choice="required"),
|
||||
output_type=Result,
|
||||
)
|
||||
|
||||
result = await Runner.run(agent, "What's the weather in Tokyo?")
|
||||
print(result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import os
|
||||
|
||||
if os.getenv("OPENROUTER_API_KEY") is None:
|
||||
raise ValueError(
|
||||
"OPENROUTER_API_KEY is not set. Please set the environment variable and try again."
|
||||
)
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,58 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from agents import Agent, Runner, function_tool, set_tracing_disabled
|
||||
from agents.extensions.models.any_llm_model import AnyLLMModel
|
||||
|
||||
"""This example uses the AnyLLMModel directly.
|
||||
|
||||
You can run it like this:
|
||||
uv run examples/model_providers/any_llm_provider.py --model openrouter/openai/gpt-5.4-mini
|
||||
or
|
||||
uv run examples/model_providers/any_llm_provider.py --model openrouter/anthropic/claude-4.5-sonnet
|
||||
"""
|
||||
|
||||
set_tracing_disabled(disabled=True)
|
||||
|
||||
|
||||
@function_tool
|
||||
def get_weather(city: str):
|
||||
print(f"[debug] getting weather for {city}")
|
||||
return f"The weather in {city} is sunny."
|
||||
|
||||
|
||||
async def main(model: str, api_key: str):
|
||||
if api_key == "dummy":
|
||||
print("Skipping run because no valid OPENROUTER_API_KEY was provided.")
|
||||
return
|
||||
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
instructions="You only respond in haikus.",
|
||||
model=AnyLLMModel(model=model, api_key=api_key),
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
result = await Runner.run(agent, "What's the weather in Tokyo?")
|
||||
print(result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--model", type=str, required=False)
|
||||
parser.add_argument("--api-key", type=str, required=False)
|
||||
args = parser.parse_args()
|
||||
|
||||
model = args.model or os.environ.get("ANY_LLM_MODEL", "openrouter/openai/gpt-5.4-mini")
|
||||
api_key = args.api_key or os.environ.get("OPENROUTER_API_KEY", "dummy")
|
||||
|
||||
if not args.model:
|
||||
print(f"Using default model: {model}")
|
||||
if not args.api_key:
|
||||
print("Using OPENROUTER_API_KEY from environment (or dummy placeholder).")
|
||||
|
||||
asyncio.run(main(model, api_key))
|
||||
@@ -0,0 +1,55 @@
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from agents import Agent, OpenAIChatCompletionsModel, Runner, function_tool, set_tracing_disabled
|
||||
|
||||
BASE_URL = os.getenv("EXAMPLE_BASE_URL") or ""
|
||||
API_KEY = os.getenv("EXAMPLE_API_KEY") or ""
|
||||
MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or ""
|
||||
|
||||
if not BASE_URL or not API_KEY or not MODEL_NAME:
|
||||
raise ValueError(
|
||||
"Please set EXAMPLE_BASE_URL, EXAMPLE_API_KEY, EXAMPLE_MODEL_NAME via env var or code."
|
||||
)
|
||||
|
||||
"""This example uses a custom provider for a specific agent. Steps:
|
||||
1. Create a custom OpenAI client.
|
||||
2. Create a `Model` that uses the custom client.
|
||||
3. Set the `model` on the Agent.
|
||||
|
||||
Note that in this example, we disable tracing under the assumption that you don't have an API key
|
||||
from platform.openai.com. If you do have one, you can either set the `OPENAI_API_KEY` env var
|
||||
or call set_tracing_export_api_key() to set a tracing specific key.
|
||||
"""
|
||||
client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)
|
||||
set_tracing_disabled(disabled=True)
|
||||
|
||||
# An alternate approach that would also work:
|
||||
# PROVIDER = OpenAIProvider(openai_client=client)
|
||||
# agent = Agent(..., model="some-custom-model")
|
||||
# Runner.run(agent, ..., run_config=RunConfig(model_provider=PROVIDER))
|
||||
|
||||
|
||||
@function_tool
|
||||
def get_weather(city: str):
|
||||
print(f"[debug] getting weather for {city}")
|
||||
return f"The weather in {city} is sunny."
|
||||
|
||||
|
||||
async def main():
|
||||
# This agent will use the custom LLM provider
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
instructions="You only respond in haikus.",
|
||||
model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client),
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
result = await Runner.run(agent, "What's the weather in Tokyo?")
|
||||
print(result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,63 @@
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from agents import (
|
||||
Agent,
|
||||
Runner,
|
||||
function_tool,
|
||||
set_default_openai_api,
|
||||
set_default_openai_client,
|
||||
set_tracing_disabled,
|
||||
)
|
||||
|
||||
BASE_URL = os.getenv("EXAMPLE_BASE_URL") or ""
|
||||
API_KEY = os.getenv("EXAMPLE_API_KEY") or ""
|
||||
MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or ""
|
||||
|
||||
if not BASE_URL or not API_KEY or not MODEL_NAME:
|
||||
raise ValueError(
|
||||
"Please set EXAMPLE_BASE_URL, EXAMPLE_API_KEY, EXAMPLE_MODEL_NAME via env var or code."
|
||||
)
|
||||
|
||||
|
||||
"""This example uses a custom provider for all requests by default. We do three things:
|
||||
1. Create a custom client.
|
||||
2. Set it as the default OpenAI client, and don't use it for tracing.
|
||||
3. Set the default API as Chat Completions, as most LLM providers don't yet support Responses API.
|
||||
|
||||
Note that in this example, we disable tracing under the assumption that you don't have an API key
|
||||
from platform.openai.com. If you do have one, you can either set the `OPENAI_API_KEY` env var
|
||||
or call set_tracing_export_api_key() to set a tracing specific key.
|
||||
"""
|
||||
|
||||
client = AsyncOpenAI(
|
||||
base_url=BASE_URL,
|
||||
api_key=API_KEY,
|
||||
)
|
||||
set_default_openai_client(client=client, use_for_tracing=False)
|
||||
set_default_openai_api("chat_completions")
|
||||
set_tracing_disabled(disabled=True)
|
||||
|
||||
|
||||
@function_tool
|
||||
def get_weather(city: str):
|
||||
print(f"[debug] getting weather for {city}")
|
||||
return f"The weather in {city} is sunny."
|
||||
|
||||
|
||||
async def main():
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
instructions="You only respond in haikus.",
|
||||
model=MODEL_NAME,
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
result = await Runner.run(agent, "What's the weather in Tokyo?")
|
||||
print(result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,77 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from agents import (
|
||||
Agent,
|
||||
Model,
|
||||
ModelProvider,
|
||||
OpenAIChatCompletionsModel,
|
||||
RunConfig,
|
||||
Runner,
|
||||
function_tool,
|
||||
set_tracing_disabled,
|
||||
)
|
||||
|
||||
BASE_URL = os.getenv("EXAMPLE_BASE_URL") or ""
|
||||
API_KEY = os.getenv("EXAMPLE_API_KEY") or ""
|
||||
MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or ""
|
||||
|
||||
if not BASE_URL or not API_KEY or not MODEL_NAME:
|
||||
raise ValueError(
|
||||
"Please set EXAMPLE_BASE_URL, EXAMPLE_API_KEY, EXAMPLE_MODEL_NAME via env var or code."
|
||||
)
|
||||
|
||||
|
||||
"""This example uses a custom provider for some calls to Runner.run(), and direct calls to OpenAI for
|
||||
others. Steps:
|
||||
1. Create a custom OpenAI client.
|
||||
2. Create a ModelProvider that uses the custom client.
|
||||
3. Use the ModelProvider in calls to Runner.run(), only when we want to use the custom LLM provider.
|
||||
|
||||
Note that in this example, we disable tracing under the assumption that you don't have an API key
|
||||
from platform.openai.com. If you do have one, you can either set the `OPENAI_API_KEY` env var
|
||||
or call set_tracing_export_api_key() to set a tracing specific key.
|
||||
"""
|
||||
client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)
|
||||
set_tracing_disabled(disabled=True)
|
||||
|
||||
|
||||
class CustomModelProvider(ModelProvider):
|
||||
def get_model(self, model_name: str | None) -> Model:
|
||||
return OpenAIChatCompletionsModel(model=model_name or MODEL_NAME, openai_client=client)
|
||||
|
||||
|
||||
CUSTOM_MODEL_PROVIDER = CustomModelProvider()
|
||||
|
||||
|
||||
@function_tool
|
||||
def get_weather(city: str):
|
||||
print(f"[debug] getting weather for {city}")
|
||||
return f"The weather in {city} is sunny."
|
||||
|
||||
|
||||
async def main():
|
||||
agent = Agent(name="Assistant", instructions="You only respond in haikus.", tools=[get_weather])
|
||||
|
||||
# This will use the custom model provider
|
||||
result = await Runner.run(
|
||||
agent,
|
||||
"What's the weather in Tokyo?",
|
||||
run_config=RunConfig(model_provider=CUSTOM_MODEL_PROVIDER),
|
||||
)
|
||||
print(result.final_output)
|
||||
|
||||
# If you uncomment this, it will use OpenAI directly, not the custom provider
|
||||
# result = await Runner.run(
|
||||
# agent,
|
||||
# "What's the weather in Tokyo?",
|
||||
# )
|
||||
# print(result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,54 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from agents import Agent, ModelSettings, Runner, function_tool, set_tracing_disabled
|
||||
|
||||
"""This example uses the built-in support for LiteLLM through OpenRouter.
|
||||
|
||||
Set OPENROUTER_API_KEY before running it.
|
||||
"""
|
||||
|
||||
set_tracing_disabled(disabled=True)
|
||||
|
||||
# import logging
|
||||
# logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
|
||||
@function_tool
|
||||
def get_weather(city: str):
|
||||
print(f"[debug] getting weather for {city}")
|
||||
return f"The weather in {city} is sunny."
|
||||
|
||||
|
||||
class Result(BaseModel):
|
||||
output_text: str
|
||||
tool_results: list[str]
|
||||
|
||||
|
||||
async def main():
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
instructions="You only respond in haikus.",
|
||||
# We prefix with litellm/ to tell the Runner to use the LitellmModel
|
||||
model="litellm/openrouter/openai/gpt-5.4-mini",
|
||||
tools=[get_weather],
|
||||
model_settings=ModelSettings(tool_choice="required"),
|
||||
output_type=Result,
|
||||
)
|
||||
|
||||
result = await Runner.run(agent, "What's the weather in Tokyo?")
|
||||
print(result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import os
|
||||
|
||||
if os.getenv("OPENROUTER_API_KEY") is None:
|
||||
raise ValueError(
|
||||
"OPENROUTER_API_KEY is not set. Please set the environment variable and try again."
|
||||
)
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,59 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from agents import Agent, Runner, function_tool, set_tracing_disabled
|
||||
from agents.extensions.models.litellm_model import LitellmModel
|
||||
|
||||
"""This example uses the LitellmModel directly, to hit any model provider.
|
||||
You can run it like this:
|
||||
uv run examples/model_providers/litellm_provider.py --model openrouter/openai/gpt-5.4-mini
|
||||
or
|
||||
uv run examples/model_providers/litellm_provider.py --model openrouter/anthropic/claude-4.5-sonnet
|
||||
|
||||
Find more providers here: https://docs.litellm.ai/docs/providers
|
||||
"""
|
||||
|
||||
set_tracing_disabled(disabled=True)
|
||||
|
||||
|
||||
@function_tool
|
||||
def get_weather(city: str):
|
||||
print(f"[debug] getting weather for {city}")
|
||||
return f"The weather in {city} is sunny."
|
||||
|
||||
|
||||
async def main(model: str, api_key: str):
|
||||
if api_key == "dummy":
|
||||
print("Skipping run because no valid OPENROUTER_API_KEY was provided.")
|
||||
return
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
instructions="You only respond in haikus.",
|
||||
model=LitellmModel(model=model, api_key=api_key),
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
result = await Runner.run(agent, "What's the weather in Tokyo?")
|
||||
print(result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Prefer non-interactive defaults in auto mode to avoid blocking.
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--model", type=str, required=False)
|
||||
parser.add_argument("--api-key", type=str, required=False)
|
||||
args = parser.parse_args()
|
||||
|
||||
model = args.model or os.environ.get("LITELLM_MODEL", "openrouter/openai/gpt-5.4-mini")
|
||||
api_key = args.api_key or os.environ.get("OPENROUTER_API_KEY", "dummy")
|
||||
|
||||
if not args.model:
|
||||
print(f"Using default model: {model}")
|
||||
if not args.api_key:
|
||||
print("Using OPENROUTER_API_KEY from environment (or dummy placeholder).")
|
||||
|
||||
asyncio.run(main(model, api_key))
|
||||
Reference in New Issue
Block a user