nemoguardrails.kb.kb

View as Markdown

Module Contents

Classes

NameDescription
KnowledgeBaseBasic implementation of a knowledge base.

Data

CACHE_FOLDER

log

API

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:

# 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.
chunks
= []
nemoguardrails.kb.kb.KnowledgeBase.build()
async

Builds the knowledge base index.

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.

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

Search the index for the most relevant chunks.

nemoguardrails.kb.kb.CACHE_FOLDER = os.path.join(os.getcwd(), '.cache')
nemoguardrails.kb.kb.log = logging.getLogger(__name__)