From 2be66a38d849cd81296e5dad9353a2d246fa158f Mon Sep 17 00:00:00 2001 From: Jiejun Tan <55200715+plageon@users.noreply.github.com> Date: Thu, 4 Jul 2024 04:30:29 +0800 Subject: [PATCH] huggingface: Fix huggingface tei support (#22653) Update former pull request: https://github.com/langchain-ai/langchain/pull/22595. Modified `libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py`, where the API call function does not match current [Text Embeddings Inference API](https://huggingface.github.io/text-embeddings-inference/#/Text%20Embeddings%20Inference/embed). One example is: ```json { "inputs": "string", "normalize": true, "truncate": false } ``` Parameters in `_model_kwargs` are not passed properly in the latest version. By the way, the issue *[why cause 413? #50](https://github.com/huggingface/text-embeddings-inference/issues/50)* might be solved. --- .../langchain_community/embeddings/huggingface_hub.py | 3 ++- .../langchain_huggingface/embeddings/huggingface_endpoint.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/community/langchain_community/embeddings/huggingface_hub.py b/libs/community/langchain_community/embeddings/huggingface_hub.py index 0ee0881d2e7..df95a2d2720 100644 --- a/libs/community/langchain_community/embeddings/huggingface_hub.py +++ b/libs/community/langchain_community/embeddings/huggingface_hub.py @@ -112,8 +112,9 @@ class HuggingFaceHubEmbeddings(BaseModel, Embeddings): # replace newlines, which can negatively affect performance. texts = [text.replace("\n", " ") for text in texts] _model_kwargs = self.model_kwargs or {} + # api doc: https://huggingface.github.io/text-embeddings-inference/#/Text%20Embeddings%20Inference/embed responses = self.client.post( - json={"inputs": texts, "parameters": _model_kwargs}, task=self.task + json={"inputs": texts, **_model_kwargs}, task=self.task ) return json.loads(responses.decode()) diff --git a/libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py b/libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py index 519a42ae393..20df972cb26 100644 --- a/libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py +++ b/libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py @@ -108,8 +108,9 @@ class HuggingFaceEndpointEmbeddings(BaseModel, Embeddings): # replace newlines, which can negatively affect performance. texts = [text.replace("\n", " ") for text in texts] _model_kwargs = self.model_kwargs or {} + # api doc: https://huggingface.github.io/text-embeddings-inference/#/Text%20Embeddings%20Inference/embed responses = self.client.post( - json={"inputs": texts, "parameters": _model_kwargs}, task=self.task + json={"inputs": texts, **_model_kwargs}, task=self.task ) return json.loads(responses.decode())