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

Fix InferenceEndpointsLLM not using cached token #690

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 8 additions & 2 deletions src/distilabel/llms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class AsyncLLM(LLM):
"""

_event_loop: "asyncio.AbstractEventLoop" = PrivateAttr(default=None)
_new_event_loop: bool = PrivateAttr(default=False)

@property
def generate_parameters(self) -> List[inspect.Parameter]:
Expand All @@ -259,8 +260,10 @@ def event_loop(self) -> "asyncio.AbstractEventLoop":
self._event_loop = asyncio.get_running_loop()
if self._event_loop.is_closed():
self._event_loop = asyncio.new_event_loop() # type: ignore
self._new_event_loop = True
except RuntimeError:
self._event_loop = asyncio.new_event_loop()
self._new_event_loop = True
asyncio.set_event_loop(self._event_loop)
return self._event_loop

Expand Down Expand Up @@ -303,8 +306,11 @@ def __del__(self) -> None:
"""Closes the event loop when the object is deleted."""
if sys.meta_path is None:
return
if self.event_loop is not None:
self.event_loop.close()

if self._new_event_loop:
if self._event_loop.is_running():
self._event_loop.stop()
self._event_loop.close()

@staticmethod
def _prepare_structured_output(
Expand Down
14 changes: 10 additions & 4 deletions src/distilabel/llms/huggingface/inference_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import random
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Any, List, Optional, Union

from pydantic import (
Expand Down Expand Up @@ -206,6 +207,7 @@ def load(self) -> None: # noqa: C901
from huggingface_hub import (
AsyncInferenceClient,
InferenceClient,
constants,
get_inference_endpoint,
)
except ImportError as ie:
Expand All @@ -215,10 +217,14 @@ def load(self) -> None: # noqa: C901
) from ie

if self.api_key is None:
raise ValueError(
f"To use `{self.__class__.__name__}` an API key must be provided via `api_key`"
f" attribute or runtime parameter, or set the environment variable `{self._api_key_env_var}`."
)
if not Path(constants.HF_TOKEN_PATH).exists():
raise ValueError(
f"To use `{self.__class__.__name__}` an API key must be provided via"
" `api_key` attribute or runtime parameter, set the environment variable"
f" `{self._api_key_env_var}` or use the `huggingface-hub` CLI to login"
" with `huggingface-cli login`."
)
self.api_key = SecretStr(open(constants.HF_TOKEN_PATH).read().strip())

if self.model_id is not None:
client = InferenceClient()
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/llms/huggingface/test_inference_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import random
from unittest import mock
from unittest.mock import AsyncMock, MagicMock, Mock, patch

import nest_asyncio
Expand All @@ -23,6 +24,36 @@
@patch("huggingface_hub.AsyncInferenceClient")
@patch("openai.AsyncOpenAI")
class TestInferenceEndpointsLLM:
def test_load_no_api_key(
self, mock_inference_client: MagicMock, mock_openai_client: MagicMock
) -> None:
llm = InferenceEndpointsLLM(
model_id="distilabel-internal-testing/tiny-random-mistral"
)

# Mock `huggingface_hub.constants.HF_TOKEN_PATH` to not exist
with mock.patch("pathlib.Path.exists") as mock_exists:
mock_exists.return_value = False
with pytest.raises(
ValueError,
match="To use `InferenceEndpointsLLM` an API key must be provided",
):
llm.load()

def test_load_with_cached_token(
self, mock_inference_client: MagicMock, mock_openai_client: MagicMock
) -> None:
llm = InferenceEndpointsLLM(
model_id="distilabel-internal-testing/tiny-random-mistral"
)

# Mock `huggingface_hub.constants.HF_TOKEN_PATH` to exist
with mock.patch("pathlib.Path.exists", return_value=True), mock.patch(
"builtins.open", new_callable=mock.mock_open, read_data="hf_token"
):
# Should not raise any errors
llm.load()

def test_serverless_inference_endpoints_llm(
self, mock_inference_client: MagicMock, mock_openai_client: MagicMock
) -> None:
Expand Down
Loading