dashscope 1.13.6__py3-none-any.whl → 1.14.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.

dashscope/__init__.py CHANGED
@@ -19,11 +19,12 @@ from dashscope.embeddings.multimodal_embedding import (
19
19
  MultiModalEmbedding, MultiModalEmbeddingItemAudio,
20
20
  MultiModalEmbeddingItemImage, MultiModalEmbeddingItemText)
21
21
  from dashscope.embeddings.text_embedding import TextEmbedding
22
- from dashscope.tokenizers import Tokenization
23
22
  from dashscope.file import File
24
23
  from dashscope.finetune import FineTune
25
24
  from dashscope.model import Model
26
25
  from dashscope.nlp.understanding import Understanding
26
+ from dashscope.tokenizers import (Tokenization, Tokenizer, get_tokenizer,
27
+ list_tokenizers)
27
28
 
28
29
  __all__ = [
29
30
  base_http_api_url, base_websocket_api_url, api_key, api_key_file_path,
@@ -32,8 +33,8 @@ __all__ = [
32
33
  TextEmbedding, MultiModalEmbedding, MultiModalEmbeddingItemAudio,
33
34
  MultiModalEmbeddingItemImage, MultiModalEmbeddingItemText,
34
35
  SpeechSynthesizer, MultiModalConversation, BatchTextEmbedding,
35
- BatchTextEmbeddingResponse, Understanding, CodeGeneration,
36
- Tokenization,
36
+ BatchTextEmbeddingResponse, Understanding, CodeGeneration, Tokenization,
37
+ Tokenizer, get_tokenizer, list_tokenizers
37
38
  ]
38
39
 
39
40
  logging.getLogger(__name__).addHandler(NullHandler())
@@ -2,12 +2,10 @@ import json
2
2
  from http import HTTPStatus
3
3
 
4
4
  import aiohttp
5
-
6
5
  from dashscope.api_entities.base_request import AioBaseRequest
7
6
  from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
8
7
  from dashscope.common.constants import (DEFAULT_REQUEST_TIMEOUT_SECONDS,
9
- SSE_CONTENT_TYPE, HTTPMethod,
10
- StreamResultMode)
8
+ SSE_CONTENT_TYPE, HTTPMethod)
11
9
  from dashscope.common.error import UnsupportedHTTPMethod
12
10
  from dashscope.common.logging import logger
13
11
  from dashscope.common.utils import async_to_sync
@@ -21,7 +19,6 @@ class AioHttpRequest(AioBaseRequest):
21
19
  stream: bool = True,
22
20
  async_request: bool = False,
23
21
  query: bool = False,
24
- stream_result_mode: str = StreamResultMode.ACCUMULATE,
25
22
  timeout: int = DEFAULT_REQUEST_TIMEOUT_SECONDS,
26
23
  task_id: str = None) -> None:
27
24
  """HttpSSERequest, processing http server sent event stream.
@@ -4,7 +4,7 @@ from dashscope.api_entities.http_request import HttpRequest
4
4
  from dashscope.api_entities.websocket_request import WebSocketRequest
5
5
  from dashscope.common.constants import (REQUEST_TIMEOUT_KEYWORD,
6
6
  SERVICE_API_PATH, ApiProtocol,
7
- HTTPMethod, StreamResultMode)
7
+ HTTPMethod)
8
8
  from dashscope.common.error import InputDataRequired, UnsupportedApiProtocol
9
9
  from dashscope.protocol.websocket import WebsocketStreamingMode
10
10
 
@@ -15,23 +15,23 @@ def _get_protocol_params(kwargs):
15
15
  is_binary_input = kwargs.pop('is_binary_input', False)
16
16
  http_method = kwargs.pop('http_method', HTTPMethod.POST)
17
17
  stream = kwargs.pop('stream', False)
18
+ if not stream and ws_stream_mode == WebsocketStreamingMode.OUT:
19
+ ws_stream_mode = WebsocketStreamingMode.NONE
20
+
18
21
  async_request = kwargs.pop('async_request', False)
19
22
  query = kwargs.pop('query', False)
20
23
  headers = kwargs.pop('headers', None)
21
24
  request_timeout = kwargs.pop(REQUEST_TIMEOUT_KEYWORD, None)
22
- stream_result_mode = kwargs.pop('stream_result_mode',
23
- StreamResultMode.ACCUMULATE)
24
25
  form = kwargs.pop('form', None)
25
26
  resources = kwargs.pop('resources', None)
26
27
  return (api_protocol, ws_stream_mode, is_binary_input, http_method, stream,
27
- async_request, query, headers, request_timeout, stream_result_mode,
28
- form, resources)
28
+ async_request, query, headers, request_timeout, form, resources)
29
29
 
30
30
 
31
31
  def _build_api_request(model: str, input: object, task_group: str, task: str,
32
32
  function: str, api_key: str, is_service=True, **kwargs):
33
33
  (api_protocol, ws_stream_mode, is_binary_input, http_method, stream,
34
- async_request, query, headers, request_timeout, stream_result_mode, form,
34
+ async_request, query, headers, request_timeout, form,
35
35
  resources) = _get_protocol_params(kwargs)
36
36
  task_id = kwargs.pop('task_id', None)
37
37
  if api_protocol in [ApiProtocol.HTTP, ApiProtocol.HTTPS]:
@@ -55,7 +55,6 @@ def _build_api_request(model: str, input: object, task_group: str, task: str,
55
55
  stream=stream,
56
56
  async_request=async_request,
57
57
  query=query,
58
- stream_result_mode=stream_result_mode,
59
58
  timeout=request_timeout,
60
59
  task_id=task_id)
61
60
  elif api_protocol == ApiProtocol.WEBSOCKET:
@@ -65,7 +64,6 @@ def _build_api_request(model: str, input: object, task_group: str, task: str,
65
64
  stream=stream,
66
65
  ws_stream_mode=ws_stream_mode,
67
66
  is_binary_input=is_binary_input,
68
- stream_result_mode=stream_result_mode,
69
67
  timeout=request_timeout)
70
68
  else:
71
69
  raise UnsupportedApiProtocol(
@@ -2,12 +2,10 @@ import json
2
2
  from http import HTTPStatus
3
3
 
4
4
  import requests
5
-
6
5
  from dashscope.api_entities.base_request import BaseRequest
7
6
  from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
8
7
  from dashscope.common.constants import (DEFAULT_REQUEST_TIMEOUT_SECONDS,
9
- SSE_CONTENT_TYPE, HTTPMethod,
10
- StreamResultMode)
8
+ SSE_CONTENT_TYPE, HTTPMethod)
11
9
  from dashscope.common.error import UnsupportedHTTPMethod
12
10
  from dashscope.common.logging import logger
13
11
 
@@ -20,7 +18,6 @@ class HttpRequest(BaseRequest):
20
18
  stream: bool = True,
21
19
  async_request: bool = False,
22
20
  query: bool = False,
23
- stream_result_mode: str = StreamResultMode.ACCUMULATE,
24
21
  timeout: int = DEFAULT_REQUEST_TIMEOUT_SECONDS,
25
22
  task_id: str = None) -> None:
26
23
  """HttpSSERequest, processing http server sent event stream.
@@ -5,12 +5,11 @@ from http import HTTPStatus
5
5
  from typing import Tuple, Union
6
6
 
7
7
  import aiohttp
8
-
9
8
  from dashscope.api_entities.base_request import AioBaseRequest
10
9
  from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
11
10
  from dashscope.common.constants import (DEFAULT_REQUEST_TIMEOUT_SECONDS,
12
11
  SERVICE_503_MESSAGE,
13
- WEBSOCKET_ERROR_CODE, StreamResultMode)
12
+ WEBSOCKET_ERROR_CODE)
14
13
  from dashscope.common.error import (RequestFailure, UnexpectedMessageReceived,
15
14
  UnknownMessageReceived)
16
15
  from dashscope.common.logging import logger
@@ -29,7 +28,6 @@ class WebSocketRequest(AioBaseRequest):
29
28
  stream: bool = True,
30
29
  ws_stream_mode: str = WebsocketStreamingMode.OUT,
31
30
  is_binary_input: bool = False,
32
- stream_result_mode: str = StreamResultMode.ACCUMULATE,
33
31
  timeout: int = DEFAULT_REQUEST_TIMEOUT_SECONDS,
34
32
  ) -> None:
35
33
  super().__init__()
@@ -50,7 +48,6 @@ class WebSocketRequest(AioBaseRequest):
50
48
  else:
51
49
  self.timeout = timeout
52
50
  self.ws_stream_mode = ws_stream_mode
53
- self.stream_result_mode = stream_result_mode
54
51
  self.is_binary_input = is_binary_input
55
52
 
56
53
  self.headers = {
@@ -177,7 +174,6 @@ class WebSocketRequest(AioBaseRequest):
177
174
  # check if request stream data, re return an iterator,
178
175
  # otherwise we collect data and return user.
179
176
  # no matter what, the response is streaming
180
- final_payload = None
181
177
  is_binary_output = False
182
178
  while True:
183
179
  msg = await ws.receive()
@@ -186,36 +182,21 @@ class WebSocketRequest(AioBaseRequest):
186
182
  msg_json = msg.json()
187
183
  logger.debug('Receive %s event' % msg_json[HEADER][EVENT_KEY])
188
184
  if msg_json[HEADER][EVENT_KEY] == EventType.GENERATED:
189
- if final_payload is None:
190
- final_payload = []
191
185
  payload = msg_json['payload']
192
- if self.stream:
193
- yield False, payload
194
- elif self.stream_result_mode == StreamResultMode.ACCUMULATE: # noqa E501
195
- final_payload = payload
196
- else:
197
- final_payload.append(payload)
186
+ yield False, payload
198
187
  elif msg_json[HEADER][EVENT_KEY] == EventType.FINISHED:
199
- if final_payload is None:
200
- final_payload = []
201
188
  payload = None
202
189
  if 'payload' in msg_json:
203
190
  payload = msg_json['payload']
204
191
  logger.debug(payload)
205
192
  if payload:
206
- if self.stream:
207
- yield False, payload
208
- elif self.stream_result_mode == StreamResultMode.ACCUMULATE: # noqa E501
209
- yield False, payload
210
- else:
211
- final_payload.extend(payload)
212
- yield False, final_payload
193
+ yield False, payload
213
194
  else:
214
195
  if not self.stream:
215
196
  if is_binary_output:
216
- yield True, final_payload
197
+ yield True, payload
217
198
  else:
218
- yield False, final_payload
199
+ yield False, payload
219
200
  break
220
201
  elif msg_json[HEADER][EVENT_KEY] == EventType.FAILED:
221
202
  self._on_failed(msg_json)
@@ -225,14 +206,7 @@ class WebSocketRequest(AioBaseRequest):
225
206
  raise UnknownMessageReceived(error)
226
207
  elif msg.type == aiohttp.WSMsgType.BINARY:
227
208
  is_binary_output = True
228
- if final_payload is None:
229
- final_payload = b''
230
- if self.stream:
231
- yield True, msg.data
232
- elif self.stream_result_mode == StreamResultMode.ACCUMULATE:
233
- final_payload = msg.data
234
- else:
235
- final_payload += msg.data
209
+ yield True, msg.data
236
210
 
237
211
  def _on_failed(self, details):
238
212
  error = RequestFailure(request_id=details[HEADER][TASK_ID],
@@ -44,22 +44,6 @@ class FilePurpose:
44
44
  fine_tune = 'fine_tune'
45
45
 
46
46
 
47
- class StreamResultMode:
48
- """Stream result mode.
49
- examples(I like apple)
50
- accumulate: will output
51
- I
52
- I like
53
- I like apple
54
- divide: will output
55
- I
56
- like
57
- apple
58
- """
59
- ACCUMULATE = 'accumulate'
60
- DIVIDE = 'divide'
61
-
62
-
63
47
  class DeploymentStatus:
64
48
  DEPLOYING = 'DEPLOYING'
65
49
  SERVING = 'RUNNING'
dashscope/common/error.py CHANGED
@@ -1,61 +1,65 @@
1
- class AuthenticationError(Exception):
1
+ class DashScopeException(Exception):
2
2
  pass
3
3
 
4
4
 
5
- class InvalidParameter(Exception):
5
+ class AuthenticationError(DashScopeException):
6
6
  pass
7
7
 
8
8
 
9
- class InvalidTask(Exception):
9
+ class InvalidParameter(DashScopeException):
10
10
  pass
11
11
 
12
12
 
13
- class UnsupportedModel(Exception):
13
+ class InvalidTask(DashScopeException):
14
14
  pass
15
15
 
16
16
 
17
- class UnsupportedTask(Exception):
17
+ class UnsupportedModel(DashScopeException):
18
18
  pass
19
19
 
20
20
 
21
- class ModelRequired(Exception):
21
+ class UnsupportedTask(DashScopeException):
22
22
  pass
23
23
 
24
24
 
25
- class InvalidModel(Exception):
25
+ class ModelRequired(DashScopeException):
26
26
  pass
27
27
 
28
28
 
29
- class InvalidInput(Exception):
29
+ class InvalidModel(DashScopeException):
30
30
  pass
31
31
 
32
32
 
33
- class InvalidFileFormat(Exception):
33
+ class InvalidInput(DashScopeException):
34
34
  pass
35
35
 
36
36
 
37
- class UnsupportedApiProtocol(Exception):
37
+ class InvalidFileFormat(DashScopeException):
38
38
  pass
39
39
 
40
40
 
41
- class NotImplemented(Exception):
41
+ class UnsupportedApiProtocol(DashScopeException):
42
42
  pass
43
43
 
44
44
 
45
- class MultiInputsWithBinaryNotSupported(Exception):
45
+ class NotImplemented(DashScopeException):
46
46
  pass
47
47
 
48
48
 
49
- class UnexpectedMessageReceived(Exception):
49
+ class MultiInputsWithBinaryNotSupported(DashScopeException):
50
50
  pass
51
51
 
52
52
 
53
- class UnsupportedData(Exception):
53
+ class UnexpectedMessageReceived(DashScopeException):
54
+ pass
55
+
56
+
57
+ class UnsupportedData(DashScopeException):
54
58
  pass
55
59
 
56
60
 
57
61
  # for server send generation or inference error.
58
- class RequestFailure(Exception):
62
+ class RequestFailure(DashScopeException):
59
63
  def __init__(self,
60
64
  request_id=None,
61
65
  message=None,
@@ -72,33 +76,33 @@ class RequestFailure(Exception):
72
76
  return msg
73
77
 
74
78
 
75
- class UnknownMessageReceived(Exception):
79
+ class UnknownMessageReceived(DashScopeException):
76
80
  pass
77
81
 
78
82
 
79
- class InputDataRequired(Exception):
83
+ class InputDataRequired(DashScopeException):
80
84
  pass
81
85
 
82
86
 
83
- class InputRequired(Exception):
87
+ class InputRequired(DashScopeException):
84
88
  pass
85
89
 
86
90
 
87
- class UnsupportedDataType(Exception):
91
+ class UnsupportedDataType(DashScopeException):
88
92
  pass
89
93
 
90
94
 
91
- class ServiceUnavailableError(Exception):
95
+ class ServiceUnavailableError(DashScopeException):
92
96
  pass
93
97
 
94
98
 
95
- class UnsupportedHTTPMethod(Exception):
99
+ class UnsupportedHTTPMethod(DashScopeException):
96
100
  pass
97
101
 
98
102
 
99
- class AsyncTaskCreateFailed(Exception):
103
+ class AsyncTaskCreateFailed(DashScopeException):
100
104
  pass
101
105
 
102
106
 
103
- class UploadFileException(Exception):
107
+ class UploadFileException(DashScopeException):
104
108
  pass
dashscope/common/utils.py CHANGED
@@ -14,7 +14,7 @@ from dashscope.version import __version__
14
14
 
15
15
 
16
16
  def is_validate_fine_tune_file(file_path):
17
- with open(file_path) as f:
17
+ with open(file_path, encoding='utf-8') as f:
18
18
  for line in f:
19
19
  try:
20
20
  json.loads(line)
@@ -88,6 +88,7 @@ class MultiModalEmbedding(BaseApi):
88
88
  task_group=task_group,
89
89
  task=MultiModalEmbedding.task,
90
90
  function=function,
91
+ api_key=api_key,
91
92
  **kwargs)
92
93
 
93
94
  @classmethod