ContextRelevanceGuardrail

org.llm4s.agent.guardrails.rag.ContextRelevanceGuardrail
See theContextRelevanceGuardrail companion object
class ContextRelevanceGuardrail(val llmClient: LLMClient, val threshold: Double, val minRelevantRatio: Double, val onFail: GuardrailAction) extends RAGGuardrail

LLM-based guardrail to validate that retrieved chunks are relevant to the query.

ContextRelevanceGuardrail uses an LLM to evaluate whether the chunks retrieved from a vector store are actually relevant to the user's original query. This is critical for RAG quality - retrieving irrelevant chunks leads to poor answers.

Evaluation process:

  1. Each chunk is evaluated for relevance to the query
  2. Relevance is scored from 0.0 (completely irrelevant) to 1.0 (highly relevant)
  3. Overall score is computed as average chunk relevance
  4. Context passes if enough chunks are relevant

Use cases:

  • Detect retrieval failures before generating responses
  • Filter out irrelevant chunks before sending to LLM
  • Measure and monitor retrieval quality

Example usage:

val guardrail = ContextRelevanceGuardrail(llmClient, threshold = 0.6)

val context = RAGContext(
 query = "What are the symptoms of diabetes?",
 retrievedChunks = Seq(
   "Diabetes symptoms include increased thirst...",
   "The history of the Roman Empire..." // Irrelevant
 )
)

// Validate that retrieved context is relevant
guardrail.validateWithContext(response, context)

Value parameters

llmClient

The LLM client for evaluation

minRelevantRatio

Minimum ratio of relevant chunks required (default: 0.5)

onFail

Action to take when relevance is insufficient (default: Block)

threshold

Minimum relevance score for a chunk to be considered relevant (default: 0.5)

Attributes

Companion
object
Graph
Supertypes
trait RAGGuardrail
trait Guardrail[String]
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

override def validate(value: String): Result[String]

Standard validate without context.

Standard validate without context.

Attributes

Definition Classes
override def validateWithContext(output: String, context: RAGContext): Result[String]

Validate that retrieved context is relevant to the query.

Validate that retrieved context is relevant to the query.

Attributes

Definition Classes

Inherited methods

def andThen(other: Guardrail[String]): Guardrail[String]

Compose this guardrail with another sequentially.

Compose this guardrail with another sequentially.

The second guardrail runs only if this one passes.

Value parameters

other

The guardrail to run after this one

Attributes

Returns

A composite guardrail that runs both in sequence

Inherited from:
Guardrail
def transform(output: String): String

Optional: Transform the output after validation. Default is identity (no transformation).

Optional: Transform the output after validation. Default is identity (no transformation).

Value parameters

output

The validated output

Attributes

Returns

The transformed output

Inherited from:
OutputGuardrail
def transformWithContext(output: String, context: RAGContext): String

Optional transformation with context.

Optional transformation with context.

Override to modify the output based on RAG context (e.g., add citations). Default implementation returns output unchanged.

Value parameters

context

The RAG context

output

The validated output

Attributes

Returns

The transformed output

Inherited from:
RAGGuardrail

Concrete fields

override val description: Option[String]

Optional description of what this guardrail validates.

Optional description of what this guardrail validates.

Attributes

val minRelevantRatio: Double
val name: String

Name of this guardrail for logging and error messages.

Name of this guardrail for logging and error messages.

Attributes

val threshold: Double