SourceAttributionGuardrail

org.llm4s.agent.guardrails.rag.SourceAttributionGuardrail
See theSourceAttributionGuardrail companion object
class SourceAttributionGuardrail(val llmClient: LLMClient, val requireAttributions: Boolean, val minAttributionScore: Double, val onFail: GuardrailAction) extends RAGGuardrail

LLM-based guardrail to validate that responses properly cite their sources.

SourceAttributionGuardrail ensures that RAG responses include proper citations to the source documents from which information was derived. This is important for transparency, verifiability, and trust.

Evaluation criteria:

  • Does the response cite sources for factual claims?
  • Are the citations accurate (pointing to the right chunks)?
  • Are all major claims properly attributed?

Use cases:

  • Ensure transparency in RAG responses
  • Enable users to verify information
  • Comply with requirements for attributing sources
  • Detect when responses fail to cite available sources

Example usage:

val guardrail = SourceAttributionGuardrail(llmClient)

val context = RAGContext.withSources(
 query = "What causes climate change?",
 chunks = Seq("Human activities release greenhouse gases..."),
 sources = Seq("IPCC Report 2023.pdf")
)

// Response should cite sources
val response = "According to the IPCC Report, human activities release greenhouse gases..."
guardrail.validateWithContext(response, context)

Value parameters

llmClient

The LLM client for evaluation

minAttributionScore

Minimum attribution quality score (default: 0.5)

onFail

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

requireAttributions

Whether citations are required (default: true)

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 transformWithContext(output: String, context: RAGContext): String

Transform response to add citations if in Fix mode.

Transform response to add citations if in Fix mode.

Attributes

Definition Classes
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 response properly attributes sources.

Validate that response properly attributes sources.

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

Concrete fields

override val description: Option[String]

Optional description of what this guardrail validates.

Optional description of what this guardrail validates.

Attributes

val minAttributionScore: Double
val name: String

Name of this guardrail for logging and error messages.

Name of this guardrail for logging and error messages.

Attributes

val requireAttributions: Boolean