> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/guardrails/_mcp/server.

# nemoguardrails.kb.kb

## Module Contents

### Classes

| Name                                                   | Description                               |
| ------------------------------------------------------ | ----------------------------------------- |
| [`KnowledgeBase`](#nemoguardrails-kb-kb-KnowledgeBase) | Basic implementation of a knowledge base. |

### Data

[`CACHE_FOLDER`](#nemoguardrails-kb-kb-CACHE_FOLDER)

[`log`](#nemoguardrails-kb-kb-log)

### API

```python
class nemoguardrails.kb.kb.KnowledgeBase(
    documents: typing.List[str],
    config: nemoguardrails.rails.llm.config.KnowledgeBaseConfig,
    get_embedding_search_provider_instance: typing.Callable[[Optional[EmbeddingSearchProvider]], nemoguardrails.embeddings.index.EmbeddingsIndex]
)
```

Basic implementation of a knowledge base.

This class represents a knowledge base that can store and index documents for efficient retrieval.
It utilizes an embedding search provider to build and search an index for relevant information.

Parameters:

* documents (List\[str]): A list of documents to initialize the knowledge base.
* config (KnowledgeBaseConfig): Configuration for the knowledge base.
* get\_embedding\_search\_provider\_instance (Callable\[\[Optional\[EmbeddingSearchProvider]], EmbeddingsIndex]):
  A callable function to get an instance of the embedding search provider.

Methods:

* init(): Initializes the knowledge base by splitting documents into topic chunks.
* build(): Builds the knowledge base index, utilizing the configured embedding search provider.
* search\_relevant\_chunks(text: str, max\_results: int = 3): Searches the index for the most relevant chunks.

Attributes:

* documents (List\[str]): The list of documents provided during initialization.
* chunks (List\[dict]): A list of topic chunks extracted from the documents.
* index (EmbeddingsIndex): The knowledge base index used for searching.
* config (KnowledgeBaseConfig): Configuration for the knowledge base.

Example:

```python
# Creating a KnowledgeBase instance
kb = KnowledgeBase(documents=["Document 1", "Document 2"], config=my_config, get_embedding_search_provider_instance=my_provider)

# Initializing and building the knowledge base
kb.init()
await kb.build()

# Searching for relevant chunks
results = await kb.search_relevant_chunks("query text", max_results=5)
```

Note:

* The knowledge base supports markdown format documents.
* The index is built using an embedding search provider, and the result is cached for future use.

```python
nemoguardrails.kb.kb.KnowledgeBase.build()
```

async

Builds the knowledge base index.

```python
nemoguardrails.kb.kb.KnowledgeBase.init()
```

Initialize the knowledge base.

The initial data is loaded from the `$kb_docs` context key. The key is populated when
the model is loaded. Currently, only markdown format is supported.

```python
nemoguardrails.kb.kb.KnowledgeBase.search_relevant_chunks(
    text,
    max_results: int = 3
)
```

async

Search the index for the most relevant chunks.

```python
nemoguardrails.kb.kb.CACHE_FOLDER = os.path.join(os.getcwd(), '.cache')
```

```python
nemoguardrails.kb.kb.log = logging.getLogger(__name__)
```