Skip to content

Commit e069279

Browse files
authored
Update custom models to use tools (#144)
2 parents 691be07 + 6ab8c91 commit e069279

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

examples/model_providers/custom_example_agent.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from openai import AsyncOpenAI
55

6-
from agents import Agent, OpenAIChatCompletionsModel, Runner, set_tracing_disabled
6+
from agents import Agent, OpenAIChatCompletionsModel, Runner, function_tool, set_tracing_disabled
77

88
BASE_URL = os.getenv("EXAMPLE_BASE_URL") or ""
99
API_KEY = os.getenv("EXAMPLE_API_KEY") or ""
@@ -32,18 +32,22 @@
3232
# Runner.run(agent, ..., run_config=RunConfig(model_provider=PROVIDER))
3333

3434

35+
@function_tool
36+
def get_weather(city: str):
37+
print(f"[debug] getting weather for {city}")
38+
return f"The weather in {city} is sunny."
39+
40+
3541
async def main():
3642
# This agent will use the custom LLM provider
3743
agent = Agent(
3844
name="Assistant",
3945
instructions="You only respond in haikus.",
4046
model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client),
47+
tools=[get_weather],
4148
)
4249

43-
result = await Runner.run(
44-
agent,
45-
"Tell me about recursion in programming.",
46-
)
50+
result = await Runner.run(agent, "What's the weather in Tokyo?")
4751
print(result.final_output)
4852

4953

examples/model_providers/custom_example_global.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from agents import (
77
Agent,
88
Runner,
9+
function_tool,
910
set_default_openai_api,
1011
set_default_openai_client,
1112
set_tracing_disabled,
@@ -40,14 +41,21 @@
4041
set_tracing_disabled(disabled=True)
4142

4243

44+
@function_tool
45+
def get_weather(city: str):
46+
print(f"[debug] getting weather for {city}")
47+
return f"The weather in {city} is sunny."
48+
49+
4350
async def main():
4451
agent = Agent(
4552
name="Assistant",
4653
instructions="You only respond in haikus.",
4754
model=MODEL_NAME,
55+
tools=[get_weather],
4856
)
4957

50-
result = await Runner.run(agent, "Tell me about recursion in programming.")
58+
result = await Runner.run(agent, "What's the weather in Tokyo?")
5159
print(result.final_output)
5260

5361

examples/model_providers/custom_example_provider.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
OpenAIChatCompletionsModel,
1313
RunConfig,
1414
Runner,
15+
function_tool,
1516
set_tracing_disabled,
1617
)
1718

@@ -47,24 +48,27 @@ def get_model(self, model_name: str | None) -> Model:
4748
CUSTOM_MODEL_PROVIDER = CustomModelProvider()
4849

4950

51+
@function_tool
52+
def get_weather(city: str):
53+
print(f"[debug] getting weather for {city}")
54+
return f"The weather in {city} is sunny."
55+
56+
5057
async def main():
51-
agent = Agent(
52-
name="Assistant",
53-
instructions="You only respond in haikus.",
54-
)
58+
agent = Agent(name="Assistant", instructions="You only respond in haikus.", tools=[get_weather])
5559

5660
# This will use the custom model provider
5761
result = await Runner.run(
5862
agent,
59-
"Tell me about recursion in programming.",
63+
"What's the weather in Tokyo?",
6064
run_config=RunConfig(model_provider=CUSTOM_MODEL_PROVIDER),
6165
)
6266
print(result.final_output)
6367

6468
# If you uncomment this, it will use OpenAI directly, not the custom provider
6569
# result = await Runner.run(
6670
# agent,
67-
# "Tell me about recursion in programming.",
71+
# "What's the weather in Tokyo?",
6872
# )
6973
# print(result.final_output)
7074

uv.lock

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)