Skip to content

The agent engineering platform.

community[patch]: Update OpenVINO embedding and reranker to support static input shape (#22171)

Ethan Yang52da6a160d99
diff --git a/libs/community/langchain_community/document_compressors/openvino_rerank.py b/libs/community/langchain_community/document_compressors/openvino_rerank.py
index b5222a2dcd..24fb926730 100644
--- a/libs/community/langchain_community/document_compressors/openvino_rerank.py
+++ b/libs/community/langchain_community/document_compressors/openvino_rerank.py
@@ -114,9 +114,19 @@ class OpenVINOReranker(BaseDocumentCompressor):
passages = request.passages
query_passage_pairs = [[query, passage["text"]] for passage in passages]
- input_tensors = self.tokenizer(
- query_passage_pairs, padding=True, truncation=True, return_tensors="pt"
- )
+ length = self.ov_model.request.inputs[0].get_partial_shape()[1]
+ if length.is_dynamic:
+ input_tensors = self.tokenizer(
+ query_passage_pairs, padding=True, truncation=True, return_tensors="pt"
+ )
+ else:
+ input_tensors = self.tokenizer(
+ query_passage_pairs,
+ padding="max_length",
+ max_length=length.get_length(),
+ truncation=True,
+ return_tensors="pt",
+ )
outputs = self.ov_model(**input_tensors, return_dict=True)
if outputs[0].shape[1] > 1:
diff --git a/libs/community/langchain_community/embeddings/openvino.py b/libs/community/langchain_community/embeddings/openvino.py
index 7e4d52fd59..dd4e939a89 100644
--- a/libs/community/langchain_community/embeddings/openvino.py
+++ b/libs/community/langchain_community/embeddings/openvino.py
@@ -210,9 +210,20 @@ class OpenVINOEmbeddings(BaseModel, Embeddings):
0, len(sentences), batch_size, desc="Batches", disable=not show_progress_bar
):
sentences_batch = sentences_sorted[start_index : start_index + batch_size]
- features = self.tokenizer(
- sentences_batch, padding=True, truncation=True, return_tensors="pt"
- )
+
+ length = self.ov_model.request.inputs[0].get_partial_shape()[1]
+ if length.is_dynamic:
+ features = self.tokenizer(
+ sentences_batch, padding=True, truncation=True, return_tensors="pt"
+ )
+ else:
+ features = self.tokenizer(
+ sentences_batch,
+ padding="max_length",
+ max_length=length.get_length(),
+ truncation=True,
+ return_tensors="pt",
+ )
out_features = self.ov_model(**features)
if mean_pooling: