dashscope 1.15.0__py3-none-any.whl → 1.18.0__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.
Potentially problematic release.
This version of dashscope might be problematic. Click here for more details.
- dashscope/__init__.py +57 -10
- dashscope/aigc/code_generation.py +12 -9
- dashscope/aigc/conversation.py +3 -0
- dashscope/aigc/generation.py +3 -0
- dashscope/aigc/image_synthesis.py +26 -6
- dashscope/aigc/multimodal_conversation.py +3 -0
- dashscope/api_entities/aiohttp_request.py +1 -0
- dashscope/api_entities/api_request_factory.py +28 -12
- dashscope/api_entities/dashscope_response.py +63 -0
- dashscope/api_entities/http_request.py +21 -31
- dashscope/api_entities/websocket_request.py +3 -0
- dashscope/app/application.py +16 -18
- dashscope/assistants/__init__.py +14 -0
- dashscope/assistants/assistant_types.py +164 -0
- dashscope/assistants/assistants.py +280 -0
- dashscope/assistants/files.py +189 -0
- dashscope/audio/asr/asr_phrase_manager.py +27 -5
- dashscope/audio/asr/recognition.py +10 -2
- dashscope/audio/asr/transcription.py +21 -3
- dashscope/audio/tts/speech_synthesizer.py +3 -0
- dashscope/cli.py +7 -7
- dashscope/client/base_api.py +317 -70
- dashscope/common/base_type.py +130 -0
- dashscope/common/constants.py +1 -0
- dashscope/common/error.py +4 -0
- dashscope/common/utils.py +159 -27
- dashscope/deployment.py +40 -6
- dashscope/embeddings/batch_text_embedding.py +24 -7
- dashscope/embeddings/multimodal_embedding.py +3 -0
- dashscope/embeddings/text_embedding.py +8 -1
- dashscope/files.py +107 -0
- dashscope/finetune.py +31 -7
- dashscope/model.py +9 -2
- dashscope/models.py +47 -0
- dashscope/nlp/understanding.py +2 -2
- dashscope/rerank/__init__.py +0 -0
- dashscope/rerank/text_rerank.py +67 -0
- dashscope/threads/__init__.py +24 -0
- dashscope/threads/messages/__init__.py +0 -0
- dashscope/threads/messages/files.py +111 -0
- dashscope/threads/messages/messages.py +218 -0
- dashscope/threads/runs/__init__.py +0 -0
- dashscope/threads/runs/runs.py +483 -0
- dashscope/threads/runs/steps.py +110 -0
- dashscope/threads/thread_types.py +651 -0
- dashscope/threads/threads.py +210 -0
- dashscope/tokenizers/tokenization.py +3 -0
- dashscope/utils/oss_utils.py +11 -0
- dashscope/version.py +1 -1
- {dashscope-1.15.0.dist-info → dashscope-1.18.0.dist-info}/METADATA +2 -3
- dashscope-1.18.0.dist-info/RECORD +84 -0
- dashscope-1.15.0.dist-info/RECORD +0 -66
- {dashscope-1.15.0.dist-info → dashscope-1.18.0.dist-info}/LICENSE +0 -0
- {dashscope-1.15.0.dist-info → dashscope-1.18.0.dist-info}/WHEEL +0 -0
- {dashscope-1.15.0.dist-info → dashscope-1.18.0.dist-info}/entry_points.txt +0 -0
- {dashscope-1.15.0.dist-info → dashscope-1.18.0.dist-info}/top_level.txt +0 -0
dashscope/client/base_api.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import time
|
|
2
2
|
from http import HTTPStatus
|
|
3
|
-
from typing import List, Union
|
|
3
|
+
from typing import Any, Dict, Iterator, List, Union
|
|
4
4
|
|
|
5
|
-
import dashscope
|
|
6
5
|
import requests
|
|
6
|
+
|
|
7
|
+
import dashscope
|
|
7
8
|
from dashscope.api_entities.api_request_factory import _build_api_request
|
|
8
9
|
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
|
9
10
|
from dashscope.common.api_key import get_default_api_key
|
|
@@ -14,13 +15,9 @@ from dashscope.common.constants import (DEFAULT_REQUEST_TIMEOUT_SECONDS,
|
|
|
14
15
|
from dashscope.common.error import InvalidParameter, InvalidTask, ModelRequired
|
|
15
16
|
from dashscope.common.logging import logger
|
|
16
17
|
from dashscope.common.utils import (_handle_http_failed_response,
|
|
17
|
-
_handle_http_response,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def _normalization_url(*args):
|
|
22
|
-
url = dashscope.base_http_api_url
|
|
23
|
-
return join_url(url, *args)
|
|
18
|
+
_handle_http_response,
|
|
19
|
+
_handle_http_stream_response,
|
|
20
|
+
default_headers, join_url)
|
|
24
21
|
|
|
25
22
|
|
|
26
23
|
class BaseAioApi():
|
|
@@ -43,6 +40,7 @@ class BaseAioApi():
|
|
|
43
40
|
task: str = None,
|
|
44
41
|
function: str = None,
|
|
45
42
|
api_key: str = None,
|
|
43
|
+
workspace: str = None,
|
|
46
44
|
**kwargs) -> DashScopeAPIResponse:
|
|
47
45
|
"""Call service and get result.
|
|
48
46
|
|
|
@@ -68,6 +66,12 @@ class BaseAioApi():
|
|
|
68
66
|
DashScopeAPIResponse: The service response.
|
|
69
67
|
"""
|
|
70
68
|
api_key, model = BaseAioApi._validate_params(api_key, model)
|
|
69
|
+
if workspace is not None:
|
|
70
|
+
headers = {
|
|
71
|
+
'X-DashScope-WorkSpace': workspace,
|
|
72
|
+
**kwargs.pop('headers', {})
|
|
73
|
+
}
|
|
74
|
+
kwargs['headers'] = headers
|
|
71
75
|
request = _build_api_request(model=model,
|
|
72
76
|
input=input,
|
|
73
77
|
task_group=task_group,
|
|
@@ -99,6 +103,7 @@ class BaseApi():
|
|
|
99
103
|
task: str = None,
|
|
100
104
|
function: str = None,
|
|
101
105
|
api_key: str = None,
|
|
106
|
+
workspace: str = None,
|
|
102
107
|
**kwargs) -> DashScopeAPIResponse:
|
|
103
108
|
"""Call service and get result.
|
|
104
109
|
|
|
@@ -124,6 +129,12 @@ class BaseApi():
|
|
|
124
129
|
DashScopeAPIResponse: The service response.
|
|
125
130
|
"""
|
|
126
131
|
api_key, model = BaseApi._validate_params(api_key, model)
|
|
132
|
+
if workspace is not None:
|
|
133
|
+
headers = {
|
|
134
|
+
'X-DashScope-WorkSpace': workspace,
|
|
135
|
+
**kwargs.pop('headers', {})
|
|
136
|
+
}
|
|
137
|
+
kwargs['headers'] = headers
|
|
127
138
|
request = _build_api_request(model=model,
|
|
128
139
|
input=input,
|
|
129
140
|
task_group=task_group,
|
|
@@ -135,14 +146,36 @@ class BaseApi():
|
|
|
135
146
|
return request.call()
|
|
136
147
|
|
|
137
148
|
|
|
149
|
+
def _workspace_header(workspace) -> Dict:
|
|
150
|
+
if workspace is not None:
|
|
151
|
+
headers = {'X-DashScope-WorkSpace': workspace}
|
|
152
|
+
else:
|
|
153
|
+
headers = {}
|
|
154
|
+
return headers
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _normalization_url(base_address, *args):
|
|
158
|
+
if base_address:
|
|
159
|
+
url = base_address
|
|
160
|
+
else:
|
|
161
|
+
url = dashscope.base_http_api_url
|
|
162
|
+
return join_url(url, *args)
|
|
163
|
+
|
|
164
|
+
|
|
138
165
|
class AsyncTaskGetMixin():
|
|
139
166
|
@classmethod
|
|
140
|
-
def _get(cls,
|
|
141
|
-
|
|
167
|
+
def _get(cls,
|
|
168
|
+
task_id: str,
|
|
169
|
+
api_key: str = None,
|
|
170
|
+
workspace: str = None,
|
|
171
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
172
|
+
base_url = kwargs.pop('base_address', None)
|
|
173
|
+
status_url = _normalization_url(base_url, 'tasks', task_id)
|
|
142
174
|
with requests.Session() as session:
|
|
143
175
|
logger.debug('Starting request: %s' % status_url)
|
|
144
176
|
response = session.get(status_url,
|
|
145
177
|
headers={
|
|
178
|
+
**_workspace_header(workspace),
|
|
146
179
|
**default_headers(api_key),
|
|
147
180
|
},
|
|
148
181
|
timeout=DEFAULT_REQUEST_TIMEOUT_SECONDS)
|
|
@@ -163,11 +196,20 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
163
196
|
return api_key, model
|
|
164
197
|
|
|
165
198
|
@classmethod
|
|
166
|
-
def call(cls,
|
|
199
|
+
def call(cls,
|
|
200
|
+
*args,
|
|
201
|
+
api_key: str = None,
|
|
202
|
+
workspace: str = None,
|
|
203
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
167
204
|
"""Call service and get result.
|
|
168
205
|
"""
|
|
169
|
-
task_response = cls.async_call(*args,
|
|
170
|
-
|
|
206
|
+
task_response = cls.async_call(*args,
|
|
207
|
+
api_key=api_key,
|
|
208
|
+
workspace=workspace,
|
|
209
|
+
**kwargs)
|
|
210
|
+
response = cls.wait(task_response,
|
|
211
|
+
api_key=api_key,
|
|
212
|
+
workspace=workspace)
|
|
171
213
|
return response
|
|
172
214
|
|
|
173
215
|
@classmethod
|
|
@@ -191,6 +233,8 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
191
233
|
cls,
|
|
192
234
|
task: Union[str, DashScopeAPIResponse],
|
|
193
235
|
api_key: str = None,
|
|
236
|
+
workspace: str = None,
|
|
237
|
+
**kwargs,
|
|
194
238
|
) -> DashScopeAPIResponse:
|
|
195
239
|
"""Cancel PENDING task.
|
|
196
240
|
|
|
@@ -203,10 +247,12 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
203
247
|
DashScopeAPIResponse: The cancel result.
|
|
204
248
|
"""
|
|
205
249
|
task_id = cls._get_task_id(task)
|
|
206
|
-
|
|
250
|
+
base_url = kwargs.pop('base_address', None)
|
|
251
|
+
url = _normalization_url(base_url, 'tasks', task_id, 'cancel')
|
|
207
252
|
with requests.Session() as session:
|
|
208
253
|
response = session.post(url,
|
|
209
254
|
headers={
|
|
255
|
+
**_workspace_header(workspace),
|
|
210
256
|
**default_headers(api_key),
|
|
211
257
|
})
|
|
212
258
|
return _handle_http_response(response)
|
|
@@ -222,6 +268,7 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
222
268
|
page_no: int = 1,
|
|
223
269
|
page_size: int = 10,
|
|
224
270
|
api_key: str = None,
|
|
271
|
+
workspace: str = None,
|
|
225
272
|
**kwargs) -> DashScopeAPIResponse:
|
|
226
273
|
"""List async tasks.
|
|
227
274
|
|
|
@@ -245,7 +292,8 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
245
292
|
Returns:
|
|
246
293
|
DashScopeAPIResponse: The response data.
|
|
247
294
|
"""
|
|
248
|
-
|
|
295
|
+
base_url = kwargs.pop('base_address', None)
|
|
296
|
+
url = _normalization_url(base_url, 'tasks')
|
|
249
297
|
params = {'page_no': page_no, 'page_size': page_size}
|
|
250
298
|
if start_time is not None:
|
|
251
299
|
params['start_time'] = start_time
|
|
@@ -264,6 +312,7 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
264
312
|
response = session.get(url,
|
|
265
313
|
params=params,
|
|
266
314
|
headers={
|
|
315
|
+
**_workspace_header(workspace),
|
|
267
316
|
**default_headers(api_key),
|
|
268
317
|
})
|
|
269
318
|
if response.status_code == HTTPStatus.OK:
|
|
@@ -286,6 +335,8 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
286
335
|
cls,
|
|
287
336
|
task: Union[str, DashScopeAPIResponse],
|
|
288
337
|
api_key: str = None,
|
|
338
|
+
workspace: str = None,
|
|
339
|
+
**kwargs,
|
|
289
340
|
) -> DashScopeAPIResponse:
|
|
290
341
|
"""Query async task status.
|
|
291
342
|
|
|
@@ -298,12 +349,13 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
298
349
|
DashScopeAPIResponse: The async task information.
|
|
299
350
|
"""
|
|
300
351
|
task_id = cls._get_task_id(task)
|
|
301
|
-
return cls._get(task_id, api_key)
|
|
352
|
+
return cls._get(task_id, api_key, workspace, **kwargs)
|
|
302
353
|
|
|
303
354
|
@classmethod
|
|
304
355
|
def wait(cls,
|
|
305
356
|
task: Union[str, DashScopeAPIResponse],
|
|
306
357
|
api_key: str = None,
|
|
358
|
+
workspace: str = None,
|
|
307
359
|
**kwargs) -> DashScopeAPIResponse:
|
|
308
360
|
"""Wait for async task completion and return task result.
|
|
309
361
|
|
|
@@ -330,7 +382,7 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
330
382
|
# (server side return immediately when ready)
|
|
331
383
|
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
|
|
332
384
|
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
|
|
333
|
-
rsp = cls._get(task_id, api_key)
|
|
385
|
+
rsp = cls._get(task_id, api_key, workspace=workspace, **kwargs)
|
|
334
386
|
if rsp.status_code == HTTPStatus.OK:
|
|
335
387
|
if rsp.output is None:
|
|
336
388
|
return rsp
|
|
@@ -361,6 +413,7 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
361
413
|
task: str = None,
|
|
362
414
|
function: str = None,
|
|
363
415
|
api_key: str = None,
|
|
416
|
+
workspace: str = None,
|
|
364
417
|
**kwargs) -> DashScopeAPIResponse:
|
|
365
418
|
"""Call async service return async task information.
|
|
366
419
|
|
|
@@ -383,6 +436,12 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
383
436
|
if is_stream:
|
|
384
437
|
logger.warn('async_call do not support stream argument')
|
|
385
438
|
api_key, model = BaseApi._validate_params(api_key, model)
|
|
439
|
+
if workspace is not None:
|
|
440
|
+
headers = {
|
|
441
|
+
'X-DashScope-WorkSpace': workspace,
|
|
442
|
+
**kwargs.pop('headers', {})
|
|
443
|
+
}
|
|
444
|
+
kwargs['headers'] = headers
|
|
386
445
|
request = _build_api_request(model=model,
|
|
387
446
|
input=input,
|
|
388
447
|
task_group=task_group,
|
|
@@ -395,20 +454,83 @@ class BaseAsyncApi(AsyncTaskGetMixin):
|
|
|
395
454
|
return request.call()
|
|
396
455
|
|
|
397
456
|
|
|
398
|
-
def _get(url,
|
|
457
|
+
def _get(url,
|
|
458
|
+
params={},
|
|
459
|
+
api_key=None,
|
|
460
|
+
flattened_output=False,
|
|
461
|
+
workspace: str = None,
|
|
462
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
399
463
|
timeout = kwargs.pop(REQUEST_TIMEOUT_KEYWORD,
|
|
400
464
|
DEFAULT_REQUEST_TIMEOUT_SECONDS)
|
|
401
465
|
with requests.Session() as session:
|
|
402
466
|
logger.debug('Starting request: %s' % url)
|
|
403
467
|
response = session.get(url,
|
|
404
468
|
headers={
|
|
469
|
+
**_workspace_header(workspace),
|
|
405
470
|
**default_headers(api_key),
|
|
406
471
|
**kwargs.pop('headers', {})
|
|
407
472
|
},
|
|
408
473
|
params=params,
|
|
409
474
|
timeout=timeout)
|
|
410
475
|
logger.debug('Starting processing response: %s' % url)
|
|
411
|
-
return _handle_http_response(response)
|
|
476
|
+
return _handle_http_response(response, flattened_output)
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
def _get_url(custom_base_url, default_path, path):
|
|
480
|
+
if not custom_base_url:
|
|
481
|
+
base_url = dashscope.base_http_api_url
|
|
482
|
+
else:
|
|
483
|
+
base_url = custom_base_url
|
|
484
|
+
if path is not None:
|
|
485
|
+
url = join_url(base_url, path)
|
|
486
|
+
else:
|
|
487
|
+
url = join_url(base_url, default_path)
|
|
488
|
+
return url
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
class ListObjectMixin():
|
|
492
|
+
@classmethod
|
|
493
|
+
def list(cls,
|
|
494
|
+
limit: int = None,
|
|
495
|
+
order: str = None,
|
|
496
|
+
after: str = None,
|
|
497
|
+
before: str = None,
|
|
498
|
+
path: str = None,
|
|
499
|
+
workspace: str = None,
|
|
500
|
+
api_key: str = None,
|
|
501
|
+
**kwargs) -> Any:
|
|
502
|
+
"""List object
|
|
503
|
+
|
|
504
|
+
Args:
|
|
505
|
+
limit (int, optional): How many object to list. Defaults to None.
|
|
506
|
+
order (str, optional): The order of result. Defaults to None.
|
|
507
|
+
after (str, optional): The id of the object begin. Defaults to None.
|
|
508
|
+
before (str, optional): The if of the object end. Defaults to None.
|
|
509
|
+
path (str, optional): The request path. Defaults to None.
|
|
510
|
+
workspace (str, optional): The DashScope workspace id. Defaults to None.
|
|
511
|
+
api_key (str, optional): The DashScope api_key. Defaults to None.
|
|
512
|
+
|
|
513
|
+
Returns:
|
|
514
|
+
Any: The object list.
|
|
515
|
+
"""
|
|
516
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
517
|
+
url = _get_url(custom_base_url, cls.SUB_PATH.lower(), path)
|
|
518
|
+
params = {}
|
|
519
|
+
if limit is not None:
|
|
520
|
+
if limit < 0:
|
|
521
|
+
raise InvalidParameter('limit should >= 0')
|
|
522
|
+
params['limit'] = limit
|
|
523
|
+
if order is not None:
|
|
524
|
+
params['order'] = order
|
|
525
|
+
if after is not None:
|
|
526
|
+
params['after'] = after
|
|
527
|
+
if before is not None:
|
|
528
|
+
params['before'] = before
|
|
529
|
+
return _get(url,
|
|
530
|
+
params=params,
|
|
531
|
+
api_key=api_key,
|
|
532
|
+
workspace=workspace,
|
|
533
|
+
**kwargs)
|
|
412
534
|
|
|
413
535
|
|
|
414
536
|
class ListMixin():
|
|
@@ -417,21 +539,29 @@ class ListMixin():
|
|
|
417
539
|
page_no=1,
|
|
418
540
|
page_size=10,
|
|
419
541
|
api_key: str = None,
|
|
420
|
-
|
|
542
|
+
path: str = None,
|
|
543
|
+
workspace: str = None,
|
|
544
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
421
545
|
"""list objects
|
|
422
546
|
|
|
423
547
|
Args:
|
|
424
548
|
api_key (str, optional): The api api_key, if not present,
|
|
425
549
|
will get by default rule(TODO: api key doc). Defaults to None.
|
|
550
|
+
path (str, optional): The path of the api, if not default.
|
|
426
551
|
page_no (int, optional): Page number. Defaults to 1.
|
|
427
552
|
page_size (int, optional): Items per page. Defaults to 10.
|
|
428
553
|
|
|
429
554
|
Returns:
|
|
430
555
|
DashScopeAPIResponse: The object list in output.
|
|
431
556
|
"""
|
|
432
|
-
|
|
557
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
558
|
+
url = _get_url(custom_base_url, cls.SUB_PATH.lower(), path)
|
|
433
559
|
params = {'page_no': page_no, 'page_size': page_size}
|
|
434
|
-
return _get(url,
|
|
560
|
+
return _get(url,
|
|
561
|
+
params=params,
|
|
562
|
+
api_key=api_key,
|
|
563
|
+
workspace=workspace,
|
|
564
|
+
**kwargs)
|
|
435
565
|
|
|
436
566
|
|
|
437
567
|
class LogMixin():
|
|
@@ -441,7 +571,9 @@ class LogMixin():
|
|
|
441
571
|
offset=1,
|
|
442
572
|
line=1000,
|
|
443
573
|
api_key: str = None,
|
|
444
|
-
|
|
574
|
+
path: str = None,
|
|
575
|
+
workspace: str = None,
|
|
576
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
445
577
|
"""Get log of the job.
|
|
446
578
|
|
|
447
579
|
Args:
|
|
@@ -453,10 +585,18 @@ class LogMixin():
|
|
|
453
585
|
Returns:
|
|
454
586
|
DashScopeAPIResponse: The response
|
|
455
587
|
"""
|
|
456
|
-
|
|
457
|
-
|
|
588
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
589
|
+
if not custom_base_url:
|
|
590
|
+
url = join_url(dashscope.base_http_api_url, cls.SUB_PATH.lower(),
|
|
591
|
+
job_id, 'logs')
|
|
592
|
+
else:
|
|
593
|
+
url = custom_base_url
|
|
458
594
|
params = {'offset': offset, 'line': line}
|
|
459
|
-
return _get(url,
|
|
595
|
+
return _get(url,
|
|
596
|
+
params=params,
|
|
597
|
+
api_key=api_key,
|
|
598
|
+
workspace=workspace,
|
|
599
|
+
**kwargs)
|
|
460
600
|
|
|
461
601
|
|
|
462
602
|
class GetMixin():
|
|
@@ -465,7 +605,9 @@ class GetMixin():
|
|
|
465
605
|
target,
|
|
466
606
|
api_key: str = None,
|
|
467
607
|
params: dict = {},
|
|
468
|
-
|
|
608
|
+
path: str = None,
|
|
609
|
+
workspace: str = None,
|
|
610
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
469
611
|
"""Get object information.
|
|
470
612
|
|
|
471
613
|
Args:
|
|
@@ -476,9 +618,23 @@ class GetMixin():
|
|
|
476
618
|
Returns:
|
|
477
619
|
DashScopeAPIResponse: The object information in output.
|
|
478
620
|
"""
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
621
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
622
|
+
if custom_base_url:
|
|
623
|
+
base_url = custom_base_url
|
|
624
|
+
else:
|
|
625
|
+
base_url = dashscope.base_http_api_url
|
|
626
|
+
|
|
627
|
+
if path is not None:
|
|
628
|
+
url = join_url(base_url, path)
|
|
629
|
+
else:
|
|
630
|
+
url = join_url(base_url, cls.SUB_PATH.lower(), target)
|
|
631
|
+
flattened_output = kwargs.pop('flattened_output', False)
|
|
632
|
+
return _get(url,
|
|
633
|
+
api_key=api_key,
|
|
634
|
+
params=params,
|
|
635
|
+
flattened_output=flattened_output,
|
|
636
|
+
workspace=workspace,
|
|
637
|
+
**kwargs)
|
|
482
638
|
|
|
483
639
|
|
|
484
640
|
class GetStatusMixin():
|
|
@@ -486,7 +642,9 @@ class GetStatusMixin():
|
|
|
486
642
|
def get(cls,
|
|
487
643
|
target,
|
|
488
644
|
api_key: str = None,
|
|
489
|
-
|
|
645
|
+
path: str = None,
|
|
646
|
+
workspace: str = None,
|
|
647
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
490
648
|
"""Get object information.
|
|
491
649
|
|
|
492
650
|
Args:
|
|
@@ -497,9 +655,21 @@ class GetStatusMixin():
|
|
|
497
655
|
Returns:
|
|
498
656
|
DashScopeAPIResponse: The object information in output.
|
|
499
657
|
"""
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
658
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
659
|
+
if custom_base_url:
|
|
660
|
+
base_url = custom_base_url
|
|
661
|
+
else:
|
|
662
|
+
base_url = dashscope.base_http_api_url
|
|
663
|
+
if path is not None:
|
|
664
|
+
url = join_url(base_url, path)
|
|
665
|
+
else:
|
|
666
|
+
url = join_url(base_url, cls.SUB_PATH.lower(), target)
|
|
667
|
+
flattened_output = kwargs.pop('flattened_output', False)
|
|
668
|
+
return _get(url,
|
|
669
|
+
api_key=api_key,
|
|
670
|
+
flattened_output=flattened_output,
|
|
671
|
+
workspace=workspace,
|
|
672
|
+
**kwargs)
|
|
503
673
|
|
|
504
674
|
|
|
505
675
|
class DeleteMixin():
|
|
@@ -507,7 +677,10 @@ class DeleteMixin():
|
|
|
507
677
|
def delete(cls,
|
|
508
678
|
target: str,
|
|
509
679
|
api_key: str = None,
|
|
510
|
-
|
|
680
|
+
path: str = None,
|
|
681
|
+
workspace: str = None,
|
|
682
|
+
flattened_output=False,
|
|
683
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
511
684
|
"""Delete object.
|
|
512
685
|
|
|
513
686
|
Args:
|
|
@@ -518,20 +691,28 @@ class DeleteMixin():
|
|
|
518
691
|
Returns:
|
|
519
692
|
DashScopeAPIResponse: The delete result.
|
|
520
693
|
"""
|
|
521
|
-
|
|
522
|
-
|
|
694
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
695
|
+
if custom_base_url:
|
|
696
|
+
base_url = custom_base_url
|
|
697
|
+
else:
|
|
698
|
+
base_url = dashscope.base_http_api_url
|
|
699
|
+
if path is not None:
|
|
700
|
+
url = join_url(base_url, path)
|
|
701
|
+
else:
|
|
702
|
+
url = join_url(base_url, cls.SUB_PATH.lower(), target)
|
|
523
703
|
timeout = kwargs.pop(REQUEST_TIMEOUT_KEYWORD,
|
|
524
704
|
DEFAULT_REQUEST_TIMEOUT_SECONDS)
|
|
525
705
|
with requests.Session() as session:
|
|
526
706
|
logger.debug('Starting request: %s' % url)
|
|
527
707
|
response = session.delete(url,
|
|
528
708
|
headers={
|
|
709
|
+
**_workspace_header(workspace),
|
|
529
710
|
**default_headers(api_key),
|
|
530
711
|
**kwargs.pop('headers', {})
|
|
531
712
|
},
|
|
532
713
|
timeout=timeout)
|
|
533
714
|
logger.debug('Starting processing response: %s' % url)
|
|
534
|
-
return _handle_http_response(response)
|
|
715
|
+
return _handle_http_response(response, flattened_output)
|
|
535
716
|
|
|
536
717
|
|
|
537
718
|
class CreateMixin():
|
|
@@ -539,7 +720,10 @@ class CreateMixin():
|
|
|
539
720
|
def call(cls,
|
|
540
721
|
data: object,
|
|
541
722
|
api_key: str = None,
|
|
542
|
-
|
|
723
|
+
path: str = None,
|
|
724
|
+
stream: bool = False,
|
|
725
|
+
workspace: str = None,
|
|
726
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
543
727
|
"""Create a object
|
|
544
728
|
|
|
545
729
|
Args:
|
|
@@ -550,20 +734,33 @@ class CreateMixin():
|
|
|
550
734
|
Returns:
|
|
551
735
|
DashScopeAPIResponse: The created object in output.
|
|
552
736
|
"""
|
|
553
|
-
url =
|
|
737
|
+
url = _get_url(kwargs.pop('base_address', None), cls.SUB_PATH.lower(),
|
|
738
|
+
path)
|
|
554
739
|
timeout = kwargs.pop(REQUEST_TIMEOUT_KEYWORD,
|
|
555
740
|
DEFAULT_REQUEST_TIMEOUT_SECONDS)
|
|
741
|
+
flattened_output = kwargs.pop('flattened_output', False)
|
|
556
742
|
with requests.Session() as session:
|
|
557
743
|
logger.debug('Starting request: %s' % url)
|
|
558
744
|
response = session.post(url,
|
|
559
745
|
json=data,
|
|
746
|
+
stream=stream,
|
|
560
747
|
headers={
|
|
748
|
+
**_workspace_header(workspace),
|
|
561
749
|
**default_headers(api_key),
|
|
562
750
|
**kwargs.pop('headers', {})
|
|
563
751
|
},
|
|
564
752
|
timeout=timeout)
|
|
565
753
|
logger.debug('Starting processing response: %s' % url)
|
|
566
|
-
|
|
754
|
+
response = _handle_http_stream_response(response, flattened_output)
|
|
755
|
+
if stream:
|
|
756
|
+
return (item for item in response)
|
|
757
|
+
else:
|
|
758
|
+
_, output = next(response)
|
|
759
|
+
try:
|
|
760
|
+
next(response)
|
|
761
|
+
except StopIteration:
|
|
762
|
+
pass
|
|
763
|
+
return output
|
|
567
764
|
|
|
568
765
|
|
|
569
766
|
class UpdateMixin():
|
|
@@ -572,7 +769,10 @@ class UpdateMixin():
|
|
|
572
769
|
target: str,
|
|
573
770
|
json: object,
|
|
574
771
|
api_key: str = None,
|
|
575
|
-
|
|
772
|
+
path: str = None,
|
|
773
|
+
workspace: str = None,
|
|
774
|
+
method: str = 'patch',
|
|
775
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
576
776
|
"""Async update a object
|
|
577
777
|
|
|
578
778
|
Args:
|
|
@@ -584,21 +784,40 @@ class UpdateMixin():
|
|
|
584
784
|
Returns:
|
|
585
785
|
DashScopeAPIResponse: The updated object information in output.
|
|
586
786
|
"""
|
|
587
|
-
|
|
588
|
-
|
|
787
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
788
|
+
if custom_base_url:
|
|
789
|
+
base_url = custom_base_url
|
|
790
|
+
else:
|
|
791
|
+
base_url = dashscope.base_http_api_url
|
|
792
|
+
if path is not None:
|
|
793
|
+
url = join_url(base_url, path)
|
|
794
|
+
else:
|
|
795
|
+
url = join_url(base_url, cls.SUB_PATH.lower(), target)
|
|
589
796
|
timeout = kwargs.pop(REQUEST_TIMEOUT_KEYWORD,
|
|
590
797
|
DEFAULT_REQUEST_TIMEOUT_SECONDS)
|
|
798
|
+
flattened_output = kwargs.pop('flattened_output', False)
|
|
591
799
|
with requests.Session() as session:
|
|
592
800
|
logger.debug('Starting request: %s' % url)
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
801
|
+
if method == 'post':
|
|
802
|
+
response = session.post(url,
|
|
803
|
+
json=json,
|
|
804
|
+
headers={
|
|
805
|
+
**_workspace_header(workspace),
|
|
806
|
+
**default_headers(api_key),
|
|
807
|
+
**kwargs.pop('headers', {})
|
|
808
|
+
},
|
|
809
|
+
timeout=timeout)
|
|
810
|
+
else:
|
|
811
|
+
response = session.patch(url,
|
|
812
|
+
json=json,
|
|
813
|
+
headers={
|
|
814
|
+
**_workspace_header(workspace),
|
|
815
|
+
**default_headers(api_key),
|
|
816
|
+
**kwargs.pop('headers', {})
|
|
817
|
+
},
|
|
818
|
+
timeout=timeout)
|
|
600
819
|
logger.debug('Starting processing response: %s' % url)
|
|
601
|
-
return _handle_http_response(response)
|
|
820
|
+
return _handle_http_response(response, flattened_output)
|
|
602
821
|
|
|
603
822
|
|
|
604
823
|
class PutMixin():
|
|
@@ -606,9 +825,10 @@ class PutMixin():
|
|
|
606
825
|
def put(cls,
|
|
607
826
|
target: str,
|
|
608
827
|
json: object,
|
|
609
|
-
|
|
828
|
+
path: str = None,
|
|
610
829
|
api_key: str = None,
|
|
611
|
-
|
|
830
|
+
workspace: str = None,
|
|
831
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
612
832
|
"""Async update a object
|
|
613
833
|
|
|
614
834
|
Args:
|
|
@@ -620,13 +840,15 @@ class PutMixin():
|
|
|
620
840
|
Returns:
|
|
621
841
|
DashScopeAPIResponse: The updated object information in output.
|
|
622
842
|
"""
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
843
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
844
|
+
if custom_base_url:
|
|
845
|
+
base_url = custom_base_url
|
|
626
846
|
else:
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
847
|
+
base_url = dashscope.base_http_api_url
|
|
848
|
+
if path is None:
|
|
849
|
+
url = join_url(base_url, cls.SUB_PATH.lower(), target)
|
|
850
|
+
else:
|
|
851
|
+
url = join_url(base_url, path)
|
|
630
852
|
timeout = kwargs.pop(REQUEST_TIMEOUT_KEYWORD,
|
|
631
853
|
DEFAULT_REQUEST_TIMEOUT_SECONDS)
|
|
632
854
|
with requests.Session() as session:
|
|
@@ -634,6 +856,7 @@ class PutMixin():
|
|
|
634
856
|
response = session.put(url,
|
|
635
857
|
json=json,
|
|
636
858
|
headers={
|
|
859
|
+
**_workspace_header(workspace),
|
|
637
860
|
**default_headers(api_key),
|
|
638
861
|
**kwargs.pop('headers', {})
|
|
639
862
|
},
|
|
@@ -649,7 +872,8 @@ class FileUploadMixin():
|
|
|
649
872
|
descriptions: List[str] = None,
|
|
650
873
|
params: dict = None,
|
|
651
874
|
api_key: str = None,
|
|
652
|
-
|
|
875
|
+
workspace: str = None,
|
|
876
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
653
877
|
"""Upload files
|
|
654
878
|
|
|
655
879
|
Args:
|
|
@@ -662,7 +886,12 @@ class FileUploadMixin():
|
|
|
662
886
|
Returns:
|
|
663
887
|
DashScopeAPIResponse: The uploaded file information in the output.
|
|
664
888
|
"""
|
|
665
|
-
|
|
889
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
890
|
+
if custom_base_url:
|
|
891
|
+
base_url = custom_base_url
|
|
892
|
+
else:
|
|
893
|
+
base_url = dashscope.base_http_api_url
|
|
894
|
+
url = join_url(base_url, cls.SUB_PATH.lower())
|
|
666
895
|
js = None
|
|
667
896
|
if descriptions:
|
|
668
897
|
js = {'descriptions': descriptions}
|
|
@@ -673,6 +902,7 @@ class FileUploadMixin():
|
|
|
673
902
|
response = session.post(url,
|
|
674
903
|
data=js,
|
|
675
904
|
headers={
|
|
905
|
+
**_workspace_header(workspace),
|
|
676
906
|
**default_headers(api_key),
|
|
677
907
|
**kwargs.pop('headers', {})
|
|
678
908
|
},
|
|
@@ -686,8 +916,10 @@ class CancelMixin():
|
|
|
686
916
|
@classmethod
|
|
687
917
|
def cancel(cls,
|
|
688
918
|
target: str,
|
|
919
|
+
path: str = None,
|
|
689
920
|
api_key: str = None,
|
|
690
|
-
|
|
921
|
+
workspace: str = None,
|
|
922
|
+
**kwargs) -> Union[DashScopeAPIResponse, Dict]:
|
|
691
923
|
"""Cancel a job.
|
|
692
924
|
|
|
693
925
|
Args:
|
|
@@ -698,20 +930,29 @@ class CancelMixin():
|
|
|
698
930
|
Returns:
|
|
699
931
|
DashScopeAPIResponse: The cancel result.
|
|
700
932
|
"""
|
|
701
|
-
|
|
702
|
-
|
|
933
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
934
|
+
if custom_base_url:
|
|
935
|
+
base_url = custom_base_url
|
|
936
|
+
else:
|
|
937
|
+
base_url = dashscope.base_http_api_url
|
|
938
|
+
if not path:
|
|
939
|
+
url = join_url(base_url, cls.SUB_PATH.lower(), target, 'cancel')
|
|
940
|
+
else:
|
|
941
|
+
url = join_url(base_url, path)
|
|
703
942
|
timeout = kwargs.pop(REQUEST_TIMEOUT_KEYWORD,
|
|
704
943
|
DEFAULT_REQUEST_TIMEOUT_SECONDS)
|
|
944
|
+
flattened_output = kwargs.pop('flattened_output', False)
|
|
705
945
|
with requests.Session() as session:
|
|
706
946
|
logger.debug('Starting request: %s' % url)
|
|
707
947
|
response = session.post(url,
|
|
708
948
|
headers={
|
|
949
|
+
**_workspace_header(workspace),
|
|
709
950
|
**default_headers(api_key),
|
|
710
951
|
**kwargs.pop('headers', {})
|
|
711
952
|
},
|
|
712
953
|
timeout=timeout)
|
|
713
954
|
logger.debug('Starting processing response: %s' % url)
|
|
714
|
-
return _handle_http_response(response)
|
|
955
|
+
return _handle_http_response(response, flattened_output)
|
|
715
956
|
|
|
716
957
|
|
|
717
958
|
class StreamEventMixin():
|
|
@@ -771,7 +1012,8 @@ class StreamEventMixin():
|
|
|
771
1012
|
def stream_events(cls,
|
|
772
1013
|
target,
|
|
773
1014
|
api_key: str = None,
|
|
774
|
-
|
|
1015
|
+
workspace: str = None,
|
|
1016
|
+
**kwargs) -> Iterator[DashScopeAPIResponse]:
|
|
775
1017
|
"""Get job log.
|
|
776
1018
|
|
|
777
1019
|
Args:
|
|
@@ -782,14 +1024,19 @@ class StreamEventMixin():
|
|
|
782
1024
|
Returns:
|
|
783
1025
|
DashScopeAPIResponse: The target outputs.
|
|
784
1026
|
"""
|
|
785
|
-
|
|
786
|
-
|
|
1027
|
+
custom_base_url = kwargs.pop('base_address', None)
|
|
1028
|
+
if custom_base_url:
|
|
1029
|
+
base_url = custom_base_url
|
|
1030
|
+
else:
|
|
1031
|
+
base_url = dashscope.base_http_api_url
|
|
1032
|
+
url = join_url(base_url, cls.SUB_PATH.lower(), target, 'stream')
|
|
787
1033
|
timeout = kwargs.pop(REQUEST_TIMEOUT_KEYWORD,
|
|
788
1034
|
DEFAULT_REQUEST_TIMEOUT_SECONDS)
|
|
789
1035
|
with requests.Session() as session:
|
|
790
1036
|
logger.debug('Starting request: %s' % url)
|
|
791
1037
|
response = session.get(url,
|
|
792
1038
|
headers={
|
|
1039
|
+
**_workspace_header(workspace),
|
|
793
1040
|
**default_headers(api_key),
|
|
794
1041
|
**kwargs.pop('headers', {})
|
|
795
1042
|
},
|