🚦 Stay Safe, Stay On Track: NeMo Guardrails ! 🔒
Tags: ai, security, python, openai
Published: 2024-03-25 12:05:15

Let's talk about **guardrails** in general before diving into the world of NeMo Guardrails. 

## 🛤️ Guardrails
> Ensuring Smooth Sailing in LLM Applications

Guardrails act as safety barriers, guiding your applications and preventing them from veering off into dangerous territory. In the context of LLM applications, guardrails ensure **trustworthiness**, **safety**, and **security**, keeping your conversations on track and free from unwanted surprises. 

---

## 🛡️ Introducing NeMo Guardrails

**[NeMo Guardrails](https://github.com/NVIDIA/NeMo-Guardrails)** is an **open-source toolkit** developed by **NVIDIA**, designed to make adding programmable guardrails to LLM-based conversational applications a breeze.

![programmable guardrails](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4723yo56i97mqk0m9nuy.png)

With **NeMo Guardrails**, developers can easily define rules and restrictions for their LLMs, ensuring they stay on topic, avoid sensitive subjects, and follow predefined conversation paths. 

But that's not all! **NeMo Guardrails** also provides mechanisms to protect against common LLM vulnerabilities, such as **[jailbreaks](https://www.lakera.ai/blog/jailbreaking-large-language-models-guide)** and **[prompt injections](https://arc.net/l/quote/tivxaruq)**, making your applications safer and more robust. 💪

---

## ⚔️ The Five Guardians in Action

![Programmable Guardrails Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkpq5v6vgwxz174v96ly.png)


| Guardian              | Description                                                                                                       | Example                                                                                                                                                          |
|-----------------------|-------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Input Rails           | Inspect incoming messages, rejecting or altering them to ensure safety.                                           | If a user sends sensitive information like a credit card number, Input Rails quickly mask it.                                                                    |
| Dialog Rails          | Orchestrate interactions, guiding conversations along predefined paths.                                            | When a user asks about a topic, Dialog Rails direct the conversation to the appropriate response.                                                               |
| Retrieval Rails       | Protect retrieved data, ensuring reliability and relevance.                                                         | In Retrieval Augmented Generation, mask the sensitive data in retrieved data to create the output.                                                  |
| Execution Rails       | Oversee actions, maintaining compliance with standards.                                                             | When a custom action accesses external data, Execution Rails ensure input/output follows security protocols.                                                     |
| Output Rails          | Safeguard final output, ensuring accuracy and appropriateness.                                                      | Output Rails review generated responses for sensitive content. If found, they modify or remove it before delivery.                                              |

---

##  💬 How Dialog Rails Drive NeMo Guardrails 

Dialog Rails stand as pivotal architects, shaping the flow of conversations with finesse and precision. These rails, defined through the ingenious Colang language, hold the power to orchestrate interactions between users and LLMs, steering them along predefined paths of engagement.

### 🌀 Colang: The Language of Control

At the heart of NeMo Guardrails lies [Colang](https://github.com/NVIDIA/NeMo-Guardrails/blob/develop/docs/user_guides/colang-language-syntax-guide.md), a specialized modeling language crafted for flexible dialogue flows. With its Python-like syntax, Colang empowers developers to design intricate conversation paths with ease and intuition.

Consider the simple yet impactful colang file which defines the dialog rails for greeting the user.

```colang
define user express greeting
  "Hello!"
  "Good afternoon!"

define flow
  user express greeting
  bot express greeting
  bot offer to help

define bot express greeting
  "Hello there!"

define bot offer to help
  "How can I help you today?"
```

Below is an additional example of Colang definitions for a dialog rail against insults:

```colang
define user express insult
  "You are stupid"

define flow
  user express insult
  bot express calmly willingness to help
```

### 🛠️ How It Works  

NeMo Guardrails employs vector embeddings of all flow instructions to navigate dialogue paths efficiently. By searching with the input question, it can swiftly find the appropriate pathway without relying on the LLM. This enables it to seamlessly deliver default messages, execute specific actions like blocking messages, fetch information from PDFs using RAG, or call external APIs. Such precision ensures the bot stays aligned with the dialogue flow, enhancing its effectiveness and user experience.

---

## 🔗 Integrations 

**[LangChain](https://github.com/NVIDIA/NeMo-Guardrails/blob/develop/docs/user_guides/langchain/langchain-integration.md)** : 
NeMo Guardrails seamlessly integrates with LangChain, enabling easy configuration wrapping around LangChain chains or any Runnable

**[AlignScore-based Fact-Checking](https://github.com/NVIDIA/NeMo-Guardrails/blob/develop/docs/user_guides/guardrails-library.md#alignscore-based-fact-checking)**: 
NeMo Guardrails provides out-of-the-box support for the AlignScore, which uses a RoBERTa-based model for scoring factual consistency in model responses with respect to the knowledge base.

**[Llama Guard-based Content Moderation](https://github.com/NVIDIA/NeMo-Guardrails/blob/develop/docs/user_guides/guardrails-library.md#llama-guard-based-content-moderation)**:
NeMo Guardrails provides out-of-the-box support for content moderation using Meta's [Llama Guard](https://ai.meta.com/research/publications/llama-guard-llm-based-input-output-safeguard-for-human-ai-conversations/) model.

---

## 🌟 Conclusion 

In the realm of LLM-based conversational applications, NeMo Guardrails distinguishes itself by offering a versatile toolkit that integrates various guardrail approaches seamlessly. It enables precise dialogue modeling, enhancing user engagement. 

However, it's important to exercise caution as the beta release is still under development. NeMo Guardrails shows promise, but users should be mindful of potential instability and unexpected behavior.