Skip to content

Commit

Permalink
Harrison/optional ids opensearch (#6684)
Browse files Browse the repository at this point in the history
Co-authored-by: taekimsmar <66041442+taekimsmar@users.noreply.github.com>
  • Loading branch information
hwchase17 and taekimsmar committed Jun 24, 2023
1 parent 2518e6c commit c289cc8
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions langchain/vectorstores/opensearch_vector_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def _bulk_ingest_embeddings(
embeddings: List[List[float]],
texts: Iterable[str],
metadatas: Optional[List[dict]] = None,
ids: Optional[List[str]] = None,
vector_field: str = "vector_field",
text_field: str = "text",
mapping: Optional[Dict] = None,
Expand All @@ -88,7 +89,7 @@ def _bulk_ingest_embeddings(
bulk = _import_bulk()
not_found_error = _import_not_found_error()
requests = []
ids = []
return_ids = []
mapping = mapping

try:
Expand All @@ -98,7 +99,7 @@ def _bulk_ingest_embeddings(

for i, text in enumerate(texts):
metadata = metadatas[i] if metadatas else {}
_id = str(uuid.uuid4())
_id = ids[i] if ids else str(uuid.uuid4())
request = {
"_op_type": "index",
"_index": index_name,
Expand All @@ -108,10 +109,10 @@ def _bulk_ingest_embeddings(
"_id": _id,
}
requests.append(request)
ids.append(_id)
return_ids.append(_id)
bulk(client, requests)
client.indices.refresh(index=index_name)
return ids
return return_ids


def _default_scripting_text_mapping(
Expand Down Expand Up @@ -318,6 +319,7 @@ def add_texts(
self,
texts: Iterable[str],
metadatas: Optional[List[dict]] = None,
ids: Optional[List[str]] = None,
bulk_size: int = 500,
**kwargs: Any,
) -> List[str]:
Expand All @@ -326,6 +328,7 @@ def add_texts(
Args:
texts: Iterable of strings to add to the vectorstore.
metadatas: Optional list of metadatas associated with the texts.
ids: Optional list of ids to associate with the texts.
bulk_size: Bulk API request count; Default: 500
Returns:
Expand Down Expand Up @@ -358,10 +361,11 @@ def add_texts(
self.index_name,
embeddings,
texts,
metadatas,
vector_field,
text_field,
mapping,
metadatas=metadatas,
ids=ids,
vector_field=vector_field,
text_field=text_field,
mapping=mapping,
)

def similarity_search(
Expand Down Expand Up @@ -679,9 +683,9 @@ def from_texts(
index_name,
embeddings,
texts,
metadatas,
vector_field,
text_field,
mapping,
metadatas=metadatas,
vector_field=vector_field,
text_field=text_field,
mapping=mapping,
)
return cls(opensearch_url, index_name, embedding, **kwargs)

0 comments on commit c289cc8

Please sign in to comment.