-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path_test_agno_helpers.py
More file actions
310 lines (222 loc) · 9.36 KB
/
_test_agno_helpers.py
File metadata and controls
310 lines (222 loc) · 9.36 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# pyright: reportMissingParameterType=false
# pyright: reportUnknownMemberType=false
# pyright: reportUnknownParameterType=false
# pyright: reportUnknownVariableType=false
from inspect import isawaitable
from braintrust.integrations.agno.patchers import wrap_agent
PROJECT_NAME = "test-agno-app"
class FakeMetrics:
def __init__(self):
self.input_tokens = 1
self.output_tokens = 2
self.total_tokens = 3
self.duration = 0.1
self.time_to_first_token = 0.01
class FakeRunOutput:
def __init__(self, content: str):
self.content = content
self.status = "COMPLETED"
self.model = "fake-model"
self.model_provider = "FakeProvider"
self.metrics = FakeMetrics()
class FakeEvent:
def __init__(self, event: str, **kwargs):
self.event = event
for k, v in kwargs.items():
setattr(self, k, v)
class FakeExecutionInput:
def __init__(self, input):
self.input = input
self.kind = "workflow-execution"
class FakeWorkflowRunResponse:
def __init__(self, input=None, content: str | None = None):
self.input = input
self.content = content
self.status = "COMPLETED"
self.metrics = FakeMetrics()
def make_fake_workflow(name: str):
class FakeWorkflow:
def __init__(self):
self.name = name
self.steps = ["first-step"]
async def _aexecute(self, session_id, user_id, execution_input, workflow_run_response, _run_context=None):
return FakeWorkflowRunResponse(input=execution_input.input, content="workflow-async")
def _execute_stream(self, session, execution_input, workflow_run_response, _run_context=None):
yield FakeEvent("WorkflowStarted", content=None)
yield FakeEvent("StepStarted", content=None)
yield FakeEvent("StepCompleted", content="hello ")
yield FakeEvent("WorkflowCompleted", content="world", metrics=FakeMetrics(), status="COMPLETED")
return FakeWorkflow
def make_fake_duplicate_content_workflow(name: str):
class FakeWorkflow:
def __init__(self):
self.name = name
self.steps = ["first-step"]
def _execute_stream(self, session, execution_input, workflow_run_response, _run_context=None):
yield FakeEvent("StepCompleted", content="hello")
yield FakeEvent("WorkflowCompleted", content="hello", metrics=FakeMetrics(), status="COMPLETED")
return FakeWorkflow
def make_fake_streaming_workflow_with_mutated_run_response(name: str):
class FakeWorkflow:
def __init__(self):
self.name = name
self.steps = ["first-step"]
def _execute_stream(self, session, execution_input, workflow_run_response, _run_context=None):
yield FakeEvent("WorkflowStarted", content=None)
yield FakeEvent("StepCompleted", content="hello ")
workflow_run_response.content = "world"
workflow_run_response.status = "FAILED"
workflow_run_response.metrics = FakeMetrics()
yield FakeEvent("WorkflowCompleted", content="world")
return FakeWorkflow
def make_fake_workflow_with_async_agent(name: str, agent_name: str):
class FakeAgent:
def __init__(self):
self.name = agent_name
async def arun(self, input, stream=False, **kwargs):
return FakeRunOutput(f"{input}-async")
WrappedAgent = wrap_agent(FakeAgent)
class FakeWorkflow:
def __init__(self):
self.name = name
self.id = "workflow-123"
self.steps = ["agent-step"]
self.agent = WrappedAgent()
async def _aexecute(self, session_id, user_id, execution_input, workflow_run_response, _run_context=None):
return await self.agent.arun(execution_input.input)
return FakeWorkflow
def make_fake_workflow_agent_path(name: str):
class FakeWorkflow:
def __init__(self):
self.name = name
self.id = "workflow-agent-123"
self.steps = ["agent-step"]
def _execute_workflow_agent(self, user_input, session, execution_input, _run_context, stream=False, **kwargs):
if stream:
def _stream():
yield FakeEvent("WorkflowStarted")
yield FakeEvent(
"WorkflowCompleted",
content=f"{user_input}-sync-stream",
metrics=FakeMetrics(),
status="COMPLETED",
)
return _stream()
return FakeRunOutput(f"{user_input}-sync")
async def _aexecute_workflow_agent(self, user_input, _run_context, execution_input, stream=False, **kwargs):
if stream:
async def _astream():
yield FakeEvent("WorkflowStarted")
yield FakeEvent(
"WorkflowCompleted",
content=f"{user_input}-async-stream",
metrics=FakeMetrics(),
status="COMPLETED",
)
return _astream()
return FakeRunOutput(f"{user_input}-async")
return FakeWorkflow
def make_fake_component(name: str):
class FakeComponent:
def __init__(self):
self.name = name
def run(self, input, stream=False, **kwargs):
if stream:
def _stream():
yield FakeEvent("RunStarted", model="fake-model", model_provider="FakeProvider")
yield FakeEvent("RunContent", content=f"{input}-sync")
yield FakeEvent("RunCompleted", metrics=FakeMetrics())
return _stream()
return FakeRunOutput(f"{input}-sync")
def arun(self, input, stream=False, **kwargs):
if stream:
async def _astream():
yield FakeEvent("RunStarted", model="fake-model", model_provider="FakeProvider")
yield FakeEvent("RunContent", content=f"{input}-async")
yield FakeEvent("RunCompleted", metrics=FakeMetrics())
return _astream()
async def _result():
return FakeRunOutput(f"{input}-async")
return _result()
return FakeComponent
def make_fake_async_dispatch_component(name: str):
class FakeComponent:
def __init__(self):
self.name = name
async def arun(self, input, stream=False, **kwargs):
if stream:
async def _astream():
yield FakeEvent("RunStarted", model="fake-model", model_provider="FakeProvider")
yield FakeEvent("RunContent", content=f"{input}-awaited-async")
yield FakeEvent("RunCompleted", metrics=FakeMetrics())
return _astream()
return {"content": f"{input}-awaited-async"}
return FakeComponent
def make_fake_error_component(name: str):
class FakeComponent:
def __init__(self):
self.name = name
def run(self, input, stream=False, **kwargs):
if stream:
def _stream():
yield FakeEvent("RunStarted", model="fake-model", model_provider="FakeProvider")
raise RuntimeError("sync-stream-error")
return _stream()
return FakeRunOutput(f"{input}-sync")
def arun(self, input, stream=False, **kwargs):
if stream:
async def _astream():
yield FakeEvent("RunStarted", model="fake-model", model_provider="FakeProvider")
raise RuntimeError("async-stream-error")
return _astream()
async def _result():
return FakeRunOutput(f"{input}-async")
return _result()
return FakeComponent
def make_fake_private_public_component(name: str):
class FakeComponent:
def __init__(self):
self.name = name
self.calls = []
def _run(self, run_response=None, run_messages=None, **kwargs):
self.calls.append("_run")
return FakeRunOutput("private-run")
def run(self, input, **kwargs):
self.calls.append("run")
return FakeRunOutput("public-run")
async def _arun(self, run_response=None, input=None, **kwargs):
self.calls.append("_arun")
return FakeRunOutput("private-arun")
def arun(self, input, **kwargs):
self.calls.append("arun")
async def _result():
return FakeRunOutput("public-arun")
return _result()
return FakeComponent
class StrictSpan:
def __init__(self):
self.ended = False
def set_current(self):
return None
def unset_current(self):
return None
def log(self, **kwargs):
if self.ended:
raise AssertionError("log called after span.end()")
def end(self):
self.ended = True
__all__ = [
"FakeExecutionInput",
"FakeWorkflowRunResponse",
"PROJECT_NAME",
"StrictSpan",
"isawaitable",
"make_fake_async_dispatch_component",
"make_fake_component",
"make_fake_workflow_agent_path",
"make_fake_workflow_with_async_agent",
"make_fake_duplicate_content_workflow",
"make_fake_error_component",
"make_fake_private_public_component",
"make_fake_workflow",
]