From 02592963376ed37fc93a6623cace57a8de6bfb83 Mon Sep 17 00:00:00 2001 From: Jonas Lammler Date: Mon, 2 Nov 2026 17:48:58 +0100 Subject: [PATCH] feat: add experimental support for Inference API Co-authored-by: Winni Neessen --- docs/api.clients.inference_models.rst | 20 + docs/api.clients.inference_token_types.rst | 17 + docs/api.clients.inference_tokens.rst | 35 ++ docs/api.prices.rst | 5 + docs/api.rst | 1 + hcloud/_client.py | 21 + hcloud/inference_models/__init__.py | 21 + hcloud/inference_models/client.py | 137 ++++++ hcloud/inference_models/domain.py | 117 +++++ hcloud/inference_token_types/__init__.py | 21 + hcloud/inference_token_types/client.py | 138 ++++++ hcloud/inference_token_types/domain.py | 86 ++++ hcloud/inference_tokens/__init__.py | 31 ++ hcloud/inference_tokens/client.py | 434 +++++++++++++++++ hcloud/inference_tokens/domain.py | 247 ++++++++++ hcloud/prices/__init__.py | 7 + hcloud/prices/domain.py | 27 ++ tests/unit/inference_models/__init__.py | 0 tests/unit/inference_models/conftest.py | 66 +++ tests/unit/inference_models/test_client.py | 196 ++++++++ tests/unit/inference_models/test_domain.py | 26 ++ tests/unit/inference_token_types/__init__.py | 0 tests/unit/inference_token_types/conftest.py | 38 ++ .../unit/inference_token_types/test_client.py | 185 ++++++++ .../unit/inference_token_types/test_domain.py | 15 + tests/unit/inference_tokens/__init__.py | 0 tests/unit/inference_tokens/conftest.py | 130 ++++++ tests/unit/inference_tokens/test_client.py | 435 ++++++++++++++++++ tests/unit/inference_tokens/test_domain.py | 19 + tests/unit/prices/__init__.py | 0 tests/unit/prices/test_domain.py | 22 + 31 files changed, 2497 insertions(+) create mode 100644 docs/api.clients.inference_models.rst create mode 100644 docs/api.clients.inference_token_types.rst create mode 100644 docs/api.clients.inference_tokens.rst create mode 100644 docs/api.prices.rst create mode 100644 hcloud/inference_models/__init__.py create mode 100644 hcloud/inference_models/client.py create mode 100644 hcloud/inference_models/domain.py create mode 100644 hcloud/inference_token_types/__init__.py create mode 100644 hcloud/inference_token_types/client.py create mode 100644 hcloud/inference_token_types/domain.py create mode 100644 hcloud/inference_tokens/__init__.py create mode 100644 hcloud/inference_tokens/client.py create mode 100644 hcloud/inference_tokens/domain.py create mode 100644 hcloud/prices/__init__.py create mode 100644 hcloud/prices/domain.py create mode 100644 tests/unit/inference_models/__init__.py create mode 100644 tests/unit/inference_models/conftest.py create mode 100644 tests/unit/inference_models/test_client.py create mode 100644 tests/unit/inference_models/test_domain.py create mode 100644 tests/unit/inference_token_types/__init__.py create mode 100644 tests/unit/inference_token_types/conftest.py create mode 100644 tests/unit/inference_token_types/test_client.py create mode 100644 tests/unit/inference_token_types/test_domain.py create mode 100644 tests/unit/inference_tokens/__init__.py create mode 100644 tests/unit/inference_tokens/conftest.py create mode 100644 tests/unit/inference_tokens/test_client.py create mode 100644 tests/unit/inference_tokens/test_domain.py create mode 100644 tests/unit/prices/__init__.py create mode 100644 tests/unit/prices/test_domain.py diff --git a/docs/api.clients.inference_models.rst b/docs/api.clients.inference_models.rst new file mode 100644 index 00000000..115e643d --- /dev/null +++ b/docs/api.clients.inference_models.rst @@ -0,0 +1,20 @@ +InferenceModelsClient +===================== + +.. autoclass:: hcloud.inference_models.client.InferenceModelsClient + :members: + +.. autoclass:: hcloud.inference_models.client.InferenceModelsPageResult + :members: + +.. autoclass:: hcloud.inference_models.client.BoundInferenceModel + :members: + +.. autoclass:: hcloud.inference_models.domain.InferenceModel + :members: + +.. autoclass:: hcloud.inference_models.domain.InferenceModelPrices + :members: + +.. autoclass:: hcloud.inference_models.domain.InferenceModelPricesUnits + :members: diff --git a/docs/api.clients.inference_token_types.rst b/docs/api.clients.inference_token_types.rst new file mode 100644 index 00000000..e4eb292a --- /dev/null +++ b/docs/api.clients.inference_token_types.rst @@ -0,0 +1,17 @@ +InferenceTokenTypesClient +========================= + +.. autoclass:: hcloud.inference_token_types.client.InferenceTokenTypesClient + :members: + +.. autoclass:: hcloud.inference_token_types.client.InferenceTokenTypesPageResult + :members: + +.. autoclass:: hcloud.inference_token_types.client.BoundInferenceTokenType + :members: + +.. autoclass:: hcloud.inference_token_types.domain.InferenceTokenType + :members: + +.. autoclass:: hcloud.inference_token_types.domain.InferenceTokenTypePrices + :members: diff --git a/docs/api.clients.inference_tokens.rst b/docs/api.clients.inference_tokens.rst new file mode 100644 index 00000000..5b81fbdd --- /dev/null +++ b/docs/api.clients.inference_tokens.rst @@ -0,0 +1,35 @@ +InferenceTokensClient +===================== + +.. autoclass:: hcloud.inference_tokens.client.InferenceTokensClient + :members: + +.. autoclass:: hcloud.inference_tokens.client.InferenceTokensPageResult + :members: + +.. autoclass:: hcloud.inference_tokens.client.InferenceTokenModelsPageResult + :members: + +.. autoclass:: hcloud.inference_tokens.client.BoundInferenceToken + :members: + +.. autoclass:: hcloud.inference_tokens.domain.InferenceToken + :members: + +.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenCostCap + :members: + +.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenModel + :members: + +.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenModelUsages + :members: + +.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenModelUsageUnits + :members: + +.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenModelUsage + :members: + +.. autoclass:: hcloud.inference_tokens.domain.CreateInferenceTokenResponse + :members: diff --git a/docs/api.prices.rst b/docs/api.prices.rst new file mode 100644 index 00000000..8b4db59a --- /dev/null +++ b/docs/api.prices.rst @@ -0,0 +1,5 @@ +Prices +====== + +.. autoclass:: hcloud.prices.domain.Price + :members: diff --git a/docs/api.rst b/docs/api.rst index 1bb9b638..120f0946 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -42,3 +42,4 @@ Other api.helpers api.deprecation + api.prices diff --git a/hcloud/_client.py b/hcloud/_client.py index 4a71bb0b..17476da2 100644 --- a/hcloud/_client.py +++ b/hcloud/_client.py @@ -16,6 +16,9 @@ from .firewalls import FirewallsClient from .floating_ips import FloatingIPsClient from .images import ImagesClient +from .inference_models import InferenceModelsClient +from .inference_token_types import InferenceTokenTypesClient +from .inference_tokens import InferenceTokensClient from .isos import IsosClient from .load_balancer_types import LoadBalancerTypesClient from .load_balancers import LoadBalancersClient @@ -289,6 +292,24 @@ def __init__( :type: :class:`StorageBoxesClient ` """ + self.inference_models = InferenceModelsClient(self) + """InferenceModelsClient Instance + + :type: :class:`InferenceModelsClient ` + """ + + self.inference_token_types = InferenceTokenTypesClient(self) + """InferenceTokenTypesClient Instance + + :type: :class:`InferenceTokenTypesClient ` + """ + + self.inference_tokens = InferenceTokensClient(self) + """InferenceTokensClient Instance + + :type: :class:`InferenceTokensClient ` + """ + def request( # type: ignore[no-untyped-def] self, method: str, diff --git a/hcloud/inference_models/__init__.py b/hcloud/inference_models/__init__.py new file mode 100644 index 00000000..f3c644a7 --- /dev/null +++ b/hcloud/inference_models/__init__.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +from .client import ( + BoundInferenceModel, + InferenceModelsClient, + InferenceModelsPageResult, +) +from .domain import ( + InferenceModel, + InferenceModelPrices, + InferenceModelPricesUnits, +) + +__all__ = [ + "BoundInferenceModel", + "InferenceModel", + "InferenceModelPrices", + "InferenceModelPricesUnits", + "InferenceModelsClient", + "InferenceModelsPageResult", +] diff --git a/hcloud/inference_models/client.py b/hcloud/inference_models/client.py new file mode 100644 index 00000000..4ba497bd --- /dev/null +++ b/hcloud/inference_models/client.py @@ -0,0 +1,137 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, NamedTuple + +from ..core import BoundModelBase, Meta, ResourceClientBase +from .domain import InferenceModel + +if TYPE_CHECKING: + from .._client import Client + +__all__ = [ + "BoundInferenceModel", + "InferenceModelsPageResult", + "InferenceModelsClient", +] + + +class BoundInferenceModel(BoundModelBase[InferenceModel], InferenceModel): + _client: InferenceModelsClient + + model = InferenceModel + + +class InferenceModelsPageResult(NamedTuple): + inference_models: list[BoundInferenceModel] + meta: Meta + + +class InferenceModelsClient(ResourceClientBase): + """ + A client for the Inference Models API. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + + _base_url = "/inference_models" + + def __init__(self, client: Client): + super().__init__(client) + self._client = client._client_hetzner + + def get_by_id(self, id: int) -> BoundInferenceModel: + """ + Returns a specific Inference Model. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models/get_inference_model + + :param id: ID of the Inference Model. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + response = self._client.request( + method="GET", + url=f"{self._base_url}/{id}", + ) + return BoundInferenceModel(self, response["inference_model"]) + + def get_by_name(self, name: str) -> BoundInferenceModel | None: + """ + Returns a specific Inference Model. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models/list_inference_models + + :param name: Name of the Inference Model. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._get_first_by(self.get_list, name=name) + + def get_list( + self, + *, + name: str | None = None, + page: int | None = None, + per_page: int | None = None, + ) -> InferenceModelsPageResult: + """ + Returns a paginated list of Inference Models for a specific page. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models/list_inference_models + + :param name: Name of the Inference Model. + :param page: Page number to return. + :param per_page: Maximum number of entries returned per page. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + params: dict[str, Any] = {} + if name is not None: + params["name"] = name + if page is not None: + params["page"] = page + if per_page is not None: + params["per_page"] = per_page + + response = self._client.request( + method="GET", + url=f"{self._base_url}", + params=params, + ) + return InferenceModelsPageResult( + inference_models=[ + BoundInferenceModel(self, o) for o in response["inference_models"] + ], + meta=Meta.parse_meta(response), + ) + + def get_all( + self, + *, + name: str | None = None, + ) -> list[BoundInferenceModel]: + """ + Returns all Inference Models. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models/list_inference_models + + :param name: Name of the Inference Model. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._iter_pages( + self.get_list, + name=name, + ) diff --git a/hcloud/inference_models/domain.py b/hcloud/inference_models/domain.py new file mode 100644 index 00000000..4b6ea684 --- /dev/null +++ b/hcloud/inference_models/domain.py @@ -0,0 +1,117 @@ +from __future__ import annotations + +from typing import Any + +from ..core import BaseDomain, DomainIdentityMixin +from ..deprecation import DeprecationInfo +from ..prices import Price + +__all__ = [ + "InferenceModel", + "InferenceModelPrices", + "InferenceModelPricesUnits", +] + + +class InferenceModel(BaseDomain, DomainIdentityMixin): + """ + Inference Model Domain. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + + __api_properties__ = ( + "id", + "name", + "description", + "category", + "tasks", + "quantization", + "parameter_size", + "context_size", + "deprecation", + "prices", + ) + __slots__ = __api_properties__ + + def __init__( + self, + id: int | None = None, + name: str | None = None, + description: str | None = None, + category: str | None = None, + tasks: list[str] | None = None, + quantization: str | None = None, + parameter_size: str | None = None, + context_size: int | None = None, + deprecation: dict[str, Any] | None = None, + prices: dict[str, Any] | None = None, + ): + self.id = id + self.name = name + self.description = description + self.category = category + self.quantization = quantization + self.parameter_size = parameter_size + self.context_size = context_size + self.tasks = tasks + self.deprecation = ( + DeprecationInfo.from_dict(deprecation) if deprecation is not None else None + ) + self.prices = ( + InferenceModelPrices.from_dict(prices) if prices is not None else None + ) + + +class InferenceModelPrices(BaseDomain): + """ + Inference Model Prices Domain. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models + """ + + __api_properties__ = ( + "unbatched", + "batched", + ) + __slots__ = __api_properties__ + + def __init__( + self, + unbatched: dict[str, Any], + batched: dict[str, Any], + ): + self.unbatched = InferenceModelPricesUnits.from_dict(unbatched) + self.batched = InferenceModelPricesUnits.from_dict(batched) + + +class InferenceModelPricesUnits(BaseDomain): + """ + Inference Model Prices Units Domain. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models + """ + + __api_properties__ = ( + "input", + "cached", + "output", + "hours", + ) + __slots__ = __api_properties__ + + def __init__( + self, + input: dict[str, Any] | None = None, + cached: dict[str, Any] | None = None, + output: dict[str, Any] | None = None, + hours: dict[str, Any] | None = None, + ): + self.input = None if input is None else Price.from_dict(input) + self.cached = None if cached is None else Price.from_dict(cached) + self.output = None if output is None else Price.from_dict(output) + self.hours = None if hours is None else Price.from_dict(hours) diff --git a/hcloud/inference_token_types/__init__.py b/hcloud/inference_token_types/__init__.py new file mode 100644 index 00000000..dd9cf7b3 --- /dev/null +++ b/hcloud/inference_token_types/__init__.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +from .client import ( + BoundInferenceTokenType, + InferenceTokenTypesClient, + InferenceTokenTypesPageResult, +) +from .domain import ( + InferenceTokenType, + InferenceTokenTypeKind, + InferenceTokenTypePrices, +) + +__all__ = [ + "BoundInferenceTokenType", + "InferenceTokenType", + "InferenceTokenTypePrices", + "InferenceTokenTypeKind", + "InferenceTokenTypesClient", + "InferenceTokenTypesPageResult", +] diff --git a/hcloud/inference_token_types/client.py b/hcloud/inference_token_types/client.py new file mode 100644 index 00000000..b6dd4f2b --- /dev/null +++ b/hcloud/inference_token_types/client.py @@ -0,0 +1,138 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, NamedTuple + +from ..core import BoundModelBase, Meta, ResourceClientBase +from .domain import InferenceTokenType + +if TYPE_CHECKING: + from .._client import Client + +__all__ = [ + "BoundInferenceTokenType", + "InferenceTokenTypesPageResult", + "InferenceTokenTypesClient", +] + + +class BoundInferenceTokenType(BoundModelBase[InferenceTokenType], InferenceTokenType): + _client: InferenceTokenTypesClient + + model = InferenceTokenType + + +class InferenceTokenTypesPageResult(NamedTuple): + inference_token_types: list[BoundInferenceTokenType] + meta: Meta + + +class InferenceTokenTypesClient(ResourceClientBase): + """ + A client for the Inference Token Types API. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-token-types + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + + _base_url = "/inference_token_types" + + def __init__(self, client: Client): + super().__init__(client) + self._client = client._client_hetzner + + def get_by_id(self, id: int) -> BoundInferenceTokenType: + """ + Returns a specific Inference Token Type. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-token-types/get_inference_token_type + + :param id: ID of the Inference Token Type. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + response = self._client.request( + method="GET", + url=f"{self._base_url}/{id}", + ) + return BoundInferenceTokenType(self, response["inference_token_type"]) + + def get_by_name(self, name: str) -> BoundInferenceTokenType | None: + """ + Returns a specific Inference Token Type. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-token-types/list_inference_token_types + + :param name: Name of the Inference Token Type. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._get_first_by(self.get_list, name=name) + + def get_list( + self, + *, + name: str | None = None, + page: int | None = None, + per_page: int | None = None, + ) -> InferenceTokenTypesPageResult: + """ + Returns a paginated list of Inference Token Types for a specific page. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-token-types/list_inference_token_types + + :param name: Name of the Inference Token Type. + :param page: Page number to return. + :param per_page: Maximum number of entries returned per page. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + params: dict[str, Any] = {} + if name is not None: + params["name"] = name + if page is not None: + params["page"] = page + if per_page is not None: + params["per_page"] = per_page + + response = self._client.request( + method="GET", + url=f"{self._base_url}", + params=params, + ) + return InferenceTokenTypesPageResult( + inference_token_types=[ + BoundInferenceTokenType(self, o) + for o in response["inference_token_types"] + ], + meta=Meta.parse_meta(response), + ) + + def get_all( + self, + *, + name: str | None = None, + ) -> list[BoundInferenceTokenType]: + """ + Returns all Inference Token Types. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-token-types/list_inference_token_types + + :param name: Name of the Inference Token Type. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._iter_pages( + self.get_list, + name=name, + ) diff --git a/hcloud/inference_token_types/domain.py b/hcloud/inference_token_types/domain.py new file mode 100644 index 00000000..13e2884a --- /dev/null +++ b/hcloud/inference_token_types/domain.py @@ -0,0 +1,86 @@ +from __future__ import annotations + +from typing import Any, Literal + +from ..core import BaseDomain, DomainIdentityMixin +from ..deprecation import DeprecationInfo +from ..prices import Price + +__all__ = [ + "InferenceTokenType", + "InferenceTokenTypeKind", + "InferenceTokenTypePrices", +] + + +InferenceTokenTypeKind = Literal[ + "usage_based", + "subscription", +] + + +class InferenceTokenType(BaseDomain, DomainIdentityMixin): + """ + Inference Token Type Domain. + + See https://docs.hetzner.cloud/reference/hetzner#inference-token-types. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + + __api_properties__ = ( + "id", + "name", + "kind", + "description", + "available", + "deprecation", + "prices", + ) + __slots__ = __api_properties__ + + def __init__( + self, + id: int | None = None, + name: str | None = None, + description: str | None = None, + kind: InferenceTokenTypeKind | None = None, + available: bool | None = None, + deprecation: dict[str, Any] | None = None, + prices: dict[str, Any] | None = None, + ): + self.id = id + self.name = name + self.kind = kind + self.description = description + self.available = available + self.deprecation = ( + DeprecationInfo.from_dict(deprecation) if deprecation is not None else None + ) + self.prices = ( + InferenceTokenTypePrices.from_dict(prices) if prices is not None else None + ) + + +class InferenceTokenTypePrices(BaseDomain): + """ + Inference Token Type Prices Domain. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-token-types + """ + + __api_properties__ = ( + "hourly", + "monthly", + ) + __slots__ = __api_properties__ + + def __init__( + self, + hourly: dict[str, Any], + monthly: dict[str, Any], + ): + self.hourly = Price.from_dict(hourly) + self.monthly = Price.from_dict(monthly) diff --git a/hcloud/inference_tokens/__init__.py b/hcloud/inference_tokens/__init__.py new file mode 100644 index 00000000..38f5cda4 --- /dev/null +++ b/hcloud/inference_tokens/__init__.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +from .client import ( + BoundInferenceToken, + InferenceTokenModelsPageResult, + InferenceTokensClient, + InferenceTokensPageResult, +) +from .domain import ( + CreateInferenceTokenResponse, + InferenceToken, + InferenceTokenCostCap, + InferenceTokenModel, + InferenceTokenModelUsage, + InferenceTokenModelUsages, + InferenceTokenModelUsageUnits, +) + +__all__ = [ + "BoundInferenceToken", + "CreateInferenceTokenResponse", + "InferenceToken", + "InferenceTokenCostCap", + "InferenceTokenModel", + "InferenceTokenModelUsages", + "InferenceTokenModelUsageUnits", + "InferenceTokenModelUsage", + "InferenceTokenModelsPageResult", + "InferenceTokensClient", + "InferenceTokensPageResult", +] diff --git a/hcloud/inference_tokens/client.py b/hcloud/inference_tokens/client.py new file mode 100644 index 00000000..c8b274ef --- /dev/null +++ b/hcloud/inference_tokens/client.py @@ -0,0 +1,434 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, NamedTuple + +from ..core import BoundModelBase, Meta, ResourceClientBase +from ..inference_token_types import BoundInferenceTokenType, InferenceTokenType +from .domain import ( + CreateInferenceTokenResponse, + InferenceToken, + InferenceTokenCostCap, + InferenceTokenModel, +) + +if TYPE_CHECKING: + from .._client import Client + +__all__ = [ + "BoundInferenceToken", + "InferenceTokensPageResult", + "InferenceTokenModelsPageResult", + "InferenceTokensClient", +] + + +class BoundInferenceToken(BoundModelBase[InferenceToken], InferenceToken): + _client: InferenceTokensClient + + model = InferenceToken + + def __init__( + self, + client: InferenceTokensClient, + data: dict[str, Any], + complete: bool = True, + ): + raw = data.get("inference_token_type") + if raw is not None: + data["inference_token_type"] = BoundInferenceTokenType( + client._parent.inference_token_types, raw + ) + + raw = data.get("cost_cap") + if raw is not None: + data["cost_cap"] = InferenceTokenCostCap.from_dict(raw) + + super().__init__(client, data, complete) + + def update( + self, + *, + name: str | None = None, + cost_cap: InferenceTokenCostCap | None = None, + labels: dict[str, str] | None = None, + ) -> BoundInferenceToken: + """ + Updates the Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/update_inference_token + + :param name: Name of the Inference Token. + :param labels: User-defined labels (key/value pairs) for the Inference Token. + :param cost_cap: Monthly cost cap of the Inference Token without VAT, as a decimal string (e.g. ``"10.00"``). + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._client.update( + self, + name=name, + cost_cap=cost_cap, + labels=labels, + ) + + def delete(self) -> None: + """ + Deletes the Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/delete_inference_token + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._client.delete(self) + + def get_model_list( + self, + *, + page: int | None = None, + per_page: int | None = None, + ) -> InferenceTokenModelsPageResult: + """ + Returns a paginated list of Inference Models for the Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/list_inference_token_models + + :param page: Page number to return. + :param per_page: Maximum number of entries returned per page. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._client.get_model_list( + self, + page=page, + per_page=per_page, + ) + + def get_model_all(self) -> list[InferenceTokenModel]: + """ + Returns all Inference Models for the Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/list_inference_token_models + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._client.get_model_all(self) + + +class InferenceTokensPageResult(NamedTuple): + inference_tokens: list[BoundInferenceToken] + meta: Meta + + +class InferenceTokenModelsPageResult(NamedTuple): + models: list[InferenceTokenModel] + meta: Meta + + +class InferenceTokensClient(ResourceClientBase): + """ + A client for the Inference Tokens API. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + + _base_url = "/inference_tokens" + + def __init__(self, client: Client): + super().__init__(client) + self._client = client._client_hetzner + + def get_by_id(self, id: int) -> BoundInferenceToken: + """ + Returns a specific Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/get_inference_token + + :param id: ID of the Inference Token. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + response = self._client.request( + method="GET", + url=f"{self._base_url}/{id}", + ) + return BoundInferenceToken(self, response["inference_token"]) + + def get_by_name(self, name: str) -> BoundInferenceToken | None: + """ + Returns a specific Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/list_inference_tokens + + :param name: Name of the Inference Token. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._get_first_by(self.get_list, name=name) + + def get_list( + self, + *, + name: str | None = None, + label_selector: str | None = None, + inference_token_type: ( + BoundInferenceTokenType | InferenceTokenType | None + ) = None, + sort: list[str] | None = None, + page: int | None = None, + per_page: int | None = None, + ) -> InferenceTokensPageResult: + """ + Returns a paginated list of Inference Tokens for a specific page. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/list_inference_tokens + + :param name: Name of the Inference Token. + :param label_selector: Filter resources by labels. The response will only contain resources matching the label selector. + :param inference_token_type: Filter resources by their Inference Token Type (ID or name). + :param sort: Sort resources by field and direction. + :param page: Page number to return. + :param per_page: Maximum number of entries returned per page. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + params: dict[str, Any] = {} + if name is not None: + params["name"] = name + if label_selector is not None: + params["label_selector"] = label_selector + if inference_token_type is not None: + params["inference_token_type"] = inference_token_type.id_or_name + if sort is not None: + params["sort"] = sort + if page is not None: + params["page"] = page + if per_page is not None: + params["per_page"] = per_page + + response = self._client.request( + method="GET", + url=f"{self._base_url}", + params=params, + ) + return InferenceTokensPageResult( + inference_tokens=[ + BoundInferenceToken(self, o) for o in response["inference_tokens"] + ], + meta=Meta.parse_meta(response), + ) + + def get_all( + self, + *, + name: str | None = None, + label_selector: str | None = None, + inference_token_type: ( + BoundInferenceTokenType | InferenceTokenType | None + ) = None, + sort: list[str] | None = None, + ) -> list[BoundInferenceToken]: + """ + Returns all Inference Tokens. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/list_inference_tokens + + :param name: Name of the Inference Token. + :param label_selector: Filter resources by labels. The response will only contain resources matching the label selector. + :param inference_token_type: Filter resources by their Inference Token Type (ID or name). + :param sort: Sort resources by field and direction. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._iter_pages( + self.get_list, + name=name, + label_selector=label_selector, + inference_token_type=inference_token_type, + sort=sort, + ) + + def create( + self, + *, + name: str, + inference_token_type: BoundInferenceTokenType | InferenceTokenType, + cost_cap: InferenceTokenCostCap | None = None, + labels: dict[str, str] | None = None, + ) -> CreateInferenceTokenResponse: + """ + Creates an Inference Token. + + The response contains the key of the Inference Token. It is only returned once + and cannot be retrieved again. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/create_inference_token + + :param name: Name of the Inference Token. + :param inference_token_type: Type of the Inference Token. + :param cost_cap: Monthly cost cap of the Inference Token without VAT, as a decimal string (e.g. ``"10.00"``). + :param labels: User-defined labels (key/value pairs) for the Inference Token. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + data: dict[str, Any] = { + "name": name, + "inference_token_type": inference_token_type.id_or_name, + } + if cost_cap is not None: + data["cost_cap"] = {"net": cost_cap.net} + if labels is not None: + data["labels"] = labels + + response = self._client.request( + method="POST", + url=f"{self._base_url}", + json=data, + ) + + return CreateInferenceTokenResponse( + inference_token=BoundInferenceToken(self, response["inference_token"]), + key=response["key"], + ) + + def update( + self, + inference_token: BoundInferenceToken | InferenceToken, + *, + name: str | None = None, + cost_cap: InferenceTokenCostCap | None = None, + labels: dict[str, str] | None = None, + ) -> BoundInferenceToken: + """ + Updates an Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/update_inference_token + + :param inference_token: Inference Token to update. + :param name: Name of the Inference Token. + :param labels: User-defined labels (key/value pairs) for the Inference Token. + :param cost_cap: Monthly cost cap of the Inference Token without VAT, as a decimal string (e.g. ``"10.00"``). + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + data: dict[str, Any] = {} + if name is not None: + data["name"] = name + if labels is not None: + data["labels"] = labels + if cost_cap is not None: + data["cost_cap"] = {"net": cost_cap.net} + + response = self._client.request( + method="PUT", + url=f"{self._base_url}/{inference_token.id}", + json=data, + ) + + return BoundInferenceToken(self, response["inference_token"]) + + def delete( + self, + inference_token: BoundInferenceToken | InferenceToken, + ) -> None: + """ + Deletes an Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/delete_inference_token + + :param inference_token: Inference Token to delete. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + self._client.request( + method="DELETE", + url=f"{self._base_url}/{inference_token.id}", + ) + + def get_model_list( + self, + inference_token: BoundInferenceToken | InferenceToken, + *, + page: int | None = None, + per_page: int | None = None, + ) -> InferenceTokenModelsPageResult: + """ + Returns a paginated list of Inference Models for an Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/list_inference_token_models + + :param inference_token: Inference Token to get the Inference Models from. + :param page: Page number to return. + :param per_page: Maximum number of entries returned per page. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + params: dict[str, Any] = {} + if page is not None: + params["page"] = page + if per_page is not None: + params["per_page"] = per_page + + response = self._client.request( + method="GET", + url=f"{self._base_url}/{inference_token.id}/models", + params=params, + ) + + models = [] + for o in response["models"]: + o["inference_token"] = BoundInferenceToken( + self, data={"id": o["inference_token"]}, complete=False + ) + models.append(InferenceTokenModel.from_dict(o)) + + return InferenceTokenModelsPageResult( + models=models, + meta=Meta.parse_meta(response), + ) + + def get_model_all( + self, + inference_token: BoundInferenceToken | InferenceToken, + ) -> list[InferenceTokenModel]: + """ + Returns all Inference Models for an Inference Token. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens/list_inference_token_models + + :param inference_token: Inference Token to get the Inference Models from. + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + return self._iter_pages( + self.get_model_list, + inference_token, + ) diff --git a/hcloud/inference_tokens/domain.py b/hcloud/inference_tokens/domain.py new file mode 100644 index 00000000..84eaa631 --- /dev/null +++ b/hcloud/inference_tokens/domain.py @@ -0,0 +1,247 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + +from ..core import BaseDomain, DomainIdentityMixin +from ..deprecation import DeprecationInfo +from ..inference_token_types import BoundInferenceTokenType, InferenceTokenType +from ..prices import Price + +if TYPE_CHECKING: + from .client import BoundInferenceToken + +__all__ = [ + "InferenceToken", + "InferenceTokenCostCap", + "InferenceTokenModel", + "InferenceTokenModelUsages", + "InferenceTokenModelUsageUnits", + "InferenceTokenModelUsage", + "CreateInferenceTokenResponse", +] + + +class InferenceToken(BaseDomain, DomainIdentityMixin): + """ + Inference Token Domain. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + + __api_properties__ = ( + "id", + "name", + "labels", + "inference_token_type", + "key_prefix", + "cost_cap", + "usage", + "created", + ) + __slots__ = __api_properties__ + + def __init__( + self, + id: int | None = None, + name: str | None = None, + labels: dict[str, str] | None = None, + inference_token_type: ( + BoundInferenceTokenType | InferenceTokenType | None + ) = None, + key_prefix: str | None = None, + cost_cap: InferenceTokenCostCap | None = None, + usage: dict[str, Any] | None = None, + created: str | None = None, + ): + self.id = id + self.name = name + self.labels = labels + self.inference_token_type = inference_token_type + self.key_prefix = key_prefix + self.cost_cap = cost_cap + self.usage = None if usage is None else Price.from_dict(usage) + self.created = self._parse_datetime(created) + + +class InferenceTokenCostCap(BaseDomain): + """ + Inference Token Cost Cap Domain. + """ + + __api_properties__ = ("net",) + __slots__ = __api_properties__ + + def __init__( + self, + net: str, + ): + self.net = net + + +class InferenceTokenModel(BaseDomain, DomainIdentityMixin): + """ + Inference Token Model Domain. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens + + Experimental: + Support for the Inference API is experimental, breaking changes may occur within minor releases. + See https://github.com/hetznercloud/hcloud-python/issues/719 for more details. + """ + + __api_properties__ = ( + "id", + "name", + "description", + "category", + "tasks", + "quantization", + "parameter_size", + "context_size", + "deprecation", + # Inference Token model specific + "inference_token", + "request_limit", + "usages", + ) + __slots__ = __api_properties__ + + def __init__( + self, + id: int | None = None, + name: str | None = None, + description: str | None = None, + category: str | None = None, + tasks: list[str] | None = None, + quantization: str | None = None, + parameter_size: str | None = None, + context_size: int | None = None, + deprecation: dict[str, Any] | None = None, + # Inference Token Model specific + inference_token: BoundInferenceToken | InferenceToken | None = None, + request_limit: int | None = None, + usages: dict[str, Any] | None = None, + ): + self.id = id + self.name = name + self.description = description + self.category = category + self.quantization = quantization + self.parameter_size = parameter_size + self.context_size = context_size + self.tasks = tasks + self.deprecation = ( + DeprecationInfo.from_dict(deprecation) if deprecation is not None else None + ) + # Inference Token Model specific + self.inference_token = inference_token + self.request_limit = request_limit + self.usages = ( + None if usages is None else InferenceTokenModelUsages.from_dict(usages) + ) + + +class InferenceTokenModelUsages(BaseDomain): + """ + Inference Token Model Usages Domain. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens + """ + + __api_properties__ = ( + "unbatched", + "batched", + ) + __slots__ = __api_properties__ + + def __init__( + self, + unbatched: dict[str, Any], + batched: dict[str, Any], + ): + self.unbatched = InferenceTokenModelUsageUnits.from_dict(unbatched) + self.batched = InferenceTokenModelUsageUnits.from_dict(batched) + + +class InferenceTokenModelUsageUnits(BaseDomain): + """ + Inference Token Model Usage Units Domain. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens + """ + + __api_properties__ = ( + "input", + "cached", + "output", + "hours", + ) + __slots__ = __api_properties__ + + def __init__( + self, + input: dict[str, Any] | None = None, + cached: dict[str, Any] | None = None, + output: dict[str, Any] | None = None, + hours: dict[str, Any] | None = None, + ): + self.input = ( + None if input is None else InferenceTokenModelUsage.from_dict(input) + ) + self.cached = ( + None if cached is None else InferenceTokenModelUsage.from_dict(cached) + ) + self.output = ( + None if output is None else InferenceTokenModelUsage.from_dict(output) + ) + self.hours = ( + None if hours is None else InferenceTokenModelUsage.from_dict(hours) + ) + + +class InferenceTokenModelUsage(BaseDomain): + """ + Inference Token Model Usage Domain. + + See https://docs.hetzner.cloud/reference/hetzner#tag/inference-tokens + """ + + __api_properties__ = ( + "amount", + "usage", + ) + __slots__ = __api_properties__ + + def __init__( + self, + amount: str, + usage: dict[str, Any], + ): + self.amount = amount + self.usage = Price.from_dict(usage) + + +class CreateInferenceTokenResponse(BaseDomain): + """ + Create Inference Token Response Domain. + + The key of the Inference Token is only returned once, and cannot be retrieved again. + """ + + __api_properties__ = ( + "inference_token", + "key", + ) + __slots__ = __api_properties__ + + def __init__( + self, + inference_token: BoundInferenceToken, + key: str, + ): + self.inference_token = inference_token + self.key = key diff --git a/hcloud/prices/__init__.py b/hcloud/prices/__init__.py new file mode 100644 index 00000000..192f8f5e --- /dev/null +++ b/hcloud/prices/__init__.py @@ -0,0 +1,7 @@ +from __future__ import annotations + +from .domain import Price + +__all__ = [ + "Price", +] diff --git a/hcloud/prices/domain.py b/hcloud/prices/domain.py new file mode 100644 index 00000000..78001a8d --- /dev/null +++ b/hcloud/prices/domain.py @@ -0,0 +1,27 @@ +from __future__ import annotations + +from ..core import BaseDomain + +__all__ = [ + "Price", +] + + +class Price(BaseDomain): + """ + Price Domain. + """ + + __api_properties__ = ( + "net", + "gross", + ) + __slots__ = __api_properties__ + + def __init__( + self, + net: str, + gross: str, + ): + self.net = net + self.gross = gross diff --git a/tests/unit/inference_models/__init__.py b/tests/unit/inference_models/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/inference_models/conftest.py b/tests/unit/inference_models/conftest.py new file mode 100644 index 00000000..e3ac7673 --- /dev/null +++ b/tests/unit/inference_models/conftest.py @@ -0,0 +1,66 @@ +from __future__ import annotations + +import pytest + + +@pytest.fixture() +def inference_model1(): + return { + "id": 7, + "name": "Qwen/Qwen3.8-27B", + "description": "Qwen3.8 27B", + "category": "public", + "quantization": "BF16", + "parameter_size": "27B", + "context_size": 32768, + "tasks": ["chat", "code", "tool_calling"], + "icon": None, + "deprecation": { + "unavailable_after": "2026-12-01T00:00:00+00:00", + "announced": "2026-09-01T00:00:00+00:00", + }, + "prices": { + "unbatched": { + "input": {"net": "0.2500", "gross": "0.2975"}, + "cached": {"net": "0.0500", "gross": "0.0595"}, + "output": {"net": "0.6000", "gross": "0.7140"}, + "hours": None, + }, + "batched": { + "input": None, + "cached": None, + "output": None, + "hours": None, + }, + }, + } + + +@pytest.fixture() +def inference_model2(): + return { + "id": 8, + "name": "openai/whisper-large-v3", + "description": "whisper large-v3", + "category": "public", + "quantization": None, + "parameter_size": None, + "context_size": None, + "tasks": None, + "icon": None, + "deprecation": None, + "prices": { + "unbatched": { + "input": None, + "cached": None, + "output": None, + "hours": {"net": "0.1000", "gross": "0.1190"}, + }, + "batched": { + "input": None, + "cached": None, + "output": None, + "hours": None, + }, + }, + } diff --git a/tests/unit/inference_models/test_client.py b/tests/unit/inference_models/test_client.py new file mode 100644 index 00000000..511a97ea --- /dev/null +++ b/tests/unit/inference_models/test_client.py @@ -0,0 +1,196 @@ +# pylint: disable=protected-access + +from __future__ import annotations + +from unittest import mock + +import pytest +from dateutil.parser import isoparse + +from hcloud import Client +from hcloud.inference_models import ( + BoundInferenceModel, + InferenceModelPrices, + InferenceModelPricesUnits, + InferenceModelsClient, +) +from hcloud.prices import Price + + +def assert_bound_model1( + o: BoundInferenceModel, + client: InferenceModelsClient, +): + assert isinstance(o, BoundInferenceModel) + assert o._client is client + assert o.id == 7 + assert o.name == "Qwen/Qwen3.8-27B" + + +def assert_bound_model2( + o: BoundInferenceModel, + client: InferenceModelsClient, +): + assert isinstance(o, BoundInferenceModel) + assert o._client is client + assert o.id == 8 + assert o.name == "openai/whisper-large-v3" + + +class TestBoundInferenceModel: + @pytest.fixture() + def resource_client(self, client: Client) -> InferenceModelsClient: + return client.inference_models + + @pytest.fixture() + def bound_model( + self, + resource_client: InferenceModelsClient, + inference_model1, + ) -> BoundInferenceModel: + return BoundInferenceModel(resource_client, data=inference_model1) + + def test_init( + self, + bound_model: BoundInferenceModel, + resource_client: InferenceModelsClient, + ): + o = bound_model + + assert_bound_model1(o, resource_client) + + assert o.description == "Qwen3.8 27B" + assert o.category == "public" + assert o.quantization == "BF16" + assert o.parameter_size == "27B" + assert o.context_size == 32768 + assert o.tasks == ["chat", "code", "tool_calling"] + assert o.deprecation.announced == isoparse("2026-09-01T00:00:00Z") + assert o.deprecation.unavailable_after == isoparse("2026-12-01T00:00:00Z") + assert isinstance(o.prices, InferenceModelPrices) + assert isinstance(o.prices.unbatched, InferenceModelPricesUnits) + assert isinstance(o.prices.unbatched.input, Price) + assert o.prices.unbatched.input.net == "0.2500" + assert o.prices.unbatched.input.gross == "0.2975" + assert isinstance(o.prices.unbatched.cached, Price) + assert o.prices.unbatched.cached.net == "0.0500" + assert o.prices.unbatched.cached.gross == "0.0595" + assert isinstance(o.prices.unbatched.output, Price) + assert o.prices.unbatched.output.net == "0.6000" + assert o.prices.unbatched.output.gross == "0.7140" + assert o.prices.unbatched.hours is None + assert isinstance(o.prices.batched, InferenceModelPricesUnits) + assert o.prices.batched.input is None + assert o.prices.batched.cached is None + assert o.prices.batched.output is None + assert o.prices.batched.hours is None + + def test_init_minimal(self, resource_client: InferenceModelsClient): + o = BoundInferenceModel(resource_client, data={"id": 7}) + assert o.id == 7 + + +class TestClient: + @pytest.fixture() + def resource_client(self, client: Client) -> InferenceModelsClient: + return client.inference_models + + def test_get_by_id( + self, + request_mock: mock.MagicMock, + resource_client: InferenceModelsClient, + inference_model1, + ): + request_mock.return_value = {"inference_model": inference_model1} + + result = resource_client.get_by_id(7) + + request_mock.assert_called_with( + method="GET", + url="/inference_models/7", + ) + + assert_bound_model1(result, resource_client) + + @pytest.mark.parametrize( + "params", + [ + {"name": "Qwen/Qwen3.8-27B", "page": 1, "per_page": 10}, + {}, + ], + ) + def test_get_list( + self, + request_mock: mock.MagicMock, + resource_client: InferenceModelsClient, + inference_model1, + inference_model2, + params, + ): + request_mock.return_value = { + "inference_models": [inference_model1, inference_model2] + } + + result = resource_client.get_list(**params) + + request_mock.assert_called_with( + method="GET", + url="/inference_models", + params=params, + ) + + assert result.meta is not None + assert len(result.inference_models) == 2 + + assert_bound_model1(result.inference_models[0], resource_client) + assert_bound_model2(result.inference_models[1], resource_client) + + @pytest.mark.parametrize( + "params", + [ + {"name": "Qwen/Qwen3.8-27B"}, + {}, + ], + ) + def test_get_all( + self, + request_mock: mock.MagicMock, + resource_client: InferenceModelsClient, + inference_model1, + inference_model2, + params, + ): + request_mock.return_value = { + "inference_models": [inference_model1, inference_model2] + } + + result = resource_client.get_all(**params) + + request_mock.assert_called_with( + url="/inference_models", + method="GET", + params={**params, "page": 1, "per_page": 50}, + ) + + assert len(result) == 2 + + assert_bound_model1(result[0], resource_client) + assert_bound_model2(result[1], resource_client) + + def test_get_by_name( + self, + request_mock: mock.MagicMock, + resource_client: InferenceModelsClient, + inference_model1, + ): + request_mock.return_value = {"inference_models": [inference_model1]} + + result = resource_client.get_by_name("Qwen/Qwen3.8-27B") + + request_mock.assert_called_with( + method="GET", + url="/inference_models", + params={"name": "Qwen/Qwen3.8-27B"}, + ) + + assert_bound_model1(result, resource_client) diff --git a/tests/unit/inference_models/test_domain.py b/tests/unit/inference_models/test_domain.py new file mode 100644 index 00000000..3f708590 --- /dev/null +++ b/tests/unit/inference_models/test_domain.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +import pytest + +from hcloud.inference_models import ( + InferenceModel, + InferenceModelPrices, + InferenceModelPricesUnits, +) + + +@pytest.mark.parametrize( + "value", + [ + (InferenceModel(id=1),), + ( + InferenceModelPrices( + unbatched={"input": {"net": "1.0000", "gross": "1.1900"}}, + batched={}, + ), + ), + (InferenceModelPricesUnits(hours={"net": "1.0000", "gross": "1.1900"}),), + ], +) +def test_eq(value): + assert value.__eq__(value) diff --git a/tests/unit/inference_token_types/__init__.py b/tests/unit/inference_token_types/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/inference_token_types/conftest.py b/tests/unit/inference_token_types/conftest.py new file mode 100644 index 00000000..3a820bf3 --- /dev/null +++ b/tests/unit/inference_token_types/conftest.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +import pytest + + +@pytest.fixture() +def inference_token_type1(): + return { + "id": 1, + "name": "usgb", + "kind": "usage_based", + "description": "Usage-based", + "available": True, + "deprecation": { + "announced": "2026-09-01T00:00:00Z", + "unavailable_after": "2026-12-01T00:00:00Z", + }, + "prices": { + "hourly": {"net": "0.0000", "gross": "0.0000"}, + "monthly": {"net": "0.0000", "gross": "0.0000"}, + }, + } + + +@pytest.fixture() +def inference_token_type2(): + return { + "id": 2, + "name": "subs", + "kind": "subscription", + "description": "Subscription", + "available": False, + "deprecation": None, + "prices": { + "hourly": {"net": "0.0274", "gross": "0.0327"}, + "monthly": {"net": "20.0006", "gross": "23.8007"}, + }, + } diff --git a/tests/unit/inference_token_types/test_client.py b/tests/unit/inference_token_types/test_client.py new file mode 100644 index 00000000..a861e0eb --- /dev/null +++ b/tests/unit/inference_token_types/test_client.py @@ -0,0 +1,185 @@ +# pylint: disable=protected-access + +from __future__ import annotations + +from unittest import mock + +import pytest +from dateutil.parser import isoparse + +from hcloud import Client +from hcloud.inference_token_types import ( + BoundInferenceTokenType, + InferenceTokenTypePrices, + InferenceTokenTypesClient, +) +from hcloud.prices import Price + +from ..conftest import BoundModelTestCase + + +def assert_bound_model1( + o: BoundInferenceTokenType, + client: InferenceTokenTypesClient, +): + assert isinstance(o, BoundInferenceTokenType) + assert o._client is client + assert o.id == 1 + assert o.name == "usgb" + + +def assert_bound_model2( + o: BoundInferenceTokenType, + client: InferenceTokenTypesClient, +): + assert isinstance(o, BoundInferenceTokenType) + assert o._client is client + assert o.id == 2 + assert o.name == "subs" + + +class TestBoundInferenceTokenType(BoundModelTestCase): + methods = [] + + @pytest.fixture() + def resource_client(self, client: Client) -> InferenceTokenTypesClient: + return client.inference_token_types + + @pytest.fixture() + def bound_model( + self, + resource_client: InferenceTokenTypesClient, + inference_token_type1, + ) -> BoundInferenceTokenType: + return BoundInferenceTokenType(resource_client, data=inference_token_type1) + + def test_init( + self, + bound_model: BoundInferenceTokenType, + resource_client: InferenceTokenTypesClient, + ): + o = bound_model + + assert_bound_model1(o, resource_client) + + assert o.kind == "usage_based" + assert o.description == "Usage-based" + assert o.available is True + assert o.deprecation.announced == isoparse("2026-09-01T00:00:00+00:00") + assert o.deprecation.unavailable_after == isoparse("2026-12-01T00:00:00+00:00") + assert isinstance(o.prices, InferenceTokenTypePrices) + assert isinstance(o.prices.hourly, Price) + assert o.prices.hourly.net == "0.0000" + assert o.prices.hourly.gross == "0.0000" + assert isinstance(o.prices.monthly, Price) + assert o.prices.monthly.net == "0.0000" + assert o.prices.monthly.gross == "0.0000" + + def test_init_minimal(self, resource_client: InferenceTokenTypesClient): + o = BoundInferenceTokenType(resource_client, data={"id": 1}) + assert o.id == 1 + + +class TestClient: + @pytest.fixture() + def resource_client(self, client: Client) -> InferenceTokenTypesClient: + return client.inference_token_types + + def test_get_by_id( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokenTypesClient, + inference_token_type1, + ): + request_mock.return_value = {"inference_token_type": inference_token_type1} + + result = resource_client.get_by_id(1) + + request_mock.assert_called_with( + method="GET", + url="/inference_token_types/1", + ) + + assert_bound_model1(result, resource_client) + + @pytest.mark.parametrize( + "params", + [ + {"name": "usgb", "page": 1, "per_page": 10}, + {}, + ], + ) + def test_get_list( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokenTypesClient, + inference_token_type1, + inference_token_type2, + params, + ): + request_mock.return_value = { + "inference_token_types": [inference_token_type1, inference_token_type2] + } + + result = resource_client.get_list(**params) + + request_mock.assert_called_with( + url="/inference_token_types", + method="GET", + params=params, + ) + + assert result.meta is not None + assert len(result.inference_token_types) == 2 + + assert_bound_model1(result.inference_token_types[0], resource_client) + assert_bound_model2(result.inference_token_types[1], resource_client) + + @pytest.mark.parametrize( + "params", + [ + {"name": "usgb"}, + {}, + ], + ) + def test_get_all( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokenTypesClient, + inference_token_type1, + inference_token_type2, + params, + ): + request_mock.return_value = { + "inference_token_types": [inference_token_type1, inference_token_type2] + } + + result = resource_client.get_all(**params) + + request_mock.assert_called_with( + url="/inference_token_types", + method="GET", + params={**params, "page": 1, "per_page": 50}, + ) + + assert len(result) == 2 + assert_bound_model1(result[0], resource_client) + assert_bound_model2(result[1], resource_client) + + def test_get_by_name( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokenTypesClient, + inference_token_type1, + ): + request_mock.return_value = {"inference_token_types": [inference_token_type1]} + + result = resource_client.get_by_name("usgb") + + request_mock.assert_called_with( + method="GET", + url="/inference_token_types", + params={"name": "usgb"}, + ) + + assert_bound_model1(result, resource_client) diff --git a/tests/unit/inference_token_types/test_domain.py b/tests/unit/inference_token_types/test_domain.py new file mode 100644 index 00000000..ce92d529 --- /dev/null +++ b/tests/unit/inference_token_types/test_domain.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +import pytest + +from hcloud.inference_token_types import InferenceTokenType + + +@pytest.mark.parametrize( + "value", + [ + (InferenceTokenType(id=1),), + ], +) +def test_eq(value): + assert value.__eq__(value) diff --git a/tests/unit/inference_tokens/__init__.py b/tests/unit/inference_tokens/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/inference_tokens/conftest.py b/tests/unit/inference_tokens/conftest.py new file mode 100644 index 00000000..0173109a --- /dev/null +++ b/tests/unit/inference_tokens/conftest.py @@ -0,0 +1,130 @@ +from __future__ import annotations + +import pytest + + +@pytest.fixture() +def inference_token1(): + return { + "id": 42, + "name": "inference-token1", + "labels": {"key": "value"}, + "inference_token_type": { + "id": 1, + "name": "usgb", + "kind": "usage_based", + "description": "Usage-based", + "available": True, + "deprecation": None, + "prices": { + "hourly": {"net": "0.0000", "gross": "0.0000"}, + "monthly": {"net": "0.0000", "gross": "0.0000"}, + }, + }, + "key_prefix": "hit_a1b2c3d4", + "cost_cap": {"net": "100.0000", "gross": "119.0000"}, + "usage": {"net": "4.0000", "gross": "4.7600"}, + "created": "2026-09-14T08:12:33Z", + } + + +@pytest.fixture() +def inference_token2(): + return { + "id": 43, + "name": "inference-token2", + "labels": {}, + "inference_token_type": { + "id": 2, + "name": "subs", + "kind": "subscription", + "description": "Subscription", + "available": True, + "deprecation": None, + "prices": { + "hourly": {"net": "0.0274", "gross": "0.0327"}, + "monthly": {"net": "20.0006", "gross": "23.8007"}, + }, + }, + "key_prefix": "hit_e5f6g7h8", + "cost_cap": None, + "usage": {"net": "0.0000", "gross": "0.0000"}, + "created": "2026-09-15T10:00:00Z", + } + + +@pytest.fixture() +def inference_token_model1(): + return { + "id": 7, + "name": "Qwen/Qwen3.8-27B", + "description": "Qwen3.8 27B", + "category": "public", + "quantization": "BF16", + "parameter_size": "27B", + "context_size": 32768, + "tasks": ["chat", "code", "tool_calling"], + "deprecation": None, + "inference_token": 42, + "request_limit": 100, + "usages": { + "unbatched": { + "input": { + "amount": "12400000.000", + "usage": {"net": "3.1000", "gross": "3.6890"}, + }, + "cached": { + "amount": "800000.000", + "usage": {"net": "0.0400", "gross": "0.0476"}, + }, + "output": { + "amount": "3100000.000", + "usage": {"net": "1.8600", "gross": "2.2134"}, + }, + "hours": None, + }, + "batched": { + "input": None, + "cached": None, + "output": None, + "hours": None, + }, + }, + } + + +@pytest.fixture() +def inference_token_model2(): + return { + "id": 8, + "name": "openai/whisper-large-v3", + "description": "whisper large-v3", + "category": "public", + "quantization": None, + "parameter_size": None, + "context_size": None, + "tasks": None, + "deprecation": { + "unavailable_after": "2026-12-01T00:00:00+00:00", + "announced": "2026-09-01T00:00:00+00:00", + }, + "inference_token": 42, + "request_limit": 10, + "usages": { + "unbatched": { + "input": None, + "cached": None, + "output": None, + "hours": { + "amount": "3600.000", + "usage": {"net": "0.1000", "gross": "0.1190"}, + }, + }, + "batched": { + "input": None, + "cached": None, + "output": None, + "hours": None, + }, + }, + } diff --git a/tests/unit/inference_tokens/test_client.py b/tests/unit/inference_tokens/test_client.py new file mode 100644 index 00000000..024e2f0d --- /dev/null +++ b/tests/unit/inference_tokens/test_client.py @@ -0,0 +1,435 @@ +# pylint: disable=protected-access + +from __future__ import annotations + +from unittest import mock + +import pytest +from dateutil.parser import isoparse + +from hcloud import Client +from hcloud.inference_token_types import BoundInferenceTokenType, InferenceTokenType +from hcloud.inference_tokens import ( + BoundInferenceToken, + InferenceToken, + InferenceTokenCostCap, + InferenceTokenModel, + InferenceTokenModelUsage, + InferenceTokenModelUsages, + InferenceTokenModelUsageUnits, + InferenceTokensClient, +) +from hcloud.prices import Price + +from ..conftest import BoundModelTestCase + + +def assert_bound_inference_token1( + o: BoundInferenceToken, + resource_client: InferenceTokensClient, +): + assert isinstance(o, BoundInferenceToken) + assert o._client is resource_client + assert o.id == 42 + assert o.name == "inference-token1" + + +def assert_bound_inference_token2( + o: BoundInferenceToken, + resource_client: InferenceTokensClient, +): + assert isinstance(o, BoundInferenceToken) + assert o._client is resource_client + assert o.id == 43 + assert o.name == "inference-token2" + + +class TestBoundInferenceToken(BoundModelTestCase): + methods = [ + BoundInferenceToken.update, + BoundInferenceToken.delete, + BoundInferenceToken.get_model_list, + BoundInferenceToken.get_model_all, + ] + + @pytest.fixture() + def resource_client(self, client: Client) -> InferenceTokensClient: + return client.inference_tokens + + @pytest.fixture() + def bound_model( + self, + resource_client: InferenceTokensClient, + inference_token1, + ) -> BoundInferenceToken: + return BoundInferenceToken(resource_client, data=inference_token1) + + def test_init(self, bound_model: BoundInferenceToken, resource_client): + o = bound_model + + assert_bound_inference_token1(o, resource_client) + + assert o.labels == {"key": "value"} + assert isinstance(o.inference_token_type, BoundInferenceTokenType) + assert o.inference_token_type.id == 1 + assert o.inference_token_type.name == "usgb" + assert o.inference_token_type.kind == "usage_based" + assert o.key_prefix == "hit_a1b2c3d4" + assert o.cost_cap == InferenceTokenCostCap(net="100.0000") + assert o.usage == Price(net="4.0000", gross="4.7600") + assert o.created == isoparse("2026-09-14T08:12:33Z") + + +class TestClient: + @pytest.fixture() + def resource_client(self, client: Client) -> InferenceTokensClient: + return client.inference_tokens + + def test_get_by_id( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token1, + ): + request_mock.return_value = {"inference_token": inference_token1} + + result = resource_client.get_by_id(42) + + request_mock.assert_called_with( + method="GET", + url="/inference_tokens/42", + ) + + assert_bound_inference_token1(result, resource_client) + + @pytest.mark.parametrize( + "params", + [ + ({"name": "inference-token1"}), + ({"label_selector": "key=value"}), + ({"inference_token_type": InferenceTokenType(name="usgb")}), + ({"inference_token_type": InferenceTokenType(id=1)}), + ({"sort": ["id:asc"]}), + ({"page": 1, "per_page": 10}), + ({}), + ], + ) + def test_get_list( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token1, + inference_token2, + params, + ): + request_mock.return_value = { + "inference_tokens": [inference_token1, inference_token2] + } + + result = resource_client.get_list(**params) + + if "inference_token_type" in params: + params["inference_token_type"] = params["inference_token_type"].id_or_name + + request_mock.assert_called_with( + method="GET", + url="/inference_tokens", + params=params, + ) + + assert result.meta is not None + assert len(result.inference_tokens) == 2 + + assert_bound_inference_token1(result.inference_tokens[0], resource_client) + assert_bound_inference_token2(result.inference_tokens[1], resource_client) + + @pytest.mark.parametrize( + "params", + [ + {"name": "inference-token1"}, + {"label_selector": "key=value"}, + {"inference_token_type": InferenceTokenType(name="usgb")}, + {"sort": ["id:asc"]}, + {}, + ], + ) + def test_get_all( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token1, + inference_token2, + params, + ): + request_mock.return_value = { + "inference_tokens": [inference_token1, inference_token2] + } + + result = resource_client.get_all(**params) + + if "inference_token_type" in params: + params["inference_token_type"] = params["inference_token_type"].id_or_name + + request_mock.assert_called_with( + url="/inference_tokens", + method="GET", + params={**params, "page": 1, "per_page": 50}, + ) + + assert len(result) == 2 + + assert_bound_inference_token1(result[0], resource_client) + assert_bound_inference_token2(result[1], resource_client) + + def test_get_by_name( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token1, + ): + request_mock.return_value = {"inference_tokens": [inference_token1]} + + result = resource_client.get_by_name("inference-token1") + + request_mock.assert_called_with( + method="GET", + url="/inference_tokens", + params={"name": "inference-token1"}, + ) + + assert_bound_inference_token1(result, resource_client) + + def test_create( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token1, + ): + request_mock.return_value = { + "inference_token": inference_token1, + "key": "hit_a1b2c3d4e5f6g7h8i9j0", + } + + result = resource_client.create( + name="inference-token1", + inference_token_type=InferenceTokenType(name="usgb"), + cost_cap=InferenceTokenCostCap(net="100.00"), + labels={"key": "value"}, + ) + + request_mock.assert_called_with( + method="POST", + url="/inference_tokens", + json={ + "name": "inference-token1", + "inference_token_type": "usgb", + "cost_cap": {"net": "100.00"}, + "labels": {"key": "value"}, + }, + ) + + assert_bound_inference_token1(result.inference_token, resource_client) + assert result.key == "hit_a1b2c3d4e5f6g7h8i9j0" + + def test_create_minimal( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token1, + ): + request_mock.return_value = { + "inference_token": inference_token1, + "key": "hit_a1b2c3d4e5f6g7h8i9j0", + } + + result = resource_client.create( + name="inference-token1", + inference_token_type=InferenceTokenType(id=1), + ) + + request_mock.assert_called_with( + method="POST", + url="/inference_tokens", + json={ + "name": "inference-token1", + "inference_token_type": 1, + }, + ) + + assert_bound_inference_token1(result.inference_token, resource_client) + assert result.key == "hit_a1b2c3d4e5f6g7h8i9j0" + + def test_update( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token1, + ): + request_mock.return_value = { + "inference_token": inference_token1, + } + + result = resource_client.update( + InferenceToken(id=42), + name="name", + labels={"key": "value"}, + cost_cap=InferenceTokenCostCap(net="50.00"), + ) + + request_mock.assert_called_with( + method="PUT", + url="/inference_tokens/42", + json={ + "name": "name", + "labels": {"key": "value"}, + "cost_cap": {"net": "50.00"}, + }, + ) + + assert_bound_inference_token1(result, resource_client) + + def test_update_minimal( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token1, + ): + request_mock.return_value = { + "inference_token": inference_token1, + } + + result = resource_client.update(InferenceToken(id=42)) + + request_mock.assert_called_with( + method="PUT", + url="/inference_tokens/42", + json={}, + ) + + assert_bound_inference_token1(result, resource_client) + + def test_delete( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + ): + request_mock.return_value = {} + + result = resource_client.delete(InferenceToken(id=42)) + + request_mock.assert_called_with( + method="DELETE", + url="/inference_tokens/42", + ) + + assert result is None + + @pytest.mark.parametrize( + "params", + [ + {"page": 1, "per_page": 10}, + {}, + ], + ) + def test_get_model_list( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token_model1, + inference_token_model2, + params, + ): + request_mock.return_value = { + "models": [inference_token_model1, inference_token_model2], + } + + result = resource_client.get_model_list(InferenceToken(id=42), **params) + + request_mock.assert_called_with( + method="GET", + url="/inference_tokens/42/models", + params=params, + ) + + assert result.meta is not None + assert len(result.models) == 2 + + result1 = result.models[0] + result2 = result.models[1] + + assert isinstance(result1, InferenceTokenModel) + assert result1.id == 7 + assert result1.name == "Qwen/Qwen3.8-27B" + assert result1.description == "Qwen3.8 27B" + assert result1.category == "public" + assert result1.quantization == "BF16" + assert result1.parameter_size == "27B" + assert result1.context_size == 32768 + assert result1.tasks == ["chat", "code", "tool_calling"] + assert result1.deprecation is None + assert isinstance(result1.inference_token, BoundInferenceToken) + assert result1.inference_token._client is resource_client + assert result1.inference_token.id == 42 + assert result1.request_limit == 100 + assert isinstance(result1.usages, InferenceTokenModelUsages) + assert isinstance(result1.usages.unbatched, InferenceTokenModelUsageUnits) + assert result1.usages.unbatched.input == InferenceTokenModelUsage( + amount="12400000.000", + usage={"net": "3.1000", "gross": "3.6890"}, + ) + assert result1.usages.unbatched.input.usage == Price( + net="3.1000", gross="3.6890" + ) + assert result1.usages.unbatched.cached == InferenceTokenModelUsage( + amount="800000.000", + usage={"net": "0.0400", "gross": "0.0476"}, + ) + assert result1.usages.unbatched.output == InferenceTokenModelUsage( + amount="3100000.000", + usage={"net": "1.8600", "gross": "2.2134"}, + ) + assert result1.usages.unbatched.hours is None + assert isinstance(result1.usages.batched, InferenceTokenModelUsageUnits) + assert result1.usages.batched.input is None + assert result1.usages.batched.cached is None + assert result1.usages.batched.output is None + assert result1.usages.batched.hours is None + + assert result2.id == 8 + assert result2.name == "openai/whisper-large-v3" + assert result2.description == "whisper large-v3" + assert result2.quantization is None + assert result2.tasks is None + assert result2.deprecation.announced == isoparse("2026-09-01T00:00:00+00:00") + assert result2.deprecation.unavailable_after == isoparse( + "2026-12-01T00:00:00+00:00" + ) + assert result2.request_limit == 10 + assert result2.usages.unbatched.input is None + assert result2.usages.unbatched.hours == InferenceTokenModelUsage( + amount="3600.000", + usage={"net": "0.1000", "gross": "0.1190"}, + ) + + def test_get_model_all( + self, + request_mock: mock.MagicMock, + resource_client: InferenceTokensClient, + inference_token_model1, + inference_token_model2, + ): + request_mock.return_value = { + "models": [inference_token_model1, inference_token_model2], + } + + result = resource_client.get_model_all(InferenceToken(id=42)) + + request_mock.assert_called_with( + method="GET", + url="/inference_tokens/42/models", + params={"page": 1, "per_page": 50}, + ) + + assert len(result) == 2 + assert result[0].id == 7 + assert result[1].id == 8 diff --git a/tests/unit/inference_tokens/test_domain.py b/tests/unit/inference_tokens/test_domain.py new file mode 100644 index 00000000..27cef27c --- /dev/null +++ b/tests/unit/inference_tokens/test_domain.py @@ -0,0 +1,19 @@ +from __future__ import annotations + +import pytest + +from hcloud.inference_tokens import ( + InferenceToken, + InferenceTokenModel, +) + + +@pytest.mark.parametrize( + "value", + [ + (InferenceToken(id=1),), + (InferenceTokenModel(id=1),), + ], +) +def test_eq(value): + assert value.__eq__(value) diff --git a/tests/unit/prices/__init__.py b/tests/unit/prices/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/prices/test_domain.py b/tests/unit/prices/test_domain.py new file mode 100644 index 00000000..85da5a4c --- /dev/null +++ b/tests/unit/prices/test_domain.py @@ -0,0 +1,22 @@ +from __future__ import annotations + +import pytest + +from hcloud.prices import Price + + +@pytest.mark.parametrize( + "value", + [ + (Price(**{"net": "20.0006", "gross": "23.8007"}),), + ], +) +def test_eq(value): + assert value.__eq__(value) + + +def test_from_dict(): + o = Price.from_dict({"net": "20.0006", "gross": "23.8007"}) + + assert o.net == "20.0006" + assert o.gross == "23.8007"