Added additional headers for HuggingFaceInferenceAPIEmbeddings endpoint. (#21282)

Thank you for contributing to LangChain!

- [ ] **HuggingFaceInferenceAPIEmbeddings**: "Additional Headers"
  - Where: langchain, community, embeddings. huggingface.py.
- Community: add additional headers when needed by custom HuggingFace
TEI embedding endpoints. HuggingFaceInferenceAPIEmbeddings"


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
- **Description:** Adding the `additional_headers` to be passed to
requests library if needed
    - **Dependencies:** none
 

- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. Tested with locally available TEI endpoints with and without
`additional_headers`
  2. Example  Usage
  
```python
embeddings=HuggingFaceInferenceAPIEmbeddings(
                             api_key=MY_CUSTOM_API_KEY,
                             api_url=MY_CUSTOM_TEI_URL,
                             additional_headers={
                                "Content-Type": "application/json"
                               }
)
```

 

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, hwchase17.

---------

Co-authored-by: Massimiliano Pronesti <massimiliano.pronesti@gmail.com>
Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
pull/15936/merge
Miroslav 2 weeks ago committed by GitHub
parent c34419e200
commit 04e2611fea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -311,6 +311,8 @@ class HuggingFaceInferenceAPIEmbeddings(BaseModel, Embeddings):
"""The name of the model to use for text embeddings."""
api_url: Optional[str] = None
"""Custom inference endpoint url. None for using default public url."""
additional_headers: Dict[str, str] = {}
"""Pass additional headers to the requests library if needed."""
@property
def _api_url(self) -> str:
@ -327,7 +329,10 @@ class HuggingFaceInferenceAPIEmbeddings(BaseModel, Embeddings):
@property
def _headers(self) -> dict:
return {"Authorization": f"Bearer {self.api_key.get_secret_value()}"}
return {
"Authorization": f"Bearer {self.api_key.get_secret_value()}",
**self.additional_headers,
}
def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""Get the embeddings for a list of texts.

Loading…
Cancel
Save