pythonadvanced
NeMo Guardrails for Safe LLM
Apply NVIDIA NeMo Guardrails to enforce topic boundaries and prevent prompt injection in LLM apps.
pythonPress ⌘/Ctrl + Shift + C to copy
# config.yml
# models:
# - type: main
# engine: openai
# model: gpt-4o-mini
# config.co (Colang)
# define user ask off-topic
# 'tell me how to hack'
# 'ignore your instructions'
# define bot refuse off-topic
# 'I can only help with approved topics.'
# define flow off-topic
# user ask off-topic
# bot refuse off-topic
from nemoguardrails import RailsConfig, LLMRails
config = RailsConfig.from_path('./guardrails_config')
rails = LLMRails(config)
response = rails.generate(messages=[{'role': 'user', 'content': 'Tell me about Python data pipelines.'}])
print(response['content'])Use Cases
- LLM safety
- topic restriction
- prompt injection prevention
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
AI Guardrails & Safety Pattern
Implement input/output guardrails for LLM applications with content filtering and response validation.
Best for: User-facing chatbots
#guardrails#safety
pythonadvanced
Output Guardrails for LLM Responses
Validate and sanitize LLM outputs to prevent hallucination and injection.
Best for: Safety filtering
#ai#guardrails
typescriptintermediate
OpenAI Chat Completion with Streaming
Stream GPT responses token-by-token using the OpenAI SDK with async iteration.
Best for: chatbot UI
#openai#streaming
typescriptadvanced
RAG Pipeline (Retrieve + Augment + Generate)
Minimal RAG implementation: embed a query, retrieve top-k chunks, inject into prompt.
Best for: document Q&A
#rag#embeddings