TopicBoundaryGuardrail

org.llm4s.agent.guardrails.rag.TopicBoundaryGuardrail
See theTopicBoundaryGuardrail companion object
class TopicBoundaryGuardrail(val llmClient: LLMClient, val allowedTopics: Seq[String], val threshold: Double, val onFail: GuardrailAction) extends InputGuardrail

LLM-based guardrail to ensure queries stay within allowed topic boundaries.

TopicBoundaryGuardrail validates that user queries are within the scope of allowed topics for the RAG application. This is useful for:

  • Keeping conversations focused on the intended domain
  • Preventing misuse of specialized assistants
  • Ensuring the knowledge base is appropriate for the query

Evaluation process:

  1. The query is analyzed to determine its topic/intent
  2. The detected topic is compared against allowed topics
  3. Query passes if it matches at least one allowed topic

Use cases:

  • Domain-specific assistants (medical, legal, technical)
  • Customer support bots with defined scope
  • Knowledge bases with specific subject matter
  • Compliance with usage policies

Example usage:

val guardrail = TopicBoundaryGuardrail(
 llmClient,
 allowedTopics = Seq("scala programming", "functional programming", "software development"),
 threshold = 0.6
)

// On-topic query
guardrail.validate("How do I use pattern matching in Scala?") // passes

// Off-topic query
guardrail.validate("What's the best pizza restaurant?") // fails

Value parameters

allowedTopics

List of topics that queries should relate to

llmClient

The LLM client for evaluation

onFail

Action to take when query is off-topic (default: Block)

threshold

Minimum relevance score to be considered on-topic (default: 0.5)

Attributes

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

Members list

Value members

Concrete methods

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

Validate that the query is within allowed topic boundaries.

Validate that the query is within allowed topic boundaries.

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(input: String): String

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

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

Value parameters

input

The validated input

Attributes

Returns

The transformed input

Inherited from:
InputGuardrail

Concrete fields

val allowedTopics: Seq[String]
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 threshold: Double