The InMemoryKnowledgeBase class is an in-memory implementation of the KnowledgeBase interface. It provides fast, local storage for documents with support for topic-based organization. This implementation is ideal for lightweight applications that do not require a persistent database or cloud-based storage. Unlike cloud-based solutions, this implementation stores all documents in memory, making it extremely fast but limited by system memory.

Key Features

  • In-Memory Storage: Stores all documents in Python dictionaries for quick access.
  • Topic-Based Organization: Documents are grouped by topics for easy categorization.
  • Fast Document Retrieval: Provides quick access to retrieving documents by ID.
  • Random Document Selection: Supports random selection of documents from the filtered set.
  • Basic Similarity Search: Returns random documents from the same topic as a given seed document.

Usage Example

from trusttest.knowledge_base.in_memory_knowledge_base import InMemoryKnowledgeBase
from trusttest.knowledge_base.base import Document

docs = [
    Document(id="1", content="AI is transforming the world", topic="AI"),
    Document(id="2", content="Python is great for data science", topic="Programming"),
]

kb = InMemoryKnowledgeBase(docs)
kb.initialize_topics()
print("Topics:", kb.topics)
print("Random Document:", kb.choose_document())