Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
70 changes: 35 additions & 35 deletions docs/source/en/guides/inference.md

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/huggingface_hub/inference/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
from huggingface_hub.utils import build_hf_headers, get_session, hf_raise_for_status
from huggingface_hub.utils._auth import get_token


if TYPE_CHECKING:
import numpy as np
from PIL.Image import Image
Expand All @@ -130,7 +129,7 @@ class InferenceClient:
Note: for better compatibility with OpenAI's client, `model` has been aliased as `base_url`. Those 2
arguments are mutually exclusive. If a URL is passed as `model` or `base_url` for chat completion, the `(/v1)/chat/completions` suffix path will be appended to the URL.
provider (`str`, *optional*):
Name of the provider to use for inference. Can be `"black-forest-labs"`, `"cerebras"`, `"cohere"`, `"fal-ai"`, `"featherless-ai"`, `"fireworks-ai"`, `"groq"`, `"hf-inference"`, `"hyperbolic"`, `"nebius"`, `"novita"`, `"nscale"`, `"openai"`, `publicai`, `"replicate"`, `"sambanova"`, `"scaleway"`, `"together"` or `"zai-org"`.
Name of the provider to use for inference. Can be `"black-forest-labs"`, `"cerebras"`, `"clarifai"`, `"cohere"`, `"fal-ai"`, `"featherless-ai"`, `"fireworks-ai"`, `"groq"`, `"hf-inference"`, `"hyperbolic"`, `"nebius"`, `"novita"`, `"nscale"`, `"openai"`, `publicai`, `"replicate"`, `"sambanova"`, `"scaleway"`, `"together"` or `"zai-org"`.
Defaults to "auto" i.e. the first of the providers available for the model, sorted by the user's order in https://hf.co/settings/inference-providers.
If model is a URL or `base_url` is passed, then `provider` is not used.
token (`str`, *optional*):
Expand Down
3 changes: 1 addition & 2 deletions src/huggingface_hub/inference/_generated/_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@

from .._common import _async_yield_from, _import_aiohttp


if TYPE_CHECKING:
import numpy as np
from aiohttp import ClientResponse, ClientSession
Expand All @@ -118,7 +117,7 @@ class AsyncInferenceClient:
Note: for better compatibility with OpenAI's client, `model` has been aliased as `base_url`. Those 2
arguments are mutually exclusive. If a URL is passed as `model` or `base_url` for chat completion, the `(/v1)/chat/completions` suffix path will be appended to the URL.
provider (`str`, *optional*):
Name of the provider to use for inference. Can be `"black-forest-labs"`, `"cerebras"`, `"cohere"`, `"fal-ai"`, `"featherless-ai"`, `"fireworks-ai"`, `"groq"`, `"hf-inference"`, `"hyperbolic"`, `"nebius"`, `"novita"`, `"nscale"`, `"openai"`, `publicai`, `"replicate"`, `"sambanova"`, `"scaleway"`, `"together"` or `"zai-org"`.
Name of the provider to use for inference. Can be `"black-forest-labs"`, `"cerebras"`, `"clarifai"`, `"cohere"`, `"fal-ai"`, `"featherless-ai"`, `"fireworks-ai"`, `"groq"`, `"hf-inference"`, `"hyperbolic"`, `"nebius"`, `"novita"`, `"nscale"`, `"openai"`, `publicai`, `"replicate"`, `"sambanova"`, `"scaleway"`, `"together"` or `"zai-org"`.
Defaults to "auto" i.e. the first of the providers available for the model, sorted by the user's order in https://hf.co/settings/inference-providers.
If model is a URL or `base_url` is passed, then `provider` is not used.
token (`str`, *optional*):
Expand Down
6 changes: 5 additions & 1 deletion src/huggingface_hub/inference/_providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ._common import TaskProviderHelper, _fetch_inference_provider_mapping
from .black_forest_labs import BlackForestLabsTextToImageTask
from .cerebras import CerebrasConversationalTask
from .clarifai import ClarifaiConversationalTask
from .cohere import CohereConversationalTask
from .fal_ai import (
FalAIAutomaticSpeechRecognitionTask,
Expand Down Expand Up @@ -43,13 +44,13 @@
from .together import TogetherConversationalTask, TogetherTextGenerationTask, TogetherTextToImageTask
from .zai_org import ZaiConversationalTask


logger = logging.get_logger(__name__)


PROVIDER_T = Literal[
"black-forest-labs",
"cerebras",
"clarifai",
"cohere",
"fal-ai",
"featherless-ai",
Expand Down Expand Up @@ -78,6 +79,9 @@
"cerebras": {
"conversational": CerebrasConversationalTask(),
},
"clarifai": {
"conversational": ClarifaiConversationalTask(),
},
"cohere": {
"conversational": CohereConversationalTask(),
},
Expand Down
2 changes: 1 addition & 1 deletion src/huggingface_hub/inference/_providers/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from huggingface_hub.inference._generated.types.chat_completion import ChatCompletionInputMessage
from huggingface_hub.utils import build_hf_headers, get_token, logging


logger = logging.get_logger(__name__)


Expand All @@ -24,6 +23,7 @@
# status="live")
"cerebras": {},
"cohere": {},
"clarifai": {},
"fal-ai": {},
"fireworks-ai": {},
"groq": {},
Expand Down
30 changes: 30 additions & 0 deletions src/huggingface_hub/inference/_providers/clarifai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Any, Dict, Optional

from huggingface_hub.hf_api import InferenceProviderMapping

from ._common import BaseConversationalTask

_PROVIDER = "clarifai"
_BASE_URL = "https://api.clarifai.com"

class ClarifaiConversationalTask(BaseConversationalTask):
def __init__(self):
super().__init__(provider=_PROVIDER, base_url=_BASE_URL)

def _prepare_route(self, mapped_model: str, api_key: str) -> str:
return "/v2/ext/openai/v1/chat/completions"

def _prepare_payload_as_dict(
self, inputs: Any, parameters: Dict, provider_mapping_info: InferenceProviderMapping
) -> Optional[Dict]:
payload = super()._prepare_payload_as_dict(inputs, parameters, provider_mapping_info)
response_format = parameters.get("response_format")
if isinstance(response_format, dict) and response_format.get("type") == "json_schema":
json_schema_details = response_format.get("json_schema")
if isinstance(json_schema_details, dict) and "schema" in json_schema_details:
payload["response_format"] = { # type: ignore [index]
"type": "json_object",
"schema": json_schema_details["schema"],
}
return payload

26 changes: 26 additions & 0 deletions tests/test_inference_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
recursive_merge,
)
from huggingface_hub.inference._providers.black_forest_labs import BlackForestLabsTextToImageTask
from huggingface_hub.inference._providers.clarifai import ClarifaiConversationalTask
from huggingface_hub.inference._providers.cohere import CohereConversationalTask
from huggingface_hub.inference._providers.fal_ai import (
_POLLING_INTERVAL,
Expand Down Expand Up @@ -293,6 +294,31 @@ def test_prepare_payload_as_dict(self):
}


class TestClarifaiProvider:
def test_prepare_url(self):
helper = ClarifaiConversationalTask()
assert helper._prepare_url("clarifai_api_key", "username/repo_name") == "https://api.clarifai.com/v2/ext/openai/v1/chat/completions"

def test_prepare_payload_as_dict(self):
helper = ClarifaiConversationalTask()
payload = helper._prepare_payload_as_dict(
[{"role": "user", "content": "Hello!"}],
{},
InferenceProviderMapping(
provider="clarifai",
hf_model_id="meta-llama/llama-3.1-8B-Instruct",
providerId="meta-llama/llama-3.1-8B-Instruct",
task="conversational",
status="live",
),
)

assert payload == {
"messages": [{"role": "user", "content": "Hello!"}],
"model": "meta-llama/llama-3.1-8B-Instruct",
}


class TestFalAIProvider:
def test_prepare_headers_fal_ai_key(self):
"""When using direct call, must use Key authorization."""
Expand Down
Loading