-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopenai_sdk_example.py
More file actions
99 lines (74 loc) · 3.08 KB
/
openai_sdk_example.py
File metadata and controls
99 lines (74 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Example: OpenAI Agents SDK agent with agentopt.
Prerequisites:
1. pip install openai-agents agentopt-py
2. Set OPENAI_API_KEY environment variable
"""
from dotenv import load_dotenv
load_dotenv()
from agents import Agent, Runner, function_tool
from agentopt import ModelSelector
@function_tool
def search(query: str) -> str:
"""Search for information about a topic."""
return f"Search results for: {query}"
# ---------------------------------------------------------------------------
# Step 1: Define your agent class.
# __init__(models) receives a dict like {"planner": "gpt-4o", "solver": "gpt-4o-mini"}.
# run(input_data) runs the agent on a single datapoint and returns the output.
# ---------------------------------------------------------------------------
class MyAgent:
"""OpenAI Agents SDK planner+solver agent pair."""
def __init__(self, models):
planner = Agent(
name="Planner",
model=models["planner"],
instructions="Create a brief plan to answer the question. Be concise.",
tools=[search],
)
solver = Agent(
name="Solver",
model=models["solver"],
instructions="Given a plan, produce a concise final answer.",
handoffs=[],
)
self.planner = planner.clone(handoffs=[solver])
def run(self, input_data):
result = Runner.run_sync(self.planner, input_data)
return result.final_output
# ---------------------------------------------------------------------------
# Step 2: Evaluation dataset — (input_data, expected_output) pairs.
# ---------------------------------------------------------------------------
dataset = [
("What is the capital of France?", "Paris"),
("What is 2 + 2?", "4"),
("What color is the sky on a clear day?", "blue"),
("What is the largest planet in our solar system?", "Jupiter"),
("What is H2O commonly known as?", "water"),
]
# ---------------------------------------------------------------------------
# Step 3: Evaluation function — score agent output against expected answer.
# ---------------------------------------------------------------------------
def eval_fn(expected, actual):
return 1.0 if expected.lower() in str(actual).lower() else 0.0
# ---------------------------------------------------------------------------
# Step 4: Run model selection.
# Map each agent step to candidate models. AgentOpt evaluates all combinations.
# ---------------------------------------------------------------------------
if __name__ == "__main__":
selector = ModelSelector(
agent=MyAgent,
models={
"planner": ["gpt-4o", "gpt-4o-mini", "gpt-4.1-nano"],
"solver": ["gpt-4o", "gpt-4o-mini", "gpt-4.1-nano"],
},
eval_fn=eval_fn,
dataset=dataset,
method="brute_force", # or "auto" for smarter selection algorithms
)
results = selector.select_best(parallel=True)
results.print_summary()
results.plot_pareto()
best = results.get_best_combo()
if best:
print(f"\nBest combination: {best}")