From 26cee2e878d4c8038ce1d283eb9a00edf0e9aae9 Mon Sep 17 00:00:00 2001 From: Philippe PRADOS Date: Wed, 3 Jul 2024 20:14:08 +0200 Subject: [PATCH] partners[patch]: MongoDB vectorstore to return and accept string IDs (#23818) The mongdb have some errors. - `add_texts() -> List` returns a list of `ObjectId`, and not a list of string - `delete()` with `id` never remove chunks. --------- Co-authored-by: Eugene Yurtsev --- libs/partners/mongodb/langchain_mongodb/vectorstores.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/partners/mongodb/langchain_mongodb/vectorstores.py b/libs/partners/mongodb/langchain_mongodb/vectorstores.py index 50848146193..4eafd5abf10 100644 --- a/libs/partners/mongodb/langchain_mongodb/vectorstores.py +++ b/libs/partners/mongodb/langchain_mongodb/vectorstores.py @@ -144,7 +144,7 @@ class MongoDBAtlasVectorSearch(VectorStore): texts: Iterable[str], metadatas: Optional[List[Dict[str, Any]]] = None, **kwargs: Any, - ) -> List: + ) -> List[str]: """Run more texts through the embeddings and add to the vectorstore. Args: @@ -174,7 +174,7 @@ class MongoDBAtlasVectorSearch(VectorStore): size = 0 if texts_batch: result_ids.extend(self._insert_texts(texts_batch, metadatas_batch)) # type: ignore - return result_ids + return [str(id) for id in result_ids] def _insert_texts(self, texts: List[str], metadatas: List[Dict[str, Any]]) -> List: if not texts: @@ -410,7 +410,7 @@ class MongoDBAtlasVectorSearch(VectorStore): """ search_params: dict[str, Any] = {} if ids: - search_params[self._text_key]["$in"] = ids + search_params["_id"] = {"$in": [ObjectId(id) for id in ids]} return self._collection.delete_many({**search_params, **kwargs}).acknowledged