devupai 1.0.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- devupai/__init__.py +85 -0
- devupai/_client.py +1154 -0
- devupai/_errors.py +314 -0
- devupai/_response.py +225 -0
- devupai/_streaming.py +515 -0
- devupai/_version.py +3 -0
- devupai/py.typed +0 -0
- devupai/resources/__init__.py +137 -0
- devupai/resources/_base.py +207 -0
- devupai/resources/audio.py +663 -0
- devupai/resources/balance.py +103 -0
- devupai/resources/chat.py +428 -0
- devupai/resources/embeddings.py +141 -0
- devupai/resources/health.py +113 -0
- devupai/resources/images.py +503 -0
- devupai/resources/inference.py +347 -0
- devupai/resources/me.py +208 -0
- devupai/resources/models.py +405 -0
- devupai/resources/rerank.py +147 -0
- devupai/resources/responses.py +400 -0
- devupai/resources/shared.py +314 -0
- devupai/resources/video.py +600 -0
- devupai/types/__init__.py +264 -0
- devupai/types/_base.py +167 -0
- devupai/types/account.py +48 -0
- devupai/types/audio.py +89 -0
- devupai/types/chat.py +284 -0
- devupai/types/embeddings.py +51 -0
- devupai/types/images.py +238 -0
- devupai/types/models.py +107 -0
- devupai/types/rerank.py +41 -0
- devupai/types/responses.py +219 -0
- devupai/types/shared.py +79 -0
- devupai/types/video.py +218 -0
- devupai/types/webhooks.py +61 -0
- devupai/webhooks.py +278 -0
- devupai-1.0.0a1.dist-info/METADATA +221 -0
- devupai-1.0.0a1.dist-info/RECORD +40 -0
- devupai-1.0.0a1.dist-info/WHEEL +4 -0
- devupai-1.0.0a1.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Account balance: ``GET /user/balance``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
|
|
7
|
+
from devupai._response import APIResponse
|
|
8
|
+
from devupai.resources._base import AsyncAPIResource, SyncAPIResource, endpoint, options, with_data
|
|
9
|
+
from devupai.types import BalanceResponse
|
|
10
|
+
from devupai.types._base import parse_model
|
|
11
|
+
|
|
12
|
+
__all__ = ["Balance", "AsyncBalance", "BalanceWithResponse", "AsyncBalanceWithResponse"]
|
|
13
|
+
|
|
14
|
+
_PATH = "/user/balance"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class BalanceWithResponse(SyncAPIResource):
|
|
18
|
+
"""The methods of :class:`Balance`, returning the whole :class:`~devupai.APIResponse`."""
|
|
19
|
+
|
|
20
|
+
@endpoint("GET", _PATH, billed=False)
|
|
21
|
+
def retrieve(
|
|
22
|
+
self,
|
|
23
|
+
*,
|
|
24
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
25
|
+
timeout: float | None = None,
|
|
26
|
+
max_retries: int | None = None,
|
|
27
|
+
) -> APIResponse[BalanceResponse]:
|
|
28
|
+
"""Retrieves the current account balance in DZD; see :meth:`Balance.retrieve`."""
|
|
29
|
+
response = self._send(
|
|
30
|
+
"GET", _PATH, options=options(timeout=timeout, max_retries=max_retries, extra_headers=extra_headers)
|
|
31
|
+
)
|
|
32
|
+
return with_data(response, parse_model(BalanceResponse, response.data))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Balance(SyncAPIResource):
|
|
36
|
+
"""Account balance: ``GET /user/balance``."""
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def with_response(self) -> BalanceWithResponse:
|
|
40
|
+
"""The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
|
|
41
|
+
return BalanceWithResponse(self._client)
|
|
42
|
+
|
|
43
|
+
@endpoint("GET", _PATH, billed=False)
|
|
44
|
+
def retrieve(
|
|
45
|
+
self,
|
|
46
|
+
*,
|
|
47
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
48
|
+
timeout: float | None = None,
|
|
49
|
+
max_retries: int | None = None,
|
|
50
|
+
) -> BalanceResponse:
|
|
51
|
+
"""Retrieves the current account balance in DZD.
|
|
52
|
+
|
|
53
|
+
Example::
|
|
54
|
+
|
|
55
|
+
balance = client.balance.retrieve()
|
|
56
|
+
"""
|
|
57
|
+
return self.with_response.retrieve(extra_headers=extra_headers, timeout=timeout, max_retries=max_retries).data
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class AsyncBalanceWithResponse(AsyncAPIResource):
|
|
61
|
+
"""The methods of :class:`AsyncBalance`, returning the whole :class:`~devupai.APIResponse`."""
|
|
62
|
+
|
|
63
|
+
@endpoint("GET", _PATH, billed=False)
|
|
64
|
+
async def retrieve(
|
|
65
|
+
self,
|
|
66
|
+
*,
|
|
67
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
68
|
+
timeout: float | None = None,
|
|
69
|
+
max_retries: int | None = None,
|
|
70
|
+
) -> APIResponse[BalanceResponse]:
|
|
71
|
+
"""Retrieves the current account balance in DZD; see :meth:`AsyncBalance.retrieve`."""
|
|
72
|
+
response = await self._send(
|
|
73
|
+
"GET", _PATH, options=options(timeout=timeout, max_retries=max_retries, extra_headers=extra_headers)
|
|
74
|
+
)
|
|
75
|
+
return with_data(response, parse_model(BalanceResponse, response.data))
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class AsyncBalance(AsyncAPIResource):
|
|
79
|
+
"""Account balance: ``GET /user/balance``."""
|
|
80
|
+
|
|
81
|
+
@property
|
|
82
|
+
def with_response(self) -> AsyncBalanceWithResponse:
|
|
83
|
+
"""The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
|
|
84
|
+
return AsyncBalanceWithResponse(self._client)
|
|
85
|
+
|
|
86
|
+
@endpoint("GET", _PATH, billed=False)
|
|
87
|
+
async def retrieve(
|
|
88
|
+
self,
|
|
89
|
+
*,
|
|
90
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
91
|
+
timeout: float | None = None,
|
|
92
|
+
max_retries: int | None = None,
|
|
93
|
+
) -> BalanceResponse:
|
|
94
|
+
"""Retrieves the current account balance in DZD.
|
|
95
|
+
|
|
96
|
+
Example::
|
|
97
|
+
|
|
98
|
+
balance = await client.balance.retrieve()
|
|
99
|
+
"""
|
|
100
|
+
response = await self.with_response.retrieve(
|
|
101
|
+
extra_headers=extra_headers, timeout=timeout, max_retries=max_retries
|
|
102
|
+
)
|
|
103
|
+
return response.data
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
"""Chat completions: ``POST /chat/completions``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import TYPE_CHECKING, TypeAlias, Unpack, cast, overload
|
|
7
|
+
|
|
8
|
+
from devupai._response import APIResponse
|
|
9
|
+
from devupai._streaming import DEFAULT_STREAM_ERROR_MESSAGE, AsyncStream, Stream, debug_hook, stream_requested
|
|
10
|
+
from devupai.resources._base import (
|
|
11
|
+
AsyncAPIResource,
|
|
12
|
+
SyncAPIResource,
|
|
13
|
+
billed_post_retry,
|
|
14
|
+
endpoint,
|
|
15
|
+
json_body,
|
|
16
|
+
options,
|
|
17
|
+
with_data,
|
|
18
|
+
)
|
|
19
|
+
from devupai.types import ChatCompletion, ChatCompletionChunk
|
|
20
|
+
from devupai.types._base import parse_model
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from devupai._client import AsyncDevupAI, DevupAI, RequestOptions
|
|
24
|
+
from devupai.types import ChatCompletionCreateParams
|
|
25
|
+
from devupai.types.chat import ChatCompletionCreateParamsNonStreaming, ChatCompletionCreateParamsStreaming
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"Chat",
|
|
29
|
+
"AsyncChat",
|
|
30
|
+
"Completions",
|
|
31
|
+
"AsyncCompletions",
|
|
32
|
+
"CompletionsWithResponse",
|
|
33
|
+
"AsyncCompletionsWithResponse",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
_PATH = "/chat/completions"
|
|
37
|
+
|
|
38
|
+
# What the ``with_response`` implementations return: one member per overload.
|
|
39
|
+
_SyncResult: TypeAlias = (
|
|
40
|
+
APIResponse[ChatCompletion]
|
|
41
|
+
| APIResponse[Stream[ChatCompletionChunk]]
|
|
42
|
+
| APIResponse[ChatCompletion | Stream[ChatCompletionChunk]]
|
|
43
|
+
)
|
|
44
|
+
_AsyncResult: TypeAlias = (
|
|
45
|
+
APIResponse[ChatCompletion]
|
|
46
|
+
| APIResponse[AsyncStream[ChatCompletionChunk]]
|
|
47
|
+
| APIResponse[ChatCompletion | AsyncStream[ChatCompletionChunk]]
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _str_or_none(value: object) -> str | None:
|
|
52
|
+
return value if isinstance(value, str) else None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _error_frame(frame: object, event: str | None) -> tuple[str, str | None, str | None, str | None] | None:
|
|
56
|
+
"""Reads the error event of a chat completion stream: an object with an ``error`` object and no ``choices``.
|
|
57
|
+
|
|
58
|
+
Any other frame is a regular chunk.
|
|
59
|
+
"""
|
|
60
|
+
if not isinstance(frame, dict):
|
|
61
|
+
return None
|
|
62
|
+
record = cast("dict[str, object]", frame)
|
|
63
|
+
error = record.get("error")
|
|
64
|
+
if "choices" in record or not isinstance(error, dict):
|
|
65
|
+
return None
|
|
66
|
+
fields = cast("dict[str, object]", error)
|
|
67
|
+
message = _str_or_none(fields.get("message"))
|
|
68
|
+
return (
|
|
69
|
+
DEFAULT_STREAM_ERROR_MESSAGE if message is None else message,
|
|
70
|
+
_str_or_none(fields.get("code")),
|
|
71
|
+
_str_or_none(fields.get("type")),
|
|
72
|
+
_str_or_none(fields.get("param")),
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _parse_chunk(frame: object) -> ChatCompletionChunk:
|
|
77
|
+
return parse_model(ChatCompletionChunk, frame)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _request_options(
|
|
81
|
+
*,
|
|
82
|
+
timeout: float | None,
|
|
83
|
+
max_retries: int | None,
|
|
84
|
+
extra_headers: Mapping[str, str] | None,
|
|
85
|
+
stream_idle_timeout: float | None,
|
|
86
|
+
) -> RequestOptions:
|
|
87
|
+
result = options(timeout=timeout, max_retries=max_retries, extra_headers=extra_headers)
|
|
88
|
+
if stream_idle_timeout is not None:
|
|
89
|
+
result["stream_idle_timeout"] = stream_idle_timeout
|
|
90
|
+
return result
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _idle_timeout(client: DevupAI | AsyncDevupAI, request_options: RequestOptions) -> float | None:
|
|
94
|
+
from devupai import _client
|
|
95
|
+
|
|
96
|
+
return _client.effective_idle_timeout(client, request_options)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class CompletionsWithResponse(SyncAPIResource):
|
|
100
|
+
"""The methods of :class:`Completions`, returning the whole :class:`~devupai.APIResponse`."""
|
|
101
|
+
|
|
102
|
+
# APIResponse is invariant in its data type, so this overload's result is not a subtype of the third one's; the
|
|
103
|
+
# overloads are tried in order, and a stream=True call always gets this one.
|
|
104
|
+
@overload
|
|
105
|
+
def create( # pyright: ignore[reportOverlappingOverload]
|
|
106
|
+
self,
|
|
107
|
+
*,
|
|
108
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
109
|
+
timeout: float | None = None,
|
|
110
|
+
max_retries: int | None = None,
|
|
111
|
+
extra_body: Mapping[str, object] | None = None,
|
|
112
|
+
stream_idle_timeout: float | None = None,
|
|
113
|
+
**params: Unpack[ChatCompletionCreateParamsStreaming],
|
|
114
|
+
) -> APIResponse[Stream[ChatCompletionChunk]]: ...
|
|
115
|
+
@overload
|
|
116
|
+
def create(
|
|
117
|
+
self,
|
|
118
|
+
*,
|
|
119
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
120
|
+
timeout: float | None = None,
|
|
121
|
+
max_retries: int | None = None,
|
|
122
|
+
extra_body: Mapping[str, object] | None = None,
|
|
123
|
+
stream_idle_timeout: float | None = None,
|
|
124
|
+
**params: Unpack[ChatCompletionCreateParamsNonStreaming],
|
|
125
|
+
) -> APIResponse[ChatCompletion]: ...
|
|
126
|
+
@overload
|
|
127
|
+
def create(
|
|
128
|
+
self,
|
|
129
|
+
*,
|
|
130
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
131
|
+
timeout: float | None = None,
|
|
132
|
+
max_retries: int | None = None,
|
|
133
|
+
extra_body: Mapping[str, object] | None = None,
|
|
134
|
+
stream_idle_timeout: float | None = None,
|
|
135
|
+
**params: Unpack[ChatCompletionCreateParams],
|
|
136
|
+
) -> APIResponse[ChatCompletion | Stream[ChatCompletionChunk]]: ...
|
|
137
|
+
@endpoint("POST", _PATH, billed=True)
|
|
138
|
+
def create(
|
|
139
|
+
self,
|
|
140
|
+
*,
|
|
141
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
142
|
+
timeout: float | None = None,
|
|
143
|
+
max_retries: int | None = None,
|
|
144
|
+
extra_body: Mapping[str, object] | None = None,
|
|
145
|
+
stream_idle_timeout: float | None = None,
|
|
146
|
+
**params: Unpack[ChatCompletionCreateParams],
|
|
147
|
+
) -> _SyncResult:
|
|
148
|
+
"""Creates a model response for the given chat conversation; see :meth:`Completions.create`."""
|
|
149
|
+
body = json_body(params, extra_body)
|
|
150
|
+
streaming = stream_requested(params, body)
|
|
151
|
+
request_options = _request_options(
|
|
152
|
+
timeout=timeout,
|
|
153
|
+
max_retries=max_retries,
|
|
154
|
+
extra_headers=extra_headers,
|
|
155
|
+
stream_idle_timeout=stream_idle_timeout,
|
|
156
|
+
)
|
|
157
|
+
if streaming:
|
|
158
|
+
response = self._send(
|
|
159
|
+
"POST", _PATH, body=body, retry=billed_post_retry(), kind="stream", options=request_options
|
|
160
|
+
)
|
|
161
|
+
stream = Stream(
|
|
162
|
+
response.data,
|
|
163
|
+
parse=_parse_chunk,
|
|
164
|
+
error_frame=_error_frame,
|
|
165
|
+
on_debug=debug_hook(self._client),
|
|
166
|
+
idle_timeout=_idle_timeout(self._client, request_options),
|
|
167
|
+
)
|
|
168
|
+
return with_data(response, stream)
|
|
169
|
+
response = self._send("POST", _PATH, body=body, retry=billed_post_retry(), options=request_options)
|
|
170
|
+
return with_data(response, parse_model(ChatCompletion, response.data))
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class Completions(SyncAPIResource):
|
|
174
|
+
"""Chat completions: ``POST /chat/completions``."""
|
|
175
|
+
|
|
176
|
+
@property
|
|
177
|
+
def with_response(self) -> CompletionsWithResponse:
|
|
178
|
+
"""The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
|
|
179
|
+
return CompletionsWithResponse(self._client)
|
|
180
|
+
|
|
181
|
+
@overload
|
|
182
|
+
def create(
|
|
183
|
+
self,
|
|
184
|
+
*,
|
|
185
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
186
|
+
timeout: float | None = None,
|
|
187
|
+
max_retries: int | None = None,
|
|
188
|
+
extra_body: Mapping[str, object] | None = None,
|
|
189
|
+
stream_idle_timeout: float | None = None,
|
|
190
|
+
**params: Unpack[ChatCompletionCreateParamsStreaming],
|
|
191
|
+
) -> Stream[ChatCompletionChunk]: ...
|
|
192
|
+
@overload
|
|
193
|
+
def create(
|
|
194
|
+
self,
|
|
195
|
+
*,
|
|
196
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
197
|
+
timeout: float | None = None,
|
|
198
|
+
max_retries: int | None = None,
|
|
199
|
+
extra_body: Mapping[str, object] | None = None,
|
|
200
|
+
stream_idle_timeout: float | None = None,
|
|
201
|
+
**params: Unpack[ChatCompletionCreateParamsNonStreaming],
|
|
202
|
+
) -> ChatCompletion: ...
|
|
203
|
+
@overload
|
|
204
|
+
def create(
|
|
205
|
+
self,
|
|
206
|
+
*,
|
|
207
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
208
|
+
timeout: float | None = None,
|
|
209
|
+
max_retries: int | None = None,
|
|
210
|
+
extra_body: Mapping[str, object] | None = None,
|
|
211
|
+
stream_idle_timeout: float | None = None,
|
|
212
|
+
**params: Unpack[ChatCompletionCreateParams],
|
|
213
|
+
) -> ChatCompletion | Stream[ChatCompletionChunk]: ...
|
|
214
|
+
@endpoint("POST", _PATH, billed=True)
|
|
215
|
+
def create(
|
|
216
|
+
self,
|
|
217
|
+
*,
|
|
218
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
219
|
+
timeout: float | None = None,
|
|
220
|
+
max_retries: int | None = None,
|
|
221
|
+
extra_body: Mapping[str, object] | None = None,
|
|
222
|
+
stream_idle_timeout: float | None = None,
|
|
223
|
+
**params: Unpack[ChatCompletionCreateParams],
|
|
224
|
+
) -> ChatCompletion | Stream[ChatCompletionChunk]:
|
|
225
|
+
"""Creates a model response for the given chat conversation.
|
|
226
|
+
|
|
227
|
+
The body fields are passed as keyword arguments. ``extra_body`` adds fields to the JSON body (merged last),
|
|
228
|
+
``extra_headers`` adds request headers, and ``timeout`` and ``max_retries`` apply to this call only.
|
|
229
|
+
|
|
230
|
+
With ``stream=True`` the result is a :class:`~devupai.Stream` of :class:`~devupai.types.ChatCompletionChunk`
|
|
231
|
+
items; the last chunk has empty ``choices`` and carries ``usage``. ``timeout`` then covers the time until the
|
|
232
|
+
response headers arrive, and ``stream_idle_timeout`` (seconds, ``0`` turns it off) ends the stream when no
|
|
233
|
+
data arrives for that long; it overrides the client option. An error event inside the stream raises a
|
|
234
|
+
:class:`~devupai.DevupAPIError` with its ``code`` and the stream's request id. Pass ``stream`` as an
|
|
235
|
+
argument: ``stream`` inside ``extra_body`` raises ``ValueError`` before anything is sent.
|
|
236
|
+
|
|
237
|
+
Example::
|
|
238
|
+
|
|
239
|
+
completion = client.chat.completions.create(
|
|
240
|
+
model="<model-id>",
|
|
241
|
+
messages=[{"role": "user", "content": "Hello"}],
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
with client.chat.completions.create(model="<model-id>", messages=messages, stream=True) as stream:
|
|
245
|
+
for chunk in stream:
|
|
246
|
+
print(chunk.choices[0].delta.content if chunk.choices else "", end="")
|
|
247
|
+
"""
|
|
248
|
+
return self.with_response.create(
|
|
249
|
+
extra_headers=extra_headers,
|
|
250
|
+
timeout=timeout,
|
|
251
|
+
max_retries=max_retries,
|
|
252
|
+
extra_body=extra_body,
|
|
253
|
+
stream_idle_timeout=stream_idle_timeout,
|
|
254
|
+
**params,
|
|
255
|
+
).data
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
class Chat(SyncAPIResource):
|
|
259
|
+
"""Chat resource."""
|
|
260
|
+
|
|
261
|
+
completions: Completions
|
|
262
|
+
"""Chat completions."""
|
|
263
|
+
|
|
264
|
+
def __init__(self, client: DevupAI) -> None:
|
|
265
|
+
super().__init__(client)
|
|
266
|
+
self.completions = Completions(client)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
class AsyncCompletionsWithResponse(AsyncAPIResource):
|
|
270
|
+
"""The methods of :class:`AsyncCompletions`, returning the whole :class:`~devupai.APIResponse`."""
|
|
271
|
+
|
|
272
|
+
@overload
|
|
273
|
+
async def create(
|
|
274
|
+
self,
|
|
275
|
+
*,
|
|
276
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
277
|
+
timeout: float | None = None,
|
|
278
|
+
max_retries: int | None = None,
|
|
279
|
+
extra_body: Mapping[str, object] | None = None,
|
|
280
|
+
stream_idle_timeout: float | None = None,
|
|
281
|
+
**params: Unpack[ChatCompletionCreateParamsStreaming],
|
|
282
|
+
) -> APIResponse[AsyncStream[ChatCompletionChunk]]: ...
|
|
283
|
+
@overload
|
|
284
|
+
async def create(
|
|
285
|
+
self,
|
|
286
|
+
*,
|
|
287
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
288
|
+
timeout: float | None = None,
|
|
289
|
+
max_retries: int | None = None,
|
|
290
|
+
extra_body: Mapping[str, object] | None = None,
|
|
291
|
+
stream_idle_timeout: float | None = None,
|
|
292
|
+
**params: Unpack[ChatCompletionCreateParamsNonStreaming],
|
|
293
|
+
) -> APIResponse[ChatCompletion]: ...
|
|
294
|
+
@overload
|
|
295
|
+
async def create(
|
|
296
|
+
self,
|
|
297
|
+
*,
|
|
298
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
299
|
+
timeout: float | None = None,
|
|
300
|
+
max_retries: int | None = None,
|
|
301
|
+
extra_body: Mapping[str, object] | None = None,
|
|
302
|
+
stream_idle_timeout: float | None = None,
|
|
303
|
+
**params: Unpack[ChatCompletionCreateParams],
|
|
304
|
+
) -> APIResponse[ChatCompletion | AsyncStream[ChatCompletionChunk]]: ...
|
|
305
|
+
@endpoint("POST", _PATH, billed=True)
|
|
306
|
+
async def create(
|
|
307
|
+
self,
|
|
308
|
+
*,
|
|
309
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
310
|
+
timeout: float | None = None,
|
|
311
|
+
max_retries: int | None = None,
|
|
312
|
+
extra_body: Mapping[str, object] | None = None,
|
|
313
|
+
stream_idle_timeout: float | None = None,
|
|
314
|
+
**params: Unpack[ChatCompletionCreateParams],
|
|
315
|
+
) -> _AsyncResult:
|
|
316
|
+
"""Creates a model response for the given chat conversation; see :meth:`AsyncCompletions.create`."""
|
|
317
|
+
body = json_body(params, extra_body)
|
|
318
|
+
streaming = stream_requested(params, body)
|
|
319
|
+
request_options = _request_options(
|
|
320
|
+
timeout=timeout,
|
|
321
|
+
max_retries=max_retries,
|
|
322
|
+
extra_headers=extra_headers,
|
|
323
|
+
stream_idle_timeout=stream_idle_timeout,
|
|
324
|
+
)
|
|
325
|
+
if streaming:
|
|
326
|
+
response = await self._send(
|
|
327
|
+
"POST", _PATH, body=body, retry=billed_post_retry(), kind="stream", options=request_options
|
|
328
|
+
)
|
|
329
|
+
stream = AsyncStream(
|
|
330
|
+
response.data,
|
|
331
|
+
parse=_parse_chunk,
|
|
332
|
+
error_frame=_error_frame,
|
|
333
|
+
on_debug=debug_hook(self._client),
|
|
334
|
+
idle_timeout=_idle_timeout(self._client, request_options),
|
|
335
|
+
)
|
|
336
|
+
return with_data(response, stream)
|
|
337
|
+
response = await self._send("POST", _PATH, body=body, retry=billed_post_retry(), options=request_options)
|
|
338
|
+
return with_data(response, parse_model(ChatCompletion, response.data))
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
class AsyncCompletions(AsyncAPIResource):
|
|
342
|
+
"""Chat completions: ``POST /chat/completions``."""
|
|
343
|
+
|
|
344
|
+
@property
|
|
345
|
+
def with_response(self) -> AsyncCompletionsWithResponse:
|
|
346
|
+
"""The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
|
|
347
|
+
return AsyncCompletionsWithResponse(self._client)
|
|
348
|
+
|
|
349
|
+
@overload
|
|
350
|
+
async def create(
|
|
351
|
+
self,
|
|
352
|
+
*,
|
|
353
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
354
|
+
timeout: float | None = None,
|
|
355
|
+
max_retries: int | None = None,
|
|
356
|
+
extra_body: Mapping[str, object] | None = None,
|
|
357
|
+
stream_idle_timeout: float | None = None,
|
|
358
|
+
**params: Unpack[ChatCompletionCreateParamsStreaming],
|
|
359
|
+
) -> AsyncStream[ChatCompletionChunk]: ...
|
|
360
|
+
@overload
|
|
361
|
+
async def create(
|
|
362
|
+
self,
|
|
363
|
+
*,
|
|
364
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
365
|
+
timeout: float | None = None,
|
|
366
|
+
max_retries: int | None = None,
|
|
367
|
+
extra_body: Mapping[str, object] | None = None,
|
|
368
|
+
stream_idle_timeout: float | None = None,
|
|
369
|
+
**params: Unpack[ChatCompletionCreateParamsNonStreaming],
|
|
370
|
+
) -> ChatCompletion: ...
|
|
371
|
+
@overload
|
|
372
|
+
async def create(
|
|
373
|
+
self,
|
|
374
|
+
*,
|
|
375
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
376
|
+
timeout: float | None = None,
|
|
377
|
+
max_retries: int | None = None,
|
|
378
|
+
extra_body: Mapping[str, object] | None = None,
|
|
379
|
+
stream_idle_timeout: float | None = None,
|
|
380
|
+
**params: Unpack[ChatCompletionCreateParams],
|
|
381
|
+
) -> ChatCompletion | AsyncStream[ChatCompletionChunk]: ...
|
|
382
|
+
@endpoint("POST", _PATH, billed=True)
|
|
383
|
+
async def create(
|
|
384
|
+
self,
|
|
385
|
+
*,
|
|
386
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
387
|
+
timeout: float | None = None,
|
|
388
|
+
max_retries: int | None = None,
|
|
389
|
+
extra_body: Mapping[str, object] | None = None,
|
|
390
|
+
stream_idle_timeout: float | None = None,
|
|
391
|
+
**params: Unpack[ChatCompletionCreateParams],
|
|
392
|
+
) -> ChatCompletion | AsyncStream[ChatCompletionChunk]:
|
|
393
|
+
"""Creates a model response for the given chat conversation.
|
|
394
|
+
|
|
395
|
+
The body fields are passed as keyword arguments. ``extra_body`` adds fields to the JSON body (merged last),
|
|
396
|
+
``extra_headers`` adds request headers, and ``timeout`` and ``max_retries`` apply to this call only.
|
|
397
|
+
|
|
398
|
+
With ``stream=True`` the result is an :class:`~devupai.AsyncStream` of
|
|
399
|
+
:class:`~devupai.types.ChatCompletionChunk` items, read with ``async for``; the rules of
|
|
400
|
+
:meth:`Completions.create` apply.
|
|
401
|
+
|
|
402
|
+
Example::
|
|
403
|
+
|
|
404
|
+
completion = await client.chat.completions.create(
|
|
405
|
+
model="<model-id>",
|
|
406
|
+
messages=[{"role": "user", "content": "Hello"}],
|
|
407
|
+
)
|
|
408
|
+
"""
|
|
409
|
+
response = await self.with_response.create(
|
|
410
|
+
extra_headers=extra_headers,
|
|
411
|
+
timeout=timeout,
|
|
412
|
+
max_retries=max_retries,
|
|
413
|
+
extra_body=extra_body,
|
|
414
|
+
stream_idle_timeout=stream_idle_timeout,
|
|
415
|
+
**params,
|
|
416
|
+
)
|
|
417
|
+
return response.data
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
class AsyncChat(AsyncAPIResource):
|
|
421
|
+
"""Chat resource."""
|
|
422
|
+
|
|
423
|
+
completions: AsyncCompletions
|
|
424
|
+
"""Chat completions."""
|
|
425
|
+
|
|
426
|
+
def __init__(self, client: AsyncDevupAI) -> None:
|
|
427
|
+
super().__init__(client)
|
|
428
|
+
self.completions = AsyncCompletions(client)
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""Embeddings: ``POST /embeddings``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import TYPE_CHECKING, Unpack
|
|
7
|
+
|
|
8
|
+
from devupai._response import APIResponse
|
|
9
|
+
from devupai.resources._base import (
|
|
10
|
+
AsyncAPIResource,
|
|
11
|
+
SyncAPIResource,
|
|
12
|
+
billed_post_retry,
|
|
13
|
+
endpoint,
|
|
14
|
+
json_body,
|
|
15
|
+
options,
|
|
16
|
+
with_data,
|
|
17
|
+
)
|
|
18
|
+
from devupai.types import EmbeddingCreateResponse
|
|
19
|
+
from devupai.types._base import parse_model
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
from devupai.types import EmbeddingCreateParams
|
|
23
|
+
|
|
24
|
+
__all__ = ["Embeddings", "AsyncEmbeddings", "EmbeddingsWithResponse", "AsyncEmbeddingsWithResponse"]
|
|
25
|
+
|
|
26
|
+
_PATH = "/embeddings"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class EmbeddingsWithResponse(SyncAPIResource):
|
|
30
|
+
"""The methods of :class:`Embeddings`, returning the whole :class:`~devupai.APIResponse`."""
|
|
31
|
+
|
|
32
|
+
@endpoint("POST", _PATH, billed=True)
|
|
33
|
+
def create(
|
|
34
|
+
self,
|
|
35
|
+
*,
|
|
36
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
37
|
+
timeout: float | None = None,
|
|
38
|
+
max_retries: int | None = None,
|
|
39
|
+
extra_body: Mapping[str, object] | None = None,
|
|
40
|
+
**params: Unpack[EmbeddingCreateParams],
|
|
41
|
+
) -> APIResponse[EmbeddingCreateResponse]:
|
|
42
|
+
"""Creates embedding vectors for the input text; see :meth:`Embeddings.create`."""
|
|
43
|
+
response = self._send(
|
|
44
|
+
"POST",
|
|
45
|
+
_PATH,
|
|
46
|
+
body=json_body(params, extra_body),
|
|
47
|
+
retry=billed_post_retry(),
|
|
48
|
+
options=options(timeout=timeout, max_retries=max_retries, extra_headers=extra_headers),
|
|
49
|
+
)
|
|
50
|
+
return with_data(response, parse_model(EmbeddingCreateResponse, response.data))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Embeddings(SyncAPIResource):
|
|
54
|
+
"""Embeddings: ``POST /embeddings``."""
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def with_response(self) -> EmbeddingsWithResponse:
|
|
58
|
+
"""The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
|
|
59
|
+
return EmbeddingsWithResponse(self._client)
|
|
60
|
+
|
|
61
|
+
@endpoint("POST", _PATH, billed=True)
|
|
62
|
+
def create(
|
|
63
|
+
self,
|
|
64
|
+
*,
|
|
65
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
66
|
+
timeout: float | None = None,
|
|
67
|
+
max_retries: int | None = None,
|
|
68
|
+
extra_body: Mapping[str, object] | None = None,
|
|
69
|
+
**params: Unpack[EmbeddingCreateParams],
|
|
70
|
+
) -> EmbeddingCreateResponse:
|
|
71
|
+
"""Creates embedding vectors for the input text.
|
|
72
|
+
|
|
73
|
+
With ``encoding_format="base64"`` each ``embedding`` is a base64 string instead of a list of numbers.
|
|
74
|
+
``extra_body`` adds fields to the JSON body (merged last), ``extra_headers`` adds request headers, and
|
|
75
|
+
``timeout`` and ``max_retries`` apply to this call only.
|
|
76
|
+
|
|
77
|
+
Example::
|
|
78
|
+
|
|
79
|
+
result = client.embeddings.create(model="<model-id>", input="Hello")
|
|
80
|
+
"""
|
|
81
|
+
return self.with_response.create(
|
|
82
|
+
extra_headers=extra_headers, timeout=timeout, max_retries=max_retries, extra_body=extra_body, **params
|
|
83
|
+
).data
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class AsyncEmbeddingsWithResponse(AsyncAPIResource):
|
|
87
|
+
"""The methods of :class:`AsyncEmbeddings`, returning the whole :class:`~devupai.APIResponse`."""
|
|
88
|
+
|
|
89
|
+
@endpoint("POST", _PATH, billed=True)
|
|
90
|
+
async def create(
|
|
91
|
+
self,
|
|
92
|
+
*,
|
|
93
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
94
|
+
timeout: float | None = None,
|
|
95
|
+
max_retries: int | None = None,
|
|
96
|
+
extra_body: Mapping[str, object] | None = None,
|
|
97
|
+
**params: Unpack[EmbeddingCreateParams],
|
|
98
|
+
) -> APIResponse[EmbeddingCreateResponse]:
|
|
99
|
+
"""Creates embedding vectors for the input text; see :meth:`AsyncEmbeddings.create`."""
|
|
100
|
+
response = await self._send(
|
|
101
|
+
"POST",
|
|
102
|
+
_PATH,
|
|
103
|
+
body=json_body(params, extra_body),
|
|
104
|
+
retry=billed_post_retry(),
|
|
105
|
+
options=options(timeout=timeout, max_retries=max_retries, extra_headers=extra_headers),
|
|
106
|
+
)
|
|
107
|
+
return with_data(response, parse_model(EmbeddingCreateResponse, response.data))
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class AsyncEmbeddings(AsyncAPIResource):
|
|
111
|
+
"""Embeddings: ``POST /embeddings``."""
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def with_response(self) -> AsyncEmbeddingsWithResponse:
|
|
115
|
+
"""The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
|
|
116
|
+
return AsyncEmbeddingsWithResponse(self._client)
|
|
117
|
+
|
|
118
|
+
@endpoint("POST", _PATH, billed=True)
|
|
119
|
+
async def create(
|
|
120
|
+
self,
|
|
121
|
+
*,
|
|
122
|
+
extra_headers: Mapping[str, str] | None = None,
|
|
123
|
+
timeout: float | None = None,
|
|
124
|
+
max_retries: int | None = None,
|
|
125
|
+
extra_body: Mapping[str, object] | None = None,
|
|
126
|
+
**params: Unpack[EmbeddingCreateParams],
|
|
127
|
+
) -> EmbeddingCreateResponse:
|
|
128
|
+
"""Creates embedding vectors for the input text.
|
|
129
|
+
|
|
130
|
+
With ``encoding_format="base64"`` each ``embedding`` is a base64 string instead of a list of numbers.
|
|
131
|
+
``extra_body`` adds fields to the JSON body (merged last), ``extra_headers`` adds request headers, and
|
|
132
|
+
``timeout`` and ``max_retries`` apply to this call only.
|
|
133
|
+
|
|
134
|
+
Example::
|
|
135
|
+
|
|
136
|
+
result = await client.embeddings.create(model="<model-id>", input="Hello")
|
|
137
|
+
"""
|
|
138
|
+
response = await self.with_response.create(
|
|
139
|
+
extra_headers=extra_headers, timeout=timeout, max_retries=max_retries, extra_body=extra_body, **params
|
|
140
|
+
)
|
|
141
|
+
return response.data
|