dashscope 1.20.7__py3-none-any.whl → 1.20.8__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/deployment.py +163 -0
- dashscope/file.py +94 -0
- dashscope/finetune.py +175 -0
- dashscope/resources/qwen.tiktoken +151643 -0
- dashscope/version.py +1 -1
- {dashscope-1.20.7.dist-info → dashscope-1.20.8.dist-info}/METADATA +3 -2
- {dashscope-1.20.7.dist-info → dashscope-1.20.8.dist-info}/RECORD +11 -7
- {dashscope-1.20.7.dist-info → dashscope-1.20.8.dist-info}/entry_points.txt +1 -0
- {dashscope-1.20.7.dist-info → dashscope-1.20.8.dist-info}/LICENSE +0 -0
- {dashscope-1.20.7.dist-info → dashscope-1.20.8.dist-info}/WHEEL +0 -0
- {dashscope-1.20.7.dist-info → dashscope-1.20.8.dist-info}/top_level.txt +0 -0
dashscope/deployment.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
|
2
|
+
from dashscope.client.base_api import (CreateMixin, DeleteMixin, GetMixin,
|
|
3
|
+
ListMixin, PutMixin, StreamEventMixin)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Deployment(CreateMixin, DeleteMixin, ListMixin, GetMixin,
|
|
7
|
+
StreamEventMixin, PutMixin):
|
|
8
|
+
SUB_PATH = 'deployments'
|
|
9
|
+
"""Deploy a model.
|
|
10
|
+
"""
|
|
11
|
+
@classmethod
|
|
12
|
+
def call(cls,
|
|
13
|
+
model: str,
|
|
14
|
+
capacity: int,
|
|
15
|
+
version: str = None,
|
|
16
|
+
suffix: str = None,
|
|
17
|
+
api_key: str = None,
|
|
18
|
+
workspace: str = None,
|
|
19
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
20
|
+
"""Call to deployment a model service.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
model (str): The model name.
|
|
24
|
+
version (str, optional): The model version, unnecessary
|
|
25
|
+
for fine-tuned model. Defaults to None.
|
|
26
|
+
suffix (str, optional): The name suffix of the model deployment,
|
|
27
|
+
If specified, the final model name will be model_suffix.
|
|
28
|
+
Defaults to None.
|
|
29
|
+
capacity (int, optional): The model service capacity.
|
|
30
|
+
api_key (str, optional): The api-key. Defaults to None.
|
|
31
|
+
workspace (str): The dashscope workspace id.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
DashScopeAPIResponse: _description_
|
|
35
|
+
"""
|
|
36
|
+
req = {'model_name': model, 'capacity': capacity}
|
|
37
|
+
|
|
38
|
+
if version is not None:
|
|
39
|
+
req['model_version'] = version
|
|
40
|
+
if suffix is not None:
|
|
41
|
+
req['suffix'] = suffix
|
|
42
|
+
return super().call(req,
|
|
43
|
+
api_key=api_key,
|
|
44
|
+
workspace=workspace,
|
|
45
|
+
**kwargs)
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def list(cls,
|
|
49
|
+
page_no=1,
|
|
50
|
+
page_size=10,
|
|
51
|
+
api_key: str = None,
|
|
52
|
+
workspace: str = None,
|
|
53
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
54
|
+
"""List deployments.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
api_key (str, optional): The api api_key, if not present,
|
|
58
|
+
will get by default rule(TODO: api key doc). Defaults to None.
|
|
59
|
+
page_no (int, optional): Page number. Defaults to 1.
|
|
60
|
+
page_size (int, optional): Items per page. Defaults to 10.
|
|
61
|
+
workspace (str): The dashscope workspace id.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
DashScopeAPIResponse: The deployment list.
|
|
65
|
+
"""
|
|
66
|
+
return super().list(page_no,
|
|
67
|
+
page_size,
|
|
68
|
+
api_key,
|
|
69
|
+
workspace=workspace,
|
|
70
|
+
**kwargs)
|
|
71
|
+
|
|
72
|
+
@classmethod
|
|
73
|
+
def get(cls,
|
|
74
|
+
deployed_model: str,
|
|
75
|
+
api_key: str = None,
|
|
76
|
+
workspace: str = None,
|
|
77
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
78
|
+
"""Get model deployment information.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
deployed_model (str): The deployment_id.
|
|
82
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
83
|
+
workspace (str): The dashscope workspace id.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
DashScopeAPIResponse: The deployment information.
|
|
87
|
+
"""
|
|
88
|
+
return super().get(deployed_model,
|
|
89
|
+
api_key,
|
|
90
|
+
workspace=workspace,
|
|
91
|
+
**kwargs)
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
def delete(cls,
|
|
95
|
+
deployment_id: str,
|
|
96
|
+
api_key: str = None,
|
|
97
|
+
workspace: str = None,
|
|
98
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
99
|
+
"""Delete model deployment.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
deployment_id (str): The deployment id.
|
|
103
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
104
|
+
workspace (str): The dashscope workspace id.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
DashScopeAPIResponse: The delete result.
|
|
108
|
+
"""
|
|
109
|
+
return super().delete(deployment_id,
|
|
110
|
+
api_key,
|
|
111
|
+
workspace=workspace,
|
|
112
|
+
**kwargs)
|
|
113
|
+
|
|
114
|
+
@classmethod
|
|
115
|
+
def update(cls,
|
|
116
|
+
deployment_id: str,
|
|
117
|
+
version: str,
|
|
118
|
+
api_key: str = None,
|
|
119
|
+
workspace: str = None,
|
|
120
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
121
|
+
"""Update model deployment.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
deployment_id (str): The deployment id.
|
|
125
|
+
version (str): The target model version.
|
|
126
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
127
|
+
workspace (str): The dashscope workspace id.
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
DashScopeAPIResponse: The delete result.
|
|
131
|
+
"""
|
|
132
|
+
req = {'deployment_model': deployment_id, 'model_version': version}
|
|
133
|
+
return super().put(deployment_id,
|
|
134
|
+
req,
|
|
135
|
+
api_key,
|
|
136
|
+
workspace=workspace,
|
|
137
|
+
**kwargs)
|
|
138
|
+
|
|
139
|
+
@classmethod
|
|
140
|
+
def scale(cls,
|
|
141
|
+
deployment_id: str,
|
|
142
|
+
capacity: int,
|
|
143
|
+
api_key: str = None,
|
|
144
|
+
workspace: str = None,
|
|
145
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
146
|
+
"""Scaling model deployment.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
deployment_id (str): The deployment id.
|
|
150
|
+
capacity (int): The target service capacity.
|
|
151
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
DashScopeAPIResponse: The delete result.
|
|
155
|
+
"""
|
|
156
|
+
req = {'deployed_model': deployment_id, 'capacity': capacity}
|
|
157
|
+
path = '%s/%s/scale' % (cls.SUB_PATH.lower(), deployment_id)
|
|
158
|
+
return super().put(deployment_id,
|
|
159
|
+
req,
|
|
160
|
+
path=path,
|
|
161
|
+
api_key=api_key,
|
|
162
|
+
workspace=workspace,
|
|
163
|
+
**kwargs)
|
dashscope/file.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
|
4
|
+
from dashscope.client.base_api import (DeleteMixin, FileUploadMixin, GetMixin,
|
|
5
|
+
ListMixin)
|
|
6
|
+
from dashscope.common.constants import FilePurpose
|
|
7
|
+
from dashscope.common.error import InvalidFileFormat
|
|
8
|
+
from dashscope.common.utils import is_validate_fine_tune_file
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class File(FileUploadMixin, ListMixin, DeleteMixin, GetMixin):
|
|
12
|
+
SUB_PATH = 'files'
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def upload(cls,
|
|
16
|
+
file_path: str,
|
|
17
|
+
purpose: str = FilePurpose.fine_tune,
|
|
18
|
+
description: str = None,
|
|
19
|
+
api_key: str = None,
|
|
20
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
21
|
+
"""Upload file for model fine-tune or other tasks.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
file_path (str): The local file name to upload.
|
|
25
|
+
purpose (str): The purpose of the file[fine-tune|inference]
|
|
26
|
+
description (str, optional): The file description message.
|
|
27
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
DashScopeAPIResponse: The upload information
|
|
31
|
+
"""
|
|
32
|
+
if purpose == FilePurpose.fine_tune:
|
|
33
|
+
if not is_validate_fine_tune_file(file_path):
|
|
34
|
+
raise InvalidFileFormat(
|
|
35
|
+
'The file %s is not in valid jsonl format' % file_path)
|
|
36
|
+
with open(file_path, 'rb') as f:
|
|
37
|
+
return super().upload(files=[('files', (os.path.basename(f.name),
|
|
38
|
+
f, None))],
|
|
39
|
+
descriptions=[description]
|
|
40
|
+
if description is not None else None,
|
|
41
|
+
api_key=api_key,
|
|
42
|
+
**kwargs)
|
|
43
|
+
|
|
44
|
+
@classmethod
|
|
45
|
+
def list(cls,
|
|
46
|
+
page=1,
|
|
47
|
+
page_size=10,
|
|
48
|
+
api_key: str = None,
|
|
49
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
50
|
+
"""List uploaded files.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
api_key (str, optional):
|
|
54
|
+
The api api_key, can be None,
|
|
55
|
+
if None, will get by default rule(TODO: api key doc).
|
|
56
|
+
page (int, optional): Page number. Defaults to 1.
|
|
57
|
+
page_size (int, optional): Items per page. Defaults to 10.
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
DashScopeAPIResponse: The fine-tune jobs in the result.
|
|
61
|
+
"""
|
|
62
|
+
return super().list(page, page_size, api_key, **kwargs)
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def get(cls,
|
|
66
|
+
file_id: str,
|
|
67
|
+
api_key: str = None,
|
|
68
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
69
|
+
"""Get the file info.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
file_id (str): The file id.
|
|
73
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
DashScopeAPIResponse: The job info
|
|
77
|
+
"""
|
|
78
|
+
return super().get(file_id, api_key, **kwargs)
|
|
79
|
+
|
|
80
|
+
@classmethod
|
|
81
|
+
def delete(cls,
|
|
82
|
+
file_id: str,
|
|
83
|
+
api_key: str = None,
|
|
84
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
85
|
+
"""Delete uploaded file.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
file_id (str): The file id want to delete.
|
|
89
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
DashScopeAPIResponse: Delete result.
|
|
93
|
+
"""
|
|
94
|
+
return super().delete(file_id, api_key, **kwargs)
|
dashscope/finetune.py
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
|
4
|
+
from dashscope.client.base_api import (CancelMixin, CreateMixin, DeleteMixin,
|
|
5
|
+
GetStatusMixin, ListMixin, LogMixin,
|
|
6
|
+
StreamEventMixin)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class FineTune(CreateMixin, CancelMixin, DeleteMixin, ListMixin,
|
|
10
|
+
GetStatusMixin, StreamEventMixin, LogMixin):
|
|
11
|
+
SUB_PATH = 'fine-tunes'
|
|
12
|
+
|
|
13
|
+
@classmethod
|
|
14
|
+
def call(cls,
|
|
15
|
+
model: str,
|
|
16
|
+
training_file_ids: Union[list, str],
|
|
17
|
+
validation_file_ids: Union[list, str] = None,
|
|
18
|
+
mode: str = None,
|
|
19
|
+
hyper_parameters: dict = {},
|
|
20
|
+
api_key: str = None,
|
|
21
|
+
workspace: str = None,
|
|
22
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
23
|
+
"""Create fine-tune job
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
model (str): The model to be fine-tuned
|
|
27
|
+
training_file_ids (list, str): Ids of the fine-tune training data,
|
|
28
|
+
which can be pre-uploaded using the File API.
|
|
29
|
+
validation_file_ids ([list,str], optional): Ids of the fine-tune
|
|
30
|
+
validating data, which can be pre-uploaded using the File API.
|
|
31
|
+
mode (str): The fine-tune mode, sft or efficient_sft.
|
|
32
|
+
hyper_parameters (dict, optional): The fine-tune hyper parameters.
|
|
33
|
+
Defaults to empty.
|
|
34
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
35
|
+
workspace (str): The dashscope workspace id.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
DashScopeAPIResponse: The request result.
|
|
39
|
+
"""
|
|
40
|
+
request = {
|
|
41
|
+
'model': model,
|
|
42
|
+
'training_file_ids': training_file_ids,
|
|
43
|
+
'validation_file_ids': validation_file_ids,
|
|
44
|
+
'hyper_parameters': hyper_parameters if hyper_parameters else {},
|
|
45
|
+
}
|
|
46
|
+
if mode is not None:
|
|
47
|
+
request['training_type'] = mode
|
|
48
|
+
if 'finetuned_output' in kwargs:
|
|
49
|
+
request['finetuned_output'] = kwargs['finetuned_output']
|
|
50
|
+
return super().call(request,
|
|
51
|
+
api_key=api_key,
|
|
52
|
+
workspace=workspace,
|
|
53
|
+
**kwargs)
|
|
54
|
+
|
|
55
|
+
@classmethod
|
|
56
|
+
def cancel(cls,
|
|
57
|
+
job_id: str,
|
|
58
|
+
api_key: str = None,
|
|
59
|
+
workspace: str = None,
|
|
60
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
61
|
+
"""Cancel a running fine-tune job.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
job_id (str): The fine-tune job id.
|
|
65
|
+
api_key (str, optional): The api api_key, can be None,
|
|
66
|
+
if None, will get by default rule(TODO: api key doc).
|
|
67
|
+
workspace (str): The dashscope workspace id.
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
DashScopeAPIResponse: The request result.
|
|
71
|
+
"""
|
|
72
|
+
return super().cancel(job_id, api_key, workspace=workspace, **kwargs)
|
|
73
|
+
|
|
74
|
+
@classmethod
|
|
75
|
+
def list(cls,
|
|
76
|
+
page=1,
|
|
77
|
+
page_size=10,
|
|
78
|
+
api_key: str = None,
|
|
79
|
+
workspace: str = None,
|
|
80
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
81
|
+
"""List fine-tune job.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
api_key (str, optional): The api key
|
|
85
|
+
page (int, optional): Page number. Defaults to 1.
|
|
86
|
+
page_size (int, optional): Items per page. Defaults to 10.
|
|
87
|
+
workspace (str): The dashscope workspace id.
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
DashScopeAPIResponse: The fine-tune jobs in the result.
|
|
91
|
+
"""
|
|
92
|
+
return super().list(page,
|
|
93
|
+
page_size,
|
|
94
|
+
api_key,
|
|
95
|
+
workspace=workspace,
|
|
96
|
+
**kwargs)
|
|
97
|
+
|
|
98
|
+
@classmethod
|
|
99
|
+
def get(cls,
|
|
100
|
+
job_id: str,
|
|
101
|
+
api_key: str = None,
|
|
102
|
+
workspace: str = None,
|
|
103
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
104
|
+
"""Get fine-tune job information.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
job_id (str): The fine-tune job id
|
|
108
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
109
|
+
workspace (str): The dashscope workspace id.
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
DashScopeAPIResponse: The job info
|
|
113
|
+
"""
|
|
114
|
+
return super().get(job_id, api_key, workspace=workspace, **kwargs)
|
|
115
|
+
|
|
116
|
+
@classmethod
|
|
117
|
+
def delete(cls,
|
|
118
|
+
job_id: str,
|
|
119
|
+
api_key: str = None,
|
|
120
|
+
workspace: str = None,
|
|
121
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
122
|
+
"""Delete a fine-tune job.
|
|
123
|
+
|
|
124
|
+
Args:
|
|
125
|
+
job_id (str): The fine-tune job id.
|
|
126
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
127
|
+
workspace (str): The dashscope workspace id.
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
DashScopeAPIResponse: The delete result.
|
|
131
|
+
"""
|
|
132
|
+
return super().delete(job_id, api_key, workspace=workspace, **kwargs)
|
|
133
|
+
|
|
134
|
+
@classmethod
|
|
135
|
+
def stream_events(cls,
|
|
136
|
+
job_id: str,
|
|
137
|
+
api_key: str = None,
|
|
138
|
+
workspace: str = None,
|
|
139
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
140
|
+
"""Get fine-tune job events.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
job_id (str): The fine-tune job id
|
|
144
|
+
api_key (str, optional): the api key. Defaults to None.
|
|
145
|
+
workspace (str): The dashscope workspace id.
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
DashScopeAPIResponse: The job log events.
|
|
149
|
+
"""
|
|
150
|
+
return super().stream_events(job_id,
|
|
151
|
+
api_key,
|
|
152
|
+
workspace=workspace,
|
|
153
|
+
**kwargs)
|
|
154
|
+
|
|
155
|
+
@classmethod
|
|
156
|
+
def logs(cls,
|
|
157
|
+
job_id: str,
|
|
158
|
+
offset=1,
|
|
159
|
+
line=1000,
|
|
160
|
+
api_key: str = None,
|
|
161
|
+
workspace: str = None,
|
|
162
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
163
|
+
"""Get log of the job.
|
|
164
|
+
|
|
165
|
+
Args:
|
|
166
|
+
job_id (str): The job id(used for fine-tune)
|
|
167
|
+
offset (int, optional): start log line. Defaults to 1.
|
|
168
|
+
line (int, optional): total line return. Defaults to 1000.
|
|
169
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
170
|
+
workspace (str): The dashscope workspace id.
|
|
171
|
+
|
|
172
|
+
Returns:
|
|
173
|
+
DashScopeAPIResponse: The response
|
|
174
|
+
"""
|
|
175
|
+
return super().logs(job_id, offset, line, workspace=workspace)
|