NeMo Guardrails Tutorial: Building Safety Rails for LLM Applications

# Tutorial NeMo Guardrails: Membangun Pagar Pengaman untuk Aplikasi LLM Dalam era adopsi Large Language Model (LLM) yang semakin masif, keamanan dan kontrol terhadap output AI menjadi tantangan kriti...

By Ruby Abdullah · · tutorial
NeMo GuardrailsLLM SafetyNVIDIAAI SecurityColang

NeMo Guardrails Tutorial: Building Safety Rails for LLM Applications

As Large Language Model (LLM) adoption accelerates across industries, controlling AI behavior has become a critical challenge for developers and organizations. LLMs like GPT-4, Claude, or Llama can generate responses that are inappropriate, harmful, or completely off-topic. This is where NeMo Guardrails comes in.

NeMo Guardrails is an open-source toolkit from NVIDIA that enables developers to add programmable guardrails to LLM-based applications. With NeMo Guardrails, you can control conversation topics, filter harmful content, prevent jailbreak attempts, and ensure your AI operates within defined boundaries.

This tutorial covers everything you need to know about NeMo Guardrails, from installation to advanced production deployment.

Why NeMo Guardrails Matters

Before diving into the technical details, let's understand why guardrails are essential:

  • Safety: Prevent LLMs from generating harmful, unethical, or illegal content
  • Consistency: Ensure chatbots stay on topic and maintain their defined persona
  • Compliance: Meet industry regulations regarding AI usage
  • Jailbreak Prevention: Protect against prompt manipulation attempts
  • Cost Control: Limit unnecessary API calls by rejecting out-of-scope queries
  • Installation and Setup

    Prerequisites

    Make sure you have Python 3.9 or newer installed on your system.

    python --version
    

    Installing NeMo Guardrails

    Install NeMo Guardrails using pip:

    pip install nemoguardrails
    

    For installation with all features enabled:

    pip install nemoguardrails[all]
    

    Additional Dependencies

    If you're using OpenAI as the LLM backend:

    pip install openai
    

    For local models with HuggingFace:

    pip install transformers torch
    

    API Key Configuration

    Set the environment variable for your API key:

    export OPENAIAPIKEY="sk-your-api-key-here"
    

    Or create a .env file in your project root:

    OPENAIAPIKEY=sk-your-api-key-here
    

    Core Concepts: Colang and Configuration

    NeMo Guardrails uses a conversation modeling language called Colang (Conversational Language). There are two versions: Colang 1.0 and Colang 2.0. This tutorial covers both.

    Project Structure

    A NeMo Guardrails project has the following structure:

    myguardrailsapp/
    

    ├── config/

    │ ├── config.yml # Main configuration

    │ ├── rails.co # Guardrails definitions (Colang)

    │ ├── prompts.yml # Custom prompts

    │ └── actions.py # Custom actions (Python)

    ├── main.py # Application entry point

    └── requirements.txt

    Basic Configuration (config.yml)

    The config.yml file is the central configuration:

    models:
    
    • type: main
    engine: openai

    model: gpt-4o-mini

    instructions:

    • type: general
    content: |

    You are an AI assistant for a technology company.

    You only answer questions related to technology and company products.

    You always respond politely and professionally.

    sampleconversation: |

    user "Hello, how are you?"

    "Hello! I'm doing well, thank you. How can I help you with our products?"

    user "Can you tell me about your products?"

    "Of course! We provide cloud computing and AI solutions for enterprises. Would you like to know more about a specific product?"

    Basic Usage: Creating Your First Guardrails

    Example 1: Topical Guardrail

    Create a config/rails.co file to restrict conversation topics:

    define user ask about politics
    

    "What's your opinion on politics?"

    "Who is the best president?"

    "What do you think about the elections?"

    "Which political party is best?"

    define user ask about religion

    "Which religion is the truest?"

    "What's your opinion on religion?"

    define bot refuse political topic

    Related Articles

    Triton Inference Server Tutorial: High-Performance Model Serving

    Tutorial 19: Triton Inference Server - Penyajian Model Berperforma Tinggi Daftar Isi Pendahuluan Prasyarat Menyiapkan Tr...

    Installation Guide TensorRT with pip on Ubuntu

    Installation Guide TensorRT with pip on Ubuntu

    TensorRT can be installed via pip for Python-based inference on Ubuntu systems with NVIDIA GPUs, provided that the NVIDI...

    Marker Tutorial: Converting PDFs and Documents to Markdown for RAG Pipelines

    Tutorial Marker: Mengubah PDF dan Dokumen Menjadi Markdown untuk Pipeline RAG Dalam dunia AI dan data engineering, kemam...

    Complete Pinecone Tutorial: Vector Database for AI and Semantic Search

    Tutorial Lengkap Pinecone: Vector Database untuk AI dan Semantic Search Pinecone adalah managed vector database yang dir...