dashscope 1.18.1__py3-none-any.whl → 1.19.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/cli.py CHANGED
@@ -47,7 +47,7 @@ class FineTunes:
47
47
  if args.params:
48
48
  params.update(args.params)
49
49
 
50
- rsp = dashscope.FineTune.call(
50
+ rsp = dashscope.FineTunes.call(
51
51
  model=args.model,
52
52
  training_file_ids=args.training_file_ids,
53
53
  validation_file_ids=args.validation_file_ids,
@@ -64,7 +64,7 @@ class FineTunes:
64
64
  def wait(cls, job_id):
65
65
  try:
66
66
  while True:
67
- rsp = dashscope.FineTune.get(job_id)
67
+ rsp = dashscope.FineTunes.get(job_id)
68
68
  if rsp.status_code == HTTPStatus.OK:
69
69
  if rsp.output['status'] == TaskStatus.FAILED:
70
70
  print('Fine-tune FAILED!')
@@ -93,7 +93,7 @@ class FineTunes:
93
93
 
94
94
  @classmethod
95
95
  def get(cls, args):
96
- rsp = dashscope.FineTune.get(args.job)
96
+ rsp = dashscope.FineTunes.get(args.job)
97
97
  if rsp.status_code == HTTPStatus.OK:
98
98
  if rsp.output['status'] == TaskStatus.FAILED:
99
99
  print('Fine-tune failed!')
@@ -109,8 +109,8 @@ class FineTunes:
109
109
 
110
110
  @classmethod
111
111
  def list(cls, args):
112
- rsp = dashscope.FineTune.list(page=args.start_page,
113
- page_size=args.page_size)
112
+ rsp = dashscope.FineTunes.list(page=args.start_page,
113
+ page_size=args.page_size)
114
114
  if rsp.status_code == HTTPStatus.OK:
115
115
  if rsp.output is not None:
116
116
  for job in rsp.output['jobs']:
@@ -131,7 +131,7 @@ class FineTunes:
131
131
  @classmethod
132
132
  def stream_events(cls, job_id):
133
133
  # check job status if job is completed, get log.
134
- rsp = dashscope.FineTune.get(job_id)
134
+ rsp = dashscope.FineTunes.get(job_id)
135
135
  if rsp.status_code == HTTPStatus.OK:
136
136
  if rsp.output['status'] in [
137
137
  TaskStatus.FAILED, TaskStatus.CANCELED,
@@ -146,7 +146,7 @@ class FineTunes:
146
146
  return
147
147
  # start streaming events.
148
148
  try:
149
- stream_events = dashscope.FineTune.stream_events(job_id)
149
+ stream_events = dashscope.FineTunes.stream_events(job_id)
150
150
  for rsp in stream_events:
151
151
  if rsp.status_code == HTTPStatus.OK:
152
152
  print(rsp.output)
@@ -166,7 +166,7 @@ class FineTunes:
166
166
  start = 1
167
167
  n_line = 1000 # 1000 line per request
168
168
  while True:
169
- rsp = dashscope.FineTune.logs(job_id, start, n_line)
169
+ rsp = dashscope.FineTunes.logs(job_id, offset=start, line=n_line)
170
170
  if rsp.status_code == HTTPStatus.OK:
171
171
  for line in rsp.output['logs']:
172
172
  print(line)
@@ -179,7 +179,7 @@ class FineTunes:
179
179
 
180
180
  @classmethod
181
181
  def cancel(cls, args):
182
- rsp = dashscope.FineTune.cancel(args.job)
182
+ rsp = dashscope.FineTunes.cancel(args.job)
183
183
  if rsp.status_code == HTTPStatus.OK:
184
184
  print('Cancel fine-tune job: %s success!')
185
185
  else:
@@ -187,7 +187,7 @@ class FineTunes:
187
187
 
188
188
  @classmethod
189
189
  def delete(cls, args):
190
- rsp = dashscope.FineTune.delete(args.job)
190
+ rsp = dashscope.FineTunes.delete(args.job)
191
191
  if rsp.status_code == HTTPStatus.OK:
192
192
  print('fine_tune job: %s delete success' % args.job)
193
193
  else:
@@ -244,15 +244,15 @@ class Files:
244
244
  class Deployments:
245
245
  @classmethod
246
246
  def call(cls, args):
247
- rsp = dashscope.Deployment.call(model=args.model,
248
- capacity=args.capacity,
249
- suffix=args.suffix)
247
+ rsp = dashscope.Deployments.call(model=args.model,
248
+ capacity=args.capacity,
249
+ suffix=args.suffix)
250
250
  if rsp.status_code == HTTPStatus.OK:
251
251
  deployed_model = rsp.output['deployed_model']
252
252
  print('Create model: %s deployment' % deployed_model)
253
253
  try:
254
254
  while True: # wait for deployment ok.
255
- status = dashscope.Deployment.get(deployed_model)
255
+ status = dashscope.Deployments.get(deployed_model)
256
256
  if status.status_code == HTTPStatus.OK:
257
257
  if status.output['status'] in [
258
258
  DeploymentStatus.PENDING,
@@ -276,7 +276,7 @@ class Deployments:
276
276
 
277
277
  @classmethod
278
278
  def get(cls, args):
279
- rsp = dashscope.Deployment.get(args.deploy)
279
+ rsp = dashscope.Deployments.get(args.deploy)
280
280
  if rsp.status_code == HTTPStatus.OK:
281
281
  print('Deployed model: %s capacity: %s status: %s' %
282
282
  (rsp.output['deployed_model'], rsp.output['capacity'],
@@ -286,11 +286,12 @@ class Deployments:
286
286
 
287
287
  @classmethod
288
288
  def list(cls, args):
289
- rsp = dashscope.Deployment.list(page_no=args.start_page,
290
- page_size=args.page_size)
289
+ rsp = dashscope.Deployments.list(page_no=args.start_page,
290
+ page_size=args.page_size)
291
291
  if rsp.status_code == HTTPStatus.OK:
292
292
  if rsp.output is not None:
293
- if 'deployments' not in rsp.output:
293
+ if 'deployments' not in rsp.output or len(
294
+ rsp.output['deployments']) == 0:
294
295
  print('There is no deployed model!')
295
296
  return
296
297
  for deployment in rsp.output['deployments']:
@@ -304,7 +305,7 @@ class Deployments:
304
305
 
305
306
  @classmethod
306
307
  def update(cls, args):
307
- rsp = dashscope.Deployment.update(args.deployed_model, args.version)
308
+ rsp = dashscope.Deployments.update(args.deployed_model, args.version)
308
309
  if rsp.status_code == HTTPStatus.OK:
309
310
  if rsp.output is not None:
310
311
  if 'deployments' not in rsp.output:
@@ -321,7 +322,7 @@ class Deployments:
321
322
 
322
323
  @classmethod
323
324
  def scale(cls, args):
324
- rsp = dashscope.Deployment.scale(args.deployed_model, args.capacity)
325
+ rsp = dashscope.Deployments.scale(args.deployed_model, args.capacity)
325
326
  if rsp.status_code == HTTPStatus.OK:
326
327
  if rsp.output is not None:
327
328
  print('Deployed_model: %s, model: %s, status: %s' %
@@ -334,7 +335,7 @@ class Deployments:
334
335
 
335
336
  @classmethod
336
337
  def delete(cls, args):
337
- rsp = dashscope.Deployment.delete(args.deploy)
338
+ rsp = dashscope.Deployments.delete(args.deploy)
338
339
  if rsp.status_code == HTTPStatus.OK:
339
340
  print('Deployed model: %s delete success' % args.deploy)
340
341
  else:
@@ -33,15 +33,15 @@ class BaseAioApi():
33
33
  return api_key, model
34
34
 
35
35
  @classmethod
36
- def aio_call(cls,
37
- model: str,
38
- input: object,
39
- task_group: str,
40
- task: str = None,
41
- function: str = None,
42
- api_key: str = None,
43
- workspace: str = None,
44
- **kwargs) -> DashScopeAPIResponse:
36
+ async def call(cls,
37
+ model: str,
38
+ input: object,
39
+ task_group: str,
40
+ task: str = None,
41
+ function: str = None,
42
+ api_key: str = None,
43
+ workspace: str = None,
44
+ **kwargs) -> DashScopeAPIResponse:
45
45
  """Call service and get result.
46
46
 
47
47
  Args:
@@ -80,7 +80,7 @@ class BaseAioApi():
80
80
  api_key=api_key,
81
81
  **kwargs)
82
82
  # call request service.
83
- return request.aio_call()
83
+ return await request.aio_call()
84
84
 
85
85
 
86
86
  class BaseApi():
@@ -1006,7 +1006,7 @@ class StreamEventMixin():
1006
1006
  output=json_content,
1007
1007
  usage=None)
1008
1008
  else:
1009
- _handle_http_failed_response(response)
1009
+ yield _handle_http_failed_response(response)
1010
1010
 
1011
1011
  @classmethod
1012
1012
  def stream_events(cls,
@@ -27,7 +27,7 @@ class BaseObjectMixin(object):
27
27
  field_type_map = self._get_fields_type()
28
28
  for k, v in kwargs.items():
29
29
  field = field_type_map.get(k, None)
30
- if field:
30
+ if field and v is not None:
31
31
  if dataclasses.is_dataclass(field.type): # process dataclasses
32
32
  self.__setattr__(k, field.type(**v))
33
33
  continue
@@ -88,6 +88,9 @@ class BaseObjectMixin(object):
88
88
  def __getitem__(self, __key: Any) -> Any:
89
89
  return self.__getattribute__(__key)
90
90
 
91
+ def __contains__(self, item):
92
+ return hasattr(self, item)
93
+
91
94
  def __delitem__(self, key):
92
95
  self.__delattr__(key)
93
96
 
File without changes
@@ -0,0 +1,190 @@
1
+ from dataclasses import dataclass
2
+ from http import HTTPStatus
3
+ from typing import Dict, List
4
+
5
+ from dashscope.common.base_type import BaseObjectMixin
6
+
7
+ __all__ = ['Deployment', 'FineTune', 'DeploymentList', 'FineTuneList']
8
+
9
+
10
+ @dataclass(init=False)
11
+ class DashScopeBaseList(BaseObjectMixin):
12
+ page_no: int
13
+ page_size: int
14
+ total: int
15
+
16
+ def __init__(self, **kwargs):
17
+ super().__init__(**kwargs)
18
+
19
+
20
+ @dataclass(init=False)
21
+ class DashScopeBase(BaseObjectMixin):
22
+ status_code: int
23
+ request_id: str
24
+ code: str
25
+ message: str
26
+
27
+ def __init__(self, **kwargs):
28
+ super().__init__(**kwargs)
29
+
30
+
31
+ @dataclass(init=False)
32
+ class FineTuneOutput(BaseObjectMixin):
33
+ job_id: str
34
+ job_name: str
35
+ status: str
36
+ model: str
37
+ base_model: str
38
+ finetuned_output: str
39
+ training_file_ids: List[str]
40
+ validation_file_ids: List[str]
41
+ hyper_parameters: Dict
42
+ training_type: str
43
+ create_time: str
44
+ end_time: str
45
+ user_identity: str
46
+ modifier: str
47
+ creator: str
48
+ group: str
49
+ usage: int
50
+
51
+ def __init__(self, **kwargs):
52
+ super().__init__(**kwargs)
53
+
54
+
55
+ @dataclass(init=False)
56
+ class FineTune(DashScopeBase):
57
+ output: FineTuneOutput
58
+ usage: Dict
59
+
60
+ def __init__(self, **kwargs):
61
+ status_code = kwargs.get('status_code', None)
62
+ if status_code == HTTPStatus.OK:
63
+ self.output = FineTuneOutput(**kwargs.pop('output', {}))
64
+ super().__init__(**kwargs)
65
+
66
+
67
+ @dataclass(init=False)
68
+ class FineTuneListOutput(DashScopeBaseList):
69
+ jobs: List[FineTuneOutput]
70
+
71
+ def __init__(self, **kwargs):
72
+ self.jobs = []
73
+ for job in kwargs.pop('jobs', []):
74
+ self.jobs.append(FineTuneOutput(**job))
75
+ super().__init__(**kwargs)
76
+
77
+
78
+ @dataclass(init=False)
79
+ class FineTuneList(DashScopeBase):
80
+ output: FineTuneListOutput
81
+
82
+ def __init__(self, **kwargs):
83
+ status_code = kwargs.get('status_code', None)
84
+ if status_code == HTTPStatus.OK:
85
+ self.output = FineTuneListOutput(**kwargs.pop('output', {}))
86
+ super().__init__(**kwargs)
87
+
88
+
89
+ @dataclass(init=False)
90
+ class CancelDeleteStatus(BaseObjectMixin):
91
+ status: str
92
+
93
+
94
+ @dataclass(init=False)
95
+ class FineTuneCancel(DashScopeBase):
96
+ output: CancelDeleteStatus
97
+
98
+ def __init__(self, **kwargs):
99
+ status_code = kwargs.get('status_code', None)
100
+ if status_code == HTTPStatus.OK:
101
+ self.output = CancelDeleteStatus(**kwargs.pop('output', {}))
102
+ super().__init__(**kwargs)
103
+
104
+
105
+ @dataclass(init=False)
106
+ class FineTuneDelete(DashScopeBase):
107
+ output: CancelDeleteStatus
108
+
109
+ def __init__(self, **kwargs):
110
+ status_code = kwargs.get('status_code', None)
111
+ if status_code == HTTPStatus.OK:
112
+ self.output = CancelDeleteStatus(**kwargs.pop('output', {}))
113
+ super().__init__(**kwargs)
114
+
115
+
116
+ @dataclass(init=False)
117
+ class FineTuneEvent(DashScopeBase):
118
+ output: str
119
+
120
+ def __init__(self, **kwargs):
121
+ status_code = kwargs.get('status_code', None)
122
+ if status_code == HTTPStatus.OK:
123
+ self.output = kwargs.pop('output', {})
124
+ super().__init__(**kwargs)
125
+
126
+
127
+ @dataclass(init=False)
128
+ class DeploymentOutput(BaseObjectMixin):
129
+ deployed_model: str
130
+ gmt_create: str
131
+ gmt_modified: str
132
+ status: str
133
+ model_name: str
134
+ base_model: str
135
+ base_capacity: int
136
+ capacity: int
137
+ ready_capacity: int
138
+ workspace_id: str
139
+ charge_type: str
140
+ modifier: str
141
+ creator: str
142
+
143
+ def __init__(self, **kwargs):
144
+ super().__init__(**kwargs)
145
+
146
+
147
+ @dataclass(init=False)
148
+ class Deployment(DashScopeBase):
149
+ output: DeploymentOutput
150
+
151
+ def __init__(self, **kwargs):
152
+ output = kwargs.pop('output', {})
153
+ if output:
154
+ self.output = DeploymentOutput(**output)
155
+ else:
156
+ self.output = None
157
+ super().__init__(**kwargs)
158
+
159
+
160
+ @dataclass(init=False)
161
+ class DeploymentListOutput(DashScopeBaseList):
162
+ deployments: List[DeploymentOutput]
163
+
164
+ def __init__(self, **kwargs):
165
+ self.deployments = []
166
+ for job in kwargs.pop('deployments', []):
167
+ self.deployments.append(DeploymentOutput(**job))
168
+ super().__init__(**kwargs)
169
+
170
+
171
+ @dataclass(init=False)
172
+ class DeploymentList(BaseObjectMixin):
173
+ output: DeploymentListOutput
174
+
175
+ def __init__(self, **kwargs):
176
+ status_code = kwargs.get('status_code', None)
177
+ if status_code == HTTPStatus.OK:
178
+ self.output = DeploymentListOutput(**kwargs.pop('output', {}))
179
+ super().__init__(**kwargs)
180
+
181
+
182
+ @dataclass(init=False)
183
+ class DeploymentDelete(DashScopeBase):
184
+ output: CancelDeleteStatus
185
+
186
+ def __init__(self, **kwargs):
187
+ status_code = kwargs.get('status_code', None)
188
+ if status_code == HTTPStatus.OK:
189
+ self.output = CancelDeleteStatus(**kwargs.pop('output', {}))
190
+ super().__init__(**kwargs)
@@ -0,0 +1,144 @@
1
+ from dashscope.client.base_api import (CreateMixin, DeleteMixin, GetMixin,
2
+ ListMixin, PutMixin, StreamEventMixin)
3
+ from dashscope.customize.customize_types import (Deployment, DeploymentDelete,
4
+ DeploymentList)
5
+
6
+
7
+ class Deployments(CreateMixin, DeleteMixin, ListMixin, GetMixin,
8
+ StreamEventMixin, PutMixin):
9
+ SUB_PATH = 'deployments'
10
+ """Deploy a model.
11
+ """
12
+ @classmethod
13
+ def call(cls,
14
+ model: str,
15
+ capacity: int,
16
+ version: str = None,
17
+ suffix: str = None,
18
+ api_key: str = None,
19
+ workspace: str = None,
20
+ **kwargs) -> Deployment:
21
+ """Call to deployment a model service.
22
+
23
+ Args:
24
+ model (str): The model name.
25
+ version (str, optional): The model version, unnecessary
26
+ for fine-tuned model. Defaults to None.
27
+ suffix (str, optional): The name suffix of the model deployment,
28
+ If specified, the final model name will be model_suffix.
29
+ Defaults to None.
30
+ capacity (int, optional): The model service capacity.
31
+ api_key (str, optional): The api-key. Defaults to None.
32
+ workspace (str): The dashscope workspace id.
33
+
34
+ Returns:
35
+ Deployment: _description_
36
+ """
37
+ req = {'model_name': model, 'capacity': capacity}
38
+
39
+ if version is not None:
40
+ req['model_version'] = version
41
+ if suffix is not None:
42
+ req['suffix'] = suffix
43
+ response = super().call(req,
44
+ api_key=api_key,
45
+ workspace=workspace,
46
+ **kwargs)
47
+ return Deployment(**response)
48
+
49
+ @classmethod
50
+ def list(cls,
51
+ page_no=1,
52
+ page_size=10,
53
+ api_key: str = None,
54
+ workspace: str = None,
55
+ **kwargs) -> DeploymentList:
56
+ """List deployments.
57
+
58
+ Args:
59
+ api_key (str, optional): The api api_key, if not present,
60
+ will get by default rule(TODO: api key doc). Defaults to None.
61
+ page_no (int, optional): Page number. Defaults to 1.
62
+ page_size (int, optional): Items per page. Defaults to 10.
63
+ workspace (str): The dashscope workspace id.
64
+
65
+ Returns:
66
+ Deployment: The deployment list.
67
+ """
68
+ response = super().list(page_no=page_no,
69
+ page_size=page_size,
70
+ api_key=api_key,
71
+ workspace=workspace,
72
+ **kwargs)
73
+ return DeploymentList(**response)
74
+
75
+ @classmethod
76
+ def get(cls,
77
+ deployed_model: str,
78
+ api_key: str = None,
79
+ workspace: str = None,
80
+ **kwargs) -> Deployment:
81
+ """Get model deployment information.
82
+
83
+ Args:
84
+ deployed_model (str): The deployment_id.
85
+ api_key (str, optional): The api key. Defaults to None.
86
+ workspace (str): The dashscope workspace id.
87
+
88
+ Returns:
89
+ Deployment: The deployment information.
90
+ """
91
+ response = super().get(deployed_model,
92
+ api_key=api_key,
93
+ workspace=workspace,
94
+ **kwargs)
95
+ return Deployment(**response)
96
+
97
+ @classmethod
98
+ def delete(cls,
99
+ deployed_model: str,
100
+ api_key: str = None,
101
+ workspace: str = None,
102
+ **kwargs) -> DeploymentDelete:
103
+ """Delete model deployment.
104
+
105
+ Args:
106
+ deployed_model (str): The deployment id.
107
+ api_key (str, optional): The api key. Defaults to None.
108
+ workspace (str): The dashscope workspace id.
109
+
110
+ Returns:
111
+ Deployment: The delete result.
112
+ """
113
+ response = super().delete(deployed_model,
114
+ api_key=api_key,
115
+ workspace=workspace,
116
+ **kwargs)
117
+ return DeploymentDelete(**response)
118
+
119
+ @classmethod
120
+ def scale(cls,
121
+ deployed_model: str,
122
+ capacity: int,
123
+ api_key: str = None,
124
+ workspace: str = None,
125
+ **kwargs) -> Deployment:
126
+ """Scaling model deployment.
127
+
128
+ Args:
129
+ deployment_id (str): The deployment id.
130
+ capacity (int): The target service capacity.
131
+ api_key (str, optional): The api key. Defaults to None.
132
+
133
+ Returns:
134
+ Deployment: The delete result.
135
+ """
136
+ req = {'deployed_model': deployed_model, 'capacity': capacity}
137
+ path = '%s/%s/scale' % (cls.SUB_PATH.lower(), deployed_model)
138
+ response = super().put(deployed_model,
139
+ req,
140
+ path=path,
141
+ api_key=api_key,
142
+ workspace=workspace,
143
+ **kwargs)
144
+ return Deployment(**response)