forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_agent_config.py
More file actions
168 lines (123 loc) · 4.42 KB
/
test_agent_config.py
File metadata and controls
168 lines (123 loc) · 4.42 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import pytest
from pydantic import BaseModel
from agents import Agent, AgentOutputSchema, Handoff, RunContextWrapper, Runner, handoff
@pytest.mark.asyncio
async def test_system_instructions():
agent = Agent[None](
name="test",
instructions="abc123",
)
context = RunContextWrapper(None)
assert await agent.get_system_prompt(context) == "abc123"
def sync_instructions(agent: Agent[None], context: RunContextWrapper[None]) -> str:
return "sync_123"
agent = agent.clone(instructions=sync_instructions)
assert await agent.get_system_prompt(context) == "sync_123"
async def async_instructions(agent: Agent[None], context: RunContextWrapper[None]) -> str:
return "async_123"
agent = agent.clone(instructions=async_instructions)
assert await agent.get_system_prompt(context) == "async_123"
@pytest.mark.asyncio
async def test_handoff_with_agents():
agent_1 = Agent(
name="agent_1",
)
agent_2 = Agent(
name="agent_2",
)
agent_3 = Agent(
name="agent_3",
handoffs=[agent_1, agent_2],
)
handoffs = Runner._get_handoffs(agent_3)
assert len(handoffs) == 2
assert handoffs[0].agent_name == "agent_1"
assert handoffs[1].agent_name == "agent_2"
first_return = await handoffs[0].on_invoke_handoff(RunContextWrapper(None), "")
assert first_return == agent_1
second_return = await handoffs[1].on_invoke_handoff(RunContextWrapper(None), "")
assert second_return == agent_2
@pytest.mark.asyncio
async def test_handoff_with_handoff_obj():
agent_1 = Agent(
name="agent_1",
)
agent_2 = Agent(
name="agent_2",
)
agent_3 = Agent(
name="agent_3",
handoffs=[
handoff(agent_1),
handoff(
agent_2,
tool_name_override="transfer_to_2",
tool_description_override="description_2",
),
],
)
handoffs = Runner._get_handoffs(agent_3)
assert len(handoffs) == 2
assert handoffs[0].agent_name == "agent_1"
assert handoffs[1].agent_name == "agent_2"
assert handoffs[0].tool_name == Handoff.default_tool_name(agent_1)
assert handoffs[1].tool_name == "transfer_to_2"
assert handoffs[0].tool_description == Handoff.default_tool_description(agent_1)
assert handoffs[1].tool_description == "description_2"
first_return = await handoffs[0].on_invoke_handoff(RunContextWrapper(None), "")
assert first_return == agent_1
second_return = await handoffs[1].on_invoke_handoff(RunContextWrapper(None), "")
assert second_return == agent_2
@pytest.mark.asyncio
async def test_handoff_with_handoff_obj_and_agent():
agent_1 = Agent(
name="agent_1",
)
agent_2 = Agent(
name="agent_2",
)
agent_3 = Agent(
name="agent_3",
handoffs=[handoff(agent_1), agent_2],
)
handoffs = Runner._get_handoffs(agent_3)
assert len(handoffs) == 2
assert handoffs[0].agent_name == "agent_1"
assert handoffs[1].agent_name == "agent_2"
assert handoffs[0].tool_name == Handoff.default_tool_name(agent_1)
assert handoffs[1].tool_name == Handoff.default_tool_name(agent_2)
assert handoffs[0].tool_description == Handoff.default_tool_description(agent_1)
assert handoffs[1].tool_description == Handoff.default_tool_description(agent_2)
first_return = await handoffs[0].on_invoke_handoff(RunContextWrapper(None), "")
assert first_return == agent_1
second_return = await handoffs[1].on_invoke_handoff(RunContextWrapper(None), "")
assert second_return == agent_2
@pytest.mark.asyncio
async def test_agent_cloning():
agent = Agent(
name="test",
handoff_description="test_description",
model="o3-mini",
)
cloned = agent.clone(
handoff_description="new_description",
model="o1",
)
assert cloned.name == "test"
assert cloned.handoff_description == "new_description"
assert cloned.model == "o1"
class Foo(BaseModel):
bar: str
@pytest.mark.asyncio
async def test_agent_final_output():
agent = Agent(
name="test",
output_type=Foo,
)
schema = Runner._get_output_schema(agent)
assert isinstance(schema, AgentOutputSchema)
assert schema is not None
assert schema.output_type == Foo
assert schema.is_strict_json_schema() is True
assert schema.json_schema() is not None
assert not schema.is_plain_text()