Run a single LLM call to get holistic alignment verdict.
(
model: "GenerativeModel",
ground_truth: dict,
agent_output: dict,
mismatches: list[str],
domain_name: str = "invoice processing",
)
| 619 | |
| 620 | |
| 621 | def llm_evaluate( |
| 622 | model: "GenerativeModel", |
| 623 | ground_truth: dict, |
| 624 | agent_output: dict, |
| 625 | mismatches: list[str], |
| 626 | domain_name: str = "invoice processing", |
| 627 | ) -> dict | None: |
| 628 | """Run a single LLM call to get holistic alignment verdict.""" |
| 629 | prompt = LLM_EVAL_PROMPT_TEMPLATE.format( |
| 630 | domain_name=domain_name, |
| 631 | ground_truth=json.dumps(ground_truth, indent=2), |
| 632 | agent_output=json.dumps(agent_output, indent=2), |
| 633 | mismatches="\n".join(f"- {m}" for m in mismatches) |
| 634 | if mismatches |
| 635 | else "None detected", |
| 636 | ) |
| 637 | |
| 638 | try: |
| 639 | response = model.generate_content(prompt) |
| 640 | text = response.text.strip() |
| 641 | |
| 642 | # Strip markdown code fences if present |
| 643 | if text.startswith("```"): |
| 644 | match = re.search(r"```(?:json)?\s*\n?(.*?)\n?```", text, re.DOTALL) |
| 645 | if match: |
| 646 | text = match.group(1).strip() |
| 647 | |
| 648 | result = json.loads(text) |
| 649 | |
| 650 | # Validate verdict value |
| 651 | verdict = result.get("verdict", "").upper() |
| 652 | if verdict not in ("ALIGNED", "PARTIALLY_ALIGNED", "NOT_ALIGNED"): |
| 653 | result["verdict"] = "NOT_ALIGNED" |
| 654 | result["reason"] = ( |
| 655 | result.get("reason", "") |
| 656 | + f" (original verdict '{verdict}' was invalid)" |
| 657 | ) |
| 658 | |
| 659 | return result |
| 660 | |
| 661 | except Exception as e: |
| 662 | return {"verdict": "ERROR", "reason": str(e)} |
| 663 | |
| 664 | |
| 665 | # ============================================================================ |
no test coverage detected