-
Notifications
You must be signed in to change notification settings - Fork 16k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use HB Inference Client in llms package
- Loading branch information
1 parent
7025fa2
commit 8033249
Showing
5 changed files
with
168 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
libs/community/langchain_community/utils/huggingface_hub.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from __future__ import annotations | ||
|
||
from importlib.metadata import version | ||
|
||
from packaging.version import parse | ||
|
||
|
||
def is_inference_client_supported() -> bool: | ||
"""Return whether HuggingFace Hub Client library supports InferenceClient.""" | ||
# InferenceAPI was deprecated 0.17. | ||
# See https://github.com/huggingface/huggingface_hub/commit/0a02b04e6cab31a906ddeaf61fce0d5df4b4f7be. | ||
_version = parse(version("hugingface_hub")) | ||
return not (_version.major == 0 and _version.minor < 17) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
libs/community/tests/unit_tests/llms/test_huggingface_hub.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
from unittest.mock import MagicMock, PropertyMock, patch | ||
|
||
import pytest | ||
|
||
from langchain_community.llms.huggingface_hub import HuggingFaceHub | ||
|
||
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hb-token" | ||
|
||
|
||
@pytest.mark.requires("huggingface_hub") | ||
@patch( | ||
"langchain_community.llms.huggingface_hub.is_inference_client_supported", | ||
return_value=True, | ||
) | ||
def test_huggingface_hub_with_model(mock_is_inference_client_supported) -> None: | ||
from huggingface_hub import InferenceClient | ||
|
||
llm = HuggingFaceHub(model="model-1") | ||
|
||
assert type(llm.client) is InferenceClient | ||
assert llm.client.model == "model-1" | ||
assert llm.client.headers["authorization"] == "Bearer hb-token" | ||
|
||
mock_response = MagicMock() | ||
type(mock_response).content = PropertyMock(return_value=b"Hello, world!") | ||
|
||
with patch("huggingface_hub.inference._client.get_session") as mock_session: | ||
mock_session.return_value.post.return_value = mock_response | ||
|
||
completion = llm("Hi, how are you?") | ||
|
||
assert completion == "Hello, world!" | ||
|
||
|
||
@pytest.mark.requires("huggingface_hub") | ||
@patch( | ||
"langchain_community.llms.huggingface_hub.is_inference_client_supported", | ||
return_value=True, | ||
) | ||
def test_huggingface_hub_with_task(mock_is_inference_client_supported) -> None: | ||
from huggingface_hub import InferenceClient | ||
|
||
llm = HuggingFaceHub(task="task-1") | ||
|
||
assert type(llm.client) is InferenceClient | ||
assert llm.client.model is None | ||
assert llm.client.headers["authorization"] == "Bearer hb-token" | ||
assert llm.task == "task-1" | ||
|
||
|
||
@pytest.mark.requires("huggingface_hub") | ||
@patch( | ||
"langchain_community.llms.huggingface_hub.is_inference_client_supported", | ||
return_value=False, | ||
) | ||
def test_huggingface_hub_param_with_inference_api( | ||
mock_is_inference_client_supported, | ||
) -> None: | ||
from huggingface_hub import InferenceApi | ||
|
||
with patch("huggingface_hub.inference_api.HfApi") as mock_hfapi: | ||
llm = HuggingFaceHub(repo_id="model-1", task="text-generation") | ||
|
||
mock_hfapi.assert_called_once_with(token="hb-token") | ||
|
||
assert type(llm.client) is InferenceApi | ||
assert "model-1" in llm.client.api_url | ||
assert llm.client.task == "text-generation" | ||
assert llm.client.headers["authorization"] == "Bearer hb-token" | ||
|
||
llm("Hi, how are you?") | ||
|
||
|
||
# TODO: Assert the model invocation selectes the task | ||