GroundingGuardrail

org.llm4s.agent.guardrails.rag.GroundingGuardrail
See theGroundingGuardrail companion object
class GroundingGuardrail(val llmClient: LLMClient, val threshold: Double, val onFail: GuardrailAction, val strictMode: Boolean) extends RAGGuardrail

LLM-based grounding guardrail for RAG validation.

GroundingGuardrail uses an LLM to evaluate whether a response is factually grounded in the retrieved context. This is critical for RAG applications to prevent hallucination and ensure answer quality.

Evaluation process:

  1. Each claim in the response is checked against the retrieved chunks
  2. Claims are classified as: supported, not supported, or contradicted
  3. An overall grounding score is computed
  4. Response passes if score >= threshold

Scoring:

  • 1.0: All claims are fully supported by the context
  • 0.5-0.9: Most claims supported, some unverifiable
  • 0.0-0.4: Many claims not supported or contradicted

Example usage:

val guardrail = GroundingGuardrail(llmClient, threshold = 0.8)

// Use in RAG pipeline
val context = RAGContext(
 query = "What causes climate change?",
 retrievedChunks = Seq(
   "Greenhouse gases trap heat in the atmosphere...",
   "Human activities release CO2 and methane..."
 )
)

guardrail.validateWithContext(response, context)

Value parameters

llmClient

The LLM client for evaluation

onFail

Action to take when grounding fails (default: Block)

strictMode

If true, ANY ungrounded claim fails. If false, uses score threshold.

threshold

Minimum grounding score to pass (default: 0.7)

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 - falls back to pass-through.

Standard validate without context - falls back to pass-through.

For full grounding validation, use validateWithContext.

Attributes

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

Validate output against RAG context for factual grounding.

Validate output against RAG context for factual grounding.

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 name: String

Name of this guardrail for logging and error messages.

Name of this guardrail for logging and error messages.

Attributes

val strictMode: Boolean
val threshold: Double