aid-runtime-python 0.1.151__py3-none-any.whl → 0.1.153__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aid_runtime/__init__.py +27 -1
- aid_runtime/agents/loop.py +42 -13
- aid_runtime/evals/__init__.py +11 -0
- aid_runtime/evals/metrics.py +233 -7
- aid_runtime/evals/runner.py +94 -10
- aid_runtime/evals/trajectory.py +69 -0
- aid_runtime/metrics.py +133 -0
- aid_runtime/retrieval/gate.py +15 -1
- aid_runtime/telemetry.py +188 -34
- {aid_runtime_python-0.1.151.dist-info → aid_runtime_python-0.1.153.dist-info}/METADATA +1 -1
- {aid_runtime_python-0.1.151.dist-info → aid_runtime_python-0.1.153.dist-info}/RECORD +13 -11
- {aid_runtime_python-0.1.151.dist-info → aid_runtime_python-0.1.153.dist-info}/WHEEL +0 -0
- {aid_runtime_python-0.1.151.dist-info → aid_runtime_python-0.1.153.dist-info}/top_level.txt +0 -0
aid_runtime/evals/runner.py
CHANGED
|
@@ -9,14 +9,17 @@ from __future__ import annotations
|
|
|
9
9
|
|
|
10
10
|
import statistics
|
|
11
11
|
from collections.abc import Awaitable, Callable, Sequence
|
|
12
|
+
from time import perf_counter
|
|
12
13
|
|
|
13
14
|
from pydantic import BaseModel, ConfigDict, Field
|
|
14
15
|
|
|
15
16
|
from aid_runtime.errors import EvalError
|
|
16
17
|
from aid_runtime.evals.dataset import Dataset, EvalCase
|
|
17
18
|
from aid_runtime.evals.gate import EvalRun, MetricRun
|
|
18
|
-
from aid_runtime.evals.metrics import Metric, MetricResult
|
|
19
|
+
from aid_runtime.evals.metrics import Metric, MetricResult, RunContext
|
|
20
|
+
from aid_runtime.evals.trajectory import RunOutput, Trajectory
|
|
19
21
|
from aid_runtime.gateway import ModelGateway
|
|
22
|
+
from aid_runtime.metrics import record_eval_case
|
|
20
23
|
from aid_runtime.types import CompletionRequest, Message
|
|
21
24
|
|
|
22
25
|
__all__ = [
|
|
@@ -29,18 +32,25 @@ __all__ = [
|
|
|
29
32
|
"run_evaluation",
|
|
30
33
|
]
|
|
31
34
|
|
|
32
|
-
#: Anything that turns a case into a prediction.
|
|
33
|
-
|
|
35
|
+
#: Anything that turns a case into a prediction. A runner that can also say how it got
|
|
36
|
+
#: there returns a :class:`RunOutput`; one that only has an answer returns a ``str``,
|
|
37
|
+
#: which is what every text metric needs and all a ``tool-trace`` metric refuses.
|
|
38
|
+
Runner = Callable[[EvalCase], Awaitable[str | RunOutput]]
|
|
34
39
|
|
|
35
40
|
|
|
36
41
|
class CaseOutcome(BaseModel):
|
|
37
|
-
"""One case's prediction and its per-metric scores, for inspection.
|
|
42
|
+
"""One case's prediction and its per-metric scores, for inspection.
|
|
43
|
+
|
|
44
|
+
``trajectory`` is ``None`` for a runner that reported no tool calls, so "the runner
|
|
45
|
+
said nothing" and "the run called no tools" stay different answers.
|
|
46
|
+
"""
|
|
38
47
|
|
|
39
48
|
model_config = ConfigDict(frozen=True, extra="forbid")
|
|
40
49
|
|
|
41
50
|
case_id: str
|
|
42
51
|
prediction: str
|
|
43
52
|
scores: tuple[MetricResult, ...] = ()
|
|
53
|
+
trajectory: Trajectory | None = None
|
|
44
54
|
|
|
45
55
|
@property
|
|
46
56
|
def passed(self) -> bool:
|
|
@@ -174,32 +184,106 @@ async def gateway_judge(
|
|
|
174
184
|
return call
|
|
175
185
|
|
|
176
186
|
|
|
177
|
-
async def evaluate_once(
|
|
178
|
-
|
|
187
|
+
async def evaluate_once(
|
|
188
|
+
dataset: Dataset,
|
|
189
|
+
runner: Runner,
|
|
190
|
+
metrics: Sequence[Metric],
|
|
191
|
+
*,
|
|
192
|
+
eval_id: str | None = None,
|
|
193
|
+
) -> DatasetRun:
|
|
194
|
+
"""One pass over ``dataset``, scoring every prediction with every metric.
|
|
195
|
+
|
|
196
|
+
Every case publishes its spend and duration as it completes, which is what makes
|
|
197
|
+
``eval.cost`` and ``eval.latency.p95`` a series an alarm can watch. ``eval_id`` is the
|
|
198
|
+
*declared* eval this dataset belongs to -- the id an alarm's ``source`` names -- and
|
|
199
|
+
falls back to the dataset's own name for a caller that runs a dataset directly.
|
|
200
|
+
"""
|
|
179
201
|
if not metrics:
|
|
180
202
|
raise ValueError("at least one metric is required")
|
|
203
|
+
label = dataset.name if eval_id is None else eval_id
|
|
181
204
|
outcomes: list[CaseOutcome] = []
|
|
182
205
|
for case in dataset.cases:
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
206
|
+
cost_before = _runner_cost(runner)
|
|
207
|
+
started = perf_counter()
|
|
208
|
+
raw = await runner(case)
|
|
209
|
+
prediction, trajectory = _split(raw)
|
|
210
|
+
# The runner call, not the scoring, is what a latency budget constrains: scoring
|
|
211
|
+
# is the harness measuring the answer, and charging its time to the pipeline would
|
|
212
|
+
# report a slower product because a judge was added to its eval.
|
|
213
|
+
latency_ms = (perf_counter() - started) * 1000.0
|
|
214
|
+
# Metrics see the pipeline's own spend, measured before scoring, because "what did
|
|
215
|
+
# this task cost" is a property of the product and not of the harness auditing it.
|
|
216
|
+
context = RunContext(
|
|
217
|
+
cost_usd=_spent_since(runner, cost_before),
|
|
218
|
+
latency_ms=latency_ms,
|
|
219
|
+
metered=cost_before is not None,
|
|
220
|
+
trajectory=trajectory,
|
|
221
|
+
)
|
|
222
|
+
scores = tuple(
|
|
223
|
+
[await metric.score(case, prediction, context=context) for metric in metrics]
|
|
224
|
+
)
|
|
225
|
+
outcomes.append(
|
|
226
|
+
CaseOutcome(
|
|
227
|
+
case_id=case.id,
|
|
228
|
+
prediction=prediction,
|
|
229
|
+
scores=scores,
|
|
230
|
+
trajectory=trajectory,
|
|
231
|
+
)
|
|
232
|
+
)
|
|
233
|
+
# The published series is read after scoring, because a model-backed metric spends
|
|
234
|
+
# money too and a cost series that ignored the judge would report half the run.
|
|
235
|
+
record_eval_case(
|
|
236
|
+
eval_id=label,
|
|
237
|
+
cost_usd=_spent_since(runner, cost_before),
|
|
238
|
+
latency_ms=latency_ms,
|
|
239
|
+
)
|
|
186
240
|
return DatasetRun(
|
|
187
241
|
dataset=dataset.name, dataset_version=dataset.version, outcomes=tuple(outcomes)
|
|
188
242
|
)
|
|
189
243
|
|
|
190
244
|
|
|
245
|
+
def _split(raw: str | RunOutput) -> tuple[str, Trajectory | None]:
|
|
246
|
+
"""The prediction, and the trajectory when the runner reported one."""
|
|
247
|
+
if isinstance(raw, RunOutput):
|
|
248
|
+
return raw.text, raw.trajectory
|
|
249
|
+
return raw, None
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _runner_cost(runner: Runner) -> float | None:
|
|
253
|
+
"""A runner's cumulative model spend, when it meters one.
|
|
254
|
+
|
|
255
|
+
A :data:`Runner` is any callable, so most say nothing here; one built by
|
|
256
|
+
:class:`EvalRunner` reads its gateway ledger. An unmetered runner reports a real
|
|
257
|
+
``0.0`` rather than a guess, which is why "does not meter" and "spent nothing" are the
|
|
258
|
+
same answer for a series that only ever sums.
|
|
259
|
+
"""
|
|
260
|
+
spent = getattr(runner, "cost_usd", None)
|
|
261
|
+
return float(spent) if isinstance(spent, int | float) else None
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _spent_since(runner: Runner, before: float | None) -> float:
|
|
265
|
+
after = _runner_cost(runner)
|
|
266
|
+
if before is None or after is None:
|
|
267
|
+
return 0.0
|
|
268
|
+
return max(after - before, 0.0)
|
|
269
|
+
|
|
270
|
+
|
|
191
271
|
async def run_evaluation(
|
|
192
272
|
dataset: Dataset,
|
|
193
273
|
runner: Runner,
|
|
194
274
|
metrics: Sequence[Metric],
|
|
195
275
|
*,
|
|
196
276
|
runs: int = 5,
|
|
277
|
+
eval_id: str | None = None,
|
|
197
278
|
) -> EvalRun:
|
|
198
279
|
"""Sample ``dataset`` ``runs`` times, recording one mean per metric per run.
|
|
199
280
|
|
|
200
281
|
Every run makes fresh calls, which is the point: the spread across runs is the
|
|
201
282
|
measurement of how much this system varies when nothing has changed. Without
|
|
202
283
|
it there is nothing to compare a "regression" against.
|
|
284
|
+
|
|
285
|
+
``eval_id`` is forwarded to :func:`evaluate_once` so every case's published series
|
|
286
|
+
carries the declared eval, which is the value an alarm scopes itself to.
|
|
203
287
|
"""
|
|
204
288
|
if runs < 1:
|
|
205
289
|
raise ValueError("runs must be at least 1")
|
|
@@ -208,7 +292,7 @@ async def run_evaluation(
|
|
|
208
292
|
|
|
209
293
|
collected: dict[str, list[float]] = {metric.name: [] for metric in metrics}
|
|
210
294
|
for _ in range(runs):
|
|
211
|
-
pass_result = await evaluate_once(dataset, runner, metrics)
|
|
295
|
+
pass_result = await evaluate_once(dataset, runner, metrics, eval_id=eval_id)
|
|
212
296
|
means = pass_result.means()
|
|
213
297
|
for metric in metrics:
|
|
214
298
|
collected[metric.name].append(means[metric.name])
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""The tool calls a run made, in the order it made them.
|
|
2
|
+
|
|
3
|
+
A prediction is what a pipeline *said*; a trajectory is how an agent got there. Scoring
|
|
4
|
+
"did it look the order up before issuing the refund" needs the second, and the agent loop
|
|
5
|
+
already records it: every :class:`~aid_runtime.agents.state.StepRecord` carries the tool
|
|
6
|
+
names that step called, so the sequence is a flatten, not a new instrumentation point.
|
|
7
|
+
|
|
8
|
+
:class:`RunOutput` is how a runner hands both back. A runner that returns a plain ``str``
|
|
9
|
+
still works and still scores every text metric; what it cannot do is satisfy a
|
|
10
|
+
``tool-trace`` metric, which refuses rather than passing over a run it was never told the
|
|
11
|
+
tool calls of.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from collections.abc import Sequence
|
|
17
|
+
from typing import TYPE_CHECKING
|
|
18
|
+
|
|
19
|
+
from pydantic import BaseModel, ConfigDict
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
# Type-only: this module reaches into a record's shape, never its definitions, so
|
|
23
|
+
# importing the agent package at runtime would only buy an import cycle.
|
|
24
|
+
from aid_runtime.agents.state import RunResult, StepRecord
|
|
25
|
+
|
|
26
|
+
__all__ = ["RunOutput", "Trajectory"]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Trajectory(BaseModel):
|
|
30
|
+
"""Every tool call a run made, flattened into the order they happened.
|
|
31
|
+
|
|
32
|
+
Deliberately just the names, not arguments or results: an expectation is written
|
|
33
|
+
against *what* a run did, and pinning the arguments of a model-authored call would
|
|
34
|
+
make the assertion fail on a paraphrase that was equally correct.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
model_config = ConfigDict(frozen=True, extra="forbid")
|
|
38
|
+
|
|
39
|
+
calls: tuple[str, ...] = ()
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def from_steps(cls, steps: Sequence[StepRecord]) -> Trajectory:
|
|
43
|
+
return cls(calls=tuple(name for step in steps for name in step.tool_names))
|
|
44
|
+
|
|
45
|
+
@classmethod
|
|
46
|
+
def from_run(cls, result: RunResult) -> Trajectory:
|
|
47
|
+
"""The trajectory of an agent run, read off the steps it recorded.
|
|
48
|
+
|
|
49
|
+
A paused or blocked run still has one: the loop records a step before it decides
|
|
50
|
+
what to do with it, so a run that stopped at an approval records the calls that
|
|
51
|
+
asked for it.
|
|
52
|
+
"""
|
|
53
|
+
return cls.from_steps(result.steps)
|
|
54
|
+
|
|
55
|
+
def __bool__(self) -> bool:
|
|
56
|
+
return bool(self.calls)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class RunOutput(BaseModel):
|
|
60
|
+
"""An answer, plus how the run arrived at it.
|
|
61
|
+
|
|
62
|
+
Returning this instead of a bare string is the whole opt-in for trajectory evals: a
|
|
63
|
+
runner that has nothing to say about tool calls keeps returning ``str``.
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
model_config = ConfigDict(frozen=True, extra="forbid")
|
|
67
|
+
|
|
68
|
+
text: str
|
|
69
|
+
trajectory: Trajectory = Trajectory()
|
aid_runtime/metrics.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Publishing the four series an ``observability.alerts`` entry can watch.
|
|
2
|
+
|
|
3
|
+
An alarm is only as real as the series under it. This module is the producer side of the
|
|
4
|
+
four metric names the deploy plan's alarm policies name -- ``agent.cost``,
|
|
5
|
+
``agent.tokens``, ``eval.cost`` and ``eval.latency.p95`` -- recorded where the values are
|
|
6
|
+
already computed rather than recomputed from a second reading of the run: the agent loop
|
|
7
|
+
knows what a run cost when it assembles the result, and the eval harness knows how long a
|
|
8
|
+
case took when it scores it.
|
|
9
|
+
|
|
10
|
+
Recording is a no-op until :func:`aid_runtime.telemetry.configure_metrics` installs a meter
|
|
11
|
+
provider, for the same reason spans are dropped until a tracer provider is installed: a
|
|
12
|
+
library must not decide its host's export destination, and a service that never asked for a
|
|
13
|
+
collector should pay nothing for one. The instruments are created through the OpenTelemetry
|
|
14
|
+
*API*, so an instrument made before the provider exists still forwards once it does.
|
|
15
|
+
|
|
16
|
+
Names and units are not invented here. ``agent.cost`` and ``eval.cost`` are dollars,
|
|
17
|
+
``agent.tokens`` is a token count and ``eval.latency.p95`` is milliseconds -- the same
|
|
18
|
+
pairing ``alertUnit`` derives in ``@aid-framework/ir``, which is why a threshold in a
|
|
19
|
+
document is never ambiguous about what its number means.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
from typing import Any, Final
|
|
25
|
+
|
|
26
|
+
from opentelemetry import metrics
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"AGENT_LABEL",
|
|
30
|
+
"EVAL_LABEL",
|
|
31
|
+
"METRIC_NAMES",
|
|
32
|
+
"record_agent_run",
|
|
33
|
+
"record_eval_case",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
#: Every series a document can name in ``observability.alerts[].metric``. Kept in the
|
|
37
|
+
#: generator's order so this module and the schema read the same four names.
|
|
38
|
+
METRIC_NAMES: Final[tuple[str, ...]] = (
|
|
39
|
+
"agent.cost",
|
|
40
|
+
"agent.tokens",
|
|
41
|
+
"eval.cost",
|
|
42
|
+
"eval.latency.p95",
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
#: The label that separates one agent's series from another's. It is the span attribute
|
|
46
|
+
#: ``aid.agent.id`` under the same name, so a trace and a metric name the same run the same
|
|
47
|
+
#: way -- and a value an operator can match against an alarm's ``source`` either way.
|
|
48
|
+
#:
|
|
49
|
+
#: The generated alarm narrows by the cloud's own spelling of that idea: ``alerts.ts``
|
|
50
|
+
#: writes ``dimensions = { Source = "<source>" }`` for CloudWatch and
|
|
51
|
+
#: ``metric.label.source`` for Cloud Monitoring, whose label keys are lowercased. Nothing
|
|
52
|
+
#: in this package renames attributes on the way out -- an OTLP collector maps
|
|
53
|
+
#: ``aid.agent.id`` to those keys once, in the exporter config, because the mapping is the
|
|
54
|
+
#: operator's account detail and the runtime's job is only to publish a labelled series.
|
|
55
|
+
AGENT_LABEL: Final = "aid.agent.id"
|
|
56
|
+
|
|
57
|
+
#: The eval counterpart of :data:`AGENT_LABEL`. The value is the *declared* eval id -- the
|
|
58
|
+
#: ``observability.alerts[].source`` an operator writes for an eval suite, and the id the
|
|
59
|
+
#: deploy plan derives an alarm for -- not a per-case identifier. A per-case label would
|
|
60
|
+
#: spread one suite's cost across as many series as it has cases, and an alarm scoped to
|
|
61
|
+
#: the suite would then watch a series nothing writes.
|
|
62
|
+
EVAL_LABEL: Final = "aid.eval.id"
|
|
63
|
+
|
|
64
|
+
#: The meter's scope name. Distinct from the tracer's only by convention; both are the
|
|
65
|
+
#: runtime's own wiring rather than the application's.
|
|
66
|
+
_METER_NAME: Final = "aid_runtime"
|
|
67
|
+
|
|
68
|
+
_instruments: dict[str, Any] | None = None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _recorders() -> dict[str, Any]:
|
|
72
|
+
"""Create the four instruments once.
|
|
73
|
+
|
|
74
|
+
Built lazily rather than at import so a process that imports this package for
|
|
75
|
+
something else does not register instruments it will never record. Safe to create
|
|
76
|
+
before a provider exists: the API returns forwarding instruments that bind to the
|
|
77
|
+
provider when one is installed.
|
|
78
|
+
"""
|
|
79
|
+
global _instruments
|
|
80
|
+
if _instruments is None:
|
|
81
|
+
meter = metrics.get_meter(_METER_NAME)
|
|
82
|
+
_instruments = {
|
|
83
|
+
"agent.cost": meter.create_counter(
|
|
84
|
+
name="agent.cost",
|
|
85
|
+
unit="usd",
|
|
86
|
+
description="Model spend of one agent run, in US dollars.",
|
|
87
|
+
),
|
|
88
|
+
"agent.tokens": meter.create_counter(
|
|
89
|
+
name="agent.tokens",
|
|
90
|
+
unit="tokens",
|
|
91
|
+
description="Total tokens one agent run consumed, input and output.",
|
|
92
|
+
),
|
|
93
|
+
"eval.cost": meter.create_counter(
|
|
94
|
+
name="eval.cost",
|
|
95
|
+
unit="usd",
|
|
96
|
+
description="Model spend of one eval case, in US dollars.",
|
|
97
|
+
),
|
|
98
|
+
"eval.latency.p95": meter.create_histogram(
|
|
99
|
+
name="eval.latency.p95",
|
|
100
|
+
unit="ms",
|
|
101
|
+
description="Wall-clock duration of one eval case, in milliseconds.",
|
|
102
|
+
),
|
|
103
|
+
}
|
|
104
|
+
return _instruments
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def record_agent_run(*, agent_id: str, cost_usd: float, tokens: int) -> None:
|
|
108
|
+
"""Publish one completed agent run's spend and token count.
|
|
109
|
+
|
|
110
|
+
Both series carry ``aid.agent.id``, so an alarm scoped to an agent watches that
|
|
111
|
+
agent's runs and an alarm with no ``source`` watches the whole environment. A run that
|
|
112
|
+
fails still spent money, so the caller records every run it assembles rather than only
|
|
113
|
+
the successful ones -- a budget alarm that ignored failures would under-report exactly
|
|
114
|
+
the runs most likely to overspend.
|
|
115
|
+
"""
|
|
116
|
+
recorders = _recorders()
|
|
117
|
+
labels = {AGENT_LABEL: agent_id}
|
|
118
|
+
recorders["agent.cost"].add(cost_usd, labels)
|
|
119
|
+
recorders["agent.tokens"].add(tokens, labels)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def record_eval_case(*, eval_id: str, cost_usd: float, latency_ms: float) -> None:
|
|
123
|
+
"""Publish one eval case's spend and duration.
|
|
124
|
+
|
|
125
|
+
``eval_id`` is the declared eval's id, so every case in one suite lands on the same
|
|
126
|
+
series and an alarm scoped to that suite sees the suite's total. ``eval.latency.p95`` is
|
|
127
|
+
a histogram rather than a gauge: a percentile is computed from a distribution, and a
|
|
128
|
+
backend that cannot reduce a histogram cannot answer a p95.
|
|
129
|
+
"""
|
|
130
|
+
recorders = _recorders()
|
|
131
|
+
labels = {EVAL_LABEL: eval_id}
|
|
132
|
+
recorders["eval.cost"].add(cost_usd, labels)
|
|
133
|
+
recorders["eval.latency.p95"].record(latency_ms, labels)
|
aid_runtime/retrieval/gate.py
CHANGED
|
@@ -33,6 +33,7 @@ mean comparison.
|
|
|
33
33
|
from __future__ import annotations
|
|
34
34
|
|
|
35
35
|
from collections.abc import Sequence
|
|
36
|
+
from time import perf_counter
|
|
36
37
|
from typing import cast
|
|
37
38
|
|
|
38
39
|
from aid_runtime.errors import EvalError
|
|
@@ -45,6 +46,7 @@ from aid_runtime.evals.gate import (
|
|
|
45
46
|
Threshold,
|
|
46
47
|
)
|
|
47
48
|
from aid_runtime.evals.metrics import Metric
|
|
49
|
+
from aid_runtime.metrics import record_eval_case
|
|
48
50
|
from aid_runtime.retrieval.metrics import RetrievalCase, RetrievalMetric
|
|
49
51
|
from aid_runtime.retrieval.retriever import Retriever
|
|
50
52
|
|
|
@@ -84,8 +86,16 @@ class RetrievalGate:
|
|
|
84
86
|
runs: int,
|
|
85
87
|
dataset: str,
|
|
86
88
|
dataset_version: str,
|
|
89
|
+
eval_id: str | None = None,
|
|
87
90
|
) -> EvalRun:
|
|
88
|
-
"""Run every case ``runs`` times, producing one mean per metric per run.
|
|
91
|
+
"""Run every case ``runs`` times, producing one mean per metric per run.
|
|
92
|
+
|
|
93
|
+
Each case also publishes its duration, which is what makes ``eval.latency.p95`` a
|
|
94
|
+
series an alarm can watch for a retrieval eval just as for a model-backed one. The
|
|
95
|
+
spend is a real ``0.0``: retrieval costs embedding and vector queries rather than
|
|
96
|
+
model calls, and a counter that stayed silent would leave a cost alarm reporting
|
|
97
|
+
no data instead of reporting that there was nothing to spend.
|
|
98
|
+
"""
|
|
89
99
|
if not cases:
|
|
90
100
|
raise EvalError("a retrieval run needs at least one case")
|
|
91
101
|
if runs < 1:
|
|
@@ -93,16 +103,20 @@ class RetrievalGate:
|
|
|
93
103
|
if not self._metrics:
|
|
94
104
|
raise EvalError("a retrieval run needs at least one metric")
|
|
95
105
|
|
|
106
|
+
label = dataset if eval_id is None else eval_id
|
|
96
107
|
samples: dict[str, list[float]] = {metric.name: [] for metric in self._metrics}
|
|
97
108
|
for _ in range(runs):
|
|
98
109
|
totals: dict[str, float] = {metric.name: 0.0 for metric in self._metrics}
|
|
99
110
|
for case in cases:
|
|
100
111
|
# k comes from the case, because a case is what says how many
|
|
101
112
|
# passages count as an answer; a retriever's own topK is a default.
|
|
113
|
+
started = perf_counter()
|
|
102
114
|
retrieved = await retriever.retrieve(case.query, top_k=case.k)
|
|
115
|
+
latency_ms = (perf_counter() - started) * 1000.0
|
|
103
116
|
for metric in self._metrics:
|
|
104
117
|
result = await metric.score(case, retrieved)
|
|
105
118
|
totals[metric.name] += result.score
|
|
119
|
+
record_eval_case(eval_id=label, cost_usd=0.0, latency_ms=latency_ms)
|
|
106
120
|
for name, total in totals.items():
|
|
107
121
|
samples[name].append(total / len(cases))
|
|
108
122
|
|