dashscope 1.8.0__py3-none-any.whl → 1.25.6__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.
- dashscope/__init__.py +61 -14
- dashscope/aigc/__init__.py +10 -3
- dashscope/aigc/chat_completion.py +282 -0
- dashscope/aigc/code_generation.py +145 -0
- dashscope/aigc/conversation.py +71 -12
- dashscope/aigc/generation.py +288 -16
- dashscope/aigc/image_synthesis.py +473 -31
- dashscope/aigc/multimodal_conversation.py +299 -14
- dashscope/aigc/video_synthesis.py +610 -0
- dashscope/api_entities/aiohttp_request.py +8 -5
- dashscope/api_entities/api_request_data.py +4 -2
- dashscope/api_entities/api_request_factory.py +68 -20
- dashscope/api_entities/base_request.py +20 -3
- dashscope/api_entities/chat_completion_types.py +344 -0
- dashscope/api_entities/dashscope_response.py +243 -15
- dashscope/api_entities/encryption.py +179 -0
- dashscope/api_entities/http_request.py +216 -62
- dashscope/api_entities/websocket_request.py +43 -34
- dashscope/app/__init__.py +5 -0
- dashscope/app/application.py +203 -0
- dashscope/app/application_response.py +246 -0
- dashscope/assistants/__init__.py +16 -0
- dashscope/assistants/assistant_types.py +175 -0
- dashscope/assistants/assistants.py +311 -0
- dashscope/assistants/files.py +197 -0
- dashscope/audio/__init__.py +4 -2
- dashscope/audio/asr/__init__.py +17 -1
- dashscope/audio/asr/asr_phrase_manager.py +203 -0
- dashscope/audio/asr/recognition.py +167 -27
- dashscope/audio/asr/transcription.py +107 -14
- dashscope/audio/asr/translation_recognizer.py +1006 -0
- dashscope/audio/asr/vocabulary.py +177 -0
- dashscope/audio/qwen_asr/__init__.py +7 -0
- dashscope/audio/qwen_asr/qwen_transcription.py +189 -0
- dashscope/audio/qwen_omni/__init__.py +11 -0
- dashscope/audio/qwen_omni/omni_realtime.py +524 -0
- dashscope/audio/qwen_tts/__init__.py +5 -0
- dashscope/audio/qwen_tts/speech_synthesizer.py +77 -0
- dashscope/audio/qwen_tts_realtime/__init__.py +10 -0
- dashscope/audio/qwen_tts_realtime/qwen_tts_realtime.py +355 -0
- dashscope/audio/tts/__init__.py +2 -0
- dashscope/audio/tts/speech_synthesizer.py +5 -0
- dashscope/audio/tts_v2/__init__.py +12 -0
- dashscope/audio/tts_v2/enrollment.py +179 -0
- dashscope/audio/tts_v2/speech_synthesizer.py +886 -0
- dashscope/cli.py +157 -37
- dashscope/client/base_api.py +652 -87
- dashscope/common/api_key.py +2 -0
- dashscope/common/base_type.py +135 -0
- dashscope/common/constants.py +13 -16
- dashscope/common/env.py +2 -0
- dashscope/common/error.py +58 -22
- dashscope/common/logging.py +2 -0
- dashscope/common/message_manager.py +2 -0
- dashscope/common/utils.py +276 -46
- dashscope/customize/__init__.py +0 -0
- dashscope/customize/customize_types.py +192 -0
- dashscope/customize/deployments.py +146 -0
- dashscope/customize/finetunes.py +234 -0
- dashscope/embeddings/__init__.py +5 -1
- dashscope/embeddings/batch_text_embedding.py +208 -0
- dashscope/embeddings/batch_text_embedding_response.py +65 -0
- dashscope/embeddings/multimodal_embedding.py +118 -10
- dashscope/embeddings/text_embedding.py +13 -1
- dashscope/{file.py → files.py} +19 -4
- dashscope/io/input_output.py +2 -0
- dashscope/model.py +11 -2
- dashscope/models.py +43 -0
- dashscope/multimodal/__init__.py +20 -0
- dashscope/multimodal/dialog_state.py +56 -0
- dashscope/multimodal/multimodal_constants.py +28 -0
- dashscope/multimodal/multimodal_dialog.py +648 -0
- dashscope/multimodal/multimodal_request_params.py +313 -0
- dashscope/multimodal/tingwu/__init__.py +10 -0
- dashscope/multimodal/tingwu/tingwu.py +80 -0
- dashscope/multimodal/tingwu/tingwu_realtime.py +579 -0
- dashscope/nlp/__init__.py +0 -0
- dashscope/nlp/understanding.py +64 -0
- dashscope/protocol/websocket.py +3 -0
- dashscope/rerank/__init__.py +0 -0
- dashscope/rerank/text_rerank.py +69 -0
- dashscope/resources/qwen.tiktoken +151643 -0
- dashscope/threads/__init__.py +26 -0
- dashscope/threads/messages/__init__.py +0 -0
- dashscope/threads/messages/files.py +113 -0
- dashscope/threads/messages/messages.py +220 -0
- dashscope/threads/runs/__init__.py +0 -0
- dashscope/threads/runs/runs.py +501 -0
- dashscope/threads/runs/steps.py +112 -0
- dashscope/threads/thread_types.py +665 -0
- dashscope/threads/threads.py +212 -0
- dashscope/tokenizers/__init__.py +7 -0
- dashscope/tokenizers/qwen_tokenizer.py +111 -0
- dashscope/tokenizers/tokenization.py +125 -0
- dashscope/tokenizers/tokenizer.py +45 -0
- dashscope/tokenizers/tokenizer_base.py +32 -0
- dashscope/utils/__init__.py +0 -0
- dashscope/utils/message_utils.py +838 -0
- dashscope/utils/oss_utils.py +243 -0
- dashscope/utils/param_utils.py +29 -0
- dashscope/version.py +3 -1
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/METADATA +53 -50
- dashscope-1.25.6.dist-info/RECORD +112 -0
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/WHEEL +1 -1
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/entry_points.txt +0 -1
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info/licenses}/LICENSE +2 -4
- dashscope/deployment.py +0 -129
- dashscope/finetune.py +0 -149
- dashscope-1.8.0.dist-info/RECORD +0 -49
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
2
|
+
|
|
3
|
+
from dashscope.client.base_api import (CreateMixin, DeleteMixin, GetMixin,
|
|
4
|
+
ListMixin, PutMixin, StreamEventMixin)
|
|
5
|
+
from dashscope.customize.customize_types import (Deployment, DeploymentDelete,
|
|
6
|
+
DeploymentList)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Deployments(CreateMixin, DeleteMixin, ListMixin, GetMixin,
|
|
10
|
+
StreamEventMixin, PutMixin):
|
|
11
|
+
SUB_PATH = 'deployments'
|
|
12
|
+
"""Deploy a model.
|
|
13
|
+
"""
|
|
14
|
+
@classmethod
|
|
15
|
+
def call(cls,
|
|
16
|
+
model: str,
|
|
17
|
+
capacity: int,
|
|
18
|
+
version: str = None,
|
|
19
|
+
suffix: str = None,
|
|
20
|
+
api_key: str = None,
|
|
21
|
+
workspace: str = None,
|
|
22
|
+
**kwargs) -> Deployment:
|
|
23
|
+
"""Call to deployment a model service.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
model (str): The model name.
|
|
27
|
+
version (str, optional): The model version, unnecessary
|
|
28
|
+
for fine-tuned model. Defaults to None.
|
|
29
|
+
suffix (str, optional): The name suffix of the model deployment,
|
|
30
|
+
If specified, the final model name will be model_suffix.
|
|
31
|
+
Defaults to None.
|
|
32
|
+
capacity (int, optional): The model service capacity.
|
|
33
|
+
api_key (str, optional): The api-key. Defaults to None.
|
|
34
|
+
workspace (str): The dashscope workspace id.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
Deployment: _description_
|
|
38
|
+
"""
|
|
39
|
+
req = {'model_name': model, 'capacity': capacity}
|
|
40
|
+
|
|
41
|
+
if version is not None:
|
|
42
|
+
req['model_version'] = version
|
|
43
|
+
if suffix is not None:
|
|
44
|
+
req['suffix'] = suffix
|
|
45
|
+
response = super().call(req,
|
|
46
|
+
api_key=api_key,
|
|
47
|
+
workspace=workspace,
|
|
48
|
+
**kwargs)
|
|
49
|
+
return Deployment(**response)
|
|
50
|
+
|
|
51
|
+
@classmethod
|
|
52
|
+
def list(cls,
|
|
53
|
+
page_no=1,
|
|
54
|
+
page_size=10,
|
|
55
|
+
api_key: str = None,
|
|
56
|
+
workspace: str = None,
|
|
57
|
+
**kwargs) -> DeploymentList:
|
|
58
|
+
"""List deployments.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
api_key (str, optional): The api api_key, if not present,
|
|
62
|
+
will get by default rule(TODO: api key doc). Defaults to None.
|
|
63
|
+
page_no (int, optional): Page number. Defaults to 1.
|
|
64
|
+
page_size (int, optional): Items per page. Defaults to 10.
|
|
65
|
+
workspace (str): The dashscope workspace id.
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
Deployment: The deployment list.
|
|
69
|
+
"""
|
|
70
|
+
response = super().list(page_no=page_no,
|
|
71
|
+
page_size=page_size,
|
|
72
|
+
api_key=api_key,
|
|
73
|
+
workspace=workspace,
|
|
74
|
+
**kwargs)
|
|
75
|
+
return DeploymentList(**response)
|
|
76
|
+
|
|
77
|
+
@classmethod
|
|
78
|
+
def get(cls,
|
|
79
|
+
deployed_model: str,
|
|
80
|
+
api_key: str = None,
|
|
81
|
+
workspace: str = None,
|
|
82
|
+
**kwargs) -> Deployment:
|
|
83
|
+
"""Get model deployment information.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
deployed_model (str): The deployment_id.
|
|
87
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
88
|
+
workspace (str): The dashscope workspace id.
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
Deployment: The deployment information.
|
|
92
|
+
"""
|
|
93
|
+
response = super().get(deployed_model,
|
|
94
|
+
api_key=api_key,
|
|
95
|
+
workspace=workspace,
|
|
96
|
+
**kwargs)
|
|
97
|
+
return Deployment(**response)
|
|
98
|
+
|
|
99
|
+
@classmethod
|
|
100
|
+
def delete(cls,
|
|
101
|
+
deployed_model: str,
|
|
102
|
+
api_key: str = None,
|
|
103
|
+
workspace: str = None,
|
|
104
|
+
**kwargs) -> DeploymentDelete:
|
|
105
|
+
"""Delete model deployment.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
deployed_model (str): The deployment id.
|
|
109
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
110
|
+
workspace (str): The dashscope workspace id.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
Deployment: The delete result.
|
|
114
|
+
"""
|
|
115
|
+
response = super().delete(deployed_model,
|
|
116
|
+
api_key=api_key,
|
|
117
|
+
workspace=workspace,
|
|
118
|
+
**kwargs)
|
|
119
|
+
return DeploymentDelete(**response)
|
|
120
|
+
|
|
121
|
+
@classmethod
|
|
122
|
+
def scale(cls,
|
|
123
|
+
deployed_model: str,
|
|
124
|
+
capacity: int,
|
|
125
|
+
api_key: str = None,
|
|
126
|
+
workspace: str = None,
|
|
127
|
+
**kwargs) -> Deployment:
|
|
128
|
+
"""Scaling model deployment.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
deployment_id (str): The deployment id.
|
|
132
|
+
capacity (int): The target service capacity.
|
|
133
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
Deployment: The delete result.
|
|
137
|
+
"""
|
|
138
|
+
req = {'deployed_model': deployed_model, 'capacity': capacity}
|
|
139
|
+
path = '%s/%s/scale' % (cls.SUB_PATH.lower(), deployed_model)
|
|
140
|
+
response = super().put(deployed_model,
|
|
141
|
+
req,
|
|
142
|
+
path=path,
|
|
143
|
+
api_key=api_key,
|
|
144
|
+
workspace=workspace,
|
|
145
|
+
**kwargs)
|
|
146
|
+
return Deployment(**response)
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from http import HTTPStatus
|
|
5
|
+
from typing import Iterator, Union
|
|
6
|
+
|
|
7
|
+
from dashscope.client.base_api import (CancelMixin, CreateMixin, DeleteMixin,
|
|
8
|
+
GetStatusMixin, ListMixin, LogMixin,
|
|
9
|
+
StreamEventMixin)
|
|
10
|
+
from dashscope.common.constants import TaskStatus
|
|
11
|
+
from dashscope.customize.customize_types import (FineTune, FineTuneCancel,
|
|
12
|
+
FineTuneDelete, FineTuneEvent,
|
|
13
|
+
FineTuneList)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class FineTunes(CreateMixin, CancelMixin, DeleteMixin, ListMixin,
|
|
17
|
+
GetStatusMixin, StreamEventMixin, LogMixin):
|
|
18
|
+
SUB_PATH = 'fine-tunes'
|
|
19
|
+
|
|
20
|
+
@classmethod
|
|
21
|
+
def call(cls,
|
|
22
|
+
model: str,
|
|
23
|
+
training_file_ids: Union[list, str],
|
|
24
|
+
validation_file_ids: Union[list, str] = None,
|
|
25
|
+
mode: str = None,
|
|
26
|
+
hyper_parameters: dict = {},
|
|
27
|
+
api_key: str = None,
|
|
28
|
+
workspace: str = None,
|
|
29
|
+
**kwargs) -> FineTune:
|
|
30
|
+
"""Create fine-tune job
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
model (str): The model to be fine-tuned
|
|
34
|
+
training_file_ids (list, str): Ids of the fine-tune training data,
|
|
35
|
+
which can be pre-uploaded using the File API.
|
|
36
|
+
validation_file_ids ([list,str], optional): Ids of the fine-tune
|
|
37
|
+
validating data, which can be pre-uploaded using the File API.
|
|
38
|
+
mode (str): The fine-tune mode, sft or efficient_sft.
|
|
39
|
+
hyper_parameters (dict, optional): The fine-tune hyper parameters.
|
|
40
|
+
Defaults to empty.
|
|
41
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
42
|
+
workspace (str): The dashscope workspace id.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
FineTune: The request result.
|
|
46
|
+
"""
|
|
47
|
+
if isinstance(training_file_ids, str):
|
|
48
|
+
training_file_ids = [training_file_ids]
|
|
49
|
+
if validation_file_ids and isinstance(validation_file_ids, str):
|
|
50
|
+
validation_file_ids = [validation_file_ids]
|
|
51
|
+
request = {
|
|
52
|
+
'model': model,
|
|
53
|
+
'training_file_ids': training_file_ids,
|
|
54
|
+
'validation_file_ids': validation_file_ids,
|
|
55
|
+
'hyper_parameters': hyper_parameters if hyper_parameters else {},
|
|
56
|
+
}
|
|
57
|
+
if mode is not None:
|
|
58
|
+
request['training_type'] = mode
|
|
59
|
+
if 'finetuned_output' in kwargs:
|
|
60
|
+
request['finetuned_output'] = kwargs['finetuned_output']
|
|
61
|
+
resp = super().call(request,
|
|
62
|
+
api_key=api_key,
|
|
63
|
+
workspace=workspace,
|
|
64
|
+
**kwargs)
|
|
65
|
+
return FineTune(**resp)
|
|
66
|
+
|
|
67
|
+
@classmethod
|
|
68
|
+
def cancel(cls,
|
|
69
|
+
job_id: str,
|
|
70
|
+
api_key: str = None,
|
|
71
|
+
workspace: str = None,
|
|
72
|
+
**kwargs) -> FineTuneCancel:
|
|
73
|
+
"""Cancel a running fine-tune job.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
job_id (str): The fine-tune job id.
|
|
77
|
+
api_key (str, optional): The api api_key, can be None,
|
|
78
|
+
if None, will get by default rule(TODO: api key doc).
|
|
79
|
+
workspace (str): The dashscope workspace id.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
FineTune: The request result.
|
|
83
|
+
"""
|
|
84
|
+
rsp = super().cancel(job_id,
|
|
85
|
+
api_key=api_key,
|
|
86
|
+
workspace=workspace,
|
|
87
|
+
**kwargs)
|
|
88
|
+
return FineTuneCancel(**rsp)
|
|
89
|
+
|
|
90
|
+
@classmethod
|
|
91
|
+
def list(cls,
|
|
92
|
+
page_no=1,
|
|
93
|
+
page_size=10,
|
|
94
|
+
api_key: str = None,
|
|
95
|
+
workspace: str = None,
|
|
96
|
+
**kwargs) -> FineTuneList:
|
|
97
|
+
"""List fine-tune job.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
api_key (str, optional): The api key
|
|
101
|
+
page_no (int, optional): Page number. Defaults to 1.
|
|
102
|
+
page_size (int, optional): Items per page. Defaults to 10.
|
|
103
|
+
workspace (str): The dashscope workspace id.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
FineTune: The fine-tune jobs in the result.
|
|
107
|
+
"""
|
|
108
|
+
response = super().list(page_no=page_no,
|
|
109
|
+
page_size=page_size,
|
|
110
|
+
api_key=api_key,
|
|
111
|
+
workspace=workspace,
|
|
112
|
+
**kwargs)
|
|
113
|
+
return FineTuneList(**response)
|
|
114
|
+
|
|
115
|
+
@classmethod
|
|
116
|
+
def get(cls,
|
|
117
|
+
job_id: str,
|
|
118
|
+
api_key: str = None,
|
|
119
|
+
workspace: str = None,
|
|
120
|
+
**kwargs) -> FineTune:
|
|
121
|
+
"""Get fine-tune job information.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
job_id (str): The fine-tune job id
|
|
125
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
126
|
+
workspace (str): The dashscope workspace id.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
FineTune: The job info
|
|
130
|
+
"""
|
|
131
|
+
response = super().get(job_id,
|
|
132
|
+
api_key=api_key,
|
|
133
|
+
workspace=workspace,
|
|
134
|
+
**kwargs)
|
|
135
|
+
return FineTune(**response)
|
|
136
|
+
|
|
137
|
+
@classmethod
|
|
138
|
+
def delete(cls,
|
|
139
|
+
job_id: str,
|
|
140
|
+
api_key: str = None,
|
|
141
|
+
workspace: str = None,
|
|
142
|
+
**kwargs) -> FineTuneDelete:
|
|
143
|
+
"""Delete a fine-tune job.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
job_id (str): The fine-tune job id.
|
|
147
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
148
|
+
workspace (str): The dashscope workspace id.
|
|
149
|
+
|
|
150
|
+
Returns:
|
|
151
|
+
FineTune: The delete result.
|
|
152
|
+
"""
|
|
153
|
+
rsp = super().delete(job_id,
|
|
154
|
+
api_key=api_key,
|
|
155
|
+
workspace=workspace,
|
|
156
|
+
**kwargs)
|
|
157
|
+
return FineTuneDelete(**rsp)
|
|
158
|
+
|
|
159
|
+
@classmethod
|
|
160
|
+
def stream_events(cls,
|
|
161
|
+
job_id: str,
|
|
162
|
+
api_key: str = None,
|
|
163
|
+
workspace: str = None,
|
|
164
|
+
**kwargs) -> Iterator[FineTuneEvent]:
|
|
165
|
+
"""Get fine-tune job events.
|
|
166
|
+
|
|
167
|
+
Args:
|
|
168
|
+
job_id (str): The fine-tune job id
|
|
169
|
+
api_key (str, optional): the api key. Defaults to None.
|
|
170
|
+
workspace (str): The dashscope workspace id.
|
|
171
|
+
|
|
172
|
+
Returns:
|
|
173
|
+
FineTune: The job log events.
|
|
174
|
+
"""
|
|
175
|
+
responses = super().stream_events(job_id,
|
|
176
|
+
api_key=api_key,
|
|
177
|
+
workspace=workspace,
|
|
178
|
+
**kwargs)
|
|
179
|
+
for rsp in responses:
|
|
180
|
+
yield FineTuneEvent(**rsp)
|
|
181
|
+
|
|
182
|
+
@classmethod
|
|
183
|
+
def logs(cls,
|
|
184
|
+
job_id: str,
|
|
185
|
+
*,
|
|
186
|
+
offset=1,
|
|
187
|
+
line=1000,
|
|
188
|
+
api_key: str = None,
|
|
189
|
+
workspace: str = None,
|
|
190
|
+
**kwargs) -> FineTune:
|
|
191
|
+
"""Get log of the job.
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
job_id (str): The job id(used for fine-tune)
|
|
195
|
+
offset (int, optional): start log line. Defaults to 1.
|
|
196
|
+
line (int, optional): total line return. Defaults to 1000.
|
|
197
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
198
|
+
workspace (str): The dashscope workspace id.
|
|
199
|
+
|
|
200
|
+
Returns:
|
|
201
|
+
FineTune: The response
|
|
202
|
+
"""
|
|
203
|
+
return super().logs(job_id,
|
|
204
|
+
offset=offset,
|
|
205
|
+
line=line,
|
|
206
|
+
workspace=workspace,
|
|
207
|
+
api_key=api_key)
|
|
208
|
+
|
|
209
|
+
@classmethod
|
|
210
|
+
def wait(cls,
|
|
211
|
+
job_id: str,
|
|
212
|
+
api_key: str = None,
|
|
213
|
+
workspace: str = None,
|
|
214
|
+
**kwargs):
|
|
215
|
+
try:
|
|
216
|
+
while True:
|
|
217
|
+
rsp = FineTunes.get(job_id,
|
|
218
|
+
api_key=api_key,
|
|
219
|
+
workspace=workspace,
|
|
220
|
+
**kwargs)
|
|
221
|
+
if rsp.status_code == HTTPStatus.OK:
|
|
222
|
+
if rsp.output['status'] in [
|
|
223
|
+
TaskStatus.FAILED, TaskStatus.CANCELED,
|
|
224
|
+
TaskStatus.SUCCEEDED
|
|
225
|
+
]:
|
|
226
|
+
return rsp
|
|
227
|
+
else:
|
|
228
|
+
time.sleep(30)
|
|
229
|
+
else:
|
|
230
|
+
return rsp
|
|
231
|
+
except Exception:
|
|
232
|
+
raise Exception(
|
|
233
|
+
'You can stream output via: dashscope fine_tunes.stream -j %s'
|
|
234
|
+
% job_id)
|
dashscope/embeddings/__init__.py
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
2
|
+
|
|
3
|
+
from .batch_text_embedding import BatchTextEmbedding
|
|
4
|
+
from .batch_text_embedding_response import BatchTextEmbeddingResponse
|
|
1
5
|
from .text_embedding import TextEmbedding
|
|
2
6
|
|
|
3
|
-
__all__ = [TextEmbedding]
|
|
7
|
+
__all__ = [TextEmbedding, BatchTextEmbedding, BatchTextEmbeddingResponse]
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
2
|
+
|
|
3
|
+
from typing import Union
|
|
4
|
+
|
|
5
|
+
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
|
6
|
+
from dashscope.client.base_api import BaseAsyncApi
|
|
7
|
+
from dashscope.common.error import InputRequired
|
|
8
|
+
from dashscope.common.utils import _get_task_group_and_task
|
|
9
|
+
from dashscope.embeddings.batch_text_embedding_response import \
|
|
10
|
+
BatchTextEmbeddingResponse
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BatchTextEmbedding(BaseAsyncApi):
|
|
14
|
+
task = 'text-embedding'
|
|
15
|
+
function = 'text-embedding'
|
|
16
|
+
"""API for async text embedding.
|
|
17
|
+
"""
|
|
18
|
+
class Models:
|
|
19
|
+
text_embedding_async_v1 = 'text-embedding-async-v1'
|
|
20
|
+
text_embedding_async_v2 = 'text-embedding-async-v2'
|
|
21
|
+
|
|
22
|
+
@classmethod
|
|
23
|
+
def call(cls,
|
|
24
|
+
model: str,
|
|
25
|
+
url: str,
|
|
26
|
+
api_key: str = None,
|
|
27
|
+
workspace: str = None,
|
|
28
|
+
**kwargs) -> BatchTextEmbeddingResponse:
|
|
29
|
+
"""Call async text embedding service and get result.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
model (str): The model, reference ``Models``.
|
|
33
|
+
url (Any): The async request file url, which contains text
|
|
34
|
+
to embedding line by line.
|
|
35
|
+
api_key (str, optional): The api api_key. Defaults to None.
|
|
36
|
+
workspace (str): The dashscope workspace id.
|
|
37
|
+
**kwargs:
|
|
38
|
+
text_type(str, `optional`): [query|document], After the
|
|
39
|
+
text is converted into a vector, it can be applied to
|
|
40
|
+
downstream tasks such as retrieval, clustering, and
|
|
41
|
+
classification. For asymmetric tasks such as retrieval,
|
|
42
|
+
in order to achieve better retrieval results, it is
|
|
43
|
+
recommended to distinguish between query text (query)
|
|
44
|
+
and bottom database text (document) types, clustering
|
|
45
|
+
Symmetric tasks such as , classification, etc. do not
|
|
46
|
+
need to be specially specified, and the system
|
|
47
|
+
default value "document" can be used
|
|
48
|
+
Raises:
|
|
49
|
+
InputRequired: The url cannot be empty.
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
AsyncTextEmbeddingResponse: The async text embedding task result.
|
|
53
|
+
"""
|
|
54
|
+
return super().call(model,
|
|
55
|
+
url,
|
|
56
|
+
api_key=api_key,
|
|
57
|
+
workspace=workspace,
|
|
58
|
+
**kwargs)
|
|
59
|
+
|
|
60
|
+
@classmethod
|
|
61
|
+
def async_call(cls,
|
|
62
|
+
model: str,
|
|
63
|
+
url: str,
|
|
64
|
+
api_key: str = None,
|
|
65
|
+
workspace: str = None,
|
|
66
|
+
**kwargs) -> BatchTextEmbeddingResponse:
|
|
67
|
+
"""Create a async text embedding task, and return task information.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
model (str): The model, reference ``Models``.
|
|
71
|
+
url (Any): The async request file url, which contains text
|
|
72
|
+
to embedding line by line.
|
|
73
|
+
api_key (str, optional): The api api_key. Defaults to None.
|
|
74
|
+
workspace (str): The dashscope workspace id.
|
|
75
|
+
**kwargs:
|
|
76
|
+
text_type(str, `optional`): [query|document], After the
|
|
77
|
+
text is converted into a vector, it can be applied to
|
|
78
|
+
downstream tasks such as retrieval, clustering, and
|
|
79
|
+
classification. For asymmetric tasks such as retrieval,
|
|
80
|
+
in order to achieve better retrieval results, it is
|
|
81
|
+
recommended to distinguish between query text (query)
|
|
82
|
+
and bottom database text (document) types, clustering
|
|
83
|
+
Symmetric tasks such as , classification, etc. do not
|
|
84
|
+
need to be specially specified, and the system
|
|
85
|
+
default value "document" can be used
|
|
86
|
+
|
|
87
|
+
Raises:
|
|
88
|
+
InputRequired: The url cannot be empty.
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
DashScopeAPIResponse: The image synthesis
|
|
92
|
+
task id in the response.
|
|
93
|
+
"""
|
|
94
|
+
if url is None or not url:
|
|
95
|
+
raise InputRequired('url is required!')
|
|
96
|
+
input = {'url': url}
|
|
97
|
+
task_group, _ = _get_task_group_and_task(__name__)
|
|
98
|
+
response = super().async_call(model=model,
|
|
99
|
+
task_group=task_group,
|
|
100
|
+
task=BatchTextEmbedding.task,
|
|
101
|
+
function=BatchTextEmbedding.function,
|
|
102
|
+
api_key=api_key,
|
|
103
|
+
input=input,
|
|
104
|
+
workspace=workspace,
|
|
105
|
+
**kwargs)
|
|
106
|
+
return BatchTextEmbeddingResponse.from_api_response(response)
|
|
107
|
+
|
|
108
|
+
@classmethod
|
|
109
|
+
def fetch(cls,
|
|
110
|
+
task: Union[str, BatchTextEmbeddingResponse],
|
|
111
|
+
api_key: str = None,
|
|
112
|
+
workspace: str = None) -> BatchTextEmbeddingResponse:
|
|
113
|
+
"""Fetch async text embedding task status or result.
|
|
114
|
+
|
|
115
|
+
Args:
|
|
116
|
+
task (Union[str, AsyncTextEmbeddingResponse]): The task_id or
|
|
117
|
+
AsyncTextEmbeddingResponse return by async_call().
|
|
118
|
+
api_key (str, optional): The api api_key. Defaults to None.
|
|
119
|
+
workspace (str): The dashscope workspace id.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
AsyncTextEmbeddingResponse: The task status or result.
|
|
123
|
+
"""
|
|
124
|
+
response = super().fetch(task, api_key, workspace=workspace)
|
|
125
|
+
return BatchTextEmbeddingResponse.from_api_response(response)
|
|
126
|
+
|
|
127
|
+
@classmethod
|
|
128
|
+
def wait(cls,
|
|
129
|
+
task: Union[str, BatchTextEmbeddingResponse],
|
|
130
|
+
api_key: str = None,
|
|
131
|
+
workspace: str = None) -> BatchTextEmbeddingResponse:
|
|
132
|
+
"""Wait for async text embedding task to complete, and return the result.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
task (Union[str, AsyncTextEmbeddingResponse]): The task_id or
|
|
136
|
+
AsyncTextEmbeddingResponse return by async_call().
|
|
137
|
+
api_key (str, optional): The api api_key. Defaults to None.
|
|
138
|
+
workspace (str): The dashscope workspace id.
|
|
139
|
+
|
|
140
|
+
Returns:
|
|
141
|
+
AsyncTextEmbeddingResponse: The task result.
|
|
142
|
+
"""
|
|
143
|
+
response = super().wait(task, api_key, workspace=workspace)
|
|
144
|
+
return BatchTextEmbeddingResponse.from_api_response(response)
|
|
145
|
+
|
|
146
|
+
@classmethod
|
|
147
|
+
def cancel(cls,
|
|
148
|
+
task: Union[str, BatchTextEmbeddingResponse],
|
|
149
|
+
api_key: str = None,
|
|
150
|
+
workspace: str = None) -> DashScopeAPIResponse:
|
|
151
|
+
"""Cancel async text embedding task.
|
|
152
|
+
Only tasks whose status is PENDING can be canceled.
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
task (Union[str, AsyncTextEmbeddingResponse]): The task_id or
|
|
156
|
+
AsyncTextEmbeddingResponse return by async_call().
|
|
157
|
+
api_key (str, optional): The api api_key. Defaults to None.
|
|
158
|
+
workspace (str): The dashscope workspace id.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
DashScopeAPIResponse: The response data.
|
|
162
|
+
"""
|
|
163
|
+
return super().cancel(task, api_key, workspace=workspace)
|
|
164
|
+
|
|
165
|
+
@classmethod
|
|
166
|
+
def list(cls,
|
|
167
|
+
start_time: str = None,
|
|
168
|
+
end_time: str = None,
|
|
169
|
+
model_name: str = None,
|
|
170
|
+
api_key_id: str = None,
|
|
171
|
+
region: str = None,
|
|
172
|
+
status: str = None,
|
|
173
|
+
page_no: int = 1,
|
|
174
|
+
page_size: int = 10,
|
|
175
|
+
api_key: str = None,
|
|
176
|
+
workspace: str = None,
|
|
177
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
178
|
+
"""List async tasks.
|
|
179
|
+
|
|
180
|
+
Args:
|
|
181
|
+
start_time (str, optional): The tasks start time,
|
|
182
|
+
for example: 20230420000000. Defaults to None.
|
|
183
|
+
end_time (str, optional): The tasks end time,
|
|
184
|
+
for example: 20230420000000. Defaults to None.
|
|
185
|
+
model_name (str, optional): The tasks model name. Defaults to None.
|
|
186
|
+
api_key_id (str, optional): The tasks api-key-id. Defaults to None.
|
|
187
|
+
region (str, optional): The service region,
|
|
188
|
+
for example: cn-beijing. Defaults to None.
|
|
189
|
+
status (str, optional): The status of tasks[PENDING,
|
|
190
|
+
RUNNING, SUCCEEDED, FAILED, CANCELED]. Defaults to None.
|
|
191
|
+
page_no (int, optional): The page number. Defaults to 1.
|
|
192
|
+
page_size (int, optional): The page size. Defaults to 10.
|
|
193
|
+
api_key (str, optional): The user api-key. Defaults to None.
|
|
194
|
+
|
|
195
|
+
Returns:
|
|
196
|
+
DashScopeAPIResponse: The response data.
|
|
197
|
+
"""
|
|
198
|
+
return super().list(start_time=start_time,
|
|
199
|
+
end_time=end_time,
|
|
200
|
+
model_name=model_name,
|
|
201
|
+
api_key_id=api_key_id,
|
|
202
|
+
region=region,
|
|
203
|
+
status=status,
|
|
204
|
+
page_no=page_no,
|
|
205
|
+
page_size=page_size,
|
|
206
|
+
api_key=api_key,
|
|
207
|
+
workspace=workspace,
|
|
208
|
+
**kwargs)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
2
|
+
|
|
3
|
+
from http import HTTPStatus
|
|
4
|
+
|
|
5
|
+
from attr import dataclass
|
|
6
|
+
|
|
7
|
+
from dashscope.api_entities.dashscope_response import (DashScopeAPIResponse,
|
|
8
|
+
DictMixin)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(init=False)
|
|
12
|
+
class BatchTextEmbeddingOutput(DictMixin):
|
|
13
|
+
task_id: str
|
|
14
|
+
task_status: str
|
|
15
|
+
url: str
|
|
16
|
+
|
|
17
|
+
def __init__(self,
|
|
18
|
+
task_id: str,
|
|
19
|
+
task_status: str,
|
|
20
|
+
url: str = None,
|
|
21
|
+
**kwargs):
|
|
22
|
+
super().__init__(self,
|
|
23
|
+
task_id=task_id,
|
|
24
|
+
task_status=task_status,
|
|
25
|
+
url=url,
|
|
26
|
+
**kwargs)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(init=False)
|
|
30
|
+
class BatchTextEmbeddingUsage(DictMixin):
|
|
31
|
+
total_tokens: int
|
|
32
|
+
|
|
33
|
+
def __init__(self, total_tokens: int=None, **kwargs):
|
|
34
|
+
super().__init__(total_tokens=total_tokens, **kwargs)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(init=False)
|
|
38
|
+
class BatchTextEmbeddingResponse(DashScopeAPIResponse):
|
|
39
|
+
output: BatchTextEmbeddingOutput
|
|
40
|
+
usage: BatchTextEmbeddingUsage
|
|
41
|
+
|
|
42
|
+
@staticmethod
|
|
43
|
+
def from_api_response(api_response: DashScopeAPIResponse):
|
|
44
|
+
if api_response.status_code == HTTPStatus.OK:
|
|
45
|
+
output = None
|
|
46
|
+
usage = None
|
|
47
|
+
if api_response.output is not None:
|
|
48
|
+
output = BatchTextEmbeddingOutput(**api_response.output)
|
|
49
|
+
if api_response.usage is not None:
|
|
50
|
+
usage = BatchTextEmbeddingUsage(**api_response.usage)
|
|
51
|
+
|
|
52
|
+
return BatchTextEmbeddingResponse(
|
|
53
|
+
status_code=api_response.status_code,
|
|
54
|
+
request_id=api_response.request_id,
|
|
55
|
+
code=api_response.code,
|
|
56
|
+
message=api_response.message,
|
|
57
|
+
output=output,
|
|
58
|
+
usage=usage)
|
|
59
|
+
|
|
60
|
+
else:
|
|
61
|
+
return BatchTextEmbeddingResponse(
|
|
62
|
+
status_code=api_response.status_code,
|
|
63
|
+
request_id=api_response.request_id,
|
|
64
|
+
code=api_response.code,
|
|
65
|
+
message=api_response.message)
|