c63a5cfe-b235-4fbe-8bbb-82a9e02a482a-python 0.1.0a7__py3-none-any.whl → 0.1.0a9__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.
- {c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a7.dist-info → c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a9.dist-info}/METADATA +1 -1
- {c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a7.dist-info → c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a9.dist-info}/RECORD +15 -13
- gradientai/_client.py +4 -0
- gradientai/_streaming.py +40 -3
- gradientai/_version.py +1 -1
- gradientai/resources/agents/chat/completions.py +530 -5
- gradientai/resources/chat/completions.py +532 -5
- gradientai/types/agents/chat/__init__.py +1 -0
- gradientai/types/agents/chat/agent_chat_completion_chunk.py +93 -0
- gradientai/types/agents/chat/completion_create_params.py +23 -8
- gradientai/types/chat/__init__.py +1 -0
- gradientai/types/chat/chat_completion_chunk.py +93 -0
- gradientai/types/chat/completion_create_params.py +23 -8
- {c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a7.dist-info → c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a9.dist-info}/WHEEL +0 -0
- {c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a7.dist-info → c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a9.dist-info}/licenses/LICENSE +0 -0
@@ -3,11 +3,12 @@
|
|
3
3
|
from __future__ import annotations
|
4
4
|
|
5
5
|
from typing import Dict, List, Union, Iterable, Optional
|
6
|
+
from typing_extensions import Literal, overload
|
6
7
|
|
7
8
|
import httpx
|
8
9
|
|
9
10
|
from ..._types import NOT_GIVEN, Body, Query, Headers, NotGiven
|
10
|
-
from ..._utils import maybe_transform, async_maybe_transform
|
11
|
+
from ..._utils import required_args, maybe_transform, async_maybe_transform
|
11
12
|
from ..._compat import cached_property
|
12
13
|
from ..._resource import SyncAPIResource, AsyncAPIResource
|
13
14
|
from ..._response import (
|
@@ -16,8 +17,10 @@ from ..._response import (
|
|
16
17
|
async_to_raw_response_wrapper,
|
17
18
|
async_to_streamed_response_wrapper,
|
18
19
|
)
|
20
|
+
from ..._streaming import Stream, AsyncStream
|
19
21
|
from ...types.chat import completion_create_params
|
20
22
|
from ..._base_client import make_request_options
|
23
|
+
from ...types.chat.chat_completion_chunk import ChatCompletionChunk
|
21
24
|
from ...types.chat.completion_create_response import CompletionCreateResponse
|
22
25
|
|
23
26
|
__all__ = ["CompletionsResource", "AsyncCompletionsResource"]
|
@@ -43,6 +46,7 @@ class CompletionsResource(SyncAPIResource):
|
|
43
46
|
"""
|
44
47
|
return CompletionsResourceWithStreamingResponse(self)
|
45
48
|
|
49
|
+
@overload
|
46
50
|
def create(
|
47
51
|
self,
|
48
52
|
*,
|
@@ -57,7 +61,7 @@ class CompletionsResource(SyncAPIResource):
|
|
57
61
|
n: Optional[int] | NotGiven = NOT_GIVEN,
|
58
62
|
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
59
63
|
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
|
60
|
-
stream: Optional[
|
64
|
+
stream: Optional[Literal[False]] | NotGiven = NOT_GIVEN,
|
61
65
|
stream_options: Optional[completion_create_params.StreamOptions] | NotGiven = NOT_GIVEN,
|
62
66
|
temperature: Optional[float] | NotGiven = NOT_GIVEN,
|
63
67
|
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
|
@@ -154,6 +158,263 @@ class CompletionsResource(SyncAPIResource):
|
|
154
158
|
timeout: Override the client-level default timeout for this request, in seconds
|
155
159
|
"""
|
156
160
|
|
161
|
+
...
|
162
|
+
|
163
|
+
@overload
|
164
|
+
def create(
|
165
|
+
self,
|
166
|
+
*,
|
167
|
+
messages: Iterable[completion_create_params.Message],
|
168
|
+
model: str,
|
169
|
+
stream: Literal[True],
|
170
|
+
frequency_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
171
|
+
logit_bias: Optional[Dict[str, int]] | NotGiven = NOT_GIVEN,
|
172
|
+
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
|
173
|
+
max_completion_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
174
|
+
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
175
|
+
metadata: Optional[Dict[str, str]] | NotGiven = NOT_GIVEN,
|
176
|
+
n: Optional[int] | NotGiven = NOT_GIVEN,
|
177
|
+
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
178
|
+
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
|
179
|
+
stream_options: Optional[completion_create_params.StreamOptions] | NotGiven = NOT_GIVEN,
|
180
|
+
temperature: Optional[float] | NotGiven = NOT_GIVEN,
|
181
|
+
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
|
182
|
+
top_p: Optional[float] | NotGiven = NOT_GIVEN,
|
183
|
+
user: str | NotGiven = NOT_GIVEN,
|
184
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
185
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
186
|
+
extra_headers: Headers | None = None,
|
187
|
+
extra_query: Query | None = None,
|
188
|
+
extra_body: Body | None = None,
|
189
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
190
|
+
) -> Stream[ChatCompletionChunk]:
|
191
|
+
"""
|
192
|
+
Creates a model response for the given chat conversation.
|
193
|
+
|
194
|
+
Args:
|
195
|
+
messages: A list of messages comprising the conversation so far.
|
196
|
+
|
197
|
+
model: Model ID used to generate the response.
|
198
|
+
|
199
|
+
stream: If set to true, the model response data will be streamed to the client as it is
|
200
|
+
generated using server-sent events.
|
201
|
+
|
202
|
+
frequency_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on their
|
203
|
+
existing frequency in the text so far, decreasing the model's likelihood to
|
204
|
+
repeat the same line verbatim.
|
205
|
+
|
206
|
+
logit_bias: Modify the likelihood of specified tokens appearing in the completion.
|
207
|
+
|
208
|
+
Accepts a JSON object that maps tokens (specified by their token ID in the
|
209
|
+
tokenizer) to an associated bias value from -100 to 100. Mathematically, the
|
210
|
+
bias is added to the logits generated by the model prior to sampling. The exact
|
211
|
+
effect will vary per model, but values between -1 and 1 should decrease or
|
212
|
+
increase likelihood of selection; values like -100 or 100 should result in a ban
|
213
|
+
or exclusive selection of the relevant token.
|
214
|
+
|
215
|
+
logprobs: Whether to return log probabilities of the output tokens or not. If true,
|
216
|
+
returns the log probabilities of each output token returned in the `content` of
|
217
|
+
`message`.
|
218
|
+
|
219
|
+
max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
|
220
|
+
run. The run will make a best effort to use only the number of completion tokens
|
221
|
+
specified, across multiple turns of the run.
|
222
|
+
|
223
|
+
max_tokens: The maximum number of tokens that can be generated in the completion.
|
224
|
+
|
225
|
+
The token count of your prompt plus `max_tokens` cannot exceed the model's
|
226
|
+
context length.
|
227
|
+
|
228
|
+
metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
|
229
|
+
for storing additional information about the object in a structured format, and
|
230
|
+
querying for objects via API or the dashboard.
|
231
|
+
|
232
|
+
Keys are strings with a maximum length of 64 characters. Values are strings with
|
233
|
+
a maximum length of 512 characters.
|
234
|
+
|
235
|
+
n: How many chat completion choices to generate for each input message. Note that
|
236
|
+
you will be charged based on the number of generated tokens across all of the
|
237
|
+
choices. Keep `n` as `1` to minimize costs.
|
238
|
+
|
239
|
+
presence_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on
|
240
|
+
whether they appear in the text so far, increasing the model's likelihood to
|
241
|
+
talk about new topics.
|
242
|
+
|
243
|
+
stop: Up to 4 sequences where the API will stop generating further tokens. The
|
244
|
+
returned text will not contain the stop sequence.
|
245
|
+
|
246
|
+
stream_options: Options for streaming response. Only set this when you set `stream: true`.
|
247
|
+
|
248
|
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
|
249
|
+
make the output more random, while lower values like 0.2 will make it more
|
250
|
+
focused and deterministic. We generally recommend altering this or `top_p` but
|
251
|
+
not both.
|
252
|
+
|
253
|
+
top_logprobs: An integer between 0 and 20 specifying the number of most likely tokens to
|
254
|
+
return at each token position, each with an associated log probability.
|
255
|
+
`logprobs` must be set to `true` if this parameter is used.
|
256
|
+
|
257
|
+
top_p: An alternative to sampling with temperature, called nucleus sampling, where the
|
258
|
+
model considers the results of the tokens with top_p probability mass. So 0.1
|
259
|
+
means only the tokens comprising the top 10% probability mass are considered.
|
260
|
+
|
261
|
+
We generally recommend altering this or `temperature` but not both.
|
262
|
+
|
263
|
+
user: A unique identifier representing your end-user, which can help DigitalOcean to
|
264
|
+
monitor and detect abuse.
|
265
|
+
|
266
|
+
extra_headers: Send extra headers
|
267
|
+
|
268
|
+
extra_query: Add additional query parameters to the request
|
269
|
+
|
270
|
+
extra_body: Add additional JSON properties to the request
|
271
|
+
|
272
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
273
|
+
"""
|
274
|
+
...
|
275
|
+
|
276
|
+
@overload
|
277
|
+
def create(
|
278
|
+
self,
|
279
|
+
*,
|
280
|
+
messages: Iterable[completion_create_params.Message],
|
281
|
+
model: str,
|
282
|
+
stream: bool,
|
283
|
+
frequency_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
284
|
+
logit_bias: Optional[Dict[str, int]] | NotGiven = NOT_GIVEN,
|
285
|
+
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
|
286
|
+
max_completion_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
287
|
+
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
288
|
+
metadata: Optional[Dict[str, str]] | NotGiven = NOT_GIVEN,
|
289
|
+
n: Optional[int] | NotGiven = NOT_GIVEN,
|
290
|
+
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
291
|
+
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
|
292
|
+
stream_options: Optional[completion_create_params.StreamOptions] | NotGiven = NOT_GIVEN,
|
293
|
+
temperature: Optional[float] | NotGiven = NOT_GIVEN,
|
294
|
+
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
|
295
|
+
top_p: Optional[float] | NotGiven = NOT_GIVEN,
|
296
|
+
user: str | NotGiven = NOT_GIVEN,
|
297
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
298
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
299
|
+
extra_headers: Headers | None = None,
|
300
|
+
extra_query: Query | None = None,
|
301
|
+
extra_body: Body | None = None,
|
302
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
303
|
+
) -> CompletionCreateResponse | Stream[ChatCompletionChunk]:
|
304
|
+
"""
|
305
|
+
Creates a model response for the given chat conversation.
|
306
|
+
|
307
|
+
Args:
|
308
|
+
messages: A list of messages comprising the conversation so far.
|
309
|
+
|
310
|
+
model: Model ID used to generate the response.
|
311
|
+
|
312
|
+
stream: If set to true, the model response data will be streamed to the client as it is
|
313
|
+
generated using server-sent events.
|
314
|
+
|
315
|
+
frequency_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on their
|
316
|
+
existing frequency in the text so far, decreasing the model's likelihood to
|
317
|
+
repeat the same line verbatim.
|
318
|
+
|
319
|
+
logit_bias: Modify the likelihood of specified tokens appearing in the completion.
|
320
|
+
|
321
|
+
Accepts a JSON object that maps tokens (specified by their token ID in the
|
322
|
+
tokenizer) to an associated bias value from -100 to 100. Mathematically, the
|
323
|
+
bias is added to the logits generated by the model prior to sampling. The exact
|
324
|
+
effect will vary per model, but values between -1 and 1 should decrease or
|
325
|
+
increase likelihood of selection; values like -100 or 100 should result in a ban
|
326
|
+
or exclusive selection of the relevant token.
|
327
|
+
|
328
|
+
logprobs: Whether to return log probabilities of the output tokens or not. If true,
|
329
|
+
returns the log probabilities of each output token returned in the `content` of
|
330
|
+
`message`.
|
331
|
+
|
332
|
+
max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
|
333
|
+
run. The run will make a best effort to use only the number of completion tokens
|
334
|
+
specified, across multiple turns of the run.
|
335
|
+
|
336
|
+
max_tokens: The maximum number of tokens that can be generated in the completion.
|
337
|
+
|
338
|
+
The token count of your prompt plus `max_tokens` cannot exceed the model's
|
339
|
+
context length.
|
340
|
+
|
341
|
+
metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
|
342
|
+
for storing additional information about the object in a structured format, and
|
343
|
+
querying for objects via API or the dashboard.
|
344
|
+
|
345
|
+
Keys are strings with a maximum length of 64 characters. Values are strings with
|
346
|
+
a maximum length of 512 characters.
|
347
|
+
|
348
|
+
n: How many chat completion choices to generate for each input message. Note that
|
349
|
+
you will be charged based on the number of generated tokens across all of the
|
350
|
+
choices. Keep `n` as `1` to minimize costs.
|
351
|
+
|
352
|
+
presence_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on
|
353
|
+
whether they appear in the text so far, increasing the model's likelihood to
|
354
|
+
talk about new topics.
|
355
|
+
|
356
|
+
stop: Up to 4 sequences where the API will stop generating further tokens. The
|
357
|
+
returned text will not contain the stop sequence.
|
358
|
+
|
359
|
+
stream_options: Options for streaming response. Only set this when you set `stream: true`.
|
360
|
+
|
361
|
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
|
362
|
+
make the output more random, while lower values like 0.2 will make it more
|
363
|
+
focused and deterministic. We generally recommend altering this or `top_p` but
|
364
|
+
not both.
|
365
|
+
|
366
|
+
top_logprobs: An integer between 0 and 20 specifying the number of most likely tokens to
|
367
|
+
return at each token position, each with an associated log probability.
|
368
|
+
`logprobs` must be set to `true` if this parameter is used.
|
369
|
+
|
370
|
+
top_p: An alternative to sampling with temperature, called nucleus sampling, where the
|
371
|
+
model considers the results of the tokens with top_p probability mass. So 0.1
|
372
|
+
means only the tokens comprising the top 10% probability mass are considered.
|
373
|
+
|
374
|
+
We generally recommend altering this or `temperature` but not both.
|
375
|
+
|
376
|
+
user: A unique identifier representing your end-user, which can help DigitalOcean to
|
377
|
+
monitor and detect abuse.
|
378
|
+
|
379
|
+
extra_headers: Send extra headers
|
380
|
+
|
381
|
+
extra_query: Add additional query parameters to the request
|
382
|
+
|
383
|
+
extra_body: Add additional JSON properties to the request
|
384
|
+
|
385
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
386
|
+
"""
|
387
|
+
...
|
388
|
+
|
389
|
+
@required_args(["messages", "model"], ["messages", "model", "stream"])
|
390
|
+
def create(
|
391
|
+
self,
|
392
|
+
*,
|
393
|
+
messages: Iterable[completion_create_params.Message],
|
394
|
+
model: str,
|
395
|
+
frequency_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
396
|
+
logit_bias: Optional[Dict[str, int]] | NotGiven = NOT_GIVEN,
|
397
|
+
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
|
398
|
+
max_completion_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
399
|
+
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
400
|
+
metadata: Optional[Dict[str, str]] | NotGiven = NOT_GIVEN,
|
401
|
+
n: Optional[int] | NotGiven = NOT_GIVEN,
|
402
|
+
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
403
|
+
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
|
404
|
+
stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,
|
405
|
+
stream_options: Optional[completion_create_params.StreamOptions] | NotGiven = NOT_GIVEN,
|
406
|
+
temperature: Optional[float] | NotGiven = NOT_GIVEN,
|
407
|
+
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
|
408
|
+
top_p: Optional[float] | NotGiven = NOT_GIVEN,
|
409
|
+
user: str | NotGiven = NOT_GIVEN,
|
410
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
411
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
412
|
+
extra_headers: Headers | None = None,
|
413
|
+
extra_query: Query | None = None,
|
414
|
+
extra_body: Body | None = None,
|
415
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
416
|
+
) -> CompletionCreateResponse | Stream[ChatCompletionChunk]:
|
417
|
+
|
157
418
|
# This method requires an inference_key to be set via client argument or environment variable
|
158
419
|
if not self._client.inference_key:
|
159
420
|
raise TypeError(
|
@@ -186,12 +447,16 @@ class CompletionsResource(SyncAPIResource):
|
|
186
447
|
"top_p": top_p,
|
187
448
|
"user": user,
|
188
449
|
},
|
189
|
-
completion_create_params.
|
450
|
+
completion_create_params.CompletionCreateParamsStreaming
|
451
|
+
if stream
|
452
|
+
else completion_create_params.CompletionCreateParamsNonStreaming,
|
190
453
|
),
|
191
454
|
options=make_request_options(
|
192
455
|
extra_headers=headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
|
193
456
|
),
|
194
457
|
cast_to=CompletionCreateResponse,
|
458
|
+
stream=stream or False,
|
459
|
+
stream_cls=Stream[ChatCompletionChunk],
|
195
460
|
)
|
196
461
|
|
197
462
|
|
@@ -215,6 +480,7 @@ class AsyncCompletionsResource(AsyncAPIResource):
|
|
215
480
|
"""
|
216
481
|
return AsyncCompletionsResourceWithStreamingResponse(self)
|
217
482
|
|
483
|
+
@overload
|
218
484
|
async def create(
|
219
485
|
self,
|
220
486
|
*,
|
@@ -229,7 +495,7 @@ class AsyncCompletionsResource(AsyncAPIResource):
|
|
229
495
|
n: Optional[int] | NotGiven = NOT_GIVEN,
|
230
496
|
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
231
497
|
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
|
232
|
-
stream: Optional[
|
498
|
+
stream: Optional[Literal[False]] | NotGiven = NOT_GIVEN,
|
233
499
|
stream_options: Optional[completion_create_params.StreamOptions] | NotGiven = NOT_GIVEN,
|
234
500
|
temperature: Optional[float] | NotGiven = NOT_GIVEN,
|
235
501
|
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
|
@@ -326,6 +592,263 @@ class AsyncCompletionsResource(AsyncAPIResource):
|
|
326
592
|
timeout: Override the client-level default timeout for this request, in seconds
|
327
593
|
"""
|
328
594
|
|
595
|
+
...
|
596
|
+
|
597
|
+
@overload
|
598
|
+
async def create(
|
599
|
+
self,
|
600
|
+
*,
|
601
|
+
messages: Iterable[completion_create_params.Message],
|
602
|
+
model: str,
|
603
|
+
stream: Literal[True],
|
604
|
+
frequency_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
605
|
+
logit_bias: Optional[Dict[str, int]] | NotGiven = NOT_GIVEN,
|
606
|
+
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
|
607
|
+
max_completion_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
608
|
+
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
609
|
+
metadata: Optional[Dict[str, str]] | NotGiven = NOT_GIVEN,
|
610
|
+
n: Optional[int] | NotGiven = NOT_GIVEN,
|
611
|
+
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
612
|
+
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
|
613
|
+
stream_options: Optional[completion_create_params.StreamOptions] | NotGiven = NOT_GIVEN,
|
614
|
+
temperature: Optional[float] | NotGiven = NOT_GIVEN,
|
615
|
+
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
|
616
|
+
top_p: Optional[float] | NotGiven = NOT_GIVEN,
|
617
|
+
user: str | NotGiven = NOT_GIVEN,
|
618
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
619
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
620
|
+
extra_headers: Headers | None = None,
|
621
|
+
extra_query: Query | None = None,
|
622
|
+
extra_body: Body | None = None,
|
623
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
624
|
+
) -> AsyncStream[ChatCompletionChunk]:
|
625
|
+
"""
|
626
|
+
Creates a model response for the given chat conversation.
|
627
|
+
|
628
|
+
Args:
|
629
|
+
messages: A list of messages comprising the conversation so far.
|
630
|
+
|
631
|
+
model: Model ID used to generate the response.
|
632
|
+
|
633
|
+
stream: If set to true, the model response data will be streamed to the client as it is
|
634
|
+
generated using server-sent events.
|
635
|
+
|
636
|
+
frequency_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on their
|
637
|
+
existing frequency in the text so far, decreasing the model's likelihood to
|
638
|
+
repeat the same line verbatim.
|
639
|
+
|
640
|
+
logit_bias: Modify the likelihood of specified tokens appearing in the completion.
|
641
|
+
|
642
|
+
Accepts a JSON object that maps tokens (specified by their token ID in the
|
643
|
+
tokenizer) to an associated bias value from -100 to 100. Mathematically, the
|
644
|
+
bias is added to the logits generated by the model prior to sampling. The exact
|
645
|
+
effect will vary per model, but values between -1 and 1 should decrease or
|
646
|
+
increase likelihood of selection; values like -100 or 100 should result in a ban
|
647
|
+
or exclusive selection of the relevant token.
|
648
|
+
|
649
|
+
logprobs: Whether to return log probabilities of the output tokens or not. If true,
|
650
|
+
returns the log probabilities of each output token returned in the `content` of
|
651
|
+
`message`.
|
652
|
+
|
653
|
+
max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
|
654
|
+
run. The run will make a best effort to use only the number of completion tokens
|
655
|
+
specified, across multiple turns of the run.
|
656
|
+
|
657
|
+
max_tokens: The maximum number of tokens that can be generated in the completion.
|
658
|
+
|
659
|
+
The token count of your prompt plus `max_tokens` cannot exceed the model's
|
660
|
+
context length.
|
661
|
+
|
662
|
+
metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
|
663
|
+
for storing additional information about the object in a structured format, and
|
664
|
+
querying for objects via API or the dashboard.
|
665
|
+
|
666
|
+
Keys are strings with a maximum length of 64 characters. Values are strings with
|
667
|
+
a maximum length of 512 characters.
|
668
|
+
|
669
|
+
n: How many chat completion choices to generate for each input message. Note that
|
670
|
+
you will be charged based on the number of generated tokens across all of the
|
671
|
+
choices. Keep `n` as `1` to minimize costs.
|
672
|
+
|
673
|
+
presence_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on
|
674
|
+
whether they appear in the text so far, increasing the model's likelihood to
|
675
|
+
talk about new topics.
|
676
|
+
|
677
|
+
stop: Up to 4 sequences where the API will stop generating further tokens. The
|
678
|
+
returned text will not contain the stop sequence.
|
679
|
+
|
680
|
+
stream_options: Options for streaming response. Only set this when you set `stream: true`.
|
681
|
+
|
682
|
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
|
683
|
+
make the output more random, while lower values like 0.2 will make it more
|
684
|
+
focused and deterministic. We generally recommend altering this or `top_p` but
|
685
|
+
not both.
|
686
|
+
|
687
|
+
top_logprobs: An integer between 0 and 20 specifying the number of most likely tokens to
|
688
|
+
return at each token position, each with an associated log probability.
|
689
|
+
`logprobs` must be set to `true` if this parameter is used.
|
690
|
+
|
691
|
+
top_p: An alternative to sampling with temperature, called nucleus sampling, where the
|
692
|
+
model considers the results of the tokens with top_p probability mass. So 0.1
|
693
|
+
means only the tokens comprising the top 10% probability mass are considered.
|
694
|
+
|
695
|
+
We generally recommend altering this or `temperature` but not both.
|
696
|
+
|
697
|
+
user: A unique identifier representing your end-user, which can help DigitalOcean to
|
698
|
+
monitor and detect abuse.
|
699
|
+
|
700
|
+
extra_headers: Send extra headers
|
701
|
+
|
702
|
+
extra_query: Add additional query parameters to the request
|
703
|
+
|
704
|
+
extra_body: Add additional JSON properties to the request
|
705
|
+
|
706
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
707
|
+
"""
|
708
|
+
...
|
709
|
+
|
710
|
+
@overload
|
711
|
+
async def create(
|
712
|
+
self,
|
713
|
+
*,
|
714
|
+
messages: Iterable[completion_create_params.Message],
|
715
|
+
model: str,
|
716
|
+
stream: bool,
|
717
|
+
frequency_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
718
|
+
logit_bias: Optional[Dict[str, int]] | NotGiven = NOT_GIVEN,
|
719
|
+
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
|
720
|
+
max_completion_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
721
|
+
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
722
|
+
metadata: Optional[Dict[str, str]] | NotGiven = NOT_GIVEN,
|
723
|
+
n: Optional[int] | NotGiven = NOT_GIVEN,
|
724
|
+
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
725
|
+
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
|
726
|
+
stream_options: Optional[completion_create_params.StreamOptions] | NotGiven = NOT_GIVEN,
|
727
|
+
temperature: Optional[float] | NotGiven = NOT_GIVEN,
|
728
|
+
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
|
729
|
+
top_p: Optional[float] | NotGiven = NOT_GIVEN,
|
730
|
+
user: str | NotGiven = NOT_GIVEN,
|
731
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
732
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
733
|
+
extra_headers: Headers | None = None,
|
734
|
+
extra_query: Query | None = None,
|
735
|
+
extra_body: Body | None = None,
|
736
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
737
|
+
) -> CompletionCreateResponse | AsyncStream[ChatCompletionChunk]:
|
738
|
+
"""
|
739
|
+
Creates a model response for the given chat conversation.
|
740
|
+
|
741
|
+
Args:
|
742
|
+
messages: A list of messages comprising the conversation so far.
|
743
|
+
|
744
|
+
model: Model ID used to generate the response.
|
745
|
+
|
746
|
+
stream: If set to true, the model response data will be streamed to the client as it is
|
747
|
+
generated using server-sent events.
|
748
|
+
|
749
|
+
frequency_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on their
|
750
|
+
existing frequency in the text so far, decreasing the model's likelihood to
|
751
|
+
repeat the same line verbatim.
|
752
|
+
|
753
|
+
logit_bias: Modify the likelihood of specified tokens appearing in the completion.
|
754
|
+
|
755
|
+
Accepts a JSON object that maps tokens (specified by their token ID in the
|
756
|
+
tokenizer) to an associated bias value from -100 to 100. Mathematically, the
|
757
|
+
bias is added to the logits generated by the model prior to sampling. The exact
|
758
|
+
effect will vary per model, but values between -1 and 1 should decrease or
|
759
|
+
increase likelihood of selection; values like -100 or 100 should result in a ban
|
760
|
+
or exclusive selection of the relevant token.
|
761
|
+
|
762
|
+
logprobs: Whether to return log probabilities of the output tokens or not. If true,
|
763
|
+
returns the log probabilities of each output token returned in the `content` of
|
764
|
+
`message`.
|
765
|
+
|
766
|
+
max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
|
767
|
+
run. The run will make a best effort to use only the number of completion tokens
|
768
|
+
specified, across multiple turns of the run.
|
769
|
+
|
770
|
+
max_tokens: The maximum number of tokens that can be generated in the completion.
|
771
|
+
|
772
|
+
The token count of your prompt plus `max_tokens` cannot exceed the model's
|
773
|
+
context length.
|
774
|
+
|
775
|
+
metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
|
776
|
+
for storing additional information about the object in a structured format, and
|
777
|
+
querying for objects via API or the dashboard.
|
778
|
+
|
779
|
+
Keys are strings with a maximum length of 64 characters. Values are strings with
|
780
|
+
a maximum length of 512 characters.
|
781
|
+
|
782
|
+
n: How many chat completion choices to generate for each input message. Note that
|
783
|
+
you will be charged based on the number of generated tokens across all of the
|
784
|
+
choices. Keep `n` as `1` to minimize costs.
|
785
|
+
|
786
|
+
presence_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on
|
787
|
+
whether they appear in the text so far, increasing the model's likelihood to
|
788
|
+
talk about new topics.
|
789
|
+
|
790
|
+
stop: Up to 4 sequences where the API will stop generating further tokens. The
|
791
|
+
returned text will not contain the stop sequence.
|
792
|
+
|
793
|
+
stream_options: Options for streaming response. Only set this when you set `stream: true`.
|
794
|
+
|
795
|
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
|
796
|
+
make the output more random, while lower values like 0.2 will make it more
|
797
|
+
focused and deterministic. We generally recommend altering this or `top_p` but
|
798
|
+
not both.
|
799
|
+
|
800
|
+
top_logprobs: An integer between 0 and 20 specifying the number of most likely tokens to
|
801
|
+
return at each token position, each with an associated log probability.
|
802
|
+
`logprobs` must be set to `true` if this parameter is used.
|
803
|
+
|
804
|
+
top_p: An alternative to sampling with temperature, called nucleus sampling, where the
|
805
|
+
model considers the results of the tokens with top_p probability mass. So 0.1
|
806
|
+
means only the tokens comprising the top 10% probability mass are considered.
|
807
|
+
|
808
|
+
We generally recommend altering this or `temperature` but not both.
|
809
|
+
|
810
|
+
user: A unique identifier representing your end-user, which can help DigitalOcean to
|
811
|
+
monitor and detect abuse.
|
812
|
+
|
813
|
+
extra_headers: Send extra headers
|
814
|
+
|
815
|
+
extra_query: Add additional query parameters to the request
|
816
|
+
|
817
|
+
extra_body: Add additional JSON properties to the request
|
818
|
+
|
819
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
820
|
+
"""
|
821
|
+
...
|
822
|
+
|
823
|
+
@required_args(["messages", "model"], ["messages", "model", "stream"])
|
824
|
+
async def create(
|
825
|
+
self,
|
826
|
+
*,
|
827
|
+
messages: Iterable[completion_create_params.Message],
|
828
|
+
model: str,
|
829
|
+
frequency_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
830
|
+
logit_bias: Optional[Dict[str, int]] | NotGiven = NOT_GIVEN,
|
831
|
+
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
|
832
|
+
max_completion_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
833
|
+
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
|
834
|
+
metadata: Optional[Dict[str, str]] | NotGiven = NOT_GIVEN,
|
835
|
+
n: Optional[int] | NotGiven = NOT_GIVEN,
|
836
|
+
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
|
837
|
+
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
|
838
|
+
stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,
|
839
|
+
stream_options: Optional[completion_create_params.StreamOptions] | NotGiven = NOT_GIVEN,
|
840
|
+
temperature: Optional[float] | NotGiven = NOT_GIVEN,
|
841
|
+
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
|
842
|
+
top_p: Optional[float] | NotGiven = NOT_GIVEN,
|
843
|
+
user: str | NotGiven = NOT_GIVEN,
|
844
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
845
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
846
|
+
extra_headers: Headers | None = None,
|
847
|
+
extra_query: Query | None = None,
|
848
|
+
extra_body: Body | None = None,
|
849
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
850
|
+
) -> CompletionCreateResponse | AsyncStream[ChatCompletionChunk]:
|
851
|
+
|
329
852
|
# This method requires an inference_key to be set via client argument or environment variable
|
330
853
|
if not hasattr(self._client, "inference_key") or not self._client.inference_key:
|
331
854
|
raise TypeError(
|
@@ -358,12 +881,16 @@ class AsyncCompletionsResource(AsyncAPIResource):
|
|
358
881
|
"top_p": top_p,
|
359
882
|
"user": user,
|
360
883
|
},
|
361
|
-
completion_create_params.
|
884
|
+
completion_create_params.CompletionCreateParamsStreaming
|
885
|
+
if stream
|
886
|
+
else completion_create_params.CompletionCreateParamsNonStreaming,
|
362
887
|
),
|
363
888
|
options=make_request_options(
|
364
889
|
extra_headers=headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
|
365
890
|
),
|
366
891
|
cast_to=CompletionCreateResponse,
|
892
|
+
stream=stream or False,
|
893
|
+
stream_cls=AsyncStream[ChatCompletionChunk],
|
367
894
|
)
|
368
895
|
|
369
896
|
|
@@ -4,3 +4,4 @@ from __future__ import annotations
|
|
4
4
|
|
5
5
|
from .completion_create_params import CompletionCreateParams as CompletionCreateParams
|
6
6
|
from .completion_create_response import CompletionCreateResponse as CompletionCreateResponse
|
7
|
+
from .agent_chat_completion_chunk import AgentChatCompletionChunk as AgentChatCompletionChunk
|