-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtest_workflow.py
188 lines (167 loc) · 6.02 KB
/
test_workflow.py
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from __future__ import annotations
import json
from collections.abc import AsyncIterator
import pytest
from inline_snapshot import snapshot
from openai.types.responses import ResponseCompletedEvent
from openai.types.responses.response_text_delta_event import ResponseTextDeltaEvent
from agents import Agent, Model, ModelSettings, ModelTracing, Tool
from agents.agent_output import AgentOutputSchema
from agents.handoffs import Handoff
from agents.items import (
ModelResponse,
TResponseInputItem,
TResponseOutputItem,
TResponseStreamEvent,
)
try:
from agents.voice import SingleAgentVoiceWorkflow
from ..fake_model import get_response_obj
from ..test_responses import get_function_tool, get_function_tool_call, get_text_message
except ImportError:
pass
class FakeStreamingModel(Model):
def __init__(self):
self.turn_outputs: list[list[TResponseOutputItem]] = []
def set_next_output(self, output: list[TResponseOutputItem]):
self.turn_outputs.append(output)
def add_multiple_turn_outputs(self, outputs: list[list[TResponseOutputItem]]):
self.turn_outputs.extend(outputs)
def get_next_output(self) -> list[TResponseOutputItem]:
if not self.turn_outputs:
return []
return self.turn_outputs.pop(0)
async def get_response(
self,
system_instructions: str | None,
input: str | list[TResponseInputItem],
model_settings: ModelSettings,
tools: list[Tool],
output_schema: AgentOutputSchema | None,
handoffs: list[Handoff],
tracing: ModelTracing,
) -> ModelResponse:
raise NotImplementedError("Not implemented")
async def stream_response(
self,
system_instructions: str | None,
input: str | list[TResponseInputItem],
model_settings: ModelSettings,
tools: list[Tool],
output_schema: AgentOutputSchema | None,
handoffs: list[Handoff],
tracing: ModelTracing,
) -> AsyncIterator[TResponseStreamEvent]:
output = self.get_next_output()
for item in output:
if (
item.type == "message"
and len(item.content) == 1
and item.content[0].type == "output_text"
):
yield ResponseTextDeltaEvent(
content_index=0,
delta=item.content[0].text,
type="response.output_text.delta",
output_index=0,
item_id=item.id,
)
yield ResponseCompletedEvent(
type="response.completed",
response=get_response_obj(output),
)
@pytest.mark.asyncio
async def test_single_agent_workflow(monkeypatch) -> None:
model = FakeStreamingModel()
model.add_multiple_turn_outputs(
[
# First turn: a message and a tool call
[
get_function_tool_call("some_function", json.dumps({"a": "b"})),
get_text_message("a_message"),
],
# Second turn: text message
[get_text_message("done")],
]
)
agent = Agent(
"initial_agent",
model=model,
tools=[get_function_tool("some_function", "tool_result")],
)
workflow = SingleAgentVoiceWorkflow(agent)
output = []
async for chunk in workflow.run("transcription_1"):
output.append(chunk)
# Validate that the text yielded matches our fake events
assert output == ["a_message", "done"]
# Validate that internal state was updated
assert workflow._input_history == snapshot(
[
{"content": "transcription_1", "role": "user"},
{
"arguments": '{"a": "b"}',
"call_id": "2",
"name": "some_function",
"type": "function_call",
"id": "1",
},
{
"id": "1",
"content": [{"annotations": [], "text": "a_message", "type": "output_text"}],
"role": "assistant",
"status": "completed",
"type": "message",
},
{"call_id": "2", "output": "tool_result", "type": "function_call_output"},
{
"id": "1",
"content": [{"annotations": [], "text": "done", "type": "output_text"}],
"role": "assistant",
"status": "completed",
"type": "message",
},
]
)
assert workflow._current_agent == agent
model.set_next_output([get_text_message("done_2")])
# Run it again with a new transcription to make sure the input history is updated
output = []
async for chunk in workflow.run("transcription_2"):
output.append(chunk)
assert workflow._input_history == snapshot(
[
{"role": "user", "content": "transcription_1"},
{
"arguments": '{"a": "b"}',
"call_id": "2",
"name": "some_function",
"type": "function_call",
"id": "1",
},
{
"id": "1",
"content": [{"annotations": [], "text": "a_message", "type": "output_text"}],
"role": "assistant",
"status": "completed",
"type": "message",
},
{"call_id": "2", "output": "tool_result", "type": "function_call_output"},
{
"id": "1",
"content": [{"annotations": [], "text": "done", "type": "output_text"}],
"role": "assistant",
"status": "completed",
"type": "message",
},
{"role": "user", "content": "transcription_2"},
{
"id": "1",
"content": [{"annotations": [], "text": "done_2", "type": "output_text"}],
"role": "assistant",
"status": "completed",
"type": "message",
},
]
)
assert workflow._current_agent == agent