Retrieval-Augmented Generation
Retrieval-Augmented Generation
This guide shows how to apply a guardrails configuration in a RAG scenario. This guide builds on the previous guide, developing further the demo ABC Bot.
Prerequisites
- Install the
openaipackage:
- Set the
OPENAI_API_KEYenvironment variable:
- If you’re running this inside a notebook, patch the AsyncIO loop.
Usage
There are two modes in which you can use a guardrails configuration in conjunction with RAG:
- Relevant Chunks: perform the retrieval yourself and pass the relevant chunks directly to the
generatemethod. - Knowledge Base: configure a knowledge base directly into the guardrails configuration and let NeMo Guardrails manage the retrieval part.
Relevant Chunks
In the previous guide, the message “How many free vacation days do I have per year” yields a general response:
ABC company’s Employee Handbook contains the following information:
You can pass this information directly to guardrails when making a generate call:
As expected, the response contains the correct answer.
Knowledge Base
There are three ways you can configure a knowledge base directly into a guardrails configuration:
- Using the kb folder.
- Using a custom
retrieve_relevant_chunksaction. - Using a custom
EmbeddingSearchProvider.
For option 1, you can add a knowledge base directly into your guardrails configuration by creating a kb folder inside the config folder and adding documents there. Currently, only the Markdown format is supported. For a quick example, check out the complete implementation of the ABC Bot.
Options 2 and 3 represent advanced use cases beyond the scope of this topic.
Wrapping Up
This guide introduced how a guardrails configuration can be used in the context of a RAG setup.
Next
To continue learning about NeMo Guardrails, check out: