From the tutorials I am using the example that is provided

from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings

embeddings = HuggingFaceInferenceAPIEmbeddings(
    api_key=inference_api_key, model_name="sentence-transformers/all-MiniLM-l6-v2"
text = "this is a sample text"
query_result = embeddings.embed_query(text)

But this results in the error:

File ~/miniconda3/envs/rag/lib/python3.12/site-packages/langchain_community/embeddings/huggingface.py:373, in HuggingFaceInferenceAPIEmbeddings.embed_query(self, text)
    364 def embed_query(self, text: str) -> List[float]:
    365     """Compute query embeddings using a HuggingFace transformer model.
    367     Args:
    371         Embeddings for the text.
    372     """
--> 373     return self.embed_documents([text])[0]

KeyError: 0

I can generate the text completions from LLMs hosted at the huggingface hub using the same inference_api_key so I guess maybe that is fine.(?)

Can you please help me understand how to use the inference API to get the embeddings from any embeddings model hosted on the huggingfacehub

  • 1
    $\begingroup$ Cross-posted on SO. $\endgroup$
    – noe
    Commented Apr 27 at 21:08
  • $\begingroup$ For visibility. There are not many sources on how to see a custom embeddings model from huggingface with langchain $\endgroup$ Commented Apr 27 at 21:50


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.