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