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.
Files changed (110) hide show
  1. dashscope/__init__.py +61 -14
  2. dashscope/aigc/__init__.py +10 -3
  3. dashscope/aigc/chat_completion.py +282 -0
  4. dashscope/aigc/code_generation.py +145 -0
  5. dashscope/aigc/conversation.py +71 -12
  6. dashscope/aigc/generation.py +288 -16
  7. dashscope/aigc/image_synthesis.py +473 -31
  8. dashscope/aigc/multimodal_conversation.py +299 -14
  9. dashscope/aigc/video_synthesis.py +610 -0
  10. dashscope/api_entities/aiohttp_request.py +8 -5
  11. dashscope/api_entities/api_request_data.py +4 -2
  12. dashscope/api_entities/api_request_factory.py +68 -20
  13. dashscope/api_entities/base_request.py +20 -3
  14. dashscope/api_entities/chat_completion_types.py +344 -0
  15. dashscope/api_entities/dashscope_response.py +243 -15
  16. dashscope/api_entities/encryption.py +179 -0
  17. dashscope/api_entities/http_request.py +216 -62
  18. dashscope/api_entities/websocket_request.py +43 -34
  19. dashscope/app/__init__.py +5 -0
  20. dashscope/app/application.py +203 -0
  21. dashscope/app/application_response.py +246 -0
  22. dashscope/assistants/__init__.py +16 -0
  23. dashscope/assistants/assistant_types.py +175 -0
  24. dashscope/assistants/assistants.py +311 -0
  25. dashscope/assistants/files.py +197 -0
  26. dashscope/audio/__init__.py +4 -2
  27. dashscope/audio/asr/__init__.py +17 -1
  28. dashscope/audio/asr/asr_phrase_manager.py +203 -0
  29. dashscope/audio/asr/recognition.py +167 -27
  30. dashscope/audio/asr/transcription.py +107 -14
  31. dashscope/audio/asr/translation_recognizer.py +1006 -0
  32. dashscope/audio/asr/vocabulary.py +177 -0
  33. dashscope/audio/qwen_asr/__init__.py +7 -0
  34. dashscope/audio/qwen_asr/qwen_transcription.py +189 -0
  35. dashscope/audio/qwen_omni/__init__.py +11 -0
  36. dashscope/audio/qwen_omni/omni_realtime.py +524 -0
  37. dashscope/audio/qwen_tts/__init__.py +5 -0
  38. dashscope/audio/qwen_tts/speech_synthesizer.py +77 -0
  39. dashscope/audio/qwen_tts_realtime/__init__.py +10 -0
  40. dashscope/audio/qwen_tts_realtime/qwen_tts_realtime.py +355 -0
  41. dashscope/audio/tts/__init__.py +2 -0
  42. dashscope/audio/tts/speech_synthesizer.py +5 -0
  43. dashscope/audio/tts_v2/__init__.py +12 -0
  44. dashscope/audio/tts_v2/enrollment.py +179 -0
  45. dashscope/audio/tts_v2/speech_synthesizer.py +886 -0
  46. dashscope/cli.py +157 -37
  47. dashscope/client/base_api.py +652 -87
  48. dashscope/common/api_key.py +2 -0
  49. dashscope/common/base_type.py +135 -0
  50. dashscope/common/constants.py +13 -16
  51. dashscope/common/env.py +2 -0
  52. dashscope/common/error.py +58 -22
  53. dashscope/common/logging.py +2 -0
  54. dashscope/common/message_manager.py +2 -0
  55. dashscope/common/utils.py +276 -46
  56. dashscope/customize/__init__.py +0 -0
  57. dashscope/customize/customize_types.py +192 -0
  58. dashscope/customize/deployments.py +146 -0
  59. dashscope/customize/finetunes.py +234 -0
  60. dashscope/embeddings/__init__.py +5 -1
  61. dashscope/embeddings/batch_text_embedding.py +208 -0
  62. dashscope/embeddings/batch_text_embedding_response.py +65 -0
  63. dashscope/embeddings/multimodal_embedding.py +118 -10
  64. dashscope/embeddings/text_embedding.py +13 -1
  65. dashscope/{file.py → files.py} +19 -4
  66. dashscope/io/input_output.py +2 -0
  67. dashscope/model.py +11 -2
  68. dashscope/models.py +43 -0
  69. dashscope/multimodal/__init__.py +20 -0
  70. dashscope/multimodal/dialog_state.py +56 -0
  71. dashscope/multimodal/multimodal_constants.py +28 -0
  72. dashscope/multimodal/multimodal_dialog.py +648 -0
  73. dashscope/multimodal/multimodal_request_params.py +313 -0
  74. dashscope/multimodal/tingwu/__init__.py +10 -0
  75. dashscope/multimodal/tingwu/tingwu.py +80 -0
  76. dashscope/multimodal/tingwu/tingwu_realtime.py +579 -0
  77. dashscope/nlp/__init__.py +0 -0
  78. dashscope/nlp/understanding.py +64 -0
  79. dashscope/protocol/websocket.py +3 -0
  80. dashscope/rerank/__init__.py +0 -0
  81. dashscope/rerank/text_rerank.py +69 -0
  82. dashscope/resources/qwen.tiktoken +151643 -0
  83. dashscope/threads/__init__.py +26 -0
  84. dashscope/threads/messages/__init__.py +0 -0
  85. dashscope/threads/messages/files.py +113 -0
  86. dashscope/threads/messages/messages.py +220 -0
  87. dashscope/threads/runs/__init__.py +0 -0
  88. dashscope/threads/runs/runs.py +501 -0
  89. dashscope/threads/runs/steps.py +112 -0
  90. dashscope/threads/thread_types.py +665 -0
  91. dashscope/threads/threads.py +212 -0
  92. dashscope/tokenizers/__init__.py +7 -0
  93. dashscope/tokenizers/qwen_tokenizer.py +111 -0
  94. dashscope/tokenizers/tokenization.py +125 -0
  95. dashscope/tokenizers/tokenizer.py +45 -0
  96. dashscope/tokenizers/tokenizer_base.py +32 -0
  97. dashscope/utils/__init__.py +0 -0
  98. dashscope/utils/message_utils.py +838 -0
  99. dashscope/utils/oss_utils.py +243 -0
  100. dashscope/utils/param_utils.py +29 -0
  101. dashscope/version.py +3 -1
  102. {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/METADATA +53 -50
  103. dashscope-1.25.6.dist-info/RECORD +112 -0
  104. {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/WHEEL +1 -1
  105. {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/entry_points.txt +0 -1
  106. {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info/licenses}/LICENSE +2 -4
  107. dashscope/deployment.py +0 -129
  108. dashscope/finetune.py +0 -149
  109. dashscope-1.8.0.dist-info/RECORD +0 -49
  110. {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,177 @@
1
+ # Copyright (c) Alibaba, Inc. and its affiliates.
2
+
3
+ import asyncio
4
+ import time
5
+ from typing import List
6
+
7
+ import aiohttp
8
+
9
+ from dashscope.client.base_api import BaseApi
10
+ from dashscope.common.constants import ApiProtocol, HTTPMethod
11
+ from dashscope.common.logging import logger
12
+
13
+
14
+ class VocabularyServiceException(Exception):
15
+ def __init__(self, request_id: str, status_code: int, code: str,
16
+ error_message: str) -> None:
17
+ self._request_id = request_id
18
+ self._status_code = status_code
19
+ self._code = code
20
+ self._error_message = error_message
21
+
22
+ def __str__(self):
23
+ return f'Request: {self._request_id}, Status Code: {self._status_code}, Code: {self._code}, Error Message: {self._error_message}'
24
+
25
+
26
+ class VocabularyService(BaseApi):
27
+ '''
28
+ API for asr vocabulary service
29
+ '''
30
+ MAX_QUERY_TRY_COUNT = 3
31
+
32
+ def __init__(self,
33
+ api_key=None,
34
+ workspace=None,
35
+ model=None,
36
+ **kwargs) -> None:
37
+ super().__init__()
38
+ self._api_key = api_key
39
+ self._workspace = workspace
40
+ self._kwargs = kwargs
41
+ self._last_request_id = None
42
+ self.model = model
43
+ if self.model is None:
44
+ self.model = 'speech-biasing'
45
+
46
+ def __call_with_input(self, input):
47
+ try_count = 0
48
+ while True:
49
+ try:
50
+ response = super().call(model=self.model,
51
+ task_group='audio',
52
+ task='asr',
53
+ function='customization',
54
+ input=input,
55
+ api_protocol=ApiProtocol.HTTP,
56
+ http_method=HTTPMethod.POST,
57
+ api_key=self._api_key,
58
+ workspace=self._workspace,
59
+ **self._kwargs)
60
+ except (asyncio.TimeoutError, aiohttp.ClientConnectorError) as e:
61
+ logger.error(e)
62
+ try_count += 1
63
+ if try_count <= VocabularyService.MAX_QUERY_TRY_COUNT:
64
+ time.sleep(2)
65
+ continue
66
+
67
+ break
68
+ logger.debug('>>>>recv', response)
69
+ return response
70
+
71
+ def create_vocabulary(self, target_model: str, prefix: str,
72
+ vocabulary: List[dict]) -> str:
73
+ '''
74
+ 创建热词表
75
+ param: target_model 热词表对应的语音识别模型版本
76
+ param: prefix 热词表自定义前缀,仅允许数字和小写字母,小于十个字符。
77
+ param: vocabulary 热词表字典
78
+ return: 热词表标识符 vocabulary_id
79
+ '''
80
+ response = self.__call_with_input(input={
81
+ 'action': 'create_vocabulary',
82
+ 'target_model': target_model,
83
+ 'prefix': prefix,
84
+ 'vocabulary': vocabulary,
85
+ }, )
86
+ if response.status_code == 200:
87
+ self._last_request_id = response.request_id
88
+ return response.output['vocabulary_id']
89
+ else:
90
+ raise VocabularyServiceException(response.request_id, response.status_code,
91
+ response.code, response.message)
92
+
93
+ def list_vocabularies(self,
94
+ prefix=None,
95
+ page_index: int = 0,
96
+ page_size: int = 10) -> List[dict]:
97
+ '''
98
+ 查询已创建的所有热词表
99
+ param: prefix 自定义前缀,如果设定则只返回指定前缀的热词表标识符列表。
100
+ param: page_index 查询的页索引
101
+ param: page_size 查询页大小
102
+ return: 热词表标识符列表
103
+ '''
104
+ if prefix:
105
+ response = self.__call_with_input(input={
106
+ 'action': 'list_vocabulary',
107
+ 'prefix': prefix,
108
+ 'page_index': page_index,
109
+ 'page_size': page_size,
110
+ }, )
111
+ else:
112
+ response = self.__call_with_input(input={
113
+ 'action': 'list_vocabulary',
114
+ 'page_index': page_index,
115
+ 'page_size': page_size,
116
+ }, )
117
+ if response.status_code == 200:
118
+ self._last_request_id = response.request_id
119
+ return response.output['vocabulary_list']
120
+ else:
121
+ raise VocabularyServiceException(response.request_id, response.status_code,
122
+ response.code, response.message)
123
+
124
+ def query_vocabulary(self, vocabulary_id: str) -> List[dict]:
125
+ '''
126
+ 获取热词表内容
127
+ param: vocabulary_id 热词表标识符
128
+ return: 热词表
129
+ '''
130
+ response = self.__call_with_input(input={
131
+ 'action': 'query_vocabulary',
132
+ 'vocabulary_id': vocabulary_id,
133
+ }, )
134
+ if response.status_code == 200:
135
+ self._last_request_id = response.request_id
136
+ return response.output
137
+ else:
138
+ raise VocabularyServiceException(response.request_id, response.status_code,
139
+ response.code, response.message)
140
+
141
+ def update_vocabulary(self, vocabulary_id: str,
142
+ vocabulary: List[dict]) -> None:
143
+ '''
144
+ 用新的热词表替换已有热词表
145
+ param: vocabulary_id 需要替换的热词表标识符
146
+ param: vocabulary 热词表
147
+ '''
148
+ response = self.__call_with_input(input={
149
+ 'action': 'update_vocabulary',
150
+ 'vocabulary_id': vocabulary_id,
151
+ 'vocabulary': vocabulary,
152
+ }, )
153
+ if response.status_code == 200:
154
+ self._last_request_id = response.request_id
155
+ return
156
+ else:
157
+ raise VocabularyServiceException(response.request_id, response.status_code,
158
+ response.code, response.message)
159
+
160
+ def delete_vocabulary(self, vocabulary_id: str) -> None:
161
+ '''
162
+ 删除热词表
163
+ param: vocabulary_id 需要删除的热词表标识符
164
+ '''
165
+ response = self.__call_with_input(input={
166
+ 'action': 'delete_vocabulary',
167
+ 'vocabulary_id': vocabulary_id,
168
+ }, )
169
+ if response.status_code == 200:
170
+ self._last_request_id = response.request_id
171
+ return
172
+ else:
173
+ raise VocabularyServiceException(response.request_id, response.status_code,
174
+ response.code, response.message)
175
+
176
+ def get_last_request_id(self):
177
+ return self._last_request_id
@@ -0,0 +1,7 @@
1
+ # Copyright (c) Alibaba, Inc. and its affiliates.
2
+
3
+ from .qwen_transcription import (QwenTranscription)
4
+
5
+ __all__ = [
6
+ 'QwenTranscription',
7
+ ]
@@ -0,0 +1,189 @@
1
+ # Copyright (c) Alibaba, Inc. and its affiliates.
2
+
3
+ import asyncio
4
+ import time
5
+ from typing import List, Union
6
+
7
+ import aiohttp
8
+
9
+ from dashscope.api_entities.dashscope_response import (DashScopeAPIResponse,
10
+ TranscriptionResponse)
11
+ from dashscope.client.base_api import BaseAsyncApi
12
+ from dashscope.common.constants import ApiProtocol, HTTPMethod
13
+ from dashscope.common.logging import logger
14
+
15
+
16
+ class QwenTranscription(BaseAsyncApi):
17
+ """API for File Transcription models.
18
+ """
19
+
20
+ MAX_QUERY_TRY_COUNT = 3
21
+
22
+ @classmethod
23
+ def call(cls,
24
+ model: str,
25
+ file_url: str,
26
+ api_key: str = None,
27
+ workspace: str = None,
28
+ **kwargs) -> TranscriptionResponse:
29
+ """Transcribe the given files synchronously.
30
+
31
+ Args:
32
+ model (str): The requested model_id.
33
+ file_url (str): stored URL.
34
+ workspace (str): The dashscope workspace id.
35
+
36
+ Returns:
37
+ TranscriptionResponse: The result of batch transcription.
38
+ """
39
+ kwargs = cls._tidy_kwargs(**kwargs)
40
+ response = super().call(model,
41
+ file_url,
42
+ api_key=api_key,
43
+ workspace=workspace,
44
+ **kwargs)
45
+ return TranscriptionResponse.from_api_response(response)
46
+
47
+ @classmethod
48
+ def async_call(cls,
49
+ model: str,
50
+ file_url: str,
51
+ api_key: str = None,
52
+ workspace: str = None,
53
+ **kwargs) -> TranscriptionResponse:
54
+ """Transcribe the given files asynchronously,
55
+ return the status of task submission for querying results subsequently.
56
+
57
+ Args:
58
+ model (str): The requested model, such as paraformer-16k-1
59
+ file_url (str): stored URL.
60
+ workspace (str): The dashscope workspace id.
61
+
62
+ Returns:
63
+ TranscriptionResponse: The response including task_id.
64
+ """
65
+ kwargs = cls._tidy_kwargs(**kwargs)
66
+ response = cls._launch_request(model,
67
+ file_url,
68
+ api_key=api_key,
69
+ workspace=workspace,
70
+ **kwargs)
71
+ return TranscriptionResponse.from_api_response(response)
72
+
73
+ @classmethod
74
+ def fetch(cls,
75
+ task: Union[str, TranscriptionResponse],
76
+ api_key: str = None,
77
+ workspace: str = None,
78
+ **kwargs) -> TranscriptionResponse:
79
+ """Fetch the status of task, including results of batch transcription when task_status is SUCCEEDED. # noqa: E501
80
+
81
+ Args:
82
+ task (Union[str, TranscriptionResponse]): The task_id or
83
+ response including task_id returned from async_call().
84
+ workspace (str): The dashscope workspace id.
85
+
86
+ Returns:
87
+ TranscriptionResponse: The status of task_id,
88
+ including results of batch transcription when task_status is SUCCEEDED.
89
+ """
90
+ try_count: int = 0
91
+ while True:
92
+ try:
93
+ response = super().fetch(task,
94
+ api_key=api_key,
95
+ workspace=workspace,
96
+ **kwargs)
97
+ except (asyncio.TimeoutError, aiohttp.ClientConnectorError) as e:
98
+ logger.error(e)
99
+ try_count += 1
100
+ if try_count <= QwenTranscription.MAX_QUERY_TRY_COUNT:
101
+ time.sleep(2)
102
+ continue
103
+
104
+ try_count = 0
105
+ break
106
+
107
+ return TranscriptionResponse.from_api_response(response)
108
+
109
+ @classmethod
110
+ def wait(cls,
111
+ task: Union[str, TranscriptionResponse],
112
+ api_key: str = None,
113
+ workspace: str = None,
114
+ **kwargs) -> TranscriptionResponse:
115
+ """Poll task until the final results of transcription is obtained.
116
+
117
+ Args:
118
+ task (Union[str, TranscriptionResponse]): The task_id or
119
+ response including task_id returned from async_call().
120
+ workspace (str): The dashscope workspace id.
121
+
122
+ Returns:
123
+ TranscriptionResponse: The result of batch transcription.
124
+ """
125
+ response = super().wait(task,
126
+ api_key=api_key,
127
+ workspace=workspace,
128
+ **kwargs)
129
+ return TranscriptionResponse.from_api_response(response)
130
+
131
+ @classmethod
132
+ def _launch_request(cls,
133
+ model: str,
134
+ file: str,
135
+ api_key: str = None,
136
+ workspace: str = None,
137
+ **kwargs) -> DashScopeAPIResponse:
138
+ """Submit transcribe request.
139
+
140
+ Args:
141
+ model (str): The requested model, such as paraformer-16k-1
142
+ files (List[str]): List of stored URLs.
143
+ workspace (str): The dashscope workspace id.
144
+
145
+ Returns:
146
+ DashScopeAPIResponse: The result of task submission.
147
+ """
148
+
149
+ try_count: int = 0
150
+ while True:
151
+ try:
152
+ response = super().async_call(model=model,
153
+ task_group='audio',
154
+ task='asr',
155
+ function='transcription',
156
+ input={'file_url': file},
157
+ api_protocol=ApiProtocol.HTTP,
158
+ http_method=HTTPMethod.POST,
159
+ api_key=api_key,
160
+ workspace=workspace,
161
+ **kwargs)
162
+ except (asyncio.TimeoutError, aiohttp.ClientConnectorError) as e:
163
+ logger.error(e)
164
+ try_count += 1
165
+ if try_count <= QwenTranscription.MAX_QUERY_TRY_COUNT:
166
+ time.sleep(2)
167
+ continue
168
+ break
169
+
170
+ return response
171
+
172
+ @classmethod
173
+ def _fill_resource_id(cls, phrase_id: str, **kwargs):
174
+ resources_list: list = []
175
+ if phrase_id is not None and len(phrase_id) > 0:
176
+ item = {'resource_id': phrase_id, 'resource_type': 'asr_phrase'}
177
+ resources_list.append(item)
178
+
179
+ if len(resources_list) > 0:
180
+ kwargs['resources'] = resources_list
181
+
182
+ return kwargs
183
+
184
+ @classmethod
185
+ def _tidy_kwargs(cls, **kwargs):
186
+ for k in kwargs.copy():
187
+ if kwargs[k] is None:
188
+ kwargs.pop(k, None)
189
+ return kwargs
@@ -0,0 +1,11 @@
1
+ # Copyright (c) Alibaba, Inc. and its affiliates.
2
+
3
+ from .omni_realtime import (AudioFormat, MultiModality, OmniRealtimeCallback,
4
+ OmniRealtimeConversation)
5
+
6
+ __all__ = [
7
+ 'OmniRealtimeCallback',
8
+ 'AudioFormat',
9
+ 'MultiModality',
10
+ 'OmniRealtimeConversation',
11
+ ]