Knowledge Base Configuration
The NeMo Guardrails library supports using a set of documents as context for generating bot responses through Retrieval-Augmented Generation (RAG). This guide explains how to configure and use the knowledge base folder.
Overview
By default, an LLMRails instance supports using documents as context for generating responses. To include documents as part of your knowledge base, place them in the kb folder inside your configuration folder:
Currently, only the Markdown format is supported.
Document Structure
Documents in the knowledge base kb folder are automatically processed and indexed for retrieval. The system:
- Splits documents into topic chunks based on markdown headers. Large chunks are further split at blank lines to stay within a maximum chunk size.
- Uses the configured embedding model to create vector representations of each chunk.
- Stores the embeddings for efficient similarity search.
Example Document
Retrieval Process
When a user query is received, the system:
- Computes embeddings for the user query using the configured embedding model.
- Performs similarity search against the indexed document chunks.
- Retrieves the most relevant chunks based on similarity scores.
- Makes the retrieved chunks available as
$relevant_chunksin the context. - Uses these chunks as additional context when generating the bot response.
Configuration
The knowledge base functionality is automatically enabled when documents are present in the kb folder. You can customize the behavior using the knowledge_base section in your config.yml:
Configuration Options
Embedding Model Configuration
The knowledge base uses the embedding model configured in the models section of your config.yml:
For more details on embedding model configuration, refer to Model Configuration.
Alternative Knowledge Base Methods
There are three ways to configure a knowledge base:
1. Using the kb Folder (Default)
Place markdown files in the kb folder as described above. This is the simplest approach for static document collections.
2. Using Custom retrieve_relevant_chunks Action
Implement a custom action to retrieve chunks from external sources:
3. Using Custom EmbeddingSearchProvider
For advanced use cases, implement a custom embedding search provider:
For more details, refer to Embedding Search Providers.
Passing Context Directly
You can also pass relevant context directly when making a generate call:
Using Knowledge Base in Colang Flows
You can reference the retrieved chunks in your Colang flows:
Colang 2.0
Colang 1.0
Best Practices
-
Organize documents logically: Use clear markdown headers to structure your documents. The system chunks documents based on headers.
-
Keep chunks focused: Each section should cover a single topic for better retrieval accuracy.
-
Use descriptive headers: Headers help the system understand the content of each chunk.
-
Test retrieval quality: Verify that the system retrieves relevant chunks for common user queries.
-
Monitor embedding model: Ensure your embedding model is appropriate for your document content and user queries.
Complete Example
Here’s a complete example configuration with a knowledge base:
Directory structure:
config.yml:
kb/company_policy.md: