dashscope 1.16.0__py3-none-any.whl → 1.17.1__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.

Files changed (53) hide show
  1. dashscope/__init__.py +25 -3
  2. dashscope/aigc/code_generation.py +12 -9
  3. dashscope/aigc/conversation.py +3 -0
  4. dashscope/aigc/generation.py +3 -0
  5. dashscope/aigc/image_synthesis.py +21 -6
  6. dashscope/aigc/multimodal_conversation.py +3 -0
  7. dashscope/api_entities/aiohttp_request.py +1 -0
  8. dashscope/api_entities/api_request_factory.py +28 -12
  9. dashscope/api_entities/http_request.py +18 -9
  10. dashscope/api_entities/websocket_request.py +3 -0
  11. dashscope/app/application.py +16 -18
  12. dashscope/assistants/__init__.py +14 -0
  13. dashscope/assistants/assistant_types.py +164 -0
  14. dashscope/assistants/assistants.py +280 -0
  15. dashscope/assistants/files.py +189 -0
  16. dashscope/audio/asr/asr_phrase_manager.py +27 -5
  17. dashscope/audio/asr/recognition.py +10 -2
  18. dashscope/audio/asr/transcription.py +21 -3
  19. dashscope/audio/tts/speech_synthesizer.py +3 -0
  20. dashscope/cli.py +7 -7
  21. dashscope/client/base_api.py +303 -68
  22. dashscope/common/base_type.py +130 -0
  23. dashscope/common/constants.py +1 -0
  24. dashscope/common/error.py +4 -0
  25. dashscope/common/utils.py +22 -6
  26. dashscope/deployment.py +40 -6
  27. dashscope/embeddings/batch_text_embedding.py +24 -7
  28. dashscope/embeddings/multimodal_embedding.py +3 -0
  29. dashscope/embeddings/text_embedding.py +8 -1
  30. dashscope/files.py +107 -0
  31. dashscope/finetune.py +31 -7
  32. dashscope/model.py +9 -2
  33. dashscope/models.py +47 -0
  34. dashscope/nlp/understanding.py +2 -2
  35. dashscope/threads/__init__.py +24 -0
  36. dashscope/threads/messages/__init__.py +0 -0
  37. dashscope/threads/messages/files.py +111 -0
  38. dashscope/threads/messages/messages.py +218 -0
  39. dashscope/threads/runs/__init__.py +0 -0
  40. dashscope/threads/runs/runs.py +408 -0
  41. dashscope/threads/runs/steps.py +110 -0
  42. dashscope/threads/thread_types.py +571 -0
  43. dashscope/threads/threads.py +210 -0
  44. dashscope/tokenizers/tokenization.py +3 -0
  45. dashscope/utils/oss_utils.py +11 -0
  46. dashscope/version.py +1 -1
  47. {dashscope-1.16.0.dist-info → dashscope-1.17.1.dist-info}/METADATA +2 -3
  48. dashscope-1.17.1.dist-info/RECORD +84 -0
  49. dashscope-1.16.0.dist-info/RECORD +0 -68
  50. {dashscope-1.16.0.dist-info → dashscope-1.17.1.dist-info}/LICENSE +0 -0
  51. {dashscope-1.16.0.dist-info → dashscope-1.17.1.dist-info}/WHEEL +0 -0
  52. {dashscope-1.16.0.dist-info → dashscope-1.17.1.dist-info}/entry_points.txt +0 -0
  53. {dashscope-1.16.0.dist-info → dashscope-1.17.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,408 @@
1
+ import time
2
+ from http import HTTPStatus
3
+ from typing import Dict, List, Optional
4
+
5
+ from dashscope.client.base_api import (CancelMixin, CreateMixin,
6
+ GetStatusMixin, ListObjectMixin,
7
+ UpdateMixin)
8
+ from dashscope.common.error import InputRequired, TimeoutException
9
+ from dashscope.common.logging import logger
10
+ from dashscope.threads.thread_types import Run, RunList
11
+
12
+ __all__ = ['Runs']
13
+
14
+
15
+ class Runs(CreateMixin, CancelMixin, ListObjectMixin, GetStatusMixin,
16
+ UpdateMixin):
17
+ SUB_PATH = 'RUNS' # useless
18
+
19
+ @classmethod
20
+ def create_thread_and_run(cls,
21
+ *,
22
+ assistant_id: str,
23
+ thread: Optional[Dict] = None,
24
+ model: Optional[str] = None,
25
+ instructions: Optional[str] = None,
26
+ additional_instructions: Optional[str] = None,
27
+ tools: Optional[List[Dict]] = None,
28
+ metadata: Optional[Dict] = None,
29
+ workspace: str = None,
30
+ api_key: str = None,
31
+ **kwargs) -> Run:
32
+ if not assistant_id:
33
+ raise InputRequired('assistant_id is required')
34
+ data = {'assistant_id': assistant_id}
35
+ if thread:
36
+ data['thread'] = thread
37
+ if model:
38
+ data['model'] = model
39
+ if instructions:
40
+ data['instructions'] = instructions
41
+ if additional_instructions:
42
+ data['additional_instructions'] = additional_instructions
43
+ if tools:
44
+ data['tools'] = tools
45
+ if metadata:
46
+ data['metadata'] = metadata
47
+
48
+ response = super().call(data=data,
49
+ path='threads/runs',
50
+ api_key=api_key,
51
+ flattened_output=True,
52
+ workspace=workspace,
53
+ **kwargs)
54
+ return Run(**response)
55
+
56
+ @classmethod
57
+ def create(cls,
58
+ thread_id: str,
59
+ *,
60
+ assistant_id: str,
61
+ model: Optional[str] = None,
62
+ instructions: Optional[str] = None,
63
+ additional_instructions: Optional[str] = None,
64
+ tools: Optional[List[Dict]] = None,
65
+ metadata: Optional[Dict] = None,
66
+ workspace: str = None,
67
+ api_key: str = None,
68
+ **kwargs) -> Run:
69
+ """Create a run.
70
+
71
+ Args:
72
+ thread_id (str): The thread to run.
73
+ assistant_id (str): The assistant id to run.
74
+ model (str): The model to use.
75
+ instructions (str, optional): The system instructions this assistant uses. Defaults to None.
76
+ additional_instructions (Optional[str], optional): Appends additional
77
+ instructions at the end of the instructions for the run. This is
78
+ useful for modifying the behavior on a per-run basis without
79
+ overriding other instructions.. Defaults to None.
80
+ tools (Optional[str], optional): List of tools to use.. Defaults to [].
81
+ metadata (Dict, optional): Custom key-value pairs associate with run. Defaults to None.
82
+ workspace (str): The DashScope workspace id.
83
+ api_key (str, optional): The DashScope workspace id. Defaults to None.
84
+
85
+ Raises:
86
+ InputRequired: thread and assistant is required.
87
+
88
+ Returns:
89
+ Run: The `Run` object.
90
+ """
91
+ if not thread_id or not assistant_id:
92
+ raise InputRequired('thread_id and assistant_id is required')
93
+ data = {'assistant_id': assistant_id}
94
+ if model:
95
+ data['model'] = model
96
+ if instructions:
97
+ data['instructions'] = instructions
98
+ if additional_instructions:
99
+ data['additional_instructions'] = additional_instructions
100
+ if tools:
101
+ data['tools'] = tools
102
+ if metadata:
103
+ data['metadata'] = metadata
104
+
105
+ response = super().call(data=data,
106
+ path=f'threads/{thread_id}/runs',
107
+ api_key=api_key,
108
+ flattened_output=True,
109
+ workspace=workspace,
110
+ **kwargs)
111
+ return Run(**response)
112
+
113
+ @classmethod
114
+ def call(cls,
115
+ thread_id: str,
116
+ *,
117
+ assistant_id: str,
118
+ model: Optional[str] = None,
119
+ instructions: Optional[str] = None,
120
+ additional_instructions: Optional[str] = None,
121
+ tools: Optional[List[Dict]] = None,
122
+ metadata: Optional[Dict] = None,
123
+ workspace: str = None,
124
+ api_key: str = None,
125
+ **kwargs) -> Run:
126
+ """Create a run.
127
+
128
+ Args:
129
+ thread_id (str): The thread to run.
130
+ assistant_id (str): The assistant id to run.
131
+ model (str): The model to use.
132
+ instructions (str, optional): The system instructions this assistant uses. Defaults to None.
133
+ additional_instructions (Optional[str], optional): Appends additional
134
+ instructions at the end of the instructions for the run. This is
135
+ useful for modifying the behavior on a per-run basis without
136
+ overriding other instructions.. Defaults to None.
137
+ tools (Optional[str], optional): List of tools to use.. Defaults to [].
138
+ metadata (Dict, optional): Custom key-value pairs associate with run. Defaults to None.
139
+ workspace (str): The DashScope workspace id.
140
+ api_key (str, optional): The DashScope workspace id. Defaults to None.
141
+
142
+ Raises:
143
+ InputRequired: thread and assistant is required.
144
+
145
+ Returns:
146
+ Run: The `Run` object.
147
+ """
148
+ return cls.create(thread_id,
149
+ assistant_id=assistant_id,
150
+ model=model,
151
+ instructions=instructions,
152
+ additional_instructions=additional_instructions,
153
+ tools=tools,
154
+ metadata=metadata,
155
+ workspace=workspace,
156
+ api_key=api_key,
157
+ **kwargs)
158
+
159
+ @classmethod
160
+ def list(cls,
161
+ thread_id: str,
162
+ *,
163
+ limit: int = None,
164
+ order: str = None,
165
+ after: str = None,
166
+ before: str = None,
167
+ workspace: str = None,
168
+ api_key: str = None,
169
+ **kwargs) -> RunList:
170
+ """List `Run`.
171
+
172
+ Args:
173
+ thread_id (str): The thread id.
174
+ limit (int, optional): How many assistant to retrieve. Defaults to None.
175
+ order (str, optional): Sort order by created_at. Defaults to None.
176
+ after (str, optional): Assistant id after. Defaults to None.
177
+ before (str, optional): Assistant id before. Defaults to None.
178
+ workspace (str, optional): The DashScope workspace id. Defaults to None.
179
+ api_key (str, optional): Your DashScope api key. Defaults to None.
180
+
181
+ Returns:
182
+ RunList: The list of runs.
183
+ """
184
+ if not thread_id:
185
+ raise InputRequired('thread_id is required!')
186
+ response = super().list(limit=limit,
187
+ order=order,
188
+ after=after,
189
+ before=before,
190
+ path=f'threads/{thread_id}/runs',
191
+ workspace=workspace,
192
+ api_key=api_key,
193
+ flattened_output=True,
194
+ **kwargs)
195
+ return RunList(**response)
196
+
197
+ @classmethod
198
+ def retrieve(cls,
199
+ run_id: str,
200
+ *,
201
+ thread_id: str,
202
+ workspace: str = None,
203
+ api_key: str = None,
204
+ **kwargs) -> Run:
205
+ """Retrieve the `Run`.
206
+
207
+ Args:
208
+ thread_id (str): The thread id.
209
+ run_id (str): The run id.
210
+ workspace (str): The dashscope workspace id.
211
+ api_key (str, optional): The api key. Defaults to None.
212
+
213
+ Returns:
214
+ Run: The `Run` object.
215
+ """
216
+ if not thread_id or not run_id:
217
+ raise InputRequired('thread_id and run_id are required!')
218
+ response = super().get(run_id,
219
+ path=f'threads/{thread_id}/runs/{run_id}',
220
+ workspace=workspace,
221
+ api_key=api_key,
222
+ flattened_output=True,
223
+ **kwargs)
224
+ return Run(**response)
225
+
226
+ @classmethod
227
+ def get(cls,
228
+ run_id: str,
229
+ *,
230
+ thread_id: str,
231
+ workspace: str = None,
232
+ api_key: str = None,
233
+ **kwargs) -> Run:
234
+ """Retrieve the `Run`.
235
+
236
+ Args:
237
+ thread_id (str): The thread id.
238
+ run_id (str): The run id.
239
+ workspace (str): The dashscope workspace id.
240
+ api_key (str, optional): The api key. Defaults to None.
241
+
242
+ Returns:
243
+ Run: The `Run` object.
244
+ """
245
+ return cls.retrieve(run_id,
246
+ thread_id=thread_id,
247
+ workspace=workspace,
248
+ api_key=api_key,
249
+ **kwargs)
250
+
251
+ @classmethod
252
+ def submit_tool_outputs(cls,
253
+ run_id: str,
254
+ *,
255
+ thread_id: str,
256
+ tool_outputs: List[Dict],
257
+ workspace: str = None,
258
+ api_key: str = None,
259
+ **kwargs) -> Run:
260
+ """_summary_
261
+
262
+ Args:
263
+ thread_id (str): The thread id.
264
+ run_id (str): The run id.
265
+ tool_outputs (List[Dict]): The tool outputs.
266
+ workspace (str): The dashscope workspace id.
267
+ api_key (str, optional): The api key. Defaults to None.
268
+
269
+ Raises:
270
+ InputRequired: The tool output thread id run id
271
+ are required.
272
+
273
+ Returns:
274
+ Run: The 'Run`.
275
+ """
276
+ if not tool_outputs:
277
+ raise InputRequired('tool_outputs is required!')
278
+ if not thread_id or not run_id:
279
+ raise InputRequired('thread_id and run_id are required!')
280
+
281
+ response = super().call(
282
+ {'tool_outputs': tool_outputs},
283
+ path=f'threads/{thread_id}/runs/{run_id}/submit_tool_outputs',
284
+ workspace=workspace,
285
+ api_key=api_key,
286
+ flattened_output=True,
287
+ **kwargs)
288
+ return Run(**response)
289
+
290
+ @classmethod
291
+ def wait(cls,
292
+ run_id: str,
293
+ *,
294
+ thread_id: str,
295
+ timeout_seconds: float = float('inf'),
296
+ workspace: str = None,
297
+ api_key: str = None,
298
+ **kwargs) -> Run:
299
+ """Wait for run to complete.
300
+
301
+ Args:
302
+ thread_id (str): The thread id.
303
+ run_id (str): The run id.
304
+ timeout_seconds (int): The timeout seconds. Defaults inf.
305
+ workspace (str): The dashscope workspace id.
306
+ api_key (str, optional): The api key. Defaults to None.
307
+
308
+ Returns:
309
+ Run: The run final status.
310
+ """
311
+ if not run_id or not thread_id:
312
+ raise InputRequired('run_id and thread_id are required!')
313
+ start_time = time.perf_counter()
314
+ while True:
315
+ run = cls.get(run_id,
316
+ thread_id=thread_id,
317
+ workspace=workspace,
318
+ api_key=api_key)
319
+ if run.status_code == HTTPStatus.OK:
320
+ if hasattr(run, 'status'):
321
+ if run.status in [
322
+ 'cancelled', 'failed', 'completed', 'expired',
323
+ 'requires_action'
324
+ ]:
325
+ break
326
+ else:
327
+ time_eclipsed = time.perf_counter() - start_time
328
+ if time_eclipsed > timeout_seconds:
329
+ raise TimeoutException('Wait run complete timeout')
330
+ time.sleep(1)
331
+ else:
332
+ logger.error('run has no status')
333
+ break
334
+ else:
335
+ logger.error(
336
+ 'Get run thread_id: %s, run_id: %s failed, message: %s' %
337
+ (thread_id, run_id, run.message))
338
+ break
339
+ return run
340
+
341
+ @classmethod
342
+ def update(cls,
343
+ run_id: str,
344
+ *,
345
+ thread_id: str,
346
+ metadata: Optional[Dict] = None,
347
+ workspace: str = None,
348
+ api_key: str = None,
349
+ **kwargs) -> Run:
350
+ """Create a run.
351
+
352
+ Args:
353
+ thread_id (str): The thread of the run id to be updated.
354
+ run_id (str): The run id to update.
355
+ model (str): The model to use.
356
+ metadata (Dict, optional): Custom key-value pairs associate with run. Defaults to None.
357
+ workspace (str): The DashScope workspace id.
358
+ api_key (str, optional): The DashScope workspace id. Defaults to None.
359
+
360
+ Raises:
361
+ InputRequired: thread id and run is required.
362
+
363
+ Returns:
364
+ Run: The `Run` object.
365
+ """
366
+ if not thread_id or not run_id:
367
+ raise InputRequired('thread id and run id are required!')
368
+ response = super().update(run_id,
369
+ json={'metadata': metadata},
370
+ path='threads/%s/runs/%s' %
371
+ (thread_id, run_id),
372
+ api_key=api_key,
373
+ workspace=workspace,
374
+ flattened_output=True,
375
+ method='post',
376
+ **kwargs)
377
+ return Run(**response)
378
+
379
+ @classmethod
380
+ def cancel(cls,
381
+ run_id: str,
382
+ *,
383
+ thread_id: str,
384
+ workspace: str = None,
385
+ api_key: str = None,
386
+ **kwargs) -> Run:
387
+ """Cancel the `Run`.
388
+
389
+ Args:
390
+ thread_id (str): The thread id.
391
+ run_id (str): The run id.
392
+ workspace (str): The dashscope workspace id.
393
+ api_key (str, optional): The api key. Defaults to None.
394
+
395
+ Returns:
396
+ Run: The `Run` object.
397
+ """
398
+ if not thread_id or not run_id:
399
+ raise InputRequired('thread id and run id are required!')
400
+ response = super().cancel(run_id,
401
+ path='threads/%s/runs/%s/cancel' %
402
+ (thread_id, run_id),
403
+ api_key=api_key,
404
+ workspace=workspace,
405
+ flattened_output=True,
406
+ **kwargs)
407
+
408
+ return Run(**response)
@@ -0,0 +1,110 @@
1
+ from dashscope.client.base_api import GetStatusMixin, ListObjectMixin
2
+ from dashscope.common.error import InputRequired
3
+ from dashscope.threads.thread_types import RunStep, RunStepList
4
+
5
+ __all__ = ['Steps']
6
+
7
+
8
+ class Steps(ListObjectMixin, GetStatusMixin):
9
+ SUB_PATH = 'RUNS' # useless
10
+
11
+ @classmethod
12
+ def list(cls,
13
+ run_id: str,
14
+ *,
15
+ thread_id: str,
16
+ limit: int = None,
17
+ order: str = None,
18
+ after: str = None,
19
+ before: str = None,
20
+ workspace: str = None,
21
+ api_key: str = None,
22
+ **kwargs) -> RunStepList:
23
+ """List `RunStep` of `Run`.
24
+
25
+ Args:
26
+ thread_id (str): The thread id.
27
+ run_id (str): The run id.
28
+ limit (int, optional): How many assistant to retrieve. Defaults to None.
29
+ order (str, optional): Sort order by created_at. Defaults to None.
30
+ after (str, optional): Assistant id after. Defaults to None.
31
+ before (str, optional): Assistant id before. Defaults to None.
32
+ workspace (str, optional): The DashScope workspace id. Defaults to None.
33
+ api_key (str, optional): Your DashScope api key. Defaults to None.
34
+
35
+ Returns:
36
+ RunList: The list of runs.
37
+ """
38
+ if not run_id:
39
+ raise InputRequired('run_id is required!')
40
+ response = super().list(
41
+ limit=limit,
42
+ order=order,
43
+ after=after,
44
+ before=before,
45
+ path=f'threads/{thread_id}/runs/{run_id}/steps',
46
+ workspace=workspace,
47
+ api_key=api_key,
48
+ flattened_output=True,
49
+ **kwargs)
50
+ return RunStepList(**response)
51
+
52
+ @classmethod
53
+ def retrieve(cls,
54
+ step_id: str,
55
+ *,
56
+ thread_id: str,
57
+ run_id: str,
58
+ workspace: str = None,
59
+ api_key: str = None,
60
+ **kwargs) -> RunStep:
61
+ """Retrieve the `RunStep`.
62
+
63
+ Args:
64
+ thread_id (str): The thread id.
65
+ run_id (str): The run id.
66
+ step_id (str): The step id.
67
+ workspace (str): The dashscope workspace id.
68
+ api_key (str, optional): The api key. Defaults to None.
69
+
70
+ Returns:
71
+ RunStep: The `RunStep` object.
72
+ """
73
+ if not thread_id or not run_id or not step_id:
74
+ raise InputRequired('thread_id, run_id and step_id are required!')
75
+ response = super().get(
76
+ run_id,
77
+ path=f'threads/{thread_id}/runs/{run_id}/steps/{step_id}',
78
+ workspace=workspace,
79
+ api_key=api_key,
80
+ flattened_output=True,
81
+ **kwargs)
82
+ return RunStep(**response)
83
+
84
+ @classmethod
85
+ def get(cls,
86
+ step_id: str,
87
+ *,
88
+ thread_id: str,
89
+ run_id: str,
90
+ workspace: str = None,
91
+ api_key: str = None,
92
+ **kwargs) -> RunStep:
93
+ """Retrieve the `RunStep`.
94
+
95
+ Args:
96
+ thread_id (str): The thread id.
97
+ run_id (str): The run id.
98
+ step_id (str): The step id.
99
+ workspace (str): The dashscope workspace id.
100
+ api_key (str, optional): The api key. Defaults to None.
101
+
102
+ Returns:
103
+ RunStep: The `RunStep` object.
104
+ """
105
+ return cls.retrieve(thread_id,
106
+ run_id,
107
+ step_id,
108
+ workspace=workspace,
109
+ api_key=api_key,
110
+ **kwargs)