-
Notifications
You must be signed in to change notification settings - Fork 32
INTPYTHON-752 Integrate pymongo-search-utils #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
cc25f33
209f13d
2ea5a14
76b2d90
91985dd
78ded9f
b75d316
aa528f3
f1f8908
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -22,9 +22,10 @@ | |||||||||||||||||||
from langchain_core.embeddings import Embeddings | ||||||||||||||||||||
from langchain_core.runnables.config import run_in_executor | ||||||||||||||||||||
from langchain_core.vectorstores import VectorStore | ||||||||||||||||||||
from pymongo import MongoClient, ReplaceOne | ||||||||||||||||||||
from pymongo import MongoClient | ||||||||||||||||||||
from pymongo.collection import Collection | ||||||||||||||||||||
from pymongo.errors import CollectionInvalid | ||||||||||||||||||||
from pymongo_search_utils import bulk_embed_and_insert_texts | ||||||||||||||||||||
|
||||||||||||||||||||
from langchain_mongodb.index import ( | ||||||||||||||||||||
create_vector_search_index, | ||||||||||||||||||||
|
@@ -238,7 +239,7 @@ def __init__( | |||||||||||||||||||
self._relevance_score_fn = relevance_score_fn | ||||||||||||||||||||
|
||||||||||||||||||||
# append_metadata was added in PyMongo 4.14.0, but is a valid database name on earlier versions | ||||||||||||||||||||
_append_client_metadata(self._collection.database.client) | ||||||||||||||||||||
_append_client_metadata(self._collection.database.client, DRIVER_METADATA) | ||||||||||||||||||||
|
||||||||||||||||||||
if auto_create_index is False: | ||||||||||||||||||||
return | ||||||||||||||||||||
|
@@ -356,17 +357,17 @@ def add_texts( | |||||||||||||||||||
metadatas_batch = [] | ||||||||||||||||||||
size = 0 | ||||||||||||||||||||
i = 0 | ||||||||||||||||||||
for j, (text, metadata) in enumerate(zip(texts, _metadatas)): | ||||||||||||||||||||
for j, (text, metadata) in enumerate(zip(texts, _metadatas, strict=False)): | ||||||||||||||||||||
size += len(text) + len(metadata) | ||||||||||||||||||||
texts_batch.append(text) | ||||||||||||||||||||
metadatas_batch.append(metadata) | ||||||||||||||||||||
if (j + 1) % batch_size == 0 or size >= 47_000_000: | ||||||||||||||||||||
if ids: | ||||||||||||||||||||
batch_res = self.bulk_embed_and_insert_texts( | ||||||||||||||||||||
batch_res = bulk_embed_and_insert_texts( | ||||||||||||||||||||
texts_batch, metadatas_batch, ids[i : j + 1] | ||||||||||||||||||||
) | ||||||||||||||||||||
else: | ||||||||||||||||||||
batch_res = self.bulk_embed_and_insert_texts( | ||||||||||||||||||||
batch_res = bulk_embed_and_insert_texts( | ||||||||||||||||||||
texts_batch, metadatas_batch | ||||||||||||||||||||
) | ||||||||||||||||||||
Comment on lines
+370
to
372
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The external Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
result_ids.extend(batch_res) | ||||||||||||||||||||
|
@@ -376,13 +377,11 @@ def add_texts( | |||||||||||||||||||
i = j + 1 | ||||||||||||||||||||
if texts_batch: | ||||||||||||||||||||
if ids: | ||||||||||||||||||||
batch_res = self.bulk_embed_and_insert_texts( | ||||||||||||||||||||
batch_res = bulk_embed_and_insert_texts( | ||||||||||||||||||||
texts_batch, metadatas_batch, ids[i : j + 1] | ||||||||||||||||||||
) | ||||||||||||||||||||
Comment on lines
+380
to
382
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The external Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
else: | ||||||||||||||||||||
batch_res = self.bulk_embed_and_insert_texts( | ||||||||||||||||||||
texts_batch, metadatas_batch | ||||||||||||||||||||
) | ||||||||||||||||||||
batch_res = bulk_embed_and_insert_texts(texts_batch, metadatas_batch) | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The external Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
result_ids.extend(batch_res) | ||||||||||||||||||||
return result_ids | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -419,37 +418,6 @@ def get_by_ids(self, ids: Sequence[str], /) -> list[Document]: | |||||||||||||||||||
docs.append(Document(page_content=text, id=oid_to_str(_id), metadata=doc)) | ||||||||||||||||||||
return docs | ||||||||||||||||||||
|
||||||||||||||||||||
def bulk_embed_and_insert_texts( | ||||||||||||||||||||
self, | ||||||||||||||||||||
texts: Union[List[str], Iterable[str]], | ||||||||||||||||||||
metadatas: Union[List[dict], Generator[dict, Any, Any]], | ||||||||||||||||||||
ids: Optional[List[str]] = None, | ||||||||||||||||||||
) -> List[str]: | ||||||||||||||||||||
"""Bulk insert single batch of texts, embeddings, and optionally ids. | ||||||||||||||||||||
|
||||||||||||||||||||
See add_texts for additional details. | ||||||||||||||||||||
""" | ||||||||||||||||||||
if not texts: | ||||||||||||||||||||
return [] | ||||||||||||||||||||
# Compute embedding vectors | ||||||||||||||||||||
embeddings = self._embedding.embed_documents(list(texts)) | ||||||||||||||||||||
if not ids: | ||||||||||||||||||||
ids = [str(ObjectId()) for _ in range(len(list(texts)))] | ||||||||||||||||||||
docs = [ | ||||||||||||||||||||
{ | ||||||||||||||||||||
"_id": str_to_oid(i), | ||||||||||||||||||||
self._text_key: t, | ||||||||||||||||||||
self._embedding_key: embedding, | ||||||||||||||||||||
**m, | ||||||||||||||||||||
} | ||||||||||||||||||||
for i, t, m, embedding in zip(ids, texts, metadatas, embeddings) | ||||||||||||||||||||
] | ||||||||||||||||||||
operations = [ReplaceOne({"_id": doc["_id"]}, doc, upsert=True) for doc in docs] | ||||||||||||||||||||
# insert the documents in MongoDB Atlas | ||||||||||||||||||||
result = self._collection.bulk_write(operations) | ||||||||||||||||||||
assert result.upserted_ids is not None | ||||||||||||||||||||
return [oid_to_str(_id) for _id in result.upserted_ids.values()] | ||||||||||||||||||||
|
||||||||||||||||||||
def add_documents( | ||||||||||||||||||||
self, | ||||||||||||||||||||
documents: List[Document], | ||||||||||||||||||||
|
@@ -478,10 +446,11 @@ def add_documents( | |||||||||||||||||||
start = 0 | ||||||||||||||||||||
for end in range(batch_size, n_docs + batch_size, batch_size): | ||||||||||||||||||||
texts, metadatas = zip( | ||||||||||||||||||||
*[(doc.page_content, doc.metadata) for doc in documents[start:end]] | ||||||||||||||||||||
*[(doc.page_content, doc.metadata) for doc in documents[start:end]], | ||||||||||||||||||||
strict=False, | ||||||||||||||||||||
) | ||||||||||||||||||||
result_ids.extend( | ||||||||||||||||||||
self.bulk_embed_and_insert_texts( | ||||||||||||||||||||
bulk_embed_and_insert_texts( | ||||||||||||||||||||
texts=texts, metadatas=metadatas, ids=ids[start:end] | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The external
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
) | ||||||||||||||||||||
) | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The external
bulk_embed_and_insert_texts
function is being called without the requiredself
parameter that was present in the original method. This function needs to receive the embedding model, collection, and field configuration to work properly.Copilot uses AI. Check for mistakes.