dashscope 1.16.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.

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 -23
  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.0.dist-info}/METADATA +2 -3
  48. dashscope-1.17.0.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.0.dist-info}/LICENSE +0 -0
  51. {dashscope-1.16.0.dist-info → dashscope-1.17.0.dist-info}/WHEEL +0 -0
  52. {dashscope-1.16.0.dist-info → dashscope-1.17.0.dist-info}/entry_points.txt +0 -0
  53. {dashscope-1.16.0.dist-info → dashscope-1.17.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,571 @@
1
+ # adapter from openai sdk
2
+ # yapf: disable
3
+ from dataclasses import dataclass
4
+ from typing import Dict, List, Literal, Optional, Union
5
+
6
+ from dashscope.assistants.assistant_types import (
7
+ Tool, convert_tools_dict_to_objects)
8
+ from dashscope.common.base_type import BaseList, BaseObjectMixin
9
+
10
+ __all__ = [
11
+ 'MessageFile', 'MessageFileList', 'Usage', 'ImageFile',
12
+ 'MessageContentImageFile', 'Text', 'MessageContentText', 'ThreadMessage',
13
+ 'ThreadMessageList', 'Thread', 'Function',
14
+ 'RequiredActionFunctionToolCall', 'RequiredActionSubmitToolOutputs',
15
+ 'RequiredAction', 'LastError', 'Run', 'RunList', 'MessageCreation',
16
+ 'MessageCreationStepDetails', 'CodeInterpreterOutputLogs',
17
+ 'CodeInterpreterOutputImageImage', 'CodeInterpreterOutputImage',
18
+ 'CodeInterpreter', 'CodeToolCall', 'RetrievalToolCall', 'FunctionToolCall',
19
+ 'ToolCallsStepDetails', 'RunStep', 'RunStepList'
20
+ ]
21
+
22
+
23
+ @dataclass(init=False)
24
+ class MessageFile(BaseObjectMixin):
25
+ id: str
26
+ message_id: str
27
+ created_at: int
28
+ object: str
29
+
30
+ def __init__(self, **kwargs):
31
+ super().__init__(**kwargs)
32
+
33
+
34
+ @dataclass(init=False)
35
+ class MessageFileList(BaseList):
36
+ data: List[MessageFile]
37
+
38
+ def __init__(self, **kwargs):
39
+ super().__init__(**kwargs)
40
+
41
+
42
+ @dataclass(init=False)
43
+ class Usage(BaseObjectMixin):
44
+ completion_tokens: int
45
+ """Number of completion tokens used over the course of the run."""
46
+
47
+ prompt_tokens: int
48
+ """Number of prompt tokens used over the course of the run."""
49
+
50
+ total_tokens: int
51
+ """Total number of tokens used (prompt + completion)."""
52
+ def __init__(self, **kwargs):
53
+ super().__init__(**kwargs)
54
+
55
+
56
+ @dataclass(init=False)
57
+ class ImageFile(BaseObjectMixin):
58
+ file_id: str
59
+
60
+ def __init__(self, file_id, **kwargs):
61
+ super().__init__(**kwargs)
62
+
63
+
64
+ @dataclass(init=False)
65
+ class MessageContentImageFile(BaseObjectMixin):
66
+ type: str = 'image_file'
67
+ image_file: ImageFile
68
+
69
+ def __init__(self, **kwargs):
70
+ self.image_file = ImageFile(**kwargs.pop(self.type, {}))
71
+ super().__init__(**kwargs)
72
+
73
+
74
+ TextAnnotation = Union[Dict]
75
+
76
+
77
+ @dataclass(init=False)
78
+ class Text(BaseObjectMixin):
79
+ annotations: List[TextAnnotation]
80
+ value: str
81
+
82
+ def __init__(self, **kwargs):
83
+ annotations = kwargs.pop('annotations', None)
84
+ if annotations:
85
+ self.annotations = []
86
+ for annotation in annotations:
87
+ self.annotations.append(annotation)
88
+ super().__init__(**kwargs)
89
+
90
+
91
+ @dataclass(init=False)
92
+ class MessageContentText(BaseObjectMixin):
93
+ text: Text
94
+ type: str = 'text'
95
+
96
+ def __init__(self, **kwargs):
97
+ input_text = kwargs.pop('text', {})
98
+ if input_text:
99
+ self.text = Text(**input_text)
100
+ super().__init__(**kwargs)
101
+
102
+
103
+ MESSAGE_SUPPORT_CONTENT = {
104
+ 'text': MessageContentText,
105
+ 'image_file': MessageContentImageFile,
106
+ }
107
+
108
+ Content = Union[MessageContentImageFile, MessageContentText]
109
+
110
+
111
+ @dataclass(init=False)
112
+ class ThreadMessage(BaseObjectMixin):
113
+ status_code: int
114
+ id: str
115
+ created_at: int
116
+ thread_id: str
117
+ role: str
118
+ content: List[Content]
119
+ metadata: Optional[object] = None
120
+ object: str = 'thread.message'
121
+ assistant_id: Optional[str] = None
122
+ file_ids: List[str] = None
123
+ run_id: Optional[str] = None
124
+
125
+ def __init__(self, **kwargs):
126
+ input_content = kwargs.pop('content', None)
127
+ if input_content:
128
+ content_list = []
129
+ for content in input_content:
130
+ if 'type' in content:
131
+ content_type = MESSAGE_SUPPORT_CONTENT.get(
132
+ content['type'], None)
133
+ if content_type:
134
+ content_list.append(content_type(**content))
135
+ else:
136
+ content_list.append(content)
137
+ else:
138
+ content_list.append(content)
139
+ self.content = content_list
140
+
141
+ super().__init__(**kwargs)
142
+
143
+
144
+ @dataclass(init=False)
145
+ class ThreadMessageList(BaseList):
146
+ data: List[ThreadMessage]
147
+
148
+ def __init__(self,
149
+ has_more: bool = None,
150
+ last_id: Optional[str] = None,
151
+ first_id: Optional[str] = None,
152
+ data: List[ThreadMessage] = [],
153
+ **kwargs):
154
+ super().__init__(has_more=has_more,
155
+ last_id=last_id,
156
+ first_id=first_id,
157
+ data=data,
158
+ **kwargs)
159
+
160
+
161
+ @dataclass(init=False)
162
+ class Thread(BaseObjectMixin):
163
+ status_code: int
164
+ id: str
165
+ created_at: int
166
+ metadata: Optional[object] = None
167
+ object: str = 'thread'
168
+
169
+ def __init__(self, **kwargs):
170
+ super().__init__(**kwargs)
171
+
172
+
173
+ @dataclass(init=False)
174
+ class Function(BaseObjectMixin):
175
+ arguments: str
176
+ name: str
177
+ output: Optional[str] = None
178
+
179
+ def __init__(self, **kwargs):
180
+ super().__init__(**kwargs)
181
+
182
+
183
+ @dataclass(init=False)
184
+ class RequiredActionFunctionToolCall(BaseObjectMixin):
185
+ id: str
186
+
187
+ function: Function
188
+
189
+ type: str = 'function'
190
+
191
+ def __init__(self, **kwargs):
192
+ self.function = Function(**kwargs.pop('function', {}))
193
+ super().__init__(**kwargs)
194
+
195
+
196
+ @dataclass(init=False)
197
+ class RequiredActionSubmitToolOutputs(BaseObjectMixin):
198
+ tool_calls: List[RequiredActionFunctionToolCall]
199
+
200
+ def __init__(self, **kwargs):
201
+ tcs = kwargs.pop('tool_calls', [])
202
+ if tcs:
203
+ self.tool_calls = []
204
+ for tc in tcs:
205
+ self.tool_calls.append(RequiredActionFunctionToolCall(**tc))
206
+ super().__init__(**kwargs)
207
+
208
+
209
+ @dataclass(init=False)
210
+ class RequiredAction(BaseObjectMixin):
211
+ submit_tool_outputs: RequiredActionSubmitToolOutputs
212
+
213
+ type: Literal['submit_tool_outputs']
214
+
215
+ def __init__(self, **kwargs):
216
+ self.submit_tool_outputs = RequiredActionSubmitToolOutputs(
217
+ **kwargs.pop('submit_tool_outputs', {}))
218
+ super().__init__(**kwargs)
219
+
220
+
221
+ @dataclass(init=False)
222
+ class LastError(BaseObjectMixin):
223
+ code: Literal['server_error', 'rate_limit_exceeded']
224
+ message: str
225
+
226
+ def __init__(self, **kwargs):
227
+ super().__init__(**kwargs)
228
+
229
+
230
+ @dataclass(init=False)
231
+ class Run(BaseObjectMixin):
232
+ status_code: int
233
+ id: str
234
+ assistant_id: str
235
+ cancelled_at: Optional[int] = None
236
+ completed_at: Optional[int] = None
237
+ created_at: int = None
238
+ expires_at: int = None
239
+ failed_at: Optional[int] = None
240
+ file_ids: List[str] = None
241
+
242
+ instructions: str = None
243
+
244
+ metadata: Optional[object] = None
245
+ last_error: Optional[LastError] = None
246
+ model: str = None
247
+
248
+ object: str = 'thread.run'
249
+
250
+ required_action: Optional[RequiredAction] = None
251
+
252
+ started_at: Optional[int] = None
253
+
254
+ status: Literal['queued', 'in_progress', 'requires_action', 'cancelling',
255
+ 'cancelled', 'failed', 'completed', 'expired']
256
+
257
+ thread_id: str
258
+
259
+ tools: List[Tool]
260
+
261
+ usage: Optional[Usage] = None
262
+
263
+ def __init__(self, **kwargs):
264
+ self.tools = convert_tools_dict_to_objects(kwargs.pop('tools', []))
265
+ actions = kwargs.pop('required_action', None)
266
+ if actions:
267
+ self.required_action = RequiredAction(**actions)
268
+
269
+ super().__init__(**kwargs)
270
+
271
+
272
+ @dataclass(init=False)
273
+ class RunList(BaseObjectMixin):
274
+ data: List[Run]
275
+
276
+ def __init__(self,
277
+ has_more: bool = None,
278
+ last_id: Optional[str] = None,
279
+ first_id: Optional[str] = None,
280
+ data: List[Run] = [],
281
+ **kwargs):
282
+ super().__init__(has_more=has_more,
283
+ last_id=last_id,
284
+ first_id=first_id,
285
+ data=data,
286
+ **kwargs)
287
+
288
+
289
+ @dataclass(init=False)
290
+ class MessageCreation(BaseObjectMixin):
291
+ message_id: str
292
+ """The ID of the message that was created by this run step."""
293
+ def __init__(self, **kwargs):
294
+ super().__init__(**kwargs)
295
+
296
+
297
+ @dataclass(init=False)
298
+ class MessageCreationStepDetails(BaseObjectMixin):
299
+ message_creation: MessageCreation
300
+
301
+ type: Literal['message_creation']
302
+ """Always `message_creation`."""
303
+ def __init__(self, **kwargs):
304
+ super().__init__(**kwargs)
305
+
306
+
307
+ @dataclass(init=False)
308
+ class CodeInterpreterOutputLogs(BaseObjectMixin):
309
+ logs: str
310
+ """The text output from the Code Interpreter tool call."""
311
+
312
+ type: Literal['logs']
313
+ """Always `logs`."""
314
+ def __init__(self, **kwargs):
315
+ super().__init__(**kwargs)
316
+
317
+
318
+ @dataclass(init=False)
319
+ class CodeInterpreterOutputImageImage(BaseObjectMixin):
320
+ file_id: str
321
+ """
322
+ The [file](https://platform.openai.com/docs/api-reference/files) ID of the
323
+ image.
324
+ """
325
+ def __init__(self, **kwargs):
326
+ super().__init__(**kwargs)
327
+
328
+
329
+ @dataclass(init=False)
330
+ class CodeInterpreterOutputImage(BaseObjectMixin):
331
+ image: CodeInterpreterOutputImageImage
332
+
333
+ type: Literal['image']
334
+ """Always `image`."""
335
+ def __init__(self, **kwargs):
336
+ self.image = CodeInterpreterOutputImageImage(**kwargs.pop('image', {}))
337
+ super().__init__(**kwargs)
338
+
339
+
340
+ CodeInterpreterOutput = Union[CodeInterpreterOutputLogs,
341
+ CodeInterpreterOutputImage]
342
+
343
+
344
+ @dataclass(init=False)
345
+ class CodeInterpreter(BaseObjectMixin):
346
+ input: str
347
+ """The input to the Code Interpreter tool call."""
348
+
349
+ outputs: List[CodeInterpreterOutput]
350
+ """The outputs from the Code Interpreter tool call.
351
+
352
+ Code Interpreter can output one or more items, including text (`logs`) or images
353
+ (`image`). Each of these are represented by a different object type.
354
+ """
355
+ def __init__(self, **kwargs):
356
+ self.outputs = []
357
+ for output in kwargs.pop('outputs', []):
358
+ self.outputs.append(CodeInterpreterOutput(**output))
359
+
360
+ super().__init__(**kwargs)
361
+
362
+
363
+ @dataclass(init=False)
364
+ class CodeToolCall(BaseObjectMixin):
365
+ id: str
366
+ """The ID of the tool call."""
367
+
368
+ code_interpreter: CodeInterpreter
369
+ """The Code Interpreter tool call definition."""
370
+
371
+ type: Literal['code_interpreter']
372
+ """The type of tool call.
373
+
374
+ This is always going to be `code_interpreter` for this type of tool call.
375
+ """
376
+ def __init__(self, **kwargs):
377
+ self.code_interpreter = CodeInterpreter(
378
+ **kwargs.pop('code_interpreter', {}))
379
+ super().__init__(**kwargs)
380
+
381
+
382
+ @dataclass(init=False)
383
+ class RetrievalToolCall(BaseObjectMixin):
384
+ id: str
385
+ """The ID of the tool call object."""
386
+
387
+ retrieval: object
388
+ """For now, this is always going to be an empty object."""
389
+
390
+ type: Literal['retrieval']
391
+ """The type of tool call.
392
+
393
+ This is always going to be `retrieval` for this type of tool call.
394
+ """
395
+ def __init__(self, **kwargs):
396
+ super().__init__(**kwargs)
397
+
398
+
399
+ @dataclass(init=False)
400
+ class FunctionToolCall(BaseObjectMixin):
401
+ id: str
402
+ """The ID of the tool call object."""
403
+
404
+ function: Function
405
+ """The definition of the function that was called."""
406
+
407
+ type: Literal['function']
408
+ """The type of tool call.
409
+
410
+ This is always going to be `function` for this type of tool call.
411
+ """
412
+ def __init__(self, **kwargs):
413
+ self.function = Function(**kwargs.pop('function', {}))
414
+ super().__init__(**kwargs)
415
+
416
+
417
+ ToolCall = Union[CodeToolCall, RetrievalToolCall, FunctionToolCall]
418
+
419
+ TOOL_CALL_TYPES = {
420
+ 'function': FunctionToolCall,
421
+ 'code_interpreter': CodeToolCall,
422
+ 'retrieval': RetrievalToolCall,
423
+ }
424
+
425
+
426
+ def convert_tool_calls_dict_to_object(tool_calls):
427
+ tool_calls_object = []
428
+ for tool_call in tool_calls:
429
+ if 'type' in tool_call:
430
+ tool_call_type = TOOL_CALL_TYPES.get(tool_call['type'], None)
431
+ if tool_call_type:
432
+ tool_calls_object.append(tool_call_type(**tool_call))
433
+ else:
434
+ tool_calls_object.append(tool_call)
435
+ else:
436
+ tool_calls_object.append(tool_call)
437
+ return tool_calls_object
438
+
439
+
440
+ @dataclass(init=False)
441
+ class ToolCallsStepDetails(BaseObjectMixin):
442
+ tool_calls: List[ToolCall]
443
+ """An array of tool calls the run step was involved in.
444
+
445
+ These can be associated with one of three types of tools: `code_interpreter`,
446
+ `retrieval`, or `function`.
447
+ """
448
+
449
+ type: Literal['tool_calls']
450
+ """Always `tool_calls`."""
451
+ def __init__(self, **kwargs):
452
+ self.tool_calls = convert_tool_calls_dict_to_object(
453
+ kwargs.pop('tool_calls', []))
454
+ super().__init__(**kwargs)
455
+
456
+
457
+ StepDetails = Union[MessageCreationStepDetails, ToolCallsStepDetails]
458
+ STEP_TYPES = {
459
+ 'tool_calls': ToolCallsStepDetails,
460
+ 'message_creation': MessageCreationStepDetails,
461
+ }
462
+
463
+
464
+ def convert_step_details_dict_to_objects(step_details):
465
+ if 'type' in step_details:
466
+ tool_type = STEP_TYPES.get(step_details['type'], None)
467
+ if tool_type:
468
+ return tool_type(**step_details)
469
+ return step_details
470
+
471
+
472
+ @dataclass(init=False)
473
+ class RunStep(BaseObjectMixin):
474
+ status_code: int
475
+ id: str
476
+ """The identifier of the run step, which can be referenced in API endpoints."""
477
+
478
+ assistant_id: str
479
+ """
480
+ The ID of the
481
+ [assistant](https://platform.openai.com/docs/api-reference/assistants)
482
+ associated with the run step.
483
+ """
484
+
485
+ cancelled_at: Optional[int] = None
486
+ """The Unix timestamp (in seconds) for when the run step was cancelled."""
487
+
488
+ completed_at: Optional[int] = None
489
+ """The Unix timestamp (in seconds) for when the run step completed."""
490
+
491
+ created_at: int
492
+ """The Unix timestamp (in seconds) for when the run step was created."""
493
+
494
+ expired_at: Optional[int] = None
495
+ """The Unix timestamp (in seconds) for when the run step expired.
496
+
497
+ A step is considered expired if the parent run is expired.
498
+ """
499
+
500
+ failed_at: Optional[int] = None
501
+ """The Unix timestamp (in seconds) for when the run step failed."""
502
+
503
+ last_error: Optional[LastError] = None
504
+ """The last error associated with this run step.
505
+
506
+ Will be `null` if there are no errors.
507
+ """
508
+
509
+ metadata: Optional[object] = None
510
+ """Set of 16 key-value pairs that can be attached to an object.
511
+
512
+ This can be useful for storing additional information about the object in a
513
+ structured format. Keys can be a maximum of 64 characters long and values can be
514
+ a maxium of 512 characters long.
515
+ """
516
+
517
+ object: Literal['thread.run.step']
518
+ """The object type, which is always `thread.run.step`."""
519
+
520
+ run_id: str
521
+ """
522
+ The ID of the [run](https://platform.openai.com/docs/api-reference/runs) that
523
+ this run step is a part of.
524
+ """
525
+
526
+ status: Literal['in_progress', 'cancelled', 'failed', 'completed',
527
+ 'expired']
528
+ """
529
+ The status of the run step, which can be either `in_progress`, `cancelled`,
530
+ `failed`, `completed`, or `expired`.
531
+ """
532
+
533
+ step_details: StepDetails
534
+ """The details of the run step."""
535
+
536
+ thread_id: str
537
+ """
538
+ The ID of the [thread](https://platform.openai.com/docs/api-reference/threads)
539
+ that was run.
540
+ """
541
+
542
+ type: Literal['message_creation', 'tool_calls']
543
+ """The type of run step, which can be either `message_creation` or `tool_calls`."""
544
+
545
+ usage: Optional[Usage] = None
546
+
547
+ def __init__(self, **kwargs):
548
+ self.step_details = convert_step_details_dict_to_objects(
549
+ kwargs.pop('step_details', {}))
550
+ self.usage = Usage(**kwargs.pop('usage', {}))
551
+ last_error = kwargs.pop('last_error', None)
552
+ if last_error:
553
+ self.last_error = LastError(**last_error)
554
+ super().__init__(**kwargs)
555
+
556
+
557
+ @dataclass(init=False)
558
+ class RunStepList(BaseList):
559
+ data: List[RunStep]
560
+
561
+ def __init__(self,
562
+ has_more: bool = None,
563
+ last_id: Optional[str] = None,
564
+ first_id: Optional[str] = None,
565
+ data: List[RunStep] = [],
566
+ **kwargs):
567
+ super().__init__(has_more=has_more,
568
+ last_id=last_id,
569
+ first_id=first_id,
570
+ data=data,
571
+ **kwargs)