Interface KnowledgeBase

All Known Implementing Classes:
InMemoryKnowledgeBase

public interface KnowledgeBase
Interface for knowledge bases supporting RAG (Retrieval-Augmented Generation).

A knowledge base stores documents and provides search capabilities for retrieving relevant context to augment LLM prompts.

Implementations may use various backends:

  • In-memory (for testing/small datasets)
  • Vector databases (Pinecone, Weaviate, Milvus)
  • Full-text search (Elasticsearch, OpenSearch)
  • Hybrid (combining vector and keyword search)

Example usage with an Agent:

 // Setup knowledge base
 KnowledgeBase kb = new InMemoryKnowledgeBase();
 kb.addDocument(Document.of("Product X supports features A, B, C"));
 kb.addDocument(Document.of("Pricing starts at $99/month"));

 // RAG query
 String query = "What features does Product X have?";
 List<SearchResult> context = kb.search(query, 3);

 // Build augmented prompt
 String augmentedPrompt = "Context:\n" +
     context.stream()
         .map(r -> r.getContent())
         .collect(Collectors.joining("\n")) +
     "\n\nQuestion: " + query;

 String answer = agent.chat(augmentedPrompt);
 
See Also:
  • Method Details

    • addDocument

      void addDocument(Document document)
      Adds a document to the knowledge base.
      Parameters:
      document - the document to add
      Throws:
      KnowledgeBaseException - if the operation fails
    • addDocuments

      default void addDocuments(List<Document> documents)
      Adds multiple documents to the knowledge base.
      Parameters:
      documents - the documents to add
      Throws:
      KnowledgeBaseException - if the operation fails
    • getDocument

      Optional<Document> getDocument(String id)
      Retrieves a document by its ID.
      Parameters:
      id - the document ID
      Returns:
      the document, or empty if not found
    • removeDocument

      boolean removeDocument(String id)
      Removes a document from the knowledge base.
      Parameters:
      id - the document ID
      Returns:
      true if the document was removed, false if not found
    • replaceDocuments

      default void replaceDocuments(Collection<String> removeIds, List<Document> add)
      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.

      Parameters:
      removeIds - document identifiers to drop
      add - documents to index after the removals
    • search

      List<SearchResult> search(String query, int topK)
      Searches the knowledge base for relevant documents.
      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

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

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

      int size()
      Gets the total number of documents in the knowledge base.
      Returns:
      document count
    • isEmpty

      default boolean isEmpty()
      Checks if the knowledge base is empty.
      Returns:
      true if empty
    • clear

      void clear()
      Clears all documents from the knowledge base.
    • contains

      default boolean contains(String id)
      Checks if a document with the given ID exists.
      Parameters:
      id - the document ID
      Returns:
      true if the document exists