Search code examples
pythonhuggingface-transformersembeddinglangchainllama-index

langchain emedding with huggingface can't pass the access token


I am creating an index of documents with llama_index and langchain. For the embedding I want to use https://huggingface.co/bert-base-multilingual-cased. I had no problem creating the embedding using the following code:

from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from llama_index import LangchainEmbedding, ServiceContext

embed_model = LangchainEmbedding(
  HuggingFaceEmbeddings(model_name="bert-base-multilingual-cased")
)
service_context = ServiceContext.from_defaults(embed_model=embed_model) 

my problem is that I don't understand how to use my HF access token when requesting the model. I want to use the access token because 1) it is suggested by the HF documentation and 2) I will probably use the HF Pro Plan in order to have an higher api rate limit.


Solution

  • I think you can't use authorization tokens in langchain.embeddings.HuggingfaceEmbeddings but you can surely use hugging face hub if you need to use the authorization tokens.

    from langchain.embeddings import HuggingFaceHubEmbeddings
    embeddings = HuggingFaceHubEmbeddings(repo_id='path/to/repo', 
                               huggingfacehub_api_token='API_TOKEN')