How to create a method to remove document from FaissStore? #2372
Replies: 1 comment
-
🤖 Hello, From your description, it seems like you're trying to remove a document from the However, you can extend the Here's a rough idea of how you might implement this: class MyFaissStore extends FaissStore {
removeDocument(id: string) {
// Find the index of the document in the _mapping
const index = Object.values(this._mapping).indexOf(id);
if (index === -1) {
throw new Error(`Document with ID ${id} not found.`);
}
// Remove the document from the docstore
this.docstore._docs.delete(id);
// Remove the document from the _mapping
delete this._mapping[index];
// Rebuild the _index without the removed document
const vectors = Object.values(this._mapping).map(id => this.docstore._docs.get(id).vector);
this._index = new faiss.IndexFlatL2(vectors[0].length);
this._index.add(vectors);
// Update the _mapping to reflect the new _index
this._mapping = Object.values(this._mapping).reduce((mapping, id, i) => {
mapping[i] = id;
return mapping;
}, {});
}
} Please note that this is a simplified example and might not cover all edge cases. For example, it assumes that each document in the Also, please be aware that this operation can be expensive in terms of time and memory, especially if you have a large number of documents in your I hope this helps! Let me know if you have any other questions. SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to create a method that takes in the
id
and deletes the respectiveDocument
within theFaissStore
.What I've tried
I can manually do this by calling
myFaissStore.docstore._docs.delete('8fc00bbd-0d8f-4713-a1b7-02f800ae311e');
but the vector store still thinks the information is present. When I callvectorStore.similaritySearch("Hello World", 1);
I get the following error.Beta Was this translation helpful? Give feedback.
All reactions