Skip to content

Commit

Permalink
Added additional headers for HuggingFaceInferenceAPIEmbeddings endpoi…
Browse files Browse the repository at this point in the history
…nt. (langchain-ai#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 <[email protected]>
Co-authored-by: Harrison Chase <[email protected]>
  • Loading branch information
3 people authored and dglogo committed May 8, 2024
1 parent 29bd7b5 commit 9adc689
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion libs/community/langchain_community/embeddings/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -327,7 +329,10 @@ def _default_api_url(self) -> str:

@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.
Expand Down

0 comments on commit 9adc689

Please sign in to comment.