Package com.tnsai.knowledge
Class InMemoryKnowledgeBase
java.lang.Object
com.tnsai.knowledge.InMemoryKnowledgeBase
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddDocument(Document document) Adds a document to the knowledge base.voidaddDocuments(List<Document> documentsToAdd) Adds multiple documents to the knowledge base.voidclear()Clears all documents from the knowledge base.getDocument(String id) Retrieves a document by its ID.booleanremoveDocument(String id) Removes a document from the knowledge base.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.Contents visible to a concurrent reader under the read lock.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.knowledge.KnowledgeBase
contains, isEmpty
-
Constructor Details
-
InMemoryKnowledgeBase
public InMemoryKnowledgeBase()
-
-
Method Details
-
addDocument
Description copied from interface:KnowledgeBaseAdds a document to the knowledge base.- Specified by:
addDocumentin interfaceKnowledgeBase- Parameters:
document- the document to add
-
addDocuments
Description copied from interface:KnowledgeBaseAdds multiple documents to the knowledge base.- Specified by:
addDocumentsin interfaceKnowledgeBase- Parameters:
documentsToAdd- the documents to add
-
getDocument
Description copied from interface:KnowledgeBaseRetrieves a document by its ID.- Specified by:
getDocumentin interfaceKnowledgeBase- Parameters:
id- the document ID- Returns:
- the document, or empty if not found
-
removeDocument
Description copied from interface:KnowledgeBaseRemoves a document from the knowledge base.- Specified by:
removeDocumentin interfaceKnowledgeBase- Parameters:
id- the document ID- Returns:
- true if the document was removed, false if not found
-
replaceDocuments
Description copied from interface:KnowledgeBaseRemoves 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:
replaceDocumentsin interfaceKnowledgeBase- Parameters:
removeIds- document identifiers to dropadd- documents to index after the removals
-
search
Description copied from interface:KnowledgeBaseSearches the knowledge base for relevant documents.- Specified by:
searchin interfaceKnowledgeBase- 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
Description copied from interface:KnowledgeBaseSearches with metadata filtering.- Specified by:
searchin interfaceKnowledgeBase- Parameters:
query- the search querytopK- maximum number of resultsfilters- metadata filters (key-value pairs that must match)- Returns:
- filtered search results
-
searchByEmbedding
Description copied from interface:KnowledgeBasePerforms similarity search using a pre-computed embedding. Useful when you have embeddings from an external source.- Specified by:
searchByEmbeddingin interfaceKnowledgeBase- Parameters:
embedding- the query embedding vectortopK- maximum number of results- Returns:
- search results ordered by similarity
-
size
public int size()Description copied from interface:KnowledgeBaseGets the total number of documents in the knowledge base.- Specified by:
sizein interfaceKnowledgeBase- Returns:
- document count
-
clear
public void clear()Description copied from interface:KnowledgeBaseClears all documents from the knowledge base.- Specified by:
clearin interfaceKnowledgeBase
-
snapshotContents
Contents visible to a concurrent reader under the read lock.
-