Skip to main content
This example demonstrates how to build a RAG (Retrieval-Augmented Generation) system with fact-checking and hallucination detection using NeMo Guardrails.

Overview

This example showcases:
  • Custom RAG implementation with knowledge base integration
  • Fact-checking using self-check mechanisms
  • Hallucination detection for bot responses
  • Integration with LangChain for retrieval

Configuration

1

Create the configuration file

Define your models and enable output rails for fact-checking and hallucination detection.
2

Implement the custom RAG action

Create a custom action that performs retrieval and prepares context for fact-checking.
3

Define the output rails flow

Create flows that trigger RAG and enable fact-checking.
4

Add your knowledge base

Create a kb/ folder and add your documents. For example:

Usage

Expected Behavior

1

Query with accurate context

When asking a question that can be answered from the knowledge base:
The fact-checking rail verifies the answer against retrieved chunks.
2

Query without sufficient context

When the answer cannot be verified:
The hallucination detection prevents making up answers.

Key Features

  • Custom RAG Integration: Implements custom retrieval logic using LangChain
  • Fact-Checking: Validates responses against retrieved context
  • Hallucination Detection: Prevents the model from generating unsupported claims
  • Context Storage: Stores relevant chunks and prompts for verification

Advanced: AlignScore Fact-Checking

For more advanced fact-checking, you can use AlignScore: