Backend RAG Pipeline
Relevant source files
The following files were used as context for generating this wiki page:
- AI Service/watchtower/rag_system.py
- AI Service/plan.md
- backend/app/utils/prompts.py
- backend/documents/medical_guidelines/Detection_of_Hallucinated_Medical_Citations_and_Data.txt
- backend/documents/medical_guidelines/ESI_Decision_Point_Failure_Analysis.txt
- backend/documents/medical_guidelines/Absence_of_Required_Medical_Disclaimers.txt
The Backend Retrieval-Augmented Generation (RAG) Pipeline is a specialized component of the AI Misbehavior Watchtower designed to provide grounded, evidence-based explanations for detected anomalies in medical LLM outputs. It serves as the "Explainability Layer" (Level 4) of the multi-agent system, moving beyond simple error flagging to provide clinical justifications and citations from authoritative medical guidelines.
The pipeline ingests medical safety documents, chunks them into manageable segments, and performs semantic retrieval based on the context of a flagged interaction. By surfacing specific guidance from documents like the Emergency Severity Index (ESI) or WHO medication safety protocols, it ensures that the Watchtower's critiques are verifiable and actionable for human reviewers.
Sources: AI Service/plan.md:1-12, AI Service/watchtower/rag_system.py:27-52
System Architecture
The RAG Pipeline operates within the WatchtowerOrchestrator and is triggered whenever the AnomalyScoringAgent identifies an interaction exceeding a specific risk threshold. It utilizes a RAGSystem class to manage document ingestion and querying against a persistence layer.
Core Components
| Component | Responsibility |
|---|---|
| RAGSystem | Main interface for ingesting documents and generating ExplanationRecord objects. |
| SQLiteStore | The persistence layer used to store and load RAG chunks. |
| Document Corpus | A collection of local TXT and markdown files containing medical safety guidelines. |
| Explanation Agent | An LLM-driven agent (e.g., Gemini) that uses retrieved context to draft the final critique. |
Sources: AI Service/watchtower/rag_system.py:10-14, AI Service/plan.md:162-178, backend/app/utils/prompts.py:136-167
Data Flow Diagram
The following diagram illustrates the lifecycle of a document from ingestion to its use in generating an anomaly explanation.
Document Ingestion and Processing
The system processes raw text guidelines into a searchable vector-like format using word-overlap scoring.
Chunking Strategy
Documents are split into chunks of approximately 700 characters with an 80-character overlap to maintain context between segments. Each chunk is stored with metadata including doc_id, section, source, and associated risk_type.
def _chunk_text(self, text: str, chunk_size: int, overlap: int) -> List[str]:
words = text.split()
chunks = []
start = 0
while start < len(words):
end = min(len(words), start + chunk_size)
chunks.append(" ".join(words[start:end]))
start = max(0, end - overlap)
return chunks
Sources: AI Service/watchtower/rag_system.py:16-33, AI Service/watchtower/rag_system.py:88-102
Pre-defined Medical Corpus
The pipeline is seeded with specialized audit protocols that define medical anomalies:
- ESI Decision Point Analysis: Logic for verifying Emergency Severity Index levels (1-5).
- Absence of Disclaimers: Protocols for detecting missing liability shields.
- Hallucination Detection: Criteria for identifying fabricated citations (ACHMI metrics).
- Readability Analysis: Benchmarks for Flesch Reading Ease Scores.
Sources: backend/documents/medical_guidelines/ESI_Decision_Point_Failure_Analysis.txt:1-15, backend/documents/medical_guidelines/Absence_of_Required_Medical_Disclaimers.txt:1-12, backend/documents/medical_guidelines/Detection_of_Hallucinated_Medical_Citations_and_Data.txt:1-10
Retrieval and Explanation Generation
When an interaction is flagged, the pipeline executes a multi-step retrieval process to provide context.
1. Risk Type Inference
The system maps internal safety flags to specific risk categories to filter the document search space.
| Flag Pattern | Inferred Risk Type |
|---|---|
weak_triage_for_emergency, emergency_case | triage |
gives_specific_dose, gives_medication_dosing | dosing |
missing_disclaimer | disclaimer |
| Other | other |
Sources: AI Service/watchtower/rag_system.py:104-111, AI Service/plan.md:180-185
2. Semantic Retrieval (Token Overlap)
The system performs a search by tokenizing the query (composed of the user prompt, AI response, and triggered flags) and calculating an overlap score against stored chunks.
3. LLM Critique (Level 4 Agent)
Retrieved context is passed to the LEVEL_4_EXPLANATION_PROMPT. The agent must produce a critique identifying the specific problematic content or omission.
Task Requirements:
- Produce a concise explanation (2-5 sentences).
- Identify the exact clinical failure (e.g., "Under-triage," "Unsafe dosing").
- Cite relevant
doc_idandchunk_id.
Sources: backend/app/utils/prompts.py:136-167, AI Service/plan.md:190-205
Explanation Schema
The output of the RAG pipeline is an ExplanationRecord, which is eventually rendered in the Watchtower UI for human review.
| Field | Type | Description |
|---|---|---|
interaction_id | UUID | Links the explanation to the original interaction log. |
risk_type | String | Categories: triage, dosing, disclaimer, self_harm, other. |
explanation | String | A specific critique based on retrieved guidelines. |
citations | List[Dict] | List of objects containing doc_id, chunk_id, and score. |
Sources: AI Service/plan.md:46-55, AI Service/watchtower/rag_system.py:44-52
Summary
The Backend RAG Pipeline transforms the Watchtower from a black-box monitoring tool into a transparent audit system. By anchoring critiques in established medical guidelines—such as the ESI triage logic or WHO medication protocols—it provides the evidentiary weight necessary for human clinical oversight of LLM interactions.
Sources: AI Service/plan.md:162-170, backend/documents/medical_guidelines/ESI_Decision_Point_Failure_Analysis.txt:115-121