DOC: HuggingFaceEmbeddings vs. HuggingFaceHubEmbeddings
Labels: name: area: embeddings, name: area: doc loader, name: auto:question, name: auto:documentation

### Issue with current documentation:

Hello folks,

I am not quite sure if my issue is an issue regarding the docs or rather the implementation.

### What is the differnece between HuggingFaceEmbeddings and HuggingFaceHubEmbeddings?

Currently in [v0.0.312](https://github.com/langchain-ai/langchain/releases/tag/v0.0.312), there are two pretty similar modules in the `langchain` package, namely `libs/langchain/langchain/embeddings/huggingface.py` and `libs/langchain/langchain/embeddings/huggingface_hub.py` (compare https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/embeddings/huggingface.py and https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/embeddings/huggingface_hub.py).

Moreover, these modules provide pretty similar classes: `HuggingFaceEmbeddings` and `HuggingFaceHubEmbeddings`.

When should I use which one? This question arises as a consequence of the described example below.

### Relevant Example

Considering the task of using a LLM with additional documents, you could consider the following code:

```
from langchain.llms.huggingface_hub import HuggingFaceHub
from langchain.prompts import PromptTemplate
from langchain.vectorstores.faiss import FAISS
from langchain.chains import LLMChain
from langchain.embeddings import HuggingFaceHubEmbeddings, HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter

model = HuggingFaceHub(repo_id=llm, model_kwargs={"temperature": 0.2, "max_length": 64, "raw_response": True}, huggingfacehub_api_token=HUGGINGFACEHUB_API_KEY)
prompt = PromptTemplate(input_variables=["question", "docs"],
                    template="""
                                    Answer the following question: {question}
                                    By searching the following documents: {docs}
                                    If you feel like you don't have enough information to answer the question, say "I don't know".
                                    Your answers should be verbose and detailed.
                                    """)
chain = LLMChain(llm=model, prompt=prompt)
docs = "bla bli blub ..."
text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=5,
            chunk_overlap=0
        )
documents = text_splitter.split_text(document)

# A) 
embeddings = HuggingFaceEmbeddings()

# B) alternatively
# embeddings = HuggingFaceHubEmbeddings(huggingfacehub_api_token=self.huggingfacehub_api_key)

self.documents_db = FAISS.from_texts(documents, embeddings)

docs = self.documents_db.similarity_search(prompt, k=4)
docs_page_content = " ".join([d.page_content for d in docs])

response = self.llm_chain.run(question=prompt, docs=docs_page_content)
response = response.replace("\n", "")
```

### Observed Behaviour

#### A)

Everything seems to work reliable and smooth without any errors or unexpected outputs.

#### B)

I did not conduct a study, but in my empirical testings I could observe the following.

Sometimes it works like A) and everything seems to work smooth and reliable,

However, sometimes (approximately 50% of the time i am running this script), I am getting one of the following two different errors:

##### KeyError

```
You're using a different task than the one specified in the repository. Be sure to know what you're doing :)
Traceback (most recent call last):
  File "/qa_docs.py", line 59, in <module>
    qa.add_document_to_context(document)
  File "/qa.py", line 79, in add_document_to_context
    self.documents_db = FAISS.from_texts(documents, embeddings)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/langchain/vectorstores/faiss.py", line 603, in from_texts
    return cls.__from(
           ^^^^^^^^^^^
  File "/lib/python3.11/site-packages/langchain/vectorstores/faiss.py", line 562, in __from
    index = faiss.IndexFlatL2(len(embeddings[0]))
                                  ~~~~~~~~~~^^^
KeyError: 0
```


##### ChunkedEncodingError

```
You're using a different task than the one specified in the repository. Be sure to know what you're doing :)
Traceback (most recent call last):
  File "/lib/python3.11/site-packages/urllib3/response.py", line 710, in _error_catcher
    yield
  File "/lib/python3.11/site-packages/urllib3/response.py", line 1073, in read_chunked
    self._update_chunk_length()
  File "/lib/python3.11/site-packages/urllib3/response.py", line 1008, in _update_chunk_length
    raise InvalidChunkLength(self, line) from None
urllib3.exceptions.InvalidChunkLength: InvalidChunkLength(got length b'', 0 bytes read)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/lib/python3.11/site-packages/requests/models.py", line 816, in generate
    yield from self.raw.stream(chunk_size, decode_content=True)
  File "/lib/python3.11/site-packages/urllib3/response.py", line 933, in stream
    yield from self.read_chunked(amt, decode_content=decode_content)
  File "/lib/python3.11/site-packages/urllib3/response.py", line 1061, in read_chunked
    with self._error_catcher():
  File "/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/lib/python3.11/site-packages/urllib3/response.py", line 727, in _error_catcher
    raise ProtocolError(f"Connection broken: {e!r}", e) from e
urllib3.exceptions.ProtocolError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "qa_docs.py", line 59, in <module>
    qa.add_document_to_context(document)
  File "qa.py", line 79, in add_document_to_context
    self.documents_db = FAISS.from_texts(documents, embeddings)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/langchain/vectorstores/faiss.py", line 602, in from_texts
    embeddings = embedding.embed_documents(texts)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/langchain/embeddings/huggingface_hub.py", line 90, in embed_documents
    responses = self.client(inputs=texts, params=_model_kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/huggingface_hub/inference_api.py", line 190, in __call__
    response = get_session().post(self.api_url, headers=self.headers, json=payload, data=data)
  File "/lib/python3.11/site-packages/requests/sessions.py", line 637, in post
    return self.request("POST", url, data=data, json=json, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/requests/sessions.py", line 747, in send
    r.content
  File "/lib/python3.11/site-packages/requests/models.py", line 899, in content
    self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b""
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/requests/models.py", line 818, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))
```

### Remarks about the documents

Important note: the value of param `documents` is calculated by another script and is not fixed. It is a textual inference of another model. I already thought about that being the cause of the errors I encounter in some script executions but I have no idea how this could be. I can assure that there is always at least 100 characters that are being the value of `documents`.

However, I think that this problem already may be answered if I fully understand the differences between the two classes `HuggingFaceHubEmbeddings` and `HuggingFaceEmbeddings`.

### Related langchain documentation

- https://python.langchain.com/docs/integrations/vectorstores/faiss
- https://python.langchain.com/docs/integrations/providers/huggingface
- https://python.langchain.com/docs/integrations/llms/huggingface_hub.html
- https://python.langchain.com/docs/integrations/text_embedding/huggingfacehub.html

If there is more information needed, feel free to ask. I am looking forward for every help I can get!

### Idea or request for content:

When to use HuggingFaceEmbeddings vs. when to use HuggingFaceHubEmbeddings.