From 7c7ee07d30c3bf2e5613cfd9bcf21710364ba038 Mon Sep 17 00:00:00 2001 From: af su <53895794+vegetablest@users.noreply.github.com> Date: Thu, 21 Nov 2024 03:08:56 +0800 Subject: [PATCH] huggingface[fix]: HuggingFaceEndpointEmbeddings model parameter passing error when async embed (#27953) This change refines the handling of _model_kwargs in POST requests. Instead of nesting _model_kwargs as a dictionary under the parameters key, it is now directly unpacked and merged into the request's JSON payload. This ensures that the model parameters are passed correctly and avoids unnecessary nesting.E. g.: ```python import asyncio from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings embedding_input = ["This input will get multiplied" * 10000] embeddings = HuggingFaceEndpointEmbeddings( model="http://127.0.0.1:8081/embed", model_kwargs={"truncate": True}, ) # Truncated parameters in synchronized methods are handled correctly embeddings.embed_documents(texts=embedding_input) # The truncate parameter is not handled correctly in the asynchronous method, # and 413 Request Entity Too Large is returned. asyncio.run(embeddings.aembed_documents(texts=embedding_input)) ``` Co-authored-by: af su Co-authored-by: Erick Friis --- .../langchain_huggingface/embeddings/huggingface_endpoint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py b/libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py index 7d9ecd1ac0b..baef1967ee1 100644 --- a/libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py +++ b/libs/partners/huggingface/langchain_huggingface/embeddings/huggingface_endpoint.py @@ -127,7 +127,7 @@ class HuggingFaceEndpointEmbeddings(BaseModel, Embeddings): texts = [text.replace("\n", " ") for text in texts] _model_kwargs = self.model_kwargs or {} responses = await self.async_client.post( - json={"inputs": texts, "parameters": _model_kwargs}, task=self.task + json={"inputs": texts, **_model_kwargs}, task=self.task ) return json.loads(responses.decode())