How to set up NeMo Guardrails colang
Quick answer
To set up
NeMo Guardrails with colang, install the nemo-guardrails Python package and define your guardrails using the colang DSL files. Then, initialize the Guardrails class in Python pointing to your colang files to enforce safety and control in your AI chat applications.PREREQUISITES
Python 3.8+pip install nemo-guardrailsBasic knowledge of Python and YAMLOpenAI API key or compatible LLM API key
Setup
Install the nemo-guardrails package via pip and prepare your environment variables for your LLM API key. Create colang files to define your guardrails rules and flows.
pip install nemo-guardrails Step by step
Create a colang file defining your conversational guardrails, then load it in Python to run a guarded chat session.
from nemo_guardrails import Guardrails
# Path to your colang file defining the guardrails
colang_file = "example_guardrails.colang"
# Initialize Guardrails with the colang file
rails = Guardrails(colang_file=colang_file)
# Example user input
user_input = "Hello, can you help me?"
# Run the guardrails chat
response = rails.chat(user_input)
print("AI response:", response) output
AI response: Hello! How can I assist you today?
Common variations
- Use async API with
await rails.chat_async()for asynchronous calls. - Integrate with OpenAI or other LLMs by setting environment variables like
OPENAI_API_KEY. - Customize guardrails by editing
colangfiles to add constraints, safety checks, or response formatting.
import asyncio
async def async_chat():
response = await rails.chat_async("Tell me a joke.")
print("Async AI response:", response)
asyncio.run(async_chat()) output
Async AI response: Why did the chicken cross the road? To get to the other side!
Troubleshooting
- If you see errors loading
colangfiles, verify the file path and syntax. - For API authentication errors, ensure your environment variable for the LLM API key is set correctly.
- If responses are not constrained as expected, check your guardrails definitions for completeness and correctness.
Key Takeaways
- Install
nemo-guardrailsand define your conversational rules incolangfiles. - Initialize
Guardrailsin Python with yourcolangfile to enforce AI safety and control. - Use async methods and customize guardrails for flexible, safe AI interactions.
- Always verify environment variables and guardrails syntax to avoid runtime errors.