-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathcounterfactual.py
More file actions
70 lines (64 loc) · 2.71 KB
/
Copy pathcounterfactual.py
File metadata and controls
70 lines (64 loc) · 2.71 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
"""Budgeted counterfactual evaluation using the official evaluator."""
from __future__ import annotations
from pathlib import Path
from trpc_agent_sdk.evaluation import EvalCase
from .diagnosis import attribute_from_evidence
from .interventions import InterventionKind, build_counterfactual
from .models import CounterfactualEvidence, FailureAttribution
from .probe import evaluate_trace_cases
async def diagnose_trace_case(case: EvalCase, workspace: Path, max_evaluations: int = 7) -> FailureAttribution:
baseline = (await evaluate_trace_cases([case], workspace))[case.eval_id]
evidence = []
singles = [
InterventionKind.REPLACE_FINAL_RESPONSE,
InterventionKind.REPLACE_TOOL_NAME,
InterventionKind.REPLACE_TOOL_ARGUMENTS,
InterventionKind.NORMALIZE_FORMAT,
]
combinations = [
InterventionKind.REPLACE_TOOL_NAME_AND_ARGUMENTS,
InterventionKind.REPLACE_TOOL_NAME_AND_FINAL_RESPONSE,
InterventionKind.REPLACE_TOOL_ARGUMENTS_AND_FINAL_RESPONSE,
]
for kind in singles + combinations:
if len(evidence) >= max_evaluations:
break
built = build_counterfactual(case, kind)
if not built.valid or built.eval_case is None:
evidence.append(
CounterfactualEvidence(
kind.value,
False,
built.status,
False,
[],
[],
baseline,
{},
structurally_valid=built.structurally_valid,
semantically_coherent=built.semantically_coherent,
coherence_warnings=list(built.coherence_warnings),
)
)
continue
after = (await evaluate_trace_cases([built.eval_case], workspace))[built.eval_case.eval_id]
repaired = sorted(k for k, v in baseline.items() if v < 1 and after.get(k, v) >= 1)
unchanged = sorted(k for k, v in baseline.items() if after.get(k) == v)
evidence.append(
CounterfactualEvidence(
kind.value,
True,
built.status,
all(v >= 1 for v in after.values()),
repaired,
unchanged,
baseline,
after,
structurally_valid=built.structurally_valid,
semantically_coherent=built.semantically_coherent,
coherence_warnings=list(built.coherence_warnings),
)
)
if evidence[-1].changed_fail_to_pass and kind in singles:
break
return attribute_from_evidence(case.eval_id, evidence, evaluations_used=len(evidence), budget=max_evaluations)