Knowledge Base Configuration

View as Markdown

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:

.
├── config
│ ├── config.yml
│ ├── kb
│ │ ├── file_1.md
│ │ ├── file_2.md
│ │ └── ...
│ └── rails
│ └── ...

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:

  1. Splits documents into topic chunks based on markdown headers. Large chunks are further split at blank lines to stay within a maximum chunk size.
  2. Uses the configured embedding model to create vector representations of each chunk.
  3. Stores the embeddings for efficient similarity search.

Example Document

1# Employee Handbook
2
3## Time Off Policy
4
5Employees are eligible for the following time off:
6* Vacation: 20 days per year, accrued monthly.
7* Sick leave: 15 days per year, accrued monthly.
8* Personal days: 5 days per year, accrued monthly.
9
10## Holiday Schedule
11
12Paid holidays include:
13* New Year's Day
14* Memorial Day
15* Independence Day
16* Thanksgiving Day
17* Christmas Day

Retrieval Process

When a user query is received, the system:

  1. Computes embeddings for the user query using the configured embedding model.
  2. Performs similarity search against the indexed document chunks.
  3. Retrieves the most relevant chunks based on similarity scores.
  4. Makes the retrieved chunks available as $relevant_chunks in the context.
  5. 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:

1knowledge_base:
2 folder: "kb" # Default folder name
3 embedding_search_provider:
4 name: "default"
5 parameters: {}

Configuration Options

OptionDescriptionDefault
folderThe folder from which documents should be loaded"kb"
embedding_search_provider.nameThe name of the embedding search provider"default"
embedding_search_provider.parametersProvider-specific parameters{}

Embedding Model Configuration

The knowledge base uses the embedding model configured in the models section of your config.yml:

1models:
2 - type: main
3 engine: openai
4 model: gpt-4
5
6 - type: embeddings
7 engine: openai
8 model: text-embedding-ada-002

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:

1from typing import Optional
2
3from nemoguardrails.actions import action
4from nemoguardrails.actions.actions import ActionResult
5from nemoguardrails.kb.kb import KnowledgeBase
6
7@action(is_system_action=True)
8async def retrieve_relevant_chunks(
9 context: Optional[dict] = None,
10 kb: Optional[KnowledgeBase] = None,
11):
12 user_message = context.get("last_user_message") if context else None
13
14 # Implement custom retrieval logic
15 # For example, query an external vector database
16 chunks = await query_external_kb(user_message)
17 relevant_chunks = "\n".join(chunks)
18
19 return ActionResult(
20 return_value=relevant_chunks,
21 context_updates={"relevant_chunks": relevant_chunks},
22 )

3. Using Custom EmbeddingSearchProvider

For advanced use cases, implement a custom embedding search provider:

1from typing import List, Optional
2from nemoguardrails.embeddings.index import EmbeddingsIndex, IndexItem
3
4class CustomEmbeddingSearchProvider(EmbeddingsIndex):
5 """Custom embedding search provider."""
6
7 async def add_item(self, item: IndexItem):
8 # Custom indexing logic
9 pass
10
11 async def search(self, text: str, max_results: int, threshold: Optional[float] = None) -> List[IndexItem]:
12 pass

For more details, refer to Embedding Search Providers.

Passing Context Directly

You can also pass relevant context directly when making a generate call:

1response = rails.generate(messages=[
2 {
3 "role": "context",
4 "content": {
5 "relevant_chunks": """
6 Employees are eligible for the following time off:
7 * Vacation: 20 days per year, accrued monthly.
8 * Sick leave: 15 days per year, accrued monthly.
9 """
10 }
11 },
12 {
13 "role": "user",
14 "content": "How many vacation days do I have per year?"
15 }
16])

Using Knowledge Base in Colang Flows

You can reference the retrieved chunks in your Colang flows:

import core
import llm
flow main
activate llm continuation
user asked question
$chunks = ..."Summarize the relevant information from the knowledge base."
bot say $chunks
flow user asked question
user said "what" or user said "how" or user said "tell me"

Best Practices

  1. Organize documents logically: Use clear markdown headers to structure your documents. The system chunks documents based on headers.

  2. Keep chunks focused: Each section should cover a single topic for better retrieval accuracy.

  3. Use descriptive headers: Headers help the system understand the content of each chunk.

  4. Test retrieval quality: Verify that the system retrieves relevant chunks for common user queries.

  5. 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
│ ├── config.yml
│ ├── kb
│ │ └── company_policy.md
│ └── rails
│ └── main.co

config.yml:

1models:
2 - type: main
3 engine: openai
4 model: gpt-4
5
6 - type: embeddings
7 engine: openai
8 model: text-embedding-ada-002
9
10instructions:
11 - type: general
12 content: |
13 You are a helpful HR assistant. Answer questions based on the
14 company policy documents provided.
15
16knowledge_base:
17 folder: "kb"

kb/company_policy.md:

1# Company Policy
2
3## Vacation Policy
4
5All full-time employees receive 20 days of paid vacation per year.
6Vacation days accrue monthly at a rate of 1.67 days per month.
7
8## Sick Leave
9
10Employees receive 15 days of paid sick leave per year.
11Unused sick days do not carry over to the next year.