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:
- The query is analyzed to determine its topic/intent
- The detected topic is compared against allowed topics
- 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
Members list
In this article