Overview
The topic safety guardrail uses specialized models to determine if user inputs are on-topic or off-topic based on your application’s purpose. This helps:- Keep conversations focused on allowed topics
- Prevent users from derailing the conversation
- Enforce domain-specific constraints
- Improve user experience by guiding them to relevant topics
Quick Start
1
Configure the topic control model
Add a topic control model to your configuration:
config.yml
2
Enable topic safety check
Add the topic safety flow to your input rails:
config.yml
3
Define allowed topics in prompts
Create a system prompt that defines your allowed topics:
prompts.yml
Configuration
Basic Configuration
config.yml
With NVIDIA AI Endpoints
config.yml
Defining Topic Constraints
The topic control model needs clear instructions about what topics are allowed. Define these in your task prompts:prompts.yml
The prompt must include the output restriction: “You must respond with ‘on-topic’ or ‘off-topic’.” This is automatically appended if not present.
Behavior
The topic safety check evaluates the conversation history and current user input:With Rails Exceptions
config.yml
TopicSafetyCheckInputException when off-topic content is detected.
Without Rails Exceptions
The bot refuses to respond and aborts the conversation.Conversation History
The topic safety check considers the full conversation history, not just the current message. This helps:- Detect topic drift over multiple turns
- Understand context better
- Make more accurate on-topic/off-topic decisions
Custom Flows
Create custom topic control flows:flows.co
Accessing Results
The topic safety result is stored in a global context variable:flows.co
Multi-Model Configuration
You can configure different topic control models for different purposes:config.yml
Caching
Topic safety checks support model-level caching:Temperature Settings
Topic safety checks use a very low temperature (0.01) for consistent, deterministic results.Max Tokens
The default max tokens is 10, which is sufficient for the “on-topic” or “off-topic” response.Implementation Details
The topic safety flows are defined in:/nemoguardrails/library/topic_safety/flows.co/nemoguardrails/library/topic_safety/actions.py
TopicSafetyCheckInputAction- Checks if user input is on-topic
Best Practices
- Be specific - Clearly define allowed and prohibited topics
- Provide examples - Include example questions for each topic category
- Test edge cases - Verify behavior on borderline topics
- Give helpful feedback - Guide users back to allowed topics when they go off-topic