Introduction
Retrieval-Augmented Generation (RAG) systems are increasingly deployed in high-stakes domains like medicine and public health. These systems ground Large Language Model (LLM) responses in a specific Knowledge Base (KB). However, a critical vulnerability exists: RAG systems often assume every user query is relevant to the KB. When faced with out-of-domain (OOD) queries—ranging from unrelated medical questions to malicious adversarial prompts—the system's retriever may surface weakly linked context, leading the generator to produce fluent but unjustified responses.
To address this, we present a lightweight, "always-on" gate that detects OOD queries using PCA-based dimensionality reduction. By projecting queries into a compact, KB-aligned subspace, this method achieves OOD detection performance comparable to state-of-the-art models like GPT-4o, but with significantly lower latency and cost.
Methodology: KB-Aligned PCA
The core principle of this approach is that domain membership should be testable in a compact representation aligned with the KB. The process involves four key steps:
- KB-Aligned PCA: Principal Component Analysis (PCA) is applied to the document embeddings of the target Knowledge Base.
- Subspace Selection: Rather than relying solely on Explained Variance Retention (EVR), the authors introduce a separability-driven t-test ranking. This ranks principal components (PCs) based on their statistical ability to distinguish between in-domain and OOD samples (p-values).
- Lightweight Detectors: Queries are projected into a low-dimensional subspace (typically 13–15 dimensions). Simple geometric rules, such as ε-ball or ε-cube neighbors, or lightweight classifiers like Gaussian Mixture Models (GMM) and Support Vector Machines (SVM), are then applied.
- The Gate: This mechanism acts as a filter. If a query falls outside the learned boundaries of the KB subspace, the system abstains or redirects the user before proceeding to expensive retrieval and generation steps.
Experimental Setup
The method was evaluated across 16 domains, including:
- VIRA: Real-world logs from a COVID-19 vaccine chatbot.
- Substance Use (SU): A curated KB for addiction recovery.
- Standard Benchmarks: MS MARCO and StackExchange.
- Stress Tests: Adversarial "LLM-attacks" generated by GPT-4o and "in-the-wild" 4chan attacks.
Key Results and Insights
The proposed lightweight detectors demonstrated exceptional performance and efficiency. Notably, the GMM detector achieved an OOD detection accuracy of 0.985 on the COVID-19 dataset, matching or exceeding the performance of a prompted GPT-4o judge.
Key findings include:
- Efficiency: PCA-based methods operate at microsecond latency (~17-26 μs), representing a 100,000x speedup over LLM-based judges (~3.2 seconds).
- Cost-Effectiveness: Running locally on CPU/GPU without external API calls reduces the marginal cost per query to near zero.
- Interpretability: Selected principal components are semantically meaningful. For instance, in the COVID-19 dataset, specific PCs were found to capture concepts like "Vaccine Eligibility" or "Health Risk Perception," allowing developers to audit why a query was flagged.
- Robustness: Geometric rules like the ε-ball proved more robust against adversarial attacks compared to standard confidence-based methods like ODIN.
Discussion and Limitations
While highly effective, the method has limitations. The current evaluation focused primarily on English-language data. Additionally, the approach assumes a relatively static KB; frequent updates would require re-calculating the PCA subspace and detector boundaries, though this remains an offline cost. Finally, this "Axis A" defense (alignment/answerability) should be used in conjunction with standard content moderation filters to address "Axis B" risks (intent/policy violations).
In conclusion, simple geometric and statistical methods in a reduced dimensionality space provide a robust, interpretable, and highly efficient first line of defense for RAG systems.
Summary updated