Skip to content

Commit

Permalink
Merge pull request #3 from Libertai/remove-documents
Browse files Browse the repository at this point in the history
feat: remove document
  • Loading branch information
amiller68 authored Apr 11, 2024
2 parents 028a10b + ff9bef0 commit 4073156
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
27 changes: 27 additions & 0 deletions src/knowledge-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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<Document> {
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
Expand Down

0 comments on commit 4073156

Please sign in to comment.