Class InMemoryKnowledgeBase

java.lang.Object
com.tnsai.knowledge.InMemoryKnowledgeBase
All Implemented Interfaces:
KnowledgeBase

public class InMemoryKnowledgeBase extends Object implements KnowledgeBase
In-memory implementation of KnowledgeBase for testing and small datasets.

This implementation provides:

  • Thread-safe document storage
  • Simple TF-IDF based keyword search
  • Cosine similarity for embedding-based search
  • Spec filtering support

For production use with large datasets, consider using a vector database implementation (Pinecone, Weaviate, etc.) instead.

Example usage:

 KnowledgeBase kb = new InMemoryKnowledgeBase();

 // Add documents
 kb.addDocument(Document.of("Java is a programming language"));
 kb.addDocument(Document.of("Python is also a programming language"));

 // Search
 List<SearchResult> results = kb.search("programming language", 5);
 
See Also:
  • Constructor Details

    • InMemoryKnowledgeBase

      public InMemoryKnowledgeBase()
  • Method Details

    • addDocument

      public void addDocument(Document document)
      Description copied from interface: KnowledgeBase
      Adds a document to the knowledge base.
      Specified by:
      addDocument in interface KnowledgeBase
      Parameters:
      document - the document to add
    • addDocuments

      public void addDocuments(List<Document> documentsToAdd)
      Description copied from interface: KnowledgeBase
      Adds multiple documents to the knowledge base.
      Specified by:
      addDocuments in interface KnowledgeBase
      Parameters:
      documentsToAdd - the documents to add
    • getDocument

      public Optional<Document> getDocument(String id)
      Description copied from interface: KnowledgeBase
      Retrieves a document by its ID.
      Specified by:
      getDocument in interface KnowledgeBase
      Parameters:
      id - the document ID
      Returns:
      the document, or empty if not found
    • removeDocument

      public boolean removeDocument(String id)
      Description copied from interface: KnowledgeBase
      Removes a document from the knowledge base.
      Specified by:
      removeDocument in interface KnowledgeBase
      Parameters:
      id - the document ID
      Returns:
      true if the document was removed, false if not found
    • replaceDocuments

      public void replaceDocuments(Collection<String> removeIds, List<Document> add)
      Description copied from interface: KnowledgeBase
      Removes then adds documents as one generation.

      The default walks the two collections sequentially. Implementations that serve concurrent readers should override so a search never observes a partial replacement.

      Specified by:
      replaceDocuments in interface KnowledgeBase
      Parameters:
      removeIds - document identifiers to drop
      add - documents to index after the removals
    • search

      public List<SearchResult> search(String query, int topK)
      Description copied from interface: KnowledgeBase
      Searches the knowledge base for relevant documents.
      Specified by:
      search in interface KnowledgeBase
      Parameters:
      query - the search query (natural language)
      topK - maximum number of results to return
      Returns:
      list of search results, ordered by relevance (highest first)
    • search

      public List<SearchResult> search(String query, int topK, Map<String,Object> filters)
      Description copied from interface: KnowledgeBase
      Searches with metadata filtering.
      Specified by:
      search in interface KnowledgeBase
      Parameters:
      query - the search query
      topK - maximum number of results
      filters - metadata filters (key-value pairs that must match)
      Returns:
      filtered search results
    • searchByEmbedding

      public List<SearchResult> searchByEmbedding(float[] embedding, int topK)
      Description copied from interface: KnowledgeBase
      Performs similarity search using a pre-computed embedding. Useful when you have embeddings from an external source.
      Specified by:
      searchByEmbedding in interface KnowledgeBase
      Parameters:
      embedding - the query embedding vector
      topK - maximum number of results
      Returns:
      search results ordered by similarity
    • size

      public int size()
      Description copied from interface: KnowledgeBase
      Gets the total number of documents in the knowledge base.
      Specified by:
      size in interface KnowledgeBase
      Returns:
      document count
    • clear

      public void clear()
      Description copied from interface: KnowledgeBase
      Clears all documents from the knowledge base.
      Specified by:
      clear in interface KnowledgeBase
    • snapshotContents

      public Set<String> snapshotContents()
      Contents visible to a concurrent reader under the read lock.