Skip to content

Commit f4aceb6

Browse files
jooolawneessen
andauthored
feat: add experimental support for the Inference API (#720)
Related to #719 Co-authored-by: Winni Neessen <[email protected]>
1 parent 62cbd25 commit f4aceb6

31 files changed

Lines changed: 2497 additions & 0 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
InferenceModelsClient
2+
=====================
3+
4+
.. autoclass:: hcloud.inference_models.client.InferenceModelsClient
5+
:members:
6+
7+
.. autoclass:: hcloud.inference_models.client.InferenceModelsPageResult
8+
:members:
9+
10+
.. autoclass:: hcloud.inference_models.client.BoundInferenceModel
11+
:members:
12+
13+
.. autoclass:: hcloud.inference_models.domain.InferenceModel
14+
:members:
15+
16+
.. autoclass:: hcloud.inference_models.domain.InferenceModelPrices
17+
:members:
18+
19+
.. autoclass:: hcloud.inference_models.domain.InferenceModelPricesUnits
20+
:members:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
InferenceTokenTypesClient
2+
=========================
3+
4+
.. autoclass:: hcloud.inference_token_types.client.InferenceTokenTypesClient
5+
:members:
6+
7+
.. autoclass:: hcloud.inference_token_types.client.InferenceTokenTypesPageResult
8+
:members:
9+
10+
.. autoclass:: hcloud.inference_token_types.client.BoundInferenceTokenType
11+
:members:
12+
13+
.. autoclass:: hcloud.inference_token_types.domain.InferenceTokenType
14+
:members:
15+
16+
.. autoclass:: hcloud.inference_token_types.domain.InferenceTokenTypePrices
17+
:members:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
InferenceTokensClient
2+
=====================
3+
4+
.. autoclass:: hcloud.inference_tokens.client.InferenceTokensClient
5+
:members:
6+
7+
.. autoclass:: hcloud.inference_tokens.client.InferenceTokensPageResult
8+
:members:
9+
10+
.. autoclass:: hcloud.inference_tokens.client.InferenceTokenModelsPageResult
11+
:members:
12+
13+
.. autoclass:: hcloud.inference_tokens.client.BoundInferenceToken
14+
:members:
15+
16+
.. autoclass:: hcloud.inference_tokens.domain.InferenceToken
17+
:members:
18+
19+
.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenCostCap
20+
:members:
21+
22+
.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenModel
23+
:members:
24+
25+
.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenModelUsages
26+
:members:
27+
28+
.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenModelUsageUnits
29+
:members:
30+
31+
.. autoclass:: hcloud.inference_tokens.domain.InferenceTokenModelUsage
32+
:members:
33+
34+
.. autoclass:: hcloud.inference_tokens.domain.CreateInferenceTokenResponse
35+
:members:

‎docs/api.prices.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Prices
2+
======
3+
4+
.. autoclass:: hcloud.prices.domain.Price
5+
:members:

‎docs/api.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ Other
4242

4343
api.helpers
4444
api.deprecation
45+
api.prices

‎hcloud/_client.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from .firewalls import FirewallsClient
1717
from .floating_ips import FloatingIPsClient
1818
from .images import ImagesClient
19+
from .inference_models import InferenceModelsClient
20+
from .inference_token_types import InferenceTokenTypesClient
21+
from .inference_tokens import InferenceTokensClient
1922
from .isos import IsosClient
2023
from .load_balancer_types import LoadBalancerTypesClient
2124
from .load_balancers import LoadBalancersClient
@@ -289,6 +292,24 @@ def __init__(
289292
:type: :class:`StorageBoxesClient <hcloud.storage_boxes.client.StorageBoxesClient>`
290293
"""
291294

295+
self.inference_models = InferenceModelsClient(self)
296+
"""InferenceModelsClient Instance
297+
298+
:type: :class:`InferenceModelsClient <hcloud.inference_models.client.InferenceModelsClient>`
299+
"""
300+
301+
self.inference_token_types = InferenceTokenTypesClient(self)
302+
"""InferenceTokenTypesClient Instance
303+
304+
:type: :class:`InferenceTokenTypesClient <hcloud.inference_token_types.client.InferenceTokenTypesClient>`
305+
"""
306+
307+
self.inference_tokens = InferenceTokensClient(self)
308+
"""InferenceTokensClient Instance
309+
310+
:type: :class:`InferenceTokensClient <hcloud.inference_tokens.client.InferenceTokensClient>`
311+
"""
312+
292313
def request( # type: ignore[no-untyped-def]
293314
self,
294315
method: str,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from __future__ import annotations
2+
3+
from .client import (
4+
BoundInferenceModel,
5+
InferenceModelsClient,
6+
InferenceModelsPageResult,
7+
)
8+
from .domain import (
9+
InferenceModel,
10+
InferenceModelPrices,
11+
InferenceModelPricesUnits,
12+
)
13+
14+
__all__ = [
15+
"BoundInferenceModel",
16+
"InferenceModel",
17+
"InferenceModelPrices",
18+
"InferenceModelPricesUnits",
19+
"InferenceModelsClient",
20+
"InferenceModelsPageResult",
21+
]

‎hcloud/inference_models/client.py‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Any, NamedTuple
4+
5+
from ..core import BoundModelBase, Meta, ResourceClientBase
6+
from .domain import InferenceModel
7+
8+
if TYPE_CHECKING:
9+
from .._client import Client
10+
11+
__all__ = [
12+
"BoundInferenceModel",
13+
"InferenceModelsPageResult",
14+
"InferenceModelsClient",
15+
]
16+
17+
18+
class BoundInferenceModel(BoundModelBase[InferenceModel], InferenceModel):
19+
_client: InferenceModelsClient
20+
21+
model = InferenceModel
22+
23+
24+
class InferenceModelsPageResult(NamedTuple):
25+
inference_models: list[BoundInferenceModel]
26+
meta: Meta
27+
28+
29+
class InferenceModelsClient(ResourceClientBase):
30+
"""
31+
A client for the Inference Models API.
32+
33+
See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models
34+
35+
Experimental:
36+
Support for the Inference API is experimental, breaking changes may occur within minor releases.
37+
See https://github.com/hetznercloud/hcloud-python/issues/719 for more details.
38+
"""
39+
40+
_base_url = "/inference_models"
41+
42+
def __init__(self, client: Client):
43+
super().__init__(client)
44+
self._client = client._client_hetzner
45+
46+
def get_by_id(self, id: int) -> BoundInferenceModel:
47+
"""
48+
Returns a specific Inference Model.
49+
50+
See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models/get_inference_model
51+
52+
:param id: ID of the Inference Model.
53+
54+
Experimental:
55+
Support for the Inference API is experimental, breaking changes may occur within minor releases.
56+
See https://github.com/hetznercloud/hcloud-python/issues/719 for more details.
57+
"""
58+
response = self._client.request(
59+
method="GET",
60+
url=f"{self._base_url}/{id}",
61+
)
62+
return BoundInferenceModel(self, response["inference_model"])
63+
64+
def get_by_name(self, name: str) -> BoundInferenceModel | None:
65+
"""
66+
Returns a specific Inference Model.
67+
68+
See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models/list_inference_models
69+
70+
:param name: Name of the Inference Model.
71+
72+
Experimental:
73+
Support for the Inference API is experimental, breaking changes may occur within minor releases.
74+
See https://github.com/hetznercloud/hcloud-python/issues/719 for more details.
75+
"""
76+
return self._get_first_by(self.get_list, name=name)
77+
78+
def get_list(
79+
self,
80+
*,
81+
name: str | None = None,
82+
page: int | None = None,
83+
per_page: int | None = None,
84+
) -> InferenceModelsPageResult:
85+
"""
86+
Returns a paginated list of Inference Models for a specific page.
87+
88+
See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models/list_inference_models
89+
90+
:param name: Name of the Inference Model.
91+
:param page: Page number to return.
92+
:param per_page: Maximum number of entries returned per page.
93+
94+
Experimental:
95+
Support for the Inference API is experimental, breaking changes may occur within minor releases.
96+
See https://github.com/hetznercloud/hcloud-python/issues/719 for more details.
97+
"""
98+
params: dict[str, Any] = {}
99+
if name is not None:
100+
params["name"] = name
101+
if page is not None:
102+
params["page"] = page
103+
if per_page is not None:
104+
params["per_page"] = per_page
105+
106+
response = self._client.request(
107+
method="GET",
108+
url=f"{self._base_url}",
109+
params=params,
110+
)
111+
return InferenceModelsPageResult(
112+
inference_models=[
113+
BoundInferenceModel(self, o) for o in response["inference_models"]
114+
],
115+
meta=Meta.parse_meta(response),
116+
)
117+
118+
def get_all(
119+
self,
120+
*,
121+
name: str | None = None,
122+
) -> list[BoundInferenceModel]:
123+
"""
124+
Returns all Inference Models.
125+
126+
See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models/list_inference_models
127+
128+
:param name: Name of the Inference Model.
129+
130+
Experimental:
131+
Support for the Inference API is experimental, breaking changes may occur within minor releases.
132+
See https://github.com/hetznercloud/hcloud-python/issues/719 for more details.
133+
"""
134+
return self._iter_pages(
135+
self.get_list,
136+
name=name,
137+
)

‎hcloud/inference_models/domain.py‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
5+
from ..core import BaseDomain, DomainIdentityMixin
6+
from ..deprecation import DeprecationInfo
7+
from ..prices import Price
8+
9+
__all__ = [
10+
"InferenceModel",
11+
"InferenceModelPrices",
12+
"InferenceModelPricesUnits",
13+
]
14+
15+
16+
class InferenceModel(BaseDomain, DomainIdentityMixin):
17+
"""
18+
Inference Model Domain.
19+
20+
See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models
21+
22+
Experimental:
23+
Support for the Inference API is experimental, breaking changes may occur within minor releases.
24+
See https://github.com/hetznercloud/hcloud-python/issues/719 for more details.
25+
"""
26+
27+
__api_properties__ = (
28+
"id",
29+
"name",
30+
"description",
31+
"category",
32+
"tasks",
33+
"quantization",
34+
"parameter_size",
35+
"context_size",
36+
"deprecation",
37+
"prices",
38+
)
39+
__slots__ = __api_properties__
40+
41+
def __init__(
42+
self,
43+
id: int | None = None,
44+
name: str | None = None,
45+
description: str | None = None,
46+
category: str | None = None,
47+
tasks: list[str] | None = None,
48+
quantization: str | None = None,
49+
parameter_size: str | None = None,
50+
context_size: int | None = None,
51+
deprecation: dict[str, Any] | None = None,
52+
prices: dict[str, Any] | None = None,
53+
):
54+
self.id = id
55+
self.name = name
56+
self.description = description
57+
self.category = category
58+
self.quantization = quantization
59+
self.parameter_size = parameter_size
60+
self.context_size = context_size
61+
self.tasks = tasks
62+
self.deprecation = (
63+
DeprecationInfo.from_dict(deprecation) if deprecation is not None else None
64+
)
65+
self.prices = (
66+
InferenceModelPrices.from_dict(prices) if prices is not None else None
67+
)
68+
69+
70+
class InferenceModelPrices(BaseDomain):
71+
"""
72+
Inference Model Prices Domain.
73+
74+
See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models
75+
"""
76+
77+
__api_properties__ = (
78+
"unbatched",
79+
"batched",
80+
)
81+
__slots__ = __api_properties__
82+
83+
def __init__(
84+
self,
85+
unbatched: dict[str, Any],
86+
batched: dict[str, Any],
87+
):
88+
self.unbatched = InferenceModelPricesUnits.from_dict(unbatched)
89+
self.batched = InferenceModelPricesUnits.from_dict(batched)
90+
91+
92+
class InferenceModelPricesUnits(BaseDomain):
93+
"""
94+
Inference Model Prices Units Domain.
95+
96+
See https://docs.hetzner.cloud/reference/hetzner#tag/inference-models
97+
"""
98+
99+
__api_properties__ = (
100+
"input",
101+
"cached",
102+
"output",
103+
"hours",
104+
)
105+
__slots__ = __api_properties__
106+
107+
def __init__(
108+
self,
109+
input: dict[str, Any] | None = None,
110+
cached: dict[str, Any] | None = None,
111+
output: dict[str, Any] | None = None,
112+
hours: dict[str, Any] | None = None,
113+
):
114+
self.input = None if input is None else Price.from_dict(input)
115+
self.cached = None if cached is None else Price.from_dict(cached)
116+
self.output = None if output is None else Price.from_dict(output)
117+
self.hours = None if hours is None else Price.from_dict(hours)

0 commit comments

Comments
 (0)