Skip to content

Commit 974a39c

Browse files
committed
Update bulk_embed_and_insert_texts
Remove code from bulk_embed_and_insert_texts class method and replace with call to bulk_embed_and_insert_texts from pymongo-search-utils.
1 parent c1fba1d commit 974a39c

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

libs/langchain-mongodb/langchain_mongodb/vectorstores.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
from langchain_core.embeddings import Embeddings
2323
from langchain_core.runnables.config import run_in_executor
2424
from langchain_core.vectorstores import VectorStore
25-
from pymongo import MongoClient, ReplaceOne
25+
from pymongo import MongoClient
2626
from pymongo.collection import Collection
2727
from pymongo.errors import CollectionInvalid
28+
from pymongo_search_utils import bulk_embed_and_insert_texts
2829

2930
from langchain_mongodb.index import (
3031
create_vector_search_index,
@@ -429,28 +430,15 @@ def bulk_embed_and_insert_texts(
429430
430431
See add_texts for additional details.
431432
"""
432-
if not texts:
433-
return []
434-
# Compute embedding vectors
435-
embeddings = self._embedding.embed_documents(list(texts))
436-
if not ids:
437-
ids = [str(ObjectId()) for _ in range(len(list(texts)))]
438-
docs = [
439-
{
440-
"_id": str_to_oid(i),
441-
self._text_key: t,
442-
self._embedding_key: embedding,
443-
**m,
444-
}
445-
for i, t, m, embedding in zip(
446-
ids, texts, metadatas, embeddings, strict=True
447-
)
448-
]
449-
operations = [ReplaceOne({"_id": doc["_id"]}, doc, upsert=True) for doc in docs]
450-
# insert the documents in MongoDB Atlas
451-
result = self._collection.bulk_write(operations)
452-
assert result.upserted_ids is not None
453-
return [oid_to_str(_id) for _id in result.upserted_ids.values()]
433+
return bulk_embed_and_insert_texts(
434+
texts,
435+
metadatas,
436+
self._embedding.embed_documents,
437+
self._collection,
438+
self._text_key,
439+
self._embedding_key,
440+
ids,
441+
)
454442

455443
def add_documents(
456444
self,

0 commit comments

Comments
 (0)