Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update custom models to use tools #144

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions examples/model_providers/custom_example_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from openai import AsyncOpenAI

from agents import Agent, OpenAIChatCompletionsModel, Runner, set_tracing_disabled
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 ""
Expand Down Expand Up @@ -32,18 +32,22 @@
# 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,
"Tell me about recursion in programming.",
)
result = await Runner.run(agent, "What's the weather in Tokyo?")
print(result.final_output)


Expand Down
10 changes: 9 additions & 1 deletion examples/model_providers/custom_example_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from agents import (
Agent,
Runner,
function_tool,
set_default_openai_api,
set_default_openai_client,
set_tracing_disabled,
Expand Down Expand Up @@ -40,14 +41,21 @@
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, "Tell me about recursion in programming.")
result = await Runner.run(agent, "What's the weather in Tokyo?")
print(result.final_output)


Expand Down
16 changes: 10 additions & 6 deletions examples/model_providers/custom_example_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
OpenAIChatCompletionsModel,
RunConfig,
Runner,
function_tool,
set_tracing_disabled,
)

Expand Down Expand Up @@ -47,24 +48,27 @@ def get_model(self, model_name: str | None) -> Model:
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.",
)
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,
"Tell me about recursion in programming.",
"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,
# "Tell me about recursion in programming.",
# "What's the weather in Tokyo?",
# )
# print(result.final_output)

Expand Down
3 changes: 1 addition & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.