ek.agents.bridge#
One-way adapters: turn whatever your agent already emits into an ek Episode.
The dependency direction is the ek -> ocracy rule restated, and it is a hard rule:
ek -> inspect_ai / deepeval / ragas (via the ek[agents] extra), never the reverse.
Note what that buys: ek core depends only on the shape of what those tools emit, so the
adapters here duck-type and import nothing. You need the extra to run Inspect or DeepEval;
you do not need it to score what they produced – exactly as ek evaluates any
OcrResult-shaped object without importing an OCR engine.
The workhorse is trajectory_from_messages(): provider-shaped chat transcripts (OpenAI- and
Anthropic-style tool_calls / tool_use blocks) are the universal wire format for a
tool-using agent, and parsing them needs no SDK at all.
Example
>>> messages = [
... {"role": "user", "content": "weather in Paris?"},
... {"role": "assistant", "tool_calls": [
... {"function": {"name": "get_weather", "arguments": '{"city": "Paris"}'}}]},
... {"role": "tool", "content": "18C"},
... {"role": "assistant", "content": "It is 18C in Paris."},
... ]
>>> traj = trajectory_from_messages(messages)
>>> traj.tools
('get_weather',)
>>> traj.steps[0].args, traj.steps[0].observation
({'city': 'Paris'}, '18C')
Module Attributes
Inspect's letter grades (CORRECT / INCORRECT / PARTIAL / NOANSWER) and common word grades as numbers -- the mapping |
|
A sample counts as a success when its score reaches this (1.0 = fully correct; a PARTIAL "P" grade is 0.5, so it is a failure unless the threshold is lowered). |
Functions
|
Wrap a plain |
|
Build a |
|
Build a full |
|
Yield one graded |
|
Adapt a DeepEval |
|
Adapt an Inspect AI |
|
An Inspect |
|
Parse a chat transcript into a |
- ek.agents.bridge.DEFAULT_SUCCESS_THRESHOLD = 1.0#
A sample counts as a success when its score reaches this (1.0 = fully correct; a PARTIAL “P” grade is 0.5, so it is a failure unless the threshold is lowered).
- ek.agents.bridge.INSPECT_SCORE_VALUES = {'C': 1.0, 'I': 0.0, 'N': 0.0, 'P': 0.5, 'false': 0.0, 'no': 0.0, 'true': 1.0, 'yes': 1.0}#
Inspect’s letter grades (CORRECT / INCORRECT / PARTIAL / NOANSWER) and common word grades as numbers – the mapping
inspect_ai.scorer.value_to_floatuses, restated so reading a log needs noinspect_aiimport.
- ek.agents.bridge.as_agent(fn)[source]#
Wrap a plain
input -> answerfunction into the harness’sTaskSpec -> Episodeshape.Progressive disclosure: the trivial agent should not have to learn the Episode type.
Example
>>> from ek.agents.base import TaskSpec >>> agent = as_agent(str.upper) >>> agent(TaskSpec("t1", input="hi")).output 'HI'
- Return type:
- ek.agents.bridge.cost_from_usage(usage, *, latency_s=None)[source]#
Build a
Costfrom a providerusageobject or dict.Understands the OpenAI (
prompt_tokens/completion_tokens) and Anthropic (input_tokens/output_tokens/cache_read_input_tokens) spellings.Example
>>> c = cost_from_usage({"prompt_tokens": 100, "completion_tokens": 20}) >>> c.input_tokens, c.output_tokens (100, 20)
- Return type:
- ek.agents.bridge.episode_from_messages(messages, *, task_id='', usage=None, output=None, final_state=None, latency_s=None)[source]#
Build a full
Episodefrom a transcript (+ optional usage/state).outputdefaults to the last assistant text – the agent’s final answer.- Return type:
- ek.agents.bridge.episodes_from_inspect_log(log, *, scorer=None, success_threshold=1.0, task_prefix='')[source]#
Yield one graded
Episodeper sample per epoch of an InspectEvalLog.task_prefix(e.g. the Inspect task name) is prepended as"<prefix>/<sample id>"– needed when pooling several logs, whose sample ids (often1, 2, 3) would otherwise collide and be counted as extra trials of one task. Ascorerthat no sample has raisesValueError(a typo would otherwise leave every episode silently ungraded).Duck-typed like
from_inspect_sample()(noinspect_aiimport): readslog.samplesandlog.eval. Each sample’s score – thescorernamed, else the first – is read withinspect_score_value()and the episode is gradedsuccess = value >= success_threshold; a sample with no scalar score stays ungraded (success=None) rather than being silently counted as a failure.Inspect’s
epochsare ek’s trials: an eval run withepochs=kyields k episodes pertask_id(the sample id), which is exactly whatreliability()needs forpass^k. The run’s model and task version are recorded asRunProvenance.Example (a log-shaped stand-in; a real
EvalLoghas the same attributes):>>> from types import SimpleNamespace as NS >>> sample = NS(id="q1", epoch=1, messages=[], output=NS(completion="4", usage=None), ... scores={"match": NS(value="C")}) >>> log = NS(samples=[sample], eval=NS(model="mockllm/model", task_version=0)) >>> [(e.task_id, e.success, e.meta["epoch"]) for e in episodes_from_inspect_log(log)] [('q1', True, 1)]
- ek.agents.bridge.from_deepeval_test_case(case, *, task_id='')[source]#
Adapt a DeepEval
LLMTestCase-shaped object into anEpisode(duck-typed).Reads
.actual_outputand.tools_called. Note DeepEval phones home by default (Confident-AI telemetry) – disable it before use if that matters to you;eknever enables it.- Return type:
- ek.agents.bridge.from_inspect_sample(sample, *, task_id='')[source]#
Adapt an Inspect AI
EvalSample-shaped object into anEpisode(duck-typed).Reads
.messages,.outputand.id– noinspect_aiimport required, so scoring an Inspect log never drags the harness intoek’s dependency closure.- Return type:
- ek.agents.bridge.inspect_score_value(value)[source]#
An Inspect
Score.valueas a float, orNonewhen it is not a scalar grade.Example
>>> inspect_score_value("C"), inspect_score_value("P"), inspect_score_value(0.25) (1.0, 0.5, 0.25) >>> inspect_score_value({"accuracy": 1.0}) is None # a multi-value score: no scalar True
- ek.agents.bridge.trajectory_from_messages(messages)[source]#
Parse a chat transcript into a
Trajectory.Each assistant tool call becomes a
Step, and the next tool/result message becomes that step’sobservation. A tool message carrying an error marks the step as errored – error recovery across later steps is itself an evaluable signal.- Return type: