mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
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 <erick@langchain.dev>
This commit is contained in:
parent
d09dda5a08
commit
aee55eda39
@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
from typing import Any, AsyncIterator, Dict, Iterator, List, Mapping, Optional
|
from typing import Any, AsyncIterator, Dict, Iterator, List, Mapping, Optional
|
||||||
|
|
||||||
from langchain_core._api.deprecation import deprecated
|
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.outputs import GenerationChunk
|
||||||
from langchain_core.pydantic_v1 import Extra, Field, root_validator
|
from langchain_core.pydantic_v1 import Extra, Field, root_validator
|
||||||
from langchain_core.utils import (
|
from langchain_core.utils import (
|
||||||
get_from_dict_or_env,
|
|
||||||
get_pydantic_field_names,
|
get_pydantic_field_names,
|
||||||
pre_init,
|
pre_init,
|
||||||
)
|
)
|
||||||
@ -177,16 +177,17 @@ class HuggingFaceEndpoint(LLM):
|
|||||||
"Could not import huggingface_hub python package. "
|
"Could not import huggingface_hub python package. "
|
||||||
"Please install it with `pip install huggingface_hub`."
|
"Please install it with `pip install huggingface_hub`."
|
||||||
)
|
)
|
||||||
try:
|
huggingfacehub_api_token = values["huggingfacehub_api_token"] or os.getenv(
|
||||||
huggingfacehub_api_token = get_from_dict_or_env(
|
"HUGGINGFACEHUB_API_TOKEN"
|
||||||
values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN"
|
)
|
||||||
)
|
if huggingfacehub_api_token is not None:
|
||||||
login(token=huggingfacehub_api_token)
|
try:
|
||||||
except Exception as e:
|
login(token=huggingfacehub_api_token)
|
||||||
raise ValueError(
|
except Exception as e:
|
||||||
"Could not authenticate with huggingface_hub. "
|
raise ValueError(
|
||||||
"Please check your API token."
|
"Could not authenticate with huggingface_hub. "
|
||||||
) from e
|
"Please check your API token."
|
||||||
|
) from e
|
||||||
|
|
||||||
from huggingface_hub import AsyncInferenceClient, InferenceClient
|
from huggingface_hub import AsyncInferenceClient, InferenceClient
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user