From 04e2611feaef4f461f1802cb38e7d8300b278fd6 Mon Sep 17 00:00:00 2001 From: Miroslav Date: Tue, 7 May 2024 13:17:53 -0500 Subject: [PATCH] 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 Co-authored-by: Harrison Chase --- .../langchain_community/embeddings/huggingface.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/embeddings/huggingface.py b/libs/community/langchain_community/embeddings/huggingface.py index 3c9cee38927..7eb175dfafc 100644 --- a/libs/community/langchain_community/embeddings/huggingface.py +++ b/libs/community/langchain_community/embeddings/huggingface.py @@ -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.