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
devupai/_streaming.py
ADDED
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
"""Server-sent event streams: :class:`Stream` for :class:`~devupai.DevupAI`, :class:`AsyncStream` for
|
|
2
|
+
:class:`~devupai.AsyncDevupAI`.
|
|
3
|
+
|
|
4
|
+
``chat.completions.create(stream=True)`` and ``responses.create(stream=True)`` return one of them. Iterate it once
|
|
5
|
+
(``for`` / ``async for``); each item is one parsed event. Leaving a ``with`` / ``async with`` block, or calling
|
|
6
|
+
``close()``, closes the response.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import codecs
|
|
12
|
+
import json
|
|
13
|
+
import re
|
|
14
|
+
import warnings
|
|
15
|
+
from collections import deque
|
|
16
|
+
from collections.abc import Callable, Mapping
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from types import TracebackType
|
|
19
|
+
from typing import TYPE_CHECKING, Generic, Self, TypeVar, cast
|
|
20
|
+
|
|
21
|
+
import anyio
|
|
22
|
+
import httpx
|
|
23
|
+
|
|
24
|
+
from devupai._errors import APIConnectionError, APITimeoutError, DevupAPIError, make_api_error, parse_warnings
|
|
25
|
+
|
|
26
|
+
if TYPE_CHECKING:
|
|
27
|
+
from collections.abc import AsyncIterator, Iterator
|
|
28
|
+
|
|
29
|
+
from devupai._client import DebugEvent
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"DEFAULT_STREAM_ERROR_MESSAGE",
|
|
33
|
+
"ErrorFrame",
|
|
34
|
+
"ServerSentEvent",
|
|
35
|
+
"SSEParser",
|
|
36
|
+
"Stream",
|
|
37
|
+
"AsyncStream",
|
|
38
|
+
"debug_hook",
|
|
39
|
+
"stream_requested",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
T = TypeVar("T")
|
|
43
|
+
|
|
44
|
+
DEFAULT_STREAM_ERROR_MESSAGE = "The request could not be completed."
|
|
45
|
+
"""The message of a stream error event that carries none."""
|
|
46
|
+
|
|
47
|
+
ErrorFrame = Callable[[object, str | None], tuple[str, str | None, str | None, str | None] | None]
|
|
48
|
+
"""Reads an error event: ``(frame, SSE event name)`` -> ``(message, code, type, param)``, or ``None`` for a regular
|
|
49
|
+
event."""
|
|
50
|
+
|
|
51
|
+
_ITERATED_MESSAGE = "A stream can be iterated only once."
|
|
52
|
+
_EXTRA_BODY_STREAM_MESSAGE = "Pass stream=True as an argument, not in extra_body."
|
|
53
|
+
_READ_ERRORS = (httpx.TransportError, httpx.DecodingError)
|
|
54
|
+
_LINE_END = re.compile(r"\r\n?|\n")
|
|
55
|
+
_DROPPED_MESSAGE = (
|
|
56
|
+
"An AsyncStream was dropped without being closed. Use `async with`, or `await stream.close()`, to close its "
|
|
57
|
+
"response."
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclass(frozen=True)
|
|
62
|
+
class ServerSentEvent:
|
|
63
|
+
"""One server-sent event. ``event`` is ``None`` when the event has no ``event:`` field."""
|
|
64
|
+
|
|
65
|
+
event: str | None
|
|
66
|
+
data: str
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class SSEParser:
|
|
70
|
+
"""Incremental parser for ``text/event-stream`` content, following the WHATWG event-stream interpretation rules.
|
|
71
|
+
|
|
72
|
+
CR, LF and CRLF end a line (a CRLF split across two pieces is one line end), ``:`` starts a comment, one
|
|
73
|
+
optional space after the ``:`` is removed, the ``data`` lines of an event are joined with ``\\n``, ``id`` and
|
|
74
|
+
``retry`` are ignored, and a blank line dispatches the event.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def __init__(self) -> None:
|
|
78
|
+
# The pieces of the line that is not ended yet, joined once its line end arrives.
|
|
79
|
+
self._partial: list[str] = []
|
|
80
|
+
self._pending_cr = False
|
|
81
|
+
self._event_type: str | None = None
|
|
82
|
+
self._data = ""
|
|
83
|
+
self._has_data = False
|
|
84
|
+
|
|
85
|
+
def feed(self, text: str) -> list[ServerSentEvent]:
|
|
86
|
+
"""Adds decoded text and returns the events completed by it."""
|
|
87
|
+
events: list[ServerSentEvent] = []
|
|
88
|
+
if self._pending_cr:
|
|
89
|
+
self._pending_cr = False
|
|
90
|
+
if text.startswith("\n"):
|
|
91
|
+
text = text[1:]
|
|
92
|
+
position = 0
|
|
93
|
+
length = len(text)
|
|
94
|
+
while position < length:
|
|
95
|
+
# One search per line end, so each character is scanned once (linear in the size of the text).
|
|
96
|
+
match = _LINE_END.search(text, position)
|
|
97
|
+
if match is None:
|
|
98
|
+
self._partial.append(text[position:])
|
|
99
|
+
break
|
|
100
|
+
piece = text[position : match.start()]
|
|
101
|
+
if self._partial:
|
|
102
|
+
self._partial.append(piece)
|
|
103
|
+
line = "".join(self._partial)
|
|
104
|
+
self._partial.clear()
|
|
105
|
+
else:
|
|
106
|
+
line = piece
|
|
107
|
+
position = match.end()
|
|
108
|
+
if match.group() == "\r" and position == length:
|
|
109
|
+
# A CR at the end of the text: an LF at the start of the next text belongs to the same line end.
|
|
110
|
+
self._pending_cr = True
|
|
111
|
+
event = self._process_line(line)
|
|
112
|
+
if event is not None:
|
|
113
|
+
events.append(event)
|
|
114
|
+
return events
|
|
115
|
+
|
|
116
|
+
def end(self) -> str | None:
|
|
117
|
+
"""Ends the input. Returns the data of an event that no blank line ended (it is not dispatched), or
|
|
118
|
+
``None``."""
|
|
119
|
+
if self._partial:
|
|
120
|
+
line = "".join(self._partial)
|
|
121
|
+
self._partial.clear()
|
|
122
|
+
self._process_line(line)
|
|
123
|
+
pending = self._data if self._has_data else None
|
|
124
|
+
self._data = ""
|
|
125
|
+
self._has_data = False
|
|
126
|
+
self._event_type = None
|
|
127
|
+
return pending
|
|
128
|
+
|
|
129
|
+
def _process_line(self, line: str) -> ServerSentEvent | None:
|
|
130
|
+
if line == "":
|
|
131
|
+
if not self._has_data:
|
|
132
|
+
self._event_type = None
|
|
133
|
+
return None
|
|
134
|
+
event = ServerSentEvent(event=self._event_type, data=self._data)
|
|
135
|
+
self._event_type = None
|
|
136
|
+
self._data = ""
|
|
137
|
+
self._has_data = False
|
|
138
|
+
return event
|
|
139
|
+
if line.startswith(":"):
|
|
140
|
+
return None
|
|
141
|
+
field, colon, value = line.partition(":")
|
|
142
|
+
if colon and value.startswith(" "):
|
|
143
|
+
value = value[1:]
|
|
144
|
+
if field == "event":
|
|
145
|
+
self._event_type = value
|
|
146
|
+
elif field == "data":
|
|
147
|
+
self._data = f"{self._data}\n{value}" if self._has_data else value
|
|
148
|
+
self._has_data = True
|
|
149
|
+
return None
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def stream_requested(params: Mapping[str, object], body: Mapping[str, object]) -> bool:
|
|
153
|
+
"""Whether a call asks for a stream: its ``stream`` argument is ``True``.
|
|
154
|
+
|
|
155
|
+
Raises ``ValueError`` before anything is sent when the merged body's ``stream`` differs from the argument, that
|
|
156
|
+
is when ``stream`` was set through ``extra_body``.
|
|
157
|
+
"""
|
|
158
|
+
wanted = params.get("stream") is True
|
|
159
|
+
if (body.get("stream") is True) != wanted:
|
|
160
|
+
raise ValueError(_EXTRA_BODY_STREAM_MESSAGE)
|
|
161
|
+
return wanted
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def debug_hook(client: object) -> Callable[[DebugEvent], None] | None:
|
|
165
|
+
"""The ``on_debug`` hook of ``client``, or ``None`` when it has none."""
|
|
166
|
+
hook: object = getattr(client, "_on_debug", None)
|
|
167
|
+
if hook is None or not callable(hook):
|
|
168
|
+
return None
|
|
169
|
+
return cast("Callable[[DebugEvent], None]", hook)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
_DONE = object()
|
|
173
|
+
_SKIP = object()
|
|
174
|
+
_END = object()
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class _Frames(Generic[T]):
|
|
178
|
+
"""The decoding shared by :class:`Stream` and :class:`AsyncStream`: bytes -> SSE events -> items."""
|
|
179
|
+
|
|
180
|
+
def __init__(
|
|
181
|
+
self,
|
|
182
|
+
response: httpx.Response,
|
|
183
|
+
parse: Callable[[object], T],
|
|
184
|
+
error_frame: ErrorFrame,
|
|
185
|
+
on_debug: Callable[[DebugEvent], None] | None,
|
|
186
|
+
idle_timeout: float | None,
|
|
187
|
+
) -> None:
|
|
188
|
+
self.response = response
|
|
189
|
+
self.parse = parse
|
|
190
|
+
self.error_frame = error_frame
|
|
191
|
+
self.on_debug = on_debug
|
|
192
|
+
self.idle_timeout = idle_timeout if idle_timeout is not None and idle_timeout > 0 else None
|
|
193
|
+
self.decoder = codecs.getincrementaldecoder("utf-8")(errors="replace")
|
|
194
|
+
self.parser = SSEParser()
|
|
195
|
+
self.events: deque[ServerSentEvent] = deque()
|
|
196
|
+
self.body_done = False
|
|
197
|
+
|
|
198
|
+
def push(self, data: bytes, *, final: bool = False) -> None:
|
|
199
|
+
"""Decodes ``data`` and keeps the events it completes; ``final`` marks the end of the body."""
|
|
200
|
+
self.events.extend(self.parser.feed(self.decoder.decode(data, final=final)))
|
|
201
|
+
if final:
|
|
202
|
+
self.body_done = True
|
|
203
|
+
|
|
204
|
+
def handle(self, event: ServerSentEvent) -> object:
|
|
205
|
+
"""The item of ``event``, or ``_DONE`` / ``_SKIP``. Raises the error of an error event."""
|
|
206
|
+
if event.data.strip() == "[DONE]":
|
|
207
|
+
return _DONE
|
|
208
|
+
try:
|
|
209
|
+
frame: object = json.loads(event.data)
|
|
210
|
+
except (ValueError, RecursionError):
|
|
211
|
+
# RecursionError: a frame nested too deeply to decode is skipped like any other frame that is not JSON.
|
|
212
|
+
self.debug_malformed(event.data)
|
|
213
|
+
return _SKIP
|
|
214
|
+
error = self.error_frame(frame, event.event)
|
|
215
|
+
if error is not None:
|
|
216
|
+
message, code, error_type, param = error
|
|
217
|
+
raise make_api_error(
|
|
218
|
+
self.response.status_code,
|
|
219
|
+
message,
|
|
220
|
+
code=code,
|
|
221
|
+
error_type=error_type,
|
|
222
|
+
param=param,
|
|
223
|
+
headers=self.response.headers,
|
|
224
|
+
)
|
|
225
|
+
return self.parse(frame)
|
|
226
|
+
|
|
227
|
+
def finish(self) -> None:
|
|
228
|
+
"""Ends the parsing at the end of the body; an event no blank line ended is reported as ``malformed_frame``."""
|
|
229
|
+
pending = self.parser.end()
|
|
230
|
+
if pending is not None:
|
|
231
|
+
self.debug_malformed(pending)
|
|
232
|
+
|
|
233
|
+
def debug_malformed(self, data: str) -> None:
|
|
234
|
+
"""Calls the ``on_debug`` hook. A hook that raises never changes the outcome of the stream."""
|
|
235
|
+
hook = self.on_debug
|
|
236
|
+
if hook is None:
|
|
237
|
+
return
|
|
238
|
+
from devupai._client import DebugEvent
|
|
239
|
+
|
|
240
|
+
try:
|
|
241
|
+
hook(DebugEvent(type="malformed_frame", detail={"data": data}))
|
|
242
|
+
except Exception:
|
|
243
|
+
# Deliberately not propagated: on_debug is a diagnostic hook, and its errors must not end the stream
|
|
244
|
+
# or replace its result.
|
|
245
|
+
return
|
|
246
|
+
|
|
247
|
+
def read_error(self, error: Exception) -> DevupAPIError:
|
|
248
|
+
"""The error raised when reading the body fails: an idle timeout, or a connection error."""
|
|
249
|
+
if self.idle_timeout is not None and isinstance(error, httpx.ReadTimeout):
|
|
250
|
+
return self.idle_error()
|
|
251
|
+
return APIConnectionError(0, f"Network error: {str(error) or 'unknown error'}", headers=self.response.headers)
|
|
252
|
+
|
|
253
|
+
def idle_error(self) -> APITimeoutError:
|
|
254
|
+
return APITimeoutError(
|
|
255
|
+
0,
|
|
256
|
+
f"No data was received on the stream for {self.idle_timeout:g} seconds.",
|
|
257
|
+
headers=self.response.headers,
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
class Stream(Generic[T]):
|
|
262
|
+
"""A server-sent event stream of the sync client. Iterate it once with ``for``; each item is one parsed event.
|
|
263
|
+
|
|
264
|
+
``request_id``, ``client_request_id`` and ``warnings`` can be read before the first item. Use it in a ``with``
|
|
265
|
+
block, or call :meth:`close`, to close the response when you stop early (a loop left by ``break`` or by an
|
|
266
|
+
exception, and a stream dropped unread, close it too). An error event raises a
|
|
267
|
+
:class:`~devupai.DevupAPIError` with the stream's status, a read failure raises
|
|
268
|
+
:class:`~devupai.APIConnectionError`, and an idle stream raises :class:`~devupai.APITimeoutError`.
|
|
269
|
+
"""
|
|
270
|
+
|
|
271
|
+
response: httpx.Response
|
|
272
|
+
"""The HTTP response."""
|
|
273
|
+
request_id: str | None
|
|
274
|
+
"""Value of the ``x-request-id`` response header, or ``None`` when it is absent."""
|
|
275
|
+
client_request_id: str | None
|
|
276
|
+
"""Value of the ``x-client-request-id`` response header, or ``None`` when it is absent."""
|
|
277
|
+
warnings: list[str]
|
|
278
|
+
"""Informational notices from the ``X-DEVUP-Warnings`` response header."""
|
|
279
|
+
|
|
280
|
+
def __init__(
|
|
281
|
+
self,
|
|
282
|
+
response: httpx.Response,
|
|
283
|
+
*,
|
|
284
|
+
parse: Callable[[object], T],
|
|
285
|
+
error_frame: ErrorFrame,
|
|
286
|
+
on_debug: Callable[[DebugEvent], None] | None,
|
|
287
|
+
idle_timeout: float | None,
|
|
288
|
+
) -> None:
|
|
289
|
+
"""Wraps the unread ``response``; built by the API methods."""
|
|
290
|
+
self.response = response
|
|
291
|
+
self.request_id = response.headers.get("x-request-id")
|
|
292
|
+
self.client_request_id = response.headers.get("x-client-request-id")
|
|
293
|
+
self.warnings = parse_warnings(response.headers)
|
|
294
|
+
self._frames = _Frames(response, parse, error_frame, on_debug, idle_timeout)
|
|
295
|
+
self._chunks: Iterator[bytes] | None = None
|
|
296
|
+
self._iterated = False
|
|
297
|
+
self._closed = False
|
|
298
|
+
self._finished = False
|
|
299
|
+
|
|
300
|
+
def __iter__(self) -> Iterator[T]:
|
|
301
|
+
if self._iterated:
|
|
302
|
+
raise RuntimeError(_ITERATED_MESSAGE)
|
|
303
|
+
self._iterated = True
|
|
304
|
+
return self._iterate()
|
|
305
|
+
|
|
306
|
+
def _iterate(self) -> Iterator[T]:
|
|
307
|
+
# A generator, so the response is also closed when the loop ends early: on ``break``, or when the caller's
|
|
308
|
+
# own code raises inside the loop, the generator is finalized and its ``finally`` runs.
|
|
309
|
+
try:
|
|
310
|
+
while True:
|
|
311
|
+
outcome = self._next()
|
|
312
|
+
if outcome is _END:
|
|
313
|
+
return
|
|
314
|
+
yield cast("T", outcome)
|
|
315
|
+
finally:
|
|
316
|
+
self.close()
|
|
317
|
+
|
|
318
|
+
def _next(self) -> object:
|
|
319
|
+
"""The next item, or ``_END``."""
|
|
320
|
+
frames = self._frames
|
|
321
|
+
while True:
|
|
322
|
+
if self._closed or self._finished:
|
|
323
|
+
return _END
|
|
324
|
+
if frames.events:
|
|
325
|
+
outcome = frames.handle(frames.events.popleft())
|
|
326
|
+
if outcome is _DONE:
|
|
327
|
+
self._end()
|
|
328
|
+
return _END
|
|
329
|
+
if outcome is _SKIP:
|
|
330
|
+
continue
|
|
331
|
+
return outcome
|
|
332
|
+
if frames.body_done:
|
|
333
|
+
frames.finish()
|
|
334
|
+
self._end()
|
|
335
|
+
return _END
|
|
336
|
+
chunk = self._read()
|
|
337
|
+
if self._closed:
|
|
338
|
+
return _END
|
|
339
|
+
if chunk is None:
|
|
340
|
+
frames.push(b"", final=True)
|
|
341
|
+
else:
|
|
342
|
+
frames.push(chunk)
|
|
343
|
+
|
|
344
|
+
def _read(self) -> bytes | None:
|
|
345
|
+
"""The next piece of the body, or ``None`` at its end."""
|
|
346
|
+
if self._chunks is None:
|
|
347
|
+
self._chunks = self.response.iter_bytes()
|
|
348
|
+
try:
|
|
349
|
+
return next(self._chunks)
|
|
350
|
+
except StopIteration:
|
|
351
|
+
return None
|
|
352
|
+
except _READ_ERRORS as error:
|
|
353
|
+
if self._closed:
|
|
354
|
+
return None
|
|
355
|
+
raise self._frames.read_error(error) from error
|
|
356
|
+
|
|
357
|
+
def _end(self) -> None:
|
|
358
|
+
self._finished = True
|
|
359
|
+
self.close()
|
|
360
|
+
|
|
361
|
+
def close(self) -> None:
|
|
362
|
+
"""Closes the response. Iteration then ends quietly. Calling it again does nothing."""
|
|
363
|
+
if self._closed:
|
|
364
|
+
return
|
|
365
|
+
self._closed = True
|
|
366
|
+
self.response.close()
|
|
367
|
+
|
|
368
|
+
def __del__(self) -> None:
|
|
369
|
+
# A stream dropped without being read or closed gives its connection back.
|
|
370
|
+
if getattr(self, "_closed", True):
|
|
371
|
+
return
|
|
372
|
+
try:
|
|
373
|
+
self.close()
|
|
374
|
+
except Exception:
|
|
375
|
+
# Deliberately not propagated: an exception in __del__ cannot reach the caller, and a response that
|
|
376
|
+
# cannot be closed here is released with its client.
|
|
377
|
+
return
|
|
378
|
+
|
|
379
|
+
def __enter__(self) -> Self:
|
|
380
|
+
return self
|
|
381
|
+
|
|
382
|
+
def __exit__(
|
|
383
|
+
self,
|
|
384
|
+
exc_type: type[BaseException] | None,
|
|
385
|
+
exc: BaseException | None,
|
|
386
|
+
traceback: TracebackType | None,
|
|
387
|
+
) -> None:
|
|
388
|
+
self.close()
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
class AsyncStream(Generic[T]):
|
|
392
|
+
"""A server-sent event stream of the async client. Iterate it once with ``async for``; each item is one parsed
|
|
393
|
+
event.
|
|
394
|
+
|
|
395
|
+
``request_id``, ``client_request_id`` and ``warnings`` can be read before the first item. Use it in an
|
|
396
|
+
``async with`` block, or call :meth:`close`, to close the response when you stop early: a stream dropped
|
|
397
|
+
unclosed emits a ``ResourceWarning``. Errors are raised as in :class:`Stream`.
|
|
398
|
+
"""
|
|
399
|
+
|
|
400
|
+
response: httpx.Response
|
|
401
|
+
"""The HTTP response."""
|
|
402
|
+
request_id: str | None
|
|
403
|
+
"""Value of the ``x-request-id`` response header, or ``None`` when it is absent."""
|
|
404
|
+
client_request_id: str | None
|
|
405
|
+
"""Value of the ``x-client-request-id`` response header, or ``None`` when it is absent."""
|
|
406
|
+
warnings: list[str]
|
|
407
|
+
"""Informational notices from the ``X-DEVUP-Warnings`` response header."""
|
|
408
|
+
|
|
409
|
+
def __init__(
|
|
410
|
+
self,
|
|
411
|
+
response: httpx.Response,
|
|
412
|
+
*,
|
|
413
|
+
parse: Callable[[object], T],
|
|
414
|
+
error_frame: ErrorFrame,
|
|
415
|
+
on_debug: Callable[[DebugEvent], None] | None,
|
|
416
|
+
idle_timeout: float | None,
|
|
417
|
+
) -> None:
|
|
418
|
+
"""Wraps the unread ``response``; built by the API methods."""
|
|
419
|
+
self.response = response
|
|
420
|
+
self.request_id = response.headers.get("x-request-id")
|
|
421
|
+
self.client_request_id = response.headers.get("x-client-request-id")
|
|
422
|
+
self.warnings = parse_warnings(response.headers)
|
|
423
|
+
self._frames = _Frames(response, parse, error_frame, on_debug, idle_timeout)
|
|
424
|
+
self._chunks: AsyncIterator[bytes] | None = None
|
|
425
|
+
self._iterated = False
|
|
426
|
+
self._closed = False
|
|
427
|
+
self._finished = False
|
|
428
|
+
|
|
429
|
+
def __aiter__(self) -> Self:
|
|
430
|
+
if self._iterated:
|
|
431
|
+
raise RuntimeError(_ITERATED_MESSAGE)
|
|
432
|
+
self._iterated = True
|
|
433
|
+
return self
|
|
434
|
+
|
|
435
|
+
async def __anext__(self) -> T:
|
|
436
|
+
try:
|
|
437
|
+
return await self._next()
|
|
438
|
+
except BaseException:
|
|
439
|
+
await self.close()
|
|
440
|
+
raise
|
|
441
|
+
|
|
442
|
+
async def _next(self) -> T:
|
|
443
|
+
frames = self._frames
|
|
444
|
+
while True:
|
|
445
|
+
if self._closed or self._finished:
|
|
446
|
+
raise StopAsyncIteration
|
|
447
|
+
if frames.events:
|
|
448
|
+
outcome = frames.handle(frames.events.popleft())
|
|
449
|
+
if outcome is _DONE:
|
|
450
|
+
await self._end()
|
|
451
|
+
raise StopAsyncIteration
|
|
452
|
+
if outcome is _SKIP:
|
|
453
|
+
continue
|
|
454
|
+
return cast("T", outcome)
|
|
455
|
+
if frames.body_done:
|
|
456
|
+
frames.finish()
|
|
457
|
+
await self._end()
|
|
458
|
+
raise StopAsyncIteration
|
|
459
|
+
chunk = await self._read()
|
|
460
|
+
if self._closed:
|
|
461
|
+
raise StopAsyncIteration
|
|
462
|
+
if chunk is None:
|
|
463
|
+
frames.push(b"", final=True)
|
|
464
|
+
else:
|
|
465
|
+
frames.push(chunk)
|
|
466
|
+
|
|
467
|
+
async def _read(self) -> bytes | None:
|
|
468
|
+
"""The next piece of the body, or ``None`` at its end. Each read waits at most the idle timeout."""
|
|
469
|
+
if self._chunks is None:
|
|
470
|
+
self._chunks = self.response.aiter_bytes()
|
|
471
|
+
chunks = self._chunks
|
|
472
|
+
idle = self._frames.idle_timeout
|
|
473
|
+
try:
|
|
474
|
+
if idle is None:
|
|
475
|
+
return await anext(chunks)
|
|
476
|
+
with anyio.move_on_after(idle):
|
|
477
|
+
return await anext(chunks)
|
|
478
|
+
except StopAsyncIteration:
|
|
479
|
+
return None
|
|
480
|
+
except _READ_ERRORS as error:
|
|
481
|
+
if self._closed:
|
|
482
|
+
return None
|
|
483
|
+
raise self._frames.read_error(error) from error
|
|
484
|
+
# The read did not return within the idle timeout: the scope cancelled it.
|
|
485
|
+
raise self._frames.idle_error()
|
|
486
|
+
|
|
487
|
+
async def _end(self) -> None:
|
|
488
|
+
self._finished = True
|
|
489
|
+
await self.close()
|
|
490
|
+
|
|
491
|
+
async def close(self) -> None:
|
|
492
|
+
"""Closes the response. Iteration then ends quietly. Calling it again does nothing."""
|
|
493
|
+
if self._closed:
|
|
494
|
+
return
|
|
495
|
+
self._closed = True
|
|
496
|
+
# Shielded, so the response is closed also when the surrounding task is being cancelled.
|
|
497
|
+
with anyio.CancelScope(shield=True):
|
|
498
|
+
await self.response.aclose()
|
|
499
|
+
|
|
500
|
+
def __del__(self) -> None:
|
|
501
|
+
# An async response cannot be closed from here: say so, as an unclosed file does.
|
|
502
|
+
if getattr(self, "_closed", True):
|
|
503
|
+
return
|
|
504
|
+
warnings.warn(_DROPPED_MESSAGE, ResourceWarning, stacklevel=2, source=self)
|
|
505
|
+
|
|
506
|
+
async def __aenter__(self) -> Self:
|
|
507
|
+
return self
|
|
508
|
+
|
|
509
|
+
async def __aexit__(
|
|
510
|
+
self,
|
|
511
|
+
exc_type: type[BaseException] | None,
|
|
512
|
+
exc: BaseException | None,
|
|
513
|
+
traceback: TracebackType | None,
|
|
514
|
+
) -> None:
|
|
515
|
+
await self.close()
|
devupai/_version.py
ADDED
devupai/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""The API resources of :class:`devupai.DevupAI` and :class:`devupai.AsyncDevupAI`.
|
|
2
|
+
|
|
3
|
+
Each resource with API methods has a ``with_response`` twin: the same methods, returning the whole
|
|
4
|
+
:class:`devupai.APIResponse` instead of its data.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from devupai.resources.audio import (
|
|
8
|
+
AsyncAudio,
|
|
9
|
+
AsyncSpeech,
|
|
10
|
+
AsyncSpeechWithResponse,
|
|
11
|
+
AsyncTranscriptions,
|
|
12
|
+
AsyncTranscriptionsWithResponse,
|
|
13
|
+
Audio,
|
|
14
|
+
Speech,
|
|
15
|
+
SpeechWithResponse,
|
|
16
|
+
Transcriptions,
|
|
17
|
+
TranscriptionsWithResponse,
|
|
18
|
+
)
|
|
19
|
+
from devupai.resources.balance import AsyncBalance, AsyncBalanceWithResponse, Balance, BalanceWithResponse
|
|
20
|
+
from devupai.resources.chat import (
|
|
21
|
+
AsyncChat,
|
|
22
|
+
AsyncCompletions,
|
|
23
|
+
AsyncCompletionsWithResponse,
|
|
24
|
+
Chat,
|
|
25
|
+
Completions,
|
|
26
|
+
CompletionsWithResponse,
|
|
27
|
+
)
|
|
28
|
+
from devupai.resources.embeddings import (
|
|
29
|
+
AsyncEmbeddings,
|
|
30
|
+
AsyncEmbeddingsWithResponse,
|
|
31
|
+
Embeddings,
|
|
32
|
+
EmbeddingsWithResponse,
|
|
33
|
+
)
|
|
34
|
+
from devupai.resources.health import AsyncHealth, AsyncHealthWithResponse, Health, HealthWithResponse
|
|
35
|
+
from devupai.resources.images import AsyncImages, AsyncImagesWithResponse, Images, ImagesWithResponse
|
|
36
|
+
from devupai.resources.inference import (
|
|
37
|
+
AsyncNativeInference,
|
|
38
|
+
AsyncNativeInferenceWithResponse,
|
|
39
|
+
NativeInference,
|
|
40
|
+
NativeInferenceWithResponse,
|
|
41
|
+
)
|
|
42
|
+
from devupai.resources.me import AsyncMe, AsyncMeWithResponse, Me, MeWithResponse
|
|
43
|
+
from devupai.resources.models import AsyncModels, AsyncModelsWithResponse, Models, ModelsWithResponse
|
|
44
|
+
from devupai.resources.rerank import AsyncRerank, AsyncRerankWithResponse, Rerank, RerankWithResponse
|
|
45
|
+
from devupai.resources.responses import (
|
|
46
|
+
AsyncResponses,
|
|
47
|
+
AsyncResponsesWithResponse,
|
|
48
|
+
Responses,
|
|
49
|
+
ResponsesWithResponse,
|
|
50
|
+
)
|
|
51
|
+
from devupai.resources.video import (
|
|
52
|
+
AsyncVideo,
|
|
53
|
+
AsyncVideoEdits,
|
|
54
|
+
AsyncVideoEditsWithResponse,
|
|
55
|
+
AsyncVideoGenerations,
|
|
56
|
+
AsyncVideoGenerationsWithResponse,
|
|
57
|
+
Video,
|
|
58
|
+
VideoEdits,
|
|
59
|
+
VideoEditsWithResponse,
|
|
60
|
+
VideoGenerations,
|
|
61
|
+
VideoGenerationsWithResponse,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
__all__ = [
|
|
65
|
+
# chat
|
|
66
|
+
"Chat",
|
|
67
|
+
"AsyncChat",
|
|
68
|
+
"Completions",
|
|
69
|
+
"AsyncCompletions",
|
|
70
|
+
"CompletionsWithResponse",
|
|
71
|
+
"AsyncCompletionsWithResponse",
|
|
72
|
+
# responses
|
|
73
|
+
"Responses",
|
|
74
|
+
"AsyncResponses",
|
|
75
|
+
"ResponsesWithResponse",
|
|
76
|
+
"AsyncResponsesWithResponse",
|
|
77
|
+
# models
|
|
78
|
+
"Models",
|
|
79
|
+
"AsyncModels",
|
|
80
|
+
"ModelsWithResponse",
|
|
81
|
+
"AsyncModelsWithResponse",
|
|
82
|
+
# images
|
|
83
|
+
"Images",
|
|
84
|
+
"AsyncImages",
|
|
85
|
+
"ImagesWithResponse",
|
|
86
|
+
"AsyncImagesWithResponse",
|
|
87
|
+
# embeddings
|
|
88
|
+
"Embeddings",
|
|
89
|
+
"AsyncEmbeddings",
|
|
90
|
+
"EmbeddingsWithResponse",
|
|
91
|
+
"AsyncEmbeddingsWithResponse",
|
|
92
|
+
# rerank
|
|
93
|
+
"Rerank",
|
|
94
|
+
"AsyncRerank",
|
|
95
|
+
"RerankWithResponse",
|
|
96
|
+
"AsyncRerankWithResponse",
|
|
97
|
+
# audio
|
|
98
|
+
"Audio",
|
|
99
|
+
"AsyncAudio",
|
|
100
|
+
"Speech",
|
|
101
|
+
"AsyncSpeech",
|
|
102
|
+
"SpeechWithResponse",
|
|
103
|
+
"AsyncSpeechWithResponse",
|
|
104
|
+
"Transcriptions",
|
|
105
|
+
"AsyncTranscriptions",
|
|
106
|
+
"TranscriptionsWithResponse",
|
|
107
|
+
"AsyncTranscriptionsWithResponse",
|
|
108
|
+
# video
|
|
109
|
+
"Video",
|
|
110
|
+
"AsyncVideo",
|
|
111
|
+
"VideoGenerations",
|
|
112
|
+
"AsyncVideoGenerations",
|
|
113
|
+
"VideoGenerationsWithResponse",
|
|
114
|
+
"AsyncVideoGenerationsWithResponse",
|
|
115
|
+
"VideoEdits",
|
|
116
|
+
"AsyncVideoEdits",
|
|
117
|
+
"VideoEditsWithResponse",
|
|
118
|
+
"AsyncVideoEditsWithResponse",
|
|
119
|
+
# inference
|
|
120
|
+
"NativeInference",
|
|
121
|
+
"AsyncNativeInference",
|
|
122
|
+
"NativeInferenceWithResponse",
|
|
123
|
+
"AsyncNativeInferenceWithResponse",
|
|
124
|
+
# balance, me, health
|
|
125
|
+
"Balance",
|
|
126
|
+
"AsyncBalance",
|
|
127
|
+
"BalanceWithResponse",
|
|
128
|
+
"AsyncBalanceWithResponse",
|
|
129
|
+
"Me",
|
|
130
|
+
"AsyncMe",
|
|
131
|
+
"MeWithResponse",
|
|
132
|
+
"AsyncMeWithResponse",
|
|
133
|
+
"Health",
|
|
134
|
+
"AsyncHealth",
|
|
135
|
+
"HealthWithResponse",
|
|
136
|
+
"AsyncHealthWithResponse",
|
|
137
|
+
]
|