Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: Chat Huggingface and HuggingFace TGI llms #15582

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0c8c768
community: add API key into huggingface TGI validation
Jan 5, 2024
4695323
community: Ignore `_resolve_model_id()` when calling a self-hosted HF…
Jan 5, 2024
bc5b951
format and linting
Jan 5, 2024
02f6745
Add integration test for HuggingFace TGI
Jan 5, 2024
3212ba5
Merge branch 'master' into chat-huggingface
minhna1112 Jan 5, 2024
a14aa1d
Merge branch 'master' into chat-huggingface
minhna1112 Jan 8, 2024
e1a5378
Merge branch 'master' into chat-huggingface
minhna1112 Jan 15, 2024
d685cd9
Fix: Spelling error in
Jan 17, 2024
051a50a
Merge branch 'master' into chat-huggingface
minhna1112 Jan 17, 2024
4907c8e
Merge branch 'master' into chat-huggingface
minhna1112 Jan 22, 2024
55f91f1
Merge branch 'master' into chat-huggingface
minhna1112 Jan 24, 2024
cb0e687
Update libs/community/langchain_community/llms/huggingface_text_gen_i…
minhna1112 Jan 24, 2024
100bb88
Merge branch 'master' into chat-huggingface
minhna1112 Jan 25, 2024
32467bb
Merge branch 'master' into chat-huggingface
minhna1112 Jan 30, 2024
0f9784c
Introduce `text_generation` dependency:
Jan 30, 2024
e9d534a
Merge branch 'langchain-ai:master' into chat-huggingface
minhna1112 Jan 30, 2024
6d0938f
Minor fix in `libs/community/pyproject.toml`
minhna1112 Jan 30, 2024
80f9dcc
Merge branch 'chat-huggingface' of https://github.com/FSoft-AI4Code/l…
minhna1112 Jan 30, 2024
bdfd0c7
Merge branch 'master' into chat-huggingface
minhna1112 Jan 30, 2024
23bf16e
Merge branch 'master' into chat-huggingface
minhna1112 Feb 1, 2024
fe74592
Merge branch 'master' into chat-huggingface
minhna1112 Feb 4, 2024
c7798e8
Merge branch 'master' into chat-huggingface
minhna1112 Feb 15, 2024
c1f6277
Re-lock poetry
minhna1112 Feb 15, 2024
85e61c7
Merge branch 'master' into chat-huggingface
minhna1112 Feb 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def __init__(self, **kwargs: Any):

from transformers import AutoTokenizer

self._resolve_model_id()
# `self.model_id` should only be resolved when not explicitly defined
# If `llm` is a HuggingFaceTextGenInference, there would exist cases
# in which the TGI server is not a HuggingFace-deployed Inference Endpoint
# that has an explicit model ID, but a self-hosted version
if not self.model_id:
self._resolve_model_id()
self.tokenizer = (
AutoTokenizer.from_pretrained(self.model_id)
if self.tokenizer is None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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_pydantic_field_names
from langchain_core.utils import get_from_dict_or_env, get_pydantic_field_names

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -95,6 +95,8 @@ class HuggingFaceTextGenInference(LLM):
"""Holds any model parameters valid for `call` not explicitly specified"""
client: Any
async_client: Any
"""Keyword arguments to pass to the model."""
huggingfacehub_api_token: Optional[str] = None

class Config:
"""Configuration for this pydantic object."""
Expand Down Expand Up @@ -129,8 +131,17 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:

@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that python package exists in environment."""

"""Validate that api key and python package exists in environment."""
huggingfacehub_api_token = get_from_dict_or_env(
values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN"
)
minhna1112 marked this conversation as resolved.
Show resolved Hide resolved
# When TGI make requests to Huggingface's Inference Endpoints,
# a bearer token must be included into the request header for authorization
# https://github.com/huggingface/text-generation-inference/issues/747
if huggingfacehub_api_token:
values["server_kwargs"]["headers"] = {
"Authorization": f"Bearer {huggingfacehub_api_token}"
}
try:
import text_generation

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from text_generation import AsyncClient, Client

from langchain_community.llms import HuggingFaceTextGenInference


Expand All @@ -17,3 +19,23 @@ def test_invocation_params_stop_sequences() -> None:
runtime_stop = ["stop"]
assert llm._invocation_params(runtime_stop)["stop_sequences"] == [".", "stop"]
assert llm._default_params["stop_sequences"] == ["."]


def test_client_type() -> None:
llm = HuggingFaceTextGenInference()

assert isinstance(llm.client, Client)
assert isinstance(llm.async_client, AsyncClient)


def test_bearer_api() -> None:
llm = HuggingFaceTextGenInference()
# If called from a self-hosted TGI server,
assert not llm.client.headers
assert not llm.async_client.headers

BEARER_TOKEN = "abcdef1230"
llm = HuggingFaceTextGenInference(huggingfacehub_api_token=BEARER_TOKEN)
# If called from a self-hosted TGI server,
assert llm.client.headers["Authorization"] == f"Bearer {BEARER_TOKEN}"
assert llm.async_client.headers["Authorization"] == f"Bearer {BEARER_TOKEN}"
Loading