Base trait for RAG-specific guardrails that need retrieval context.
RAGGuardrail extends OutputGuardrail with additional context-aware validation methods. This allows guardrails to validate responses against the retrieved chunks from a RAG pipeline, enabling:
Grounding checks: Verify the response is supported by retrieved context
Context relevance: Check if retrieved chunks are relevant to the query
Source attribution: Ensure citations are accurate and present
RAG guardrails should implement validateWithContext for full RAG validation. The standard validate method provides a fallback for non-RAG usage.
Example usage:
// Create a grounding guardrail
val grounding = GroundingGuardrail(llmClient, threshold = 0.8)
// Validate with RAG context
val context = RAGContext(
query = "What is photosynthesis?",
retrievedChunks = Seq("Plants convert sunlight...", "Chlorophyll absorbs...")
)
grounding.validateWithContext("Photosynthesis converts light to energy", context)
Attributes
See also
GroundingGuardrail for factuality validation
ContextRelevanceGuardrail for chunk relevance checks
SourceAttributionGuardrail for citation verification
This is the primary validation method for RAG guardrails. It receives the response along with the original query and retrieved chunks, allowing for context-aware validation.
Value parameters
context
The RAG context containing query and retrieved chunks
output
The generated response to validate
Attributes
Returns
Right(output) if valid, Left(error) if validation fails