Martin Burke
Term-Sheet Risk Agent: agentic pipeline, RAG knowledge base, Gradio UI, BYO-key, eval harness
262432d
Raw History Blame Contribute Delete
2.78 kB
"""Evaluation harness for the term-sheet pipeline.
Runs the full pipeline on each synthetic term sheet in the golden set and scores:
- model accuracy: is the classified pricing model in the acceptable set?
- product accuracy: does the product family contain an expected keyword?
- risk recall: fraction of expected risk keywords that appear in the
risks-not-in-model text.
Requires an API key for the configured provider. Run from the repo root:
uv run python tests/eval/run_eval.py
"""
from __future__ import annotations
import json
from pathlib import Path
from termsheet_agent.agents import run_pipeline_sync
from termsheet_agent.config import REPO_ROOT, get_settings
from termsheet_agent.ingest import load_termsheet
GOLDEN = Path(__file__).with_name("golden.jsonl")
SAMPLE_DIR = REPO_ROOT / "data" / "sample_termsheets"
def _contains_any(text: str, needles: list[str]) -> bool:
t = text.lower()
return any(n.lower() in t for n in needles)
def _recall(text: str, needles: list[str]) -> float:
if not needles:
return 1.0
t = text.lower()
hits = sum(1 for n in needles if n.lower() in t)
return hits / len(needles)
def main() -> int:
settings = get_settings()
if not settings.has_api_key():
print(f"SKIP: no API key for provider '{settings.provider}'. Set it in .env.")
return 0
cases = [json.loads(line) for line in GOLDEN.read_text().splitlines() if line.strip()]
model_ok = product_ok = 0
recall_total = 0.0
for case in cases:
text = load_termsheet(SAMPLE_DIR / case["file"])
report = run_pipeline_sync(text, run_critic=False)
model_match = report.classification.required_model.value in case["acceptable_models"]
product_match = _contains_any(report.classification.product_family, case["product_keywords"])
risk_text = " ".join(
f"{f.name} {f.category} {f.description}" for f in report.risk_assessment.risks_not_in_model
)
recall = _recall(risk_text, case["risk_keywords"])
model_ok += int(model_match)
product_ok += int(product_match)
recall_total += recall
print(
f"{case['file']:30s} "
f"model={report.classification.required_model.value:24s} "
f"{'OK ' if model_match else 'MISS':4s} "
f"product={'OK ' if product_match else 'MISS':4s} "
f"risk_recall={recall:.0%}"
)
n = len(cases)
print("-" * 80)
print(f"Model accuracy: {model_ok}/{n} ({model_ok / n:.0%})")
print(f"Product accuracy: {product_ok}/{n} ({product_ok / n:.0%})")
print(f"Mean risk recall: {recall_total / n:.0%}")
return 0
if __name__ == "__main__":
raise SystemExit(main())