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.
This commit is contained in:
Jiejun Tan 2024-07-04 04:30:29 +08:00 committed by GitHub
parent 9ccc4b1616
commit 2be66a38d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 2 deletions

View File

@ -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())

View File

@ -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())