-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_evaluate.py
More file actions
294 lines (255 loc) · 9.83 KB
/
Copy pathtest_evaluate.py
File metadata and controls
294 lines (255 loc) · 9.83 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
"""Tests for evaluation framework: EvalTask, EvalResult, BenchmarkResults, run_benchmark."""
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock
import pytest
from hawk.evaluate import BenchmarkResults, EvalResult, EvalTask, run_benchmark, run_benchmark_async
class TestEvalTask:
"""Tests for EvalTask dataclass."""
def test_defaults(self) -> None:
task = EvalTask(name="t1", prompt="What is 2+2?")
assert task.name == "t1"
assert task.prompt == "What is 2+2?"
assert task.category == "general"
assert task.expected_tools == []
assert task.validate is None
assert task.max_turns == 10
def test_custom_values(self) -> None:
task = EvalTask(
name="weather",
prompt="What's the weather?",
category="tools",
expected_tools=["get_weather"],
validate=lambda r: "sunny" in r.response,
max_turns=5,
)
assert task.category == "tools"
assert task.expected_tools == ["get_weather"]
assert task.max_turns == 5
class TestEvalResult:
"""Tests for EvalResult dataclass."""
def test_defaults(self) -> None:
result = EvalResult(task_name="t1", success=True, duration_ms=100.0)
assert result.tokens_in == 0
assert result.tokens_out == 0
assert result.turns_taken == 0
assert result.error is None
def test_with_error(self) -> None:
result = EvalResult(task_name="t1", success=False, duration_ms=50.0, error="timeout")
assert result.success is False
assert result.error == "timeout"
class TestBenchmarkResults:
"""Tests for BenchmarkResults aggregation."""
def test_empty(self) -> None:
br = BenchmarkResults()
assert br.total_tasks == 0
assert br.passed == 0
assert br.failed == 0
assert br.pass_rate == 0.0
assert br.avg_duration_ms == 0.0
assert br.total_tokens == 0
def test_all_passed(self) -> None:
br = BenchmarkResults(
results=[
EvalResult(
task_name="t1", success=True, duration_ms=100.0, tokens_in=10, tokens_out=5
),
EvalResult(
task_name="t2", success=True, duration_ms=200.0, tokens_in=20, tokens_out=10
),
]
)
assert br.total_tasks == 2
assert br.passed == 2
assert br.failed == 0
assert br.pass_rate == 1.0
assert br.avg_duration_ms == 150.0
assert br.total_tokens == 45
def test_mixed_results(self) -> None:
br = BenchmarkResults(
results=[
EvalResult(task_name="t1", success=True, duration_ms=100.0),
EvalResult(task_name="t2", success=False, duration_ms=200.0, error="fail"),
]
)
assert br.passed == 1
assert br.failed == 1
assert br.pass_rate == 0.5
def test_by_category(self) -> None:
# by_category splits on "/" — names without "/" get "general"
br = BenchmarkResults(
results=[
EvalResult(task_name="math/add", success=True, duration_ms=100.0),
EvalResult(task_name="math/mul", success=True, duration_ms=100.0),
EvalResult(task_name="general/weather", success=False, duration_ms=100.0),
]
)
cats = br.by_category()
assert len(cats["math"]) == 2
assert len(cats["general"]) == 1
def test_summary(self) -> None:
br = BenchmarkResults(
results=[
EvalResult(
task_name="t1", success=True, duration_ms=100.0, tokens_in=10, tokens_out=5
),
]
)
summary = br.summary()
assert "1/1 passed" in summary
assert "100ms" in summary
assert "15" in summary # total tokens
def test_summary_with_failures(self) -> None:
br = BenchmarkResults(
results=[
EvalResult(task_name="t1", success=True, duration_ms=100.0),
EvalResult(task_name="t2", success=False, duration_ms=50.0, error="bad output"),
]
)
summary = br.summary()
assert "1/2 passed" in summary
assert "Failures:" in summary
assert "t2" in summary
assert "bad output" in summary
def _make_mock_agent(response: str = "ok", tokens_in: int = 10, tokens_out: int = 5) -> MagicMock:
"""Create a mock agent with chat and reset methods."""
agent = MagicMock()
resp = MagicMock()
resp.response = response
resp.tokens_in = tokens_in
resp.tokens_out = tokens_out
resp.turns_taken = 1
resp.duration = "1.0s"
agent.chat.return_value = resp
return agent
class TestRunBenchmark:
"""Tests for run_benchmark."""
def test_single_task(self) -> None:
agent = _make_mock_agent()
tasks = [EvalTask(name="t1", prompt="hello")]
results = run_benchmark(agent, tasks)
assert results.total_tasks == 1
assert results.passed == 1
def test_multiple_tasks(self) -> None:
agent = _make_mock_agent()
tasks = [
EvalTask(name="t1", prompt="hello"),
EvalTask(name="t2", prompt="world"),
]
results = run_benchmark(agent, tasks)
assert results.total_tasks == 2
assert agent.chat.call_count == 2
def test_multiple_runs(self) -> None:
agent = _make_mock_agent()
tasks = [EvalTask(name="t1", prompt="hello")]
results = run_benchmark(agent, tasks, runs=3)
assert results.total_tasks == 3
assert agent.chat.call_count == 3
def test_reset_between_tasks(self) -> None:
agent = _make_mock_agent()
tasks = [
EvalTask(name="t1", prompt="hello"),
EvalTask(name="t2", prompt="world"),
]
run_benchmark(agent, tasks, reset_between_tasks=True)
assert agent.reset.call_count == 2
def test_no_reset(self) -> None:
agent = _make_mock_agent()
tasks = [
EvalTask(name="t1", prompt="hello"),
EvalTask(name="t2", prompt="world"),
]
run_benchmark(agent, tasks, reset_between_tasks=False)
agent.reset.assert_not_called()
def test_validation_pass(self) -> None:
agent = _make_mock_agent(response="The temperature is 72F")
tasks = [
EvalTask(
name="t1",
prompt="weather?",
validate=lambda r: "temperature" in r.response,
)
]
results = run_benchmark(agent, tasks)
assert results.passed == 1
def test_validation_fail(self) -> None:
agent = _make_mock_agent(response="ok")
tasks = [
EvalTask(
name="t1",
prompt="weather?",
validate=lambda r: "temperature" in r.response,
)
]
results = run_benchmark(agent, tasks)
assert results.passed == 0
assert results.failed == 1
def test_agent_exception(self) -> None:
agent = MagicMock()
agent.chat.side_effect = RuntimeError("agent crashed")
tasks = [EvalTask(name="t1", prompt="hello")]
results = run_benchmark(agent, tasks)
assert results.passed == 0
assert results.failed == 1
assert results.results[0].error == "agent crashed"
def test_category_in_task_name(self) -> None:
agent = _make_mock_agent()
tasks = [EvalTask(name="add", prompt="2+2", category="math")]
results = run_benchmark(agent, tasks)
assert results.results[0].task_name == "math/add"
@pytest.mark.asyncio
class TestRunBenchmarkAsync:
"""Tests for run_benchmark_async."""
async def test_single_task(self) -> None:
agent = AsyncMock()
agent.reset = MagicMock() # reset is sync; AsyncMock would leak a coroutine
resp = MagicMock()
resp.response = "ok"
resp.tokens_in = 10
resp.tokens_out = 5
resp.turns_taken = 1
resp.duration = "1.0s"
agent.chat.return_value = resp
tasks = [EvalTask(name="t1", prompt="hello")]
results = await run_benchmark_async(agent, tasks)
assert results.total_tasks == 1
assert results.passed == 1
async def test_multiple_runs(self) -> None:
agent = AsyncMock()
agent.reset = MagicMock() # reset is sync; AsyncMock would leak a coroutine
resp = MagicMock()
resp.response = "ok"
resp.tokens_in = 10
resp.tokens_out = 5
resp.turns_taken = 1
resp.duration = "1.0s"
agent.chat.return_value = resp
tasks = [EvalTask(name="t1", prompt="hello")]
results = await run_benchmark_async(agent, tasks, runs=2)
assert results.total_tasks == 2
async def test_validation_fail(self) -> None:
agent = AsyncMock()
agent.reset = MagicMock() # reset is sync; AsyncMock would leak a coroutine
resp = MagicMock()
resp.response = "no match"
resp.tokens_in = 10
resp.tokens_out = 5
resp.turns_taken = 1
resp.duration = "1.0s"
agent.chat.return_value = resp
tasks = [
EvalTask(
name="t1",
prompt="test",
validate=lambda r: "target" in r.response,
)
]
results = await run_benchmark_async(agent, tasks)
assert results.failed == 1
async def test_agent_exception(self) -> None:
agent = AsyncMock()
agent.reset = MagicMock() # reset is sync; AsyncMock would leak a coroutine
agent.chat.side_effect = RuntimeError("async crash")
tasks = [EvalTask(name="t1", prompt="hello")]
results = await run_benchmark_async(agent, tasks)
assert results.failed == 1
assert results.results[0].error == "async crash"