diff --git a/package.json b/package.json index e00b881..9c7544e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@libertai/libertai-js", - "version": "0.0.0", + "version": "0.0.1", "description": "In-browser SDK for interacting with LibertAI Decentralized AI Network", "keywords": [], "type": "module", diff --git a/src/knowledge-store.ts b/src/knowledge-store.ts index bc7fe31..c420519 100644 --- a/src/knowledge-store.ts +++ b/src/knowledge-store.ts @@ -35,6 +35,7 @@ export class KnowledgeStore { this.load = this.load.bind(this); this.addDocument = this.addDocument.bind(this); + this.removeDocument = this.removeDocument.bind(this); this.searchDocuments = this.searchDocuments.bind(this); } @@ -83,6 +84,32 @@ export class KnowledgeStore { return doc; } + /** + * Remove a document from the store + * @param documentIdd The ID of the document to remove + * @returns The document that was removed + * @throws An error if the document is not found + */ + async removeDocument(documentId: string): Promise { + const doc = this.documents.get(documentId); + if (!doc) { + throw new Error(`Document not found: documentId = ${documentId}`); + } + // Remove all embeddings for the document + await this.store.iterate((obj, id, _iterationNumber) => { + if (id === this.config.documentsKey) return; + const embedding = obj as Embedding; + if (embedding.documentId === documentId) { + this.store.removeItem(id); + } + }); + + // Remove + this.documents.delete(documentId); + await this.save(); + return doc; + } + /** * Search the documents in the store for the given query for similarity by euclidean distance * @param query The query to search for