From aee55eda3932d7dd526db214be6b8161b43f4ff6 Mon Sep 17 00:00:00 2001 From: Miroslav Date: Fri, 12 Jul 2024 17:10:32 -0500 Subject: [PATCH] community: Skip Login to HuggubgFaceHub when token is not set (#21561) Thank you for contributing to LangChain! - [ ] **HuggingFaceEndpoint**: "Skip Login to HuggingFaceHub" - Where: langchain, community, llm, huggingface_endpoint - [ ] **PR message**: ***Delete this entire checklist*** and replace with - **Description:** Skip login to huggingface hub when when `huggingfacehub_api_token` is not set. This is needed when using custom `endpoint_url` outside of HuggingFaceHub. - **Issue:** the issue # it fixes https://github.com/langchain-ai/langchain/issues/20342 and https://github.com/langchain-ai/langchain/issues/19685 - **Dependencies:** None - [ ] **Add tests and docs**: 1. Tested with locally available TGI endpoint 2. Example Usage ```python from langchain_community.llms import HuggingFaceEndpoint llm = HuggingFaceEndpoint( endpoint_url='http://localhost:8080', server_kwargs={ "headers": {"Content-Type": "application/json"} } ) resp = llm.invoke("Tell me a joke") print(resp) ``` Also tested against HF Endpoints ```python from langchain_community.llms import HuggingFaceEndpoint huggingfacehub_api_token = "hf_xyz" repo_id = "mistralai/Mistral-7B-Instruct-v0.2" llm = HuggingFaceEndpoint( huggingfacehub_api_token=huggingfacehub_api_token, repo_id=repo_id, ) resp = llm.invoke("Tell me a joke") print(resp) ``` 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: Erick Friis --- .../llms/huggingface_endpoint.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/libs/community/langchain_community/llms/huggingface_endpoint.py b/libs/community/langchain_community/llms/huggingface_endpoint.py index 31884057ff..2fe1bdc862 100644 --- a/libs/community/langchain_community/llms/huggingface_endpoint.py +++ b/libs/community/langchain_community/llms/huggingface_endpoint.py @@ -1,5 +1,6 @@ import json import logging +import os from typing import Any, AsyncIterator, Dict, Iterator, List, Mapping, Optional from langchain_core._api.deprecation import deprecated @@ -11,7 +12,6 @@ from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk from langchain_core.pydantic_v1 import Extra, Field, root_validator from langchain_core.utils import ( - get_from_dict_or_env, get_pydantic_field_names, pre_init, ) @@ -177,16 +177,17 @@ class HuggingFaceEndpoint(LLM): "Could not import huggingface_hub python package. " "Please install it with `pip install huggingface_hub`." ) - try: - huggingfacehub_api_token = get_from_dict_or_env( - values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN" - ) - login(token=huggingfacehub_api_token) - except Exception as e: - raise ValueError( - "Could not authenticate with huggingface_hub. " - "Please check your API token." - ) from e + huggingfacehub_api_token = values["huggingfacehub_api_token"] or os.getenv( + "HUGGINGFACEHUB_API_TOKEN" + ) + if huggingfacehub_api_token is not None: + try: + login(token=huggingfacehub_api_token) + except Exception as e: + raise ValueError( + "Could not authenticate with huggingface_hub. " + "Please check your API token." + ) from e from huggingface_hub import AsyncInferenceClient, InferenceClient