From 8477618db8477f0fbbe9b527e4c341dd690af4d6 Mon Sep 17 00:00:00 2001 From: Felipe Martins Diel <41558831+felipediel@users.noreply.github.com> Date: Mon, 11 Nov 2024 21:44:35 -0300 Subject: [PATCH] feat(qdrant): Add a Function to Delete Points in Qdrant (#7176) Co-authored-by: jacoblee93 --- libs/langchain-qdrant/src/vectorstores.ts | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/libs/langchain-qdrant/src/vectorstores.ts b/libs/langchain-qdrant/src/vectorstores.ts index 4e48421f71f2..c70e2e36656a 100644 --- a/libs/langchain-qdrant/src/vectorstores.ts +++ b/libs/langchain-qdrant/src/vectorstores.ts @@ -36,6 +36,14 @@ export type QdrantAddDocumentOptions = { customPayload: Record[]; }; +/** + * Type that defines the parameters for the delete operation in the + * QdrantStore class. It includes ids, filter and shard key. + */ +export type QdrantDeleteParams = + | { ids: string[]; shardKey?: string; filter?: never } + | { filter: object; shardKey?: string; ids?: never }; + export type QdrantFilter = QdrantSchemas["Filter"]; export type QdrantCondition = QdrantSchemas["FieldCondition"]; @@ -174,6 +182,37 @@ export class QdrantVectorStore extends VectorStore { } } + /** + * Method that deletes points from the Qdrant database. + * @param params Parameters for the delete operation. + * @returns Promise that resolves when the delete operation is complete. + */ + async delete(params: QdrantDeleteParams): Promise { + const { ids, filter, shardKey } = params; + + if (ids) { + const batchSize = 1000; + for (let i = 0; i < ids.length; i += batchSize) { + const batchIds = ids.slice(i, i + batchSize); + await this.client.delete(this.collectionName, { + wait: true, + ordering: "weak", + points: batchIds, + shard_key: shardKey, + }); + } + } else if (filter) { + await this.client.delete(this.collectionName, { + wait: true, + ordering: "weak", + filter, + shard_key: shardKey, + }); + } else { + throw new Error("Either ids or filter must be provided."); + } + } + /** * Method to search for vectors in the Qdrant database that are similar to * a given query vector. The search results include the score and payload