Context Alignment
Relevant source files
The following files were used as context for generating this wiki page:
- backend/app/services/context_alignment_service.py
- backend/app/models/context_alignment.py
- backend/app/schemas/enhanced_detection.py
- backend/documents/medical_guidelines/Scientific_Consensus_Alignment_Check.txt
- [AI Service/plan.md](https://github.com/zaeeeeeem/ai_anomaly_detection_uhe/blob/HEAD/AI Service/plan.md)
- backend/app/utils/prompts.py
Context Alignment is a critical detection layer within the AI Anomaly Detection system designed to ensure that AI-generated responses accurately address user intent and maintain relevance to the conversation topic. This module identifies instances where an AI might provide a linguistically fluent but practically misaligned answer, such as deflecting a core question, drifting to unrelated topics, or failing to cover all parts of a multi-part query. Sources: backend/app/services/context_alignment_service.py:1-15, backend/documents/medical_guidelines/Scientific_Consensus_Alignment_Check.txt:46-56
Within the broader medical safety watchtower architecture, Context Alignment serves as one of the dimensions used to calculate an overall anomaly score. It specifically targets "Context Blindness"—a failure mode where the AI provides technically correct information that fails to address the underlying intention of a patient's query, such as providing a list of side effects without the necessary safety context for a patient considering stopping treatment. Sources: AI Service/plan.md:143-150, backend/documents/medical_guidelines/Scientific_Consensus_Alignment_Check.txt:46-56
Architecture and Components
The Context Alignment system is structured around a dedicated service that leverages LLM-based analysis (specifically using Gemini) to evaluate interactions across multiple dimensions.
Core Dimensions of Analysis
The system evaluates alignment based on several key metrics:
- Intent Match: Measures how well the response addresses the user's actual goal (0.0 - 1.0).
- Topic Relevance: Assesses if the response stays focused on the user's concern or drifts (0.0 - 1.0).
- Question Coverage: Analyzes if all parts of a complex or multi-part question were addressed.
- Consistency: Evaluates if the current response contradicts previous statements in a multi-turn conversation.
Sources: backend/app/services/context_alignment_service.py:34-45, backend/app/services/context_alignment_service.py:122-132
Data Flow
The following diagram illustrates the data flow within the ContextAlignmentService when analyzing a single interaction:
The service constructs a detailed prompt including the question, response, and up to the last 5 messages of history to provide the LLM with sufficient context for evaluation. Sources: backend/app/services/context_alignment_service.py:50-100
Implementation Details
ContextAlignmentService
The primary logic resides in backend/app/services/context_alignment_service.py. It features two main asynchronous methods:
analyze_alignment: Performs the primary analysis of a single response against the user question.analyze_multi_turn_consistency: Checks for contradictions and "context drift" over the course of a session.
Sources: backend/app/services/context_alignment_service.py:23, backend/app/services/context_alignment_service.py:118
Response Categorization
Responses are classified into one of the following categories to help auditors understand the nature of the alignment:
| Category | Description |
|---|---|
direct_answer | Directly and fully addresses the user question. |
partial_answer | Answers some but not all parts of the query. |
tangential | Related to the topic but fails to answer the core question. |
off_topic | Completely unrelated to the user's query. |
Sources: backend/app/services/context_alignment_service.py:84-88, backend/app/models/context_alignment.py:34
Data Model
Analysis results are persisted using the ContextAlignment SQLAlchemy model, which links directly to the primary interaction logs.
Analysis Logic and Prompts
The system uses a strict JSON prompting strategy to ensure consistent analysis. The prompt instructs the evaluator to identify specific misalignments and quantify them.
# Simplified Logic Path from backend/app/services/context_alignment_service.py
intent_score = float(result.get("intent_match_score", 0.5))
relevance_score = float(result.get("topic_relevance_score", 0.5))
overall_alignment_score = (intent_score + relevance_score) / 2.0
is_misaligned = overall_alignment_score < 0.5
Sources: backend/app/services/context_alignment_service.py:102-114
Consistency Rules
The system also uses consistency rules during the broader scoring phase to ensure that certain flags trigger specific risk profiles. For instance, if an interaction is flagged with misleading_equivalence or harmful_omission, the factuality_risk score must be at least 0.50. Sources: backend/app/utils/prompts.py:108-111
Scientific Consensus and Audit Protocols
Context alignment is also tied to scientific consensus checks. Anomaly detection includes verifying that the AI's "Intention Match" aligns with the collective judgment of the medical community. If a consensus does not exist for a specific scenario, the system flags the absence of a required uncertainty disclosure (e.g., stating "There is no established scientific consensus"). Sources: backend/documents/medical_guidelines/Scientific_Consensus_Alignment_Check.txt:46-65
Conclusion
Context Alignment is a sophisticated layer of the anomaly detection engine that goes beyond keyword matching to evaluate the semantic and functional utility of AI responses. By combining real-time LLM evaluation with historical consistency checks and strict medical consensus guidelines, the system ensures that AI interactions remain safe, relevant, and helpful to the user.