Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
chore(evals): remove the unreferenced prompt_comparison and memory runner_v3 modules (#4077)
Tejas Chopra8e61cca229c3
diff --git a/headroom/evals/memory/runner_v3.py b/headroom/evals/memory/runner_v3.pydeleted file mode 100644index 08f793325..000000000--- a/headroom/evals/memory/runner_v3.py+++ /dev/null@@ -1,588 +0,0 @@-"""LoCoMo Evaluator V3 - Tests retrieval quality of memory systems.--This evaluator reflects how real-world memory systems work:-1. Store raw conversation turns (no LLM extraction)-2. Retrieve relevant turns for each question-3. Measure retrieval recall against ground truth evidence-4. Pass retrieved context to LLM for answer synthesis--Key insight: The memory system's job is RETRIEVAL, not extraction.-The LLM handles comprehension given the right context.--Metrics:-- Retrieval Recall@k: % of evidence turns found in top-k retrieved-- Retrieval MRR: Mean reciprocal rank of first evidence turn-- End-to-end Accuracy: Using LLM-as-judge for semantic correctness-"""--from __future__ import annotations--import asyncio-import json-import logging-import tempfile-import time-import uuid-from dataclasses import dataclass, field-from datetime import datetime-from pathlib import Path-from typing import Any--import litellm--from headroom.evals.memory.locomo import (- LOCOMO_CATEGORIES,- LoCoMoCase,- LoCoMoConversation,- load_locomo,-)-from headroom.memory.backends.local import LocalBackend, LocalBackendConfig-from headroom.memory.backends.mem0 import Mem0Backend, Mem0Config-from headroom.memory.models import Memory-from headroom.memory.ports import MemorySearchResult, VectorSearchResult--logger = logging.getLogger(__name__)---@dataclass-class EvalConfigV3:- """Configuration for V3 memory evaluation.-- This evaluator tests retrieval quality by:- 1. Storing raw dialogue turns as memories- 2. Measuring if evidence turns are retrieved for each question- """-- n_conversations: int | None = None- categories: list[int] | None = None- skip_adversarial: bool = True-- # Backend selection- backend_type: str = "local" # "local" or "mem0"-- # Mem0 settings (when backend_type="mem0")- mem0_api_key: str | None = None # For Mem0 cloud mode- mem0_mode: str = "cloud" # "local" or "cloud"- mem0_enable_graph: bool = True # Enable graph storage (Neo4j)-- # Retrieval settings- top_k: int = 10 # How many turns to retrieve per question- use_hybrid_search: bool = False # Combine vector + text search (local only)- vector_weight: float = 0.5 # Weight for vector similarity (hybrid mode)- text_weight: float = 0.5 # Weight for text/BM25 matching (hybrid mode)-- # Answer generation- answer_model: str = "gpt-4o-mini"- use_llm_judge: bool = True- judge_model: str = "gpt-4o-mini"-- # Debug- debug: bool = False- db_path: str | None = None---@dataclass-class RetrievalMetrics:- """Metrics for retrieval quality."""-- recall_at_k: float = 0.0 # % of evidence turns in top-k- mrr: float = 0.0 # Mean reciprocal rank- precision_at_k: float = 0.0 # % of retrieved that are evidence-- # Per-category breakdown- recall_by_category: dict[str, float] = field(default_factory=dict)---@dataclass-class CaseResultV3:- """Result for a single QA case."""-- case: LoCoMoCase-- # Retrieval results- retrieved_turn_ids: list[str]- evidence_turn_ids: list[str]- retrieval_recall: float # What % of evidence was retrieved- retrieval_rank: int | None # Rank of first evidence (None if not found)-- # Answer results- predicted_answer: str- is_correct: bool- judge_score: float | None = None- judge_reasoning: str | None = None-- # Context used- retrieved_context: str = ""-- def to_dict(self) -> dict:- return {- "question": self.case.question,- "ground_truth": self.case.answer,- "predicted": self.predicted_answer,- "category": self.case.category_name,- "evidence_turns": self.evidence_turn_ids,- "retrieved_turns": self.retrieved_turn_ids,- "retrieval_recall": self.retrieval_recall,- "retrieval_rank": self.retrieval_rank,- "is_correct": self.is_correct,- "judge_score": self.judge_score,- }---@dataclass-class EvalResultV3:- """Aggregated evaluation results."""-- total_cases: int-- # Retrieval metrics (the main thing we're testing)- avg_retrieval_recall: float- avg_mrr: float- retrieval_by_category: dict[str, dict[str, float]]-- # End-to-end accuracy- accuracy: float- accuracy_by_category: dict[str, float]-- # Individual results- results: list[CaseResultV3] = field(default_factory=list)-- # Metadata- config: dict[str, Any] = field(default_factory=dict)- duration_seconds: float = 0.0- timestamp: str = field(default_factory=lambda: datetime.now().isoformat())-- def summary(self) -> str:- lines = [- "=" * 60,- "LoCoMo V3 Evaluation Results",- "(Retrieval-Focused Memory Evaluation)",- "=" * 60,- "",- "RETRIEVAL QUALITY (Memory System Performance):",- f" Average Recall@{self.config.get('top_k', 10)}: {self.avg_retrieval_recall:.1%}",- f" Mean Reciprocal Rank: {self.avg_mrr:.3f}",- "",- "Retrieval by Category:",- ]-- for cat_name, metrics in sorted(self.retrieval_by_category.items()):- lines.append(- f" {cat_name}: {metrics['recall']:.1%} recall ({metrics['count']:.0f} questions)"- )-- lines.extend(- [- "",- "END-TO-END ACCURACY (LLM + Retrieval):",- f" Overall: {self.accuracy:.1%}",- "",- "Accuracy by Category:",- ]- )-- for cat_name, acc in sorted(self.accuracy_by_category.items()):- lines.append(f" {cat_name}: {acc:.1%}")-- lines.extend(- [- "",- f"Total Duration: {self.duration_seconds:.1f}s",- f"Total Cases: {self.total_cases}",- ]- )-- return "\n".join(lines)-- def save(self, path: Path | str) -> None:- path = Path(path)- with open(path, "w") as f:- json.dump(- {- "total_cases": self.total_cases,- "avg_retrieval_recall": self.avg_retrieval_recall,- "avg_mrr": self.avg_mrr,- "accuracy": self.accuracy,- "retrieval_by_category": self.retrieval_by_category,- "accuracy_by_category": self.accuracy_by_category,- "config": self.config,- "results": [r.to_dict() for r in self.results],- },- f,- indent=2,- )---class LoCoMoEvaluatorV3:- """Evaluator focused on retrieval quality.-- This evaluator answers the question:- "Can the memory system retrieve the dialogue turns that contain the answer?"-- Process:- 1. Store each dialogue turn as a separate memory (raw text, no extraction)- 2. For each question, retrieve top-k turns via semantic search- 3. Check if the evidence turns (ground truth) are in the retrieved set- 4. Optionally: generate answer from retrieved context, judge correctness- """-- def __init__(self, config: EvalConfigV3 | None = None):- self._config = config or EvalConfigV3()- self._backend: LocalBackend | Mem0Backend | None = None- self._turn_id_to_content: dict[str, str] = {} # Map dia_id -> memory content- self._debug_logs: list[dict] = []-- async def run(self) -> EvalResultV3:- start_time = time.time()-- # Load conversations- conversations = load_locomo(- n_conversations=self._config.n_conversations,- categories=self._config.categories,- skip_adversarial=self._config.skip_adversarial,- )-- logger.info(f"Loaded {len(conversations)} conversations")-- all_results: list[CaseResultV3] = []-- for conv_idx, conv in enumerate(conversations):- logger.info(- f"Processing conversation {conv_idx + 1}/{len(conversations)}: {conv.sample_id}"- )-- # Fresh backend for each conversation- if self._config.backend_type == "mem0":- mem0_config = Mem0Config(- mode=self._config.mem0_mode,- api_key=self._config.mem0_api_key,- enable_graph=self._config.mem0_enable_graph,- )- self._backend = Mem0Backend(mem0_config)- graph_status = "with graph" if self._config.mem0_enable_graph else "vector-only"- logger.info(f"Using Mem0 backend ({self._config.mem0_mode} mode, {graph_status})")- else:- db_dir = tempfile.gettempdir()- db_path = self._config.db_path or f"{db_dir}/locomo_v3_{uuid.uuid4().hex[:8]}.db"- self._backend = LocalBackend(LocalBackendConfig(db_path=db_path))- logger.info("Using LocalBackend")-- self._turn_id_to_content = {}-- try:- # Phase 1: Store raw dialogue turns- await self._store_dialogue_turns(conv)-- # Phase 2: Evaluate each question- for case in conv.qa_cases:- result = await self._evaluate_case(case, conv)- all_results.append(result)-- finally:- if self._backend and hasattr(self._backend, "close"):- await self._backend.close() # type: ignore[union-attr]-- # Aggregate results- duration = time.time() - start_time- return self._aggregate_results(all_results, duration)-- async def _store_dialogue_turns(self, conv: LoCoMoConversation) -> int:- """Store each dialogue turn as a memory.-- No LLM extraction - just raw dialogue with metadata.- """- assert self._backend is not None, "Backend must be initialized"- stored = 0- user_id = f"locomo_{conv.sample_id}"-- for session in conv.sessions:- for dialogue in session.dialogues:- # Format: include session date for temporal context- content = f"[{session.datetime}] {dialogue.speaker}: {dialogue.text}"-- # Store mapping for retrieval checking- self._turn_id_to_content[dialogue.dia_id] = content-- metadata = {- "dia_id": dialogue.dia_id,- "speaker": dialogue.speaker,- "session_num": session.session_num,- "session_datetime": session.datetime,- }-- # Save to memory system (different interface for Mem0 vs Local)- try:- if isinstance(self._backend, Mem0Backend):- memory = Memory(- content=content,- user_id=user_id,- importance=0.5,- metadata=metadata,- )- await self._backend.save_memory(memory)- else:- await self._backend.save_memory(- content=content,- user_id=user_id,- importance=0.5,- metadata=metadata,- )- stored += 1- except Exception as e:- logger.error(f"Failed to store dialogue {dialogue.dia_id}: {e}")- logger.error(f"Content was: {content[:200]}...")- raise # Re-raise to see full traceback-- logger.info(f"Stored {stored} dialogue turns")- return stored-- async def _evaluate_case(self, case: LoCoMoCase, conv: LoCoMoConversation) -> CaseResultV3:- """Evaluate a single QA case."""- assert self._backend is not None, "Backend must be initialized"- user_id = f"locomo_{conv.sample_id}"-- # Retrieve relevant turns - handle different backend interfaces- results: list[MemorySearchResult] | list[VectorSearchResult]- if isinstance(self._backend, Mem0Backend):- # Mem0 has its own optimized search with graph expansion- results = await self._backend.search_memories(- query=case.question,- user_id=user_id,- limit=self._config.top_k,- )- elif self._config.use_hybrid_search:- # LocalBackend hybrid search (vector + BM25)- results = await self._backend.hybrid_search(- query=case.question,- user_id=user_id,- top_k=self._config.top_k,- vector_weight=self._config.vector_weight,- text_weight=self._config.text_weight,- )- else:- # LocalBackend vector-only search- results = await self._backend.search_memories(- query=case.question,- user_id=user_id,- top_k=self._config.top_k,- )-- # Extract retrieved turn IDs from metadata- # Note: Mem0 may store our metadata in "custom_metadata", Local stores in "metadata"- retrieved_ids = []- retrieved_contents = []- for r in results:- metadata = r.memory.metadata or {}- dia_id = metadata.get("dia_id")- if dia_id:- retrieved_ids.append(dia_id)- retrieved_contents.append(r.memory.content)-- # Calculate retrieval metrics- evidence_ids = case.evidence or []-- # Recall: what % of evidence turns were retrieved?- if evidence_ids:- found = sum(1 for e in evidence_ids if e in retrieved_ids)- retrieval_recall = found / len(evidence_ids)- else:- retrieval_recall = 1.0 # No evidence needed-- # Rank of first evidence turn- retrieval_rank = None- for i, rid in enumerate(retrieved_ids):- if rid in evidence_ids:- retrieval_rank = i + 1 # 1-indexed- break-- # Generate answer from retrieved context- context = "\n".join(retrieved_contents)- predicted, judge_score, judge_reasoning = await self._generate_answer(- case.question,- str(case.answer),- context,- conv.speaker_a,- conv.speaker_b,- )-- # Determine correctness- is_correct = judge_score is not None and judge_score >= 0.7-- return CaseResultV3(- case=case,- retrieved_turn_ids=retrieved_ids,- evidence_turn_ids=evidence_ids,- retrieval_recall=retrieval_recall,- retrieval_rank=retrieval_rank,- predicted_answer=predicted,- is_correct=is_correct,- judge_score=judge_score,- judge_reasoning=judge_reasoning,- retrieved_context=context[:500], # Truncate for storage- )-- async def _generate_answer(- self,- question: str,- ground_truth: str,- context: str,- speaker_a: str,- speaker_b: str,- ) -> tuple[str, float | None, str | None]:- """Generate answer from context and judge correctness."""-- # Generate answer- answer_prompt = f"""Based on the following conversation excerpts, answer the question.-If the answer is not in the context, say "Information not found".--Give a SHORT, DIRECT answer - just the specific information requested.--Context:-{context}--Question: {question}--Answer:"""-- try:- response = await asyncio.to_thread(- litellm.completion,- model=self._config.answer_model,- messages=[{"role": "user", "content": answer_prompt}],- temperature=0.0,- max_tokens=100,- )- predicted = response.choices[0].message.content or "Information not found"- except Exception as e:- logger.warning(f"Answer generation failed: {e}")- predicted = "Error generating answer"-- # Judge correctness- judge_score = None- judge_reasoning = None-- if self._config.use_llm_judge:- judge_prompt = f"""You are evaluating if a predicted answer is semantically correct.--Question: {question}-Ground Truth Answer: {ground_truth}-Predicted Answer: {predicted}--Score the predicted answer from 0.0 to 1.0:-- 1.0: Completely correct, matches ground truth semantically-- 0.7-0.9: Mostly correct, captures the key information-- 0.4-0.6: Partially correct, some relevant information-- 0.1-0.3: Mostly incorrect but shows some understanding-- 0.0: Completely wrong or "not found" when answer exists--Respond in JSON format:-{{"score": 0.0, "reasoning": "brief explanation"}}"""-- try:- response = await asyncio.to_thread(- litellm.completion,- model=self._config.judge_model,- messages=[{"role": "user", "content": judge_prompt}],- temperature=0.0,- max_tokens=150,- )- content = response.choices[0].message.content or ""-- # Parse JSON response- import re-- json_match = re.search(r"\{.*\}", content, re.DOTALL)- if json_match:- data = json.loads(json_match.group())- judge_score = float(data.get("score", 0))- judge_reasoning = data.get("reasoning", "")- except Exception as e:- logger.warning(f"Judge failed: {e}")-- return predicted, judge_score, judge_reasoning-- def _aggregate_results(self, results: list[CaseResultV3], duration: float) -> EvalResultV3:- """Aggregate individual results."""- if not results:- return EvalResultV3(- total_cases=0,- avg_retrieval_recall=0.0,- avg_mrr=0.0,- retrieval_by_category={},- accuracy=0.0,- accuracy_by_category={},- config={"top_k": self._config.top_k},- duration_seconds=duration,- )-- # Overall retrieval metrics- avg_recall = sum(r.retrieval_recall for r in results) / len(results)-- # MRR: mean of 1/rank for first evidence found- mrr_sum = 0.0- for r in results:- if r.retrieval_rank is not None:- mrr_sum += 1.0 / r.retrieval_rank- avg_mrr = mrr_sum / len(results)-- # Per-category retrieval- retrieval_by_category = {}- for cat_id, cat_name in LOCOMO_CATEGORIES.items():- cat_results = [r for r in results if r.case.category == cat_id]- if cat_results:- cat_recall = sum(r.retrieval_recall for r in cat_results) / len(cat_results)- retrieval_by_category[cat_name] = {- "recall": cat_recall,- "count": len(cat_results),- }-- # End-to-end accuracy- correct = sum(1 for r in results if r.is_correct)- accuracy = correct / len(results)-- # Per-category accuracy- accuracy_by_category = {}- for cat_id, cat_name in LOCOMO_CATEGORIES.items():- cat_results = [r for r in results if r.case.category == cat_id]- if cat_results:- cat_correct = sum(1 for r in cat_results if r.is_correct)- accuracy_by_category[cat_name] = cat_correct / len(cat_results)-- return EvalResultV3(- total_cases=len(results),- avg_retrieval_recall=avg_recall,- avg_mrr=avg_mrr,- retrieval_by_category=retrieval_by_category,- accuracy=accuracy,- accuracy_by_category=accuracy_by_category,- results=results,- config={- "top_k": self._config.top_k,- "answer_model": self._config.answer_model,- "use_llm_judge": self._config.use_llm_judge,- },- duration_seconds=duration,- )---async def run_locomo_eval_v3(- config: EvalConfigV3 | None = None,- output_path: Path | str | None = None,-) -> EvalResultV3:- """Run V3 evaluation."""- evaluator = LoCoMoEvaluatorV3(config)- result = await evaluator.run()-- if output_path:- result.save(output_path)- logger.info(f"Results saved to {output_path}")-- return result---def run_locomo_eval_v3_sync(- config: EvalConfigV3 | None = None,- output_path: Path | str | None = None,-) -> EvalResultV3:- """Synchronous wrapper."""- return asyncio.run(run_locomo_eval_v3(config, output_path))diff --git a/headroom/evals/prompt_comparison.py b/headroom/evals/prompt_comparison.pydeleted file mode 100644index ea84af246..000000000--- a/headroom/evals/prompt_comparison.py+++ /dev/null@@ -1,639 +0,0 @@-"""Prompt Comparison System using LLM-as-Judge.--This module provides tools to verify that Headroom preserves the semantic-meaning of prompts by comparing prompts before and after processing.--This is CRITICAL for proving that Headroom doesn't change the meaning of prompts-when they go through the proxy.--Usage:- from headroom.evals.prompt_comparison import compare_prompts, PromptComparer-- # Simple comparison- result = compare_prompts(- original_prompt="What is the capital of France?",- headroom_modified_prompt="What is the capital of France?",- )- print(f"Equivalent: {result.are_equivalent}")-- # Full comparer with logging- comparer = PromptComparer()- result = comparer.compare(original, modified)- comparer.log_result(result)-"""--from __future__ import annotations--import json-import logging-import os-from dataclasses import dataclass, field-from datetime import datetime-from pathlib import Path-from typing import Any--from headroom.offline import guard_egress--logger = logging.getLogger(__name__)---# Judge prompt template for semantic equivalence checking-SEMANTIC_EQUIVALENCE_JUDGE_PROMPT = """You are an expert evaluator assessing whether two prompts are semantically equivalent.--Your task is to determine if a modified prompt preserves the exact same meaning, intent, and information as the original prompt.--CRITICAL: Even small changes in meaning matter. You must be extremely precise.--Consider these aspects:-1. **Core Intent**: Does the modified prompt ask for the same thing?-2. **Context Preservation**: Is all contextual information preserved?-3. **Constraints**: Are all constraints, requirements, and specifications preserved?-4. **Tone and Formality**: Are there significant tone changes that might affect the response?-5. **Information Content**: Is all factual information in the original still present?-6. **Ambiguity**: Does the modification introduce or remove ambiguity?--ORIGINAL PROMPT:-```-{original_prompt}-```--MODIFIED PROMPT:-```-{modified_prompt}-```--Analyze both prompts carefully and provide your assessment.--Respond in EXACTLY this format:-EQUIVALENT: <YES or NO>-CONFIDENCE: <HIGH, MEDIUM, or LOW>-DIFFERENCES: <List specific differences found, or "None" if equivalent>-REASONING: <Brief explanation of your assessment>-CONCERN_LEVEL: <NONE, LOW, MEDIUM, HIGH, or CRITICAL>"""---@dataclass-class PromptComparisonResult:- """Result of comparing two prompts for semantic equivalence.-- Attributes:- original: The original prompt before Headroom processing.- headroom_modified: The prompt after Headroom processing.- are_equivalent: Whether the prompts are semantically equivalent.- confidence: Confidence level of the judgment (HIGH, MEDIUM, LOW).- differences: List of specific differences found between prompts.- reasoning: The judge's explanation for the assessment.- concern_level: Level of concern about the differences (NONE to CRITICAL).- judge_model: The model used for judging.- timestamp: When the comparison was made.- raw_judge_response: The full response from the judge LLM.- metadata: Additional metadata about the comparison.- """-- original: str- headroom_modified: str- are_equivalent: bool- confidence: str = "MEDIUM"- differences: list[str] = field(default_factory=list)- reasoning: str = ""- concern_level: str = "NONE"- judge_model: str = "gpt-4o"- timestamp: str = field(default_factory=lambda: datetime.now().isoformat())- raw_judge_response: str = ""- metadata: dict[str, Any] = field(default_factory=dict)-- def to_dict(self) -> dict[str, Any]:- """Convert result to dictionary."""- return {- "original": self.original,- "headroom_modified": self.headroom_modified,- "are_equivalent": self.are_equivalent,- "confidence": self.confidence,- "differences": self.differences,- "reasoning": self.reasoning,- "concern_level": self.concern_level,- "judge_model": self.judge_model,- "timestamp": self.timestamp,- "raw_judge_response": self.raw_judge_response,- "metadata": self.metadata,- }-- @classmethod- def from_dict(cls, data: dict[str, Any]) -> PromptComparisonResult:- """Create result from dictionary."""- return cls(- original=data["original"],- headroom_modified=data["headroom_modified"],- are_equivalent=data["are_equivalent"],- confidence=data.get("confidence", "MEDIUM"),- differences=data.get("differences", []),- reasoning=data.get("reasoning", ""),- concern_level=data.get("concern_level", "NONE"),- judge_model=data.get("judge_model", "gpt-4o"),- timestamp=data.get("timestamp", datetime.now().isoformat()),- raw_judge_response=data.get("raw_judge_response", ""),- metadata=data.get("metadata", {}),- )-- def is_concerning(self) -> bool:- """Check if the result indicates concerning differences."""- return not self.are_equivalent or self.concern_level in ["HIGH", "CRITICAL"]-- def summary(self) -> str:- """Generate a human-readable summary."""- status = "PASS" if self.are_equivalent else "FAIL"- lines = [- f"=== Prompt Comparison Result: {status} ===",- f"Equivalent: {self.are_equivalent}",- f"Confidence: {self.confidence}",- f"Concern Level: {self.concern_level}",- f"Judge Model: {self.judge_model}",- ]-- if self.differences:- lines.append("Differences:")- for diff in self.differences:- lines.append(f" - {diff}")-- if self.reasoning:- lines.append(f"Reasoning: {self.reasoning}")-- return "\n".join(lines)---def _parse_judge_response(response_text: str) -> dict[str, Any]:- """Parse the judge's response to extract structured fields.-- Args:- response_text: Raw response from the judge LLM.-- Returns:- Dictionary with parsed fields.- """- result = {- "are_equivalent": False,- "confidence": "MEDIUM",- "differences": [],- "reasoning": "",- "concern_level": "MEDIUM",- }-- lines = response_text.strip().split("\n")-- for line in lines:- line = line.strip()-- if line.upper().startswith("EQUIVALENT:"):- value = line[len("EQUIVALENT:") :].strip().upper()- result["are_equivalent"] = value == "YES"-- elif line.upper().startswith("CONFIDENCE:"):- value = line[len("CONFIDENCE:") :].strip().upper()- if value in ["HIGH", "MEDIUM", "LOW"]:- result["confidence"] = value-- elif line.upper().startswith("DIFFERENCES:"):- diff_text = line[len("DIFFERENCES:") :].strip()- if diff_text.lower() != "none":- # Parse differences - could be comma-separated or a single item- if "," in diff_text:- result["differences"] = [d.strip() for d in diff_text.split(",")]- elif diff_text:- result["differences"] = [diff_text]-- elif line.upper().startswith("REASONING:"):- result["reasoning"] = line[len("REASONING:") :].strip()-- elif line.upper().startswith("CONCERN_LEVEL:"):- value = line[len("CONCERN_LEVEL:") :].strip().upper()- if value in ["NONE", "LOW", "MEDIUM", "HIGH", "CRITICAL"]:- result["concern_level"] = value-- return result---def compare_prompts(- original_prompt: str,- headroom_modified_prompt: str,- judge_model: str = "gpt-4o",- api_key: str | None = None,- metadata: dict[str, Any] | None = None,-) -> PromptComparisonResult:- """Compare two prompts using an LLM judge to verify semantic equivalence.-- This function sends both prompts to GPT-4o (or another specified model)- to determine if they are semantically equivalent.-- Args:- original_prompt: The original prompt before Headroom processing.- headroom_modified_prompt: The prompt after Headroom processing.- judge_model: The OpenAI model to use as judge (default: gpt-4o).- api_key: OpenAI API key (uses OPENAI_API_KEY env var if not provided).- metadata: Optional metadata to attach to the result.-- Returns:- PromptComparisonResult with the comparison outcome.-- Raises:- ImportError: If openai package is not installed.- ValueError: If API key is not provided and not in environment.-- Example:- result = compare_prompts(- original_prompt="Explain quantum computing in simple terms.",- headroom_modified_prompt="Explain quantum computing in simple terms.",- )- if not result.are_equivalent:- print(f"WARNING: Prompts differ! {result.differences}")- """- try:- from openai import OpenAI- except ImportError as e:- raise ImportError(- "OpenAI package required for prompt comparison. Install with: pip install openai"- ) from e-- # Get API key- resolved_api_key = api_key or os.environ.get("OPENAI_API_KEY")- if not resolved_api_key:- raise ValueError(- "OpenAI API key required. Set OPENAI_API_KEY environment variable "- "or pass api_key parameter."- )-- guard_egress("OpenAI API for the prompt-comparison judge", "api.openai.com")- client = OpenAI(api_key=resolved_api_key)-- # Build the judge prompt- judge_prompt = SEMANTIC_EQUIVALENCE_JUDGE_PROMPT.format(- original_prompt=original_prompt,- modified_prompt=headroom_modified_prompt,- )-- # Call the judge- response = client.chat.completions.create(- model=judge_model,- messages=[{"role": "user", "content": judge_prompt}],- temperature=0.0, # Deterministic for consistent evaluation- max_tokens=500,- )-- raw_response = response.choices[0].message.content or ""-- # Parse the response- parsed = _parse_judge_response(raw_response)-- return PromptComparisonResult(- original=original_prompt,- headroom_modified=headroom_modified_prompt,- are_equivalent=parsed["are_equivalent"],- confidence=parsed["confidence"],- differences=parsed["differences"],- reasoning=parsed["reasoning"],- concern_level=parsed["concern_level"],- judge_model=judge_model,- raw_judge_response=raw_response,- metadata=metadata or {},- )---class PromptComparer:- """A class for comparing prompts with logging and persistence.-- This class provides a stateful interface for comparing prompts,- with support for logging results to files and tracking history.-- Attributes:- judge_model: The model to use for judging.- log_dir: Directory for storing comparison logs.- comparison_history: List of all comparisons made.-- Example:- comparer = PromptComparer(log_dir="./comparison_logs")-- # Compare prompts- result = comparer.compare(original, modified)-- # Log the result- comparer.log_result(result)-- # Get summary of all comparisons- print(comparer.summary())- """-- def __init__(- self,- judge_model: str = "gpt-4o",- api_key: str | None = None,- log_dir: str | Path | None = None,- ):- """Initialize the PromptComparer.-- Args:- judge_model: The OpenAI model to use as judge.- api_key: OpenAI API key (uses OPENAI_API_KEY env var if not provided).- log_dir: Optional directory for storing comparison logs.- """- self.judge_model = judge_model- self.api_key = api_key- self.log_dir = Path(log_dir) if log_dir else None- self.comparison_history: list[PromptComparisonResult] = []-- if self.log_dir:- self.log_dir.mkdir(parents=True, exist_ok=True)-- def compare(- self,- original_prompt: str,- headroom_modified_prompt: str,- metadata: dict[str, Any] | None = None,- ) -> PromptComparisonResult:- """Compare two prompts and track the result.-- Args:- original_prompt: The original prompt before Headroom processing.- headroom_modified_prompt: The prompt after Headroom processing.- metadata: Optional metadata to attach to the result.-- Returns:- PromptComparisonResult with the comparison outcome.- """- result = compare_prompts(- original_prompt=original_prompt,- headroom_modified_prompt=headroom_modified_prompt,- judge_model=self.judge_model,- api_key=self.api_key,- metadata=metadata,- )-- self.comparison_history.append(result)- return result-- def log_result(self, result: PromptComparisonResult) -> None:- """Log a comparison result.-- Args:- result: The comparison result to log.- """- # Log to standard logger- if result.are_equivalent:- logger.info(- f"Prompt comparison PASSED - Confidence: {result.confidence}, "- f"Concern: {result.concern_level}"- )- else:- logger.warning(- f"Prompt comparison FAILED - Differences: {result.differences}, "- f"Concern: {result.concern_level}"- )-- # Log to file if configured- if self.log_dir:- log_file = self.log_dir / "comparisons.jsonl"- with open(log_file, "a") as f:- f.write(json.dumps(result.to_dict()) + "\n")-- def compare_and_log(- self,- original_prompt: str,- headroom_modified_prompt: str,- metadata: dict[str, Any] | None = None,- ) -> PromptComparisonResult:- """Compare two prompts and immediately log the result.-- Args:- original_prompt: The original prompt before Headroom processing.- headroom_modified_prompt: The prompt after Headroom processing.- metadata: Optional metadata to attach to the result.-- Returns:- PromptComparisonResult with the comparison outcome.- """- result = self.compare(original_prompt, headroom_modified_prompt, metadata)- self.log_result(result)- return result-- def get_concerning_results(self) -> list[PromptComparisonResult]:- """Get all results with concerning differences.-- Returns:- List of results where prompts were not equivalent or had high concern.- """- return [r for r in self.comparison_history if r.is_concerning()]-- def summary(self) -> str:- """Generate a summary of all comparisons.-- Returns:- Human-readable summary string.- """- if not self.comparison_history:- return "No comparisons performed yet."-- total = len(self.comparison_history)- equivalent = sum(1 for r in self.comparison_history if r.are_equivalent)- concerning = len(self.get_concerning_results())-- lines = [- "=== Prompt Comparison Summary ===",- f"Total comparisons: {total}",- f"Equivalent: {equivalent} ({equivalent / total * 100:.1f}%)",- f"Non-equivalent: {total - equivalent}",- f"Concerning: {concerning}",- ]-- if concerning > 0:- lines.append("\nConcerning comparisons:")- for result in self.get_concerning_results():- lines.append(f" - {result.concern_level}: {result.differences}")-- return "\n".join(lines)-- def export_history(self, path: str | Path) -> None:- """Export comparison history to a JSON file.-- Args:- path: Path to save the history.- """- path = Path(path)- path.parent.mkdir(parents=True, exist_ok=True)-- with open(path, "w") as f:- json.dump(- {- "judge_model": self.judge_model,- "total_comparisons": len(self.comparison_history),- "comparisons": [r.to_dict() for r in self.comparison_history],- "summary": {- "equivalent_count": sum(- 1 for r in self.comparison_history if r.are_equivalent- ),- "concerning_count": len(self.get_concerning_results()),- },- },- f,- indent=2,- )---def compare_messages(- original_messages: list[dict[str, Any]],- modified_messages: list[dict[str, Any]],- judge_model: str = "gpt-4o",- api_key: str | None = None,-) -> PromptComparisonResult:- """Compare two message lists (OpenAI/Anthropic format) for semantic equivalence.-- This is useful for comparing full conversation histories, not just single prompts.-- Args:- original_messages: Original message list before Headroom.- modified_messages: Modified message list after Headroom.- judge_model: The OpenAI model to use as judge.- api_key: OpenAI API key.-- Returns:- PromptComparisonResult with the comparison outcome.- """-- # Convert messages to string representation for comparison- def messages_to_string(messages: list[dict[str, Any]]) -> str:- parts = []- for msg in messages:- role = msg.get("role", "unknown")- content = msg.get("content", "")-- # Handle content that's a list (multimodal)- if isinstance(content, list):- text_parts = []- for item in content:- if isinstance(item, dict):- if item.get("type") == "text":- text_parts.append(item.get("text", ""))- elif item.get("type") == "image_url":- text_parts.append("[IMAGE]")- elif item.get("type") == "image":- text_parts.append("[IMAGE]")- else:- text_parts.append(str(item))- content = " ".join(text_parts)-- parts.append(f"[{role}]: {content}")-- return "\n\n".join(parts)-- original_str = messages_to_string(original_messages)- modified_str = messages_to_string(modified_messages)-- result = compare_prompts(- original_prompt=original_str,- headroom_modified_prompt=modified_str,- judge_model=judge_model,- api_key=api_key,- metadata={- "comparison_type": "messages",- "original_message_count": len(original_messages),- "modified_message_count": len(modified_messages),- },- )-- return result---def batch_compare_prompts(- prompt_pairs: list[tuple[str, str]],- judge_model: str = "gpt-4o",- api_key: str | None = None,- max_concurrent: int = 5,-) -> list[PromptComparisonResult]:- """Compare multiple prompt pairs in parallel.-- Args:- prompt_pairs: List of (original, modified) prompt pairs.- judge_model: The OpenAI model to use as judge.- api_key: OpenAI API key.- max_concurrent: Maximum concurrent API calls.-- Returns:- List of comparison results in the same order as input pairs.- """- from concurrent.futures import ThreadPoolExecutor, as_completed-- results: list[PromptComparisonResult | None] = [None] * len(prompt_pairs)-- def compare_pair(- index: int, original: str, modified: str- ) -> tuple[int, PromptComparisonResult]:- result = compare_prompts(- original_prompt=original,- headroom_modified_prompt=modified,- judge_model=judge_model,- api_key=api_key,- metadata={"batch_index": index},- )- return index, result-- with ThreadPoolExecutor(max_workers=max_concurrent) as executor:- futures = [- executor.submit(compare_pair, i, original, modified)- for i, (original, modified) in enumerate(prompt_pairs)- ]-- for future in as_completed(futures):- index, result = future.result()- results[index] = result-- # Filter out None values (shouldn't happen, but for type safety)- return [r for r in results if r is not None]---def verify_headroom_preservation(- original_messages: list[dict[str, Any]],- headroom_messages: list[dict[str, Any]],- judge_model: str = "gpt-4o",- api_key: str | None = None,- fail_on_difference: bool = False,-) -> PromptComparisonResult:- """Verify that Headroom preserves the semantic meaning of a request.-- This is the main entry point for verifying Headroom's accuracy preservation.- It compares the original messages with those modified by Headroom and- raises an error if differences are found (when fail_on_difference=True).-- Args:- original_messages: Messages before Headroom processing.- headroom_messages: Messages after Headroom processing.- judge_model: The model to use for judging.- api_key: OpenAI API key.- fail_on_difference: If True, raise an error when prompts differ.-- Returns:- PromptComparisonResult with the verification outcome.-- Raises:- ValueError: If fail_on_difference=True and prompts are not equivalent.-- Example:- # Capture messages before and after Headroom- original = [{"role": "user", "content": "Hello, how are you?"}]- after_headroom = [{"role": "user", "content": "Hello, how are you?"}]-- result = verify_headroom_preservation(- original, after_headroom, fail_on_difference=True- )- """- result = compare_messages(- original_messages=original_messages,- modified_messages=headroom_messages,- judge_model=judge_model,- api_key=api_key,- )-- if fail_on_difference and not result.are_equivalent:- raise ValueError(- f"Headroom modified prompt semantics! "- f"Differences: {result.differences}. "- f"Reasoning: {result.reasoning}"- )-- return result