Package com.tnsai.knowledge
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 Summary
Modifier and TypeMethodDescriptionvoidaddDocument(Document document) Adds a document to the knowledge base.default voidaddDocuments(List<Document> documents) Adds multiple documents to the knowledge base.voidclear()Clears all documents from the knowledge base.default booleanChecks if a document with the given ID exists.getDocument(String id) Retrieves a document by its ID.default booleanisEmpty()Checks if the knowledge base is empty.booleanremoveDocument(String id) Removes a document from the knowledge base.default voidreplaceDocuments(Collection<String> removeIds, List<Document> add) Removes then adds documents as one generation.Searches the knowledge base for relevant documents.Searches with metadata filtering.searchByEmbedding(float[] embedding, int topK) Performs similarity search using a pre-computed embedding.intsize()Gets the total number of documents in the knowledge base.
-
Method Details
-
addDocument
Adds a document to the knowledge base.- Parameters:
document- the document to add- Throws:
KnowledgeBaseException- if the operation fails
-
addDocuments
Adds multiple documents to the knowledge base.- Parameters:
documents- the documents to add- Throws:
KnowledgeBaseException- if the operation fails
-
getDocument
Retrieves a document by its ID.- Parameters:
id- the document ID- Returns:
- the document, or empty if not found
-
removeDocument
Removes a document from the knowledge base.- Parameters:
id- the document ID- Returns:
- true if the document was removed, false if not found
-
replaceDocuments
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 dropadd- documents to index after the removals
-
search
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
Searches with metadata filtering.- Parameters:
query- the search querytopK- maximum number of resultsfilters- metadata filters (key-value pairs that must match)- Returns:
- filtered search results
-
searchByEmbedding
Performs similarity search using a pre-computed embedding. Useful when you have embeddings from an external source.- Parameters:
embedding- the query embedding vectortopK- 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
Checks if a document with the given ID exists.- Parameters:
id- the document ID- Returns:
- true if the document exists
-