Skip to content

Commit d449dc9

Browse files
committed
docs(examples): add NeMoGuard rails config for colang 2
1 parent 57a3abc commit d449dc9

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# NeMoGuard Safety Rails Example
2+
3+
This example showcases the use of NVIDIA's NeMoGuard NIMs for comprehensive AI safety including content moderation, topic control, and jailbreak detection.
4+
5+
## Configuration Files
6+
7+
- `config.yml` - Defines the models configuration including the main LLM and three NeMoGuard NIMs for safety checks
8+
- `prompts.yml` - Contains prompt templates for content safety and topic control checks
9+
- `rails.co` - Implements input and output rails that integrate content safety, topic safety, and jailbreak detection checks
10+
- `main.co` - The entry point Colang 2 file that imports core functionality and activates the LLM continuation flow
11+
12+
## NeMoGuard NIMs Used
13+
14+
1. **Content Safety** (`nvidia/llama-3.1-nemoguard-8b-content-safety`) - Checks for unsafe content across 23 safety categories
15+
2. **Topic Control** (`nvidia/llama-3.1-nemoguard-8b-topic-control`) - Ensures conversations stay within allowed topics
16+
3. **Jailbreak Detection** - Detects and prevents jailbreak attempts (configured via `nim_server_endpoint`)
17+
18+
## Documentation
19+
20+
For more details about NeMoGuard NIMs and deployment options, see:
21+
22+
- [NeMo Guardrails Documentation](https://docs.nvidia.com/nemo/guardrails/index.html)
23+
- [Llama 3.1 NemoGuard 8B ContentSafety NIM](https://docs.nvidia.com/nim/llama-3-1-nemoguard-8b-contentsafety/latest/)
24+
- [Llama 3.1 NemoGuard 8B TopicControl NIM](https://docs.nvidia.com/nim/llama-3-1-nemoguard-8b-topiccontrol/latest/)
25+
- [NemoGuard JailbreakDetect NIM](https://docs.nvidia.com/nim/nemoguard-jailbreakdetect/latest/)
26+
- [NeMoGuard Models on NVIDIA API Catalog](https://build.nvidia.com/search?q=nemoguard)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
colang_version: 2.x
2+
models:
3+
- type: main
4+
engine: nim
5+
model: meta/llama-3.3-70b-instruct
6+
- type: content_safety
7+
engine: nim
8+
model: nvidia/llama-3.1-nemoguard-8b-content-safety
9+
- type: topic_control
10+
engine: nim
11+
model: nvidia/llama-3.1-nemoguard-8b-topic-control
12+
13+
rails:
14+
config:
15+
jailbreak_detection:
16+
nim_base_url: "https://ai.api.nvidia.com"
17+
nim_server_endpoint: "/v1/security/nvidia/nemoguard-jailbreak-detect"
18+
api_key_env_var: NVIDIA_API_KEY
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import core
2+
import llm
3+
4+
flow main
5+
activate llm continuation
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
prompts:
2+
- task: topic_safety_check_input $model=topic_control
3+
content: |
4+
You are to act as a customer service agent, providing users with factual information in accordance to the knowledge base. Your role is to ensure that you respond only to relevant queries and adhere to the following guidelines
5+
6+
Guidelines for the user messages:
7+
- Do not answer questions related to personal opinions or advice on user's order, future recommendations
8+
- Do not provide any information on non-company products or services.
9+
- Do not answer enquiries unrelated to the company policies.
10+
- Do not answer questions asking for personal details about the agent or its creators.
11+
- Do not answer questions about sensitive topics related to politics, religion, or other sensitive subjects.
12+
- If a user asks topics irrelevant to the company's customer service relations, politely redirect the conversation or end the interaction.
13+
- Your responses should be professional, accurate, and compliant with customer relations guidelines, focusing solely on providing transparent, up-to-date information about the company that is already publicly available.
14+
- allow user comments that are related to small talk and chit-chat.
15+
16+
- task: content_safety_check_input $model=content_safety
17+
content: |
18+
Task: Check if there is unsafe content in the user or agent messages in the below conversation in accordance with our safety policy with the below categories. Provide a 'safe' or 'unsafe' rating for both the user message and the agent response. If there is unsafe content, please also provide a list of violated categories according to our safety policy below.
19+
20+
<BEGIN UNSAFE CONTENT CATEGORIES>
21+
S1: Violence.
22+
S2: Sexual.
23+
S3: Criminal Planning/Confessions.
24+
S4: Guns and Illegal Weapons.
25+
S5: Controlled/Regulated Substances.
26+
S6: Suicide and Self Harm.
27+
S7: Sexual (minor).
28+
S8: Hate/Identity Hate.
29+
S9: PII/Privacy.
30+
S10: Harassment.
31+
S11: Threat.
32+
S12: Profanity.
33+
S13: Needs Caution.
34+
S14: Other.
35+
S15: Manipulation.
36+
S16: Fraud/Deception.
37+
S17: Malware.
38+
S18: High Risk Gov Decision Making.
39+
S19: Political/Misinformation/Conspiracy.
40+
S20: Copyright/Trademark/Plagiarism.
41+
S21: Unauthorized Advice.
42+
S22: Illegal Activity.
43+
S23: Immoral/Unethical.
44+
<END UNSAFE CONTENT CATEGORIES>
45+
46+
<BEGIN CONVERSATION>
47+
48+
user: {{ user_input }}
49+
50+
<END CONVERSATION>
51+
52+
Provide your safety assessment for the above conversation in the below output JSON format:
53+
{"User Safety": safe or unsafe rating for the user message, "Response Safety": safe or unsafe rating for the agent response. Omit if no agent response present. "Safety Categories": a comma-separated list of applicable safety categories from the provided taxonomy. Omit if all safe.}
54+
55+
Do not include anything other than the output JSON in your response.
56+
Output JSON:
57+
output_parser: nemoguard_parse_prompt_safety
58+
max_tokens: 50
59+
60+
- task: content_safety_check_output $model=content_safety
61+
content: |
62+
Task: Check if there is unsafe content in the user or agent messages in the below conversation in accordance with our safety policy with the below categories. Provide a 'safe' or 'unsafe' rating for both the user message and the agent response. If there is unsafe content, please also provide a list of violated categories according to our safety policy below.
63+
64+
<BEGIN UNSAFE CONTENT CATEGORIES>
65+
S1: Violence.
66+
S2: Sexual.
67+
S3: Criminal Planning/Confessions.
68+
S4: Guns and Illegal Weapons.
69+
S5: Controlled/Regulated Substances.
70+
S6: Suicide and Self Harm.
71+
S7: Sexual (minor).
72+
S8: Hate/Identity Hate.
73+
S9: PII/Privacy.
74+
S10: Harassment.
75+
S11: Threat.
76+
S12: Profanity.
77+
S13: Needs Caution.
78+
S14: Other.
79+
S15: Manipulation.
80+
S16: Fraud/Deception.
81+
S17: Malware.
82+
S18: High Risk Gov Decision Making.
83+
S19: Political/Misinformation/Conspiracy.
84+
S20: Copyright/Trademark/Plagiarism.
85+
S21: Unauthorized Advice.
86+
S22: Illegal Activity.
87+
S23: Immoral/Unethical.
88+
<END UNSAFE CONTENT CATEGORIES>
89+
90+
<BEGIN CONVERSATION>
91+
92+
user: {{ user_input }}
93+
94+
response: agent: {{ bot_response }}
95+
96+
<END CONVERSATION>
97+
98+
Provide your safety assessment for the above conversation in the below output JSON format:
99+
{"User Safety": safe or unsafe rating for the user message, "Response Safety": safe or unsafe rating for the agent response. Omit if no agent response present. "Safety Categories": a comma-separated list of applicable safety categories from the provided taxonomy. Omit if all safe.}
100+
101+
Do not include anything other than the output JSON in your response.
102+
Output JSON:
103+
output_parser: nemoguard_parse_response_safety
104+
max_tokens: 50
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import guardrails
2+
import nemoguardrails.library.content_safety
3+
import nemoguardrails.library.topic_safety
4+
import nemoguardrails.library.jailbreak_detection
5+
6+
flow input rails $input_text
7+
content safety check input $model="content_safety"
8+
topic safety check input $model="topic_control"
9+
jailbreak detection model
10+
11+
flow output rails $output_text
12+
content safety check output $model="content_safety"

0 commit comments

Comments
 (0)