dashscope 1.19.0__py3-none-any.whl → 1.19.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/api_entities/http_request.py +26 -80
- dashscope/api_entities/websocket_request.py +7 -0
- dashscope/common/utils.py +109 -23
- dashscope/version.py +1 -1
- {dashscope-1.19.0.dist-info → dashscope-1.19.1.dist-info}/METADATA +1 -1
- {dashscope-1.19.0.dist-info → dashscope-1.19.1.dist-info}/RECORD +10 -10
- {dashscope-1.19.0.dist-info → dashscope-1.19.1.dist-info}/LICENSE +0 -0
- {dashscope-1.19.0.dist-info → dashscope-1.19.1.dist-info}/WHEEL +0 -0
- {dashscope-1.19.0.dist-info → dashscope-1.19.1.dist-info}/entry_points.txt +0 -0
- {dashscope-1.19.0.dist-info → dashscope-1.19.1.dist-info}/top_level.txt +0 -0
|
@@ -10,7 +10,10 @@ from dashscope.common.constants import (DEFAULT_REQUEST_TIMEOUT_SECONDS,
|
|
|
10
10
|
SSE_CONTENT_TYPE, HTTPMethod)
|
|
11
11
|
from dashscope.common.error import UnsupportedHTTPMethod
|
|
12
12
|
from dashscope.common.logging import logger
|
|
13
|
-
from dashscope.common.utils import
|
|
13
|
+
from dashscope.common.utils import (_handle_aio_stream,
|
|
14
|
+
_handle_aiohttp_failed_response,
|
|
15
|
+
_handle_http_failed_response,
|
|
16
|
+
_handle_stream)
|
|
14
17
|
|
|
15
18
|
|
|
16
19
|
class HttpRequest(AioBaseRequest):
|
|
@@ -127,38 +130,25 @@ class HttpRequest(AioBaseRequest):
|
|
|
127
130
|
async for rsp in self._handle_aio_response(response):
|
|
128
131
|
yield rsp
|
|
129
132
|
except aiohttp.ClientConnectorError as e:
|
|
130
|
-
logger.
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
line = line.rstrip('\n').rstrip('\r')
|
|
144
|
-
if line.startswith('event:error'):
|
|
145
|
-
is_error = True
|
|
146
|
-
elif line.startswith('status:'):
|
|
147
|
-
status_code = line[len('status:'):]
|
|
148
|
-
status_code = int(status_code.strip())
|
|
149
|
-
elif line.startswith('data:'):
|
|
150
|
-
line = line[len('data:'):]
|
|
151
|
-
yield (is_error, status_code, line)
|
|
152
|
-
if is_error:
|
|
153
|
-
break
|
|
154
|
-
else:
|
|
155
|
-
continue # ignore heartbeat...
|
|
133
|
+
logger.exception(e)
|
|
134
|
+
yield DashScopeAPIResponse(-1,
|
|
135
|
+
'',
|
|
136
|
+
'Unknown',
|
|
137
|
+
message='Error type: %s, message: %s' %
|
|
138
|
+
(type(e), e))
|
|
139
|
+
except BaseException as e:
|
|
140
|
+
logger.exception(e)
|
|
141
|
+
yield DashScopeAPIResponse(-1,
|
|
142
|
+
'',
|
|
143
|
+
'Unknown',
|
|
144
|
+
message='Error type: %s, message: %s' %
|
|
145
|
+
(type(e), e))
|
|
156
146
|
|
|
157
147
|
async def _handle_aio_response(self, response: aiohttp.ClientResponse):
|
|
158
148
|
request_id = ''
|
|
159
149
|
if (response.status == HTTPStatus.OK and self.stream
|
|
160
150
|
and SSE_CONTENT_TYPE in response.content_type):
|
|
161
|
-
async for is_error, status_code, data in
|
|
151
|
+
async for is_error, status_code, data in _handle_aio_stream(
|
|
162
152
|
response):
|
|
163
153
|
try:
|
|
164
154
|
output = None
|
|
@@ -217,29 +207,7 @@ class HttpRequest(AioBaseRequest):
|
|
|
217
207
|
output=output,
|
|
218
208
|
usage=usage)
|
|
219
209
|
else:
|
|
220
|
-
|
|
221
|
-
error = await response.json()
|
|
222
|
-
if 'request_id' in error:
|
|
223
|
-
request_id = error['request_id']
|
|
224
|
-
if 'message' not in error:
|
|
225
|
-
message = ''
|
|
226
|
-
logger.error('Request: %s failed, status: %s' %
|
|
227
|
-
(self.url, response.status))
|
|
228
|
-
else:
|
|
229
|
-
message = error['message']
|
|
230
|
-
logger.error(
|
|
231
|
-
'Request: %s failed, status: %s, message: %s' %
|
|
232
|
-
(self.url, response.status, error['message']))
|
|
233
|
-
yield DashScopeAPIResponse(request_id=request_id,
|
|
234
|
-
status_code=response.status,
|
|
235
|
-
code=error['code'],
|
|
236
|
-
message=message)
|
|
237
|
-
else:
|
|
238
|
-
msg = await response.read()
|
|
239
|
-
yield DashScopeAPIResponse(request_id=request_id,
|
|
240
|
-
status_code=response.status,
|
|
241
|
-
code='Unknown',
|
|
242
|
-
message=msg.decode('utf-8'))
|
|
210
|
+
yield await _handle_aiohttp_failed_response(response)
|
|
243
211
|
|
|
244
212
|
def _handle_response(self, response: requests.Response):
|
|
245
213
|
request_id = ''
|
|
@@ -306,33 +274,7 @@ class HttpRequest(AioBaseRequest):
|
|
|
306
274
|
output=output,
|
|
307
275
|
usage=usage)
|
|
308
276
|
else:
|
|
309
|
-
|
|
310
|
-
error = response.json()
|
|
311
|
-
if 'request_id' in error:
|
|
312
|
-
request_id = error['request_id']
|
|
313
|
-
if 'message' not in error:
|
|
314
|
-
message = ''
|
|
315
|
-
logger.error('Request: %s failed, status: %s' %
|
|
316
|
-
(self.url, response.status_code))
|
|
317
|
-
else:
|
|
318
|
-
message = error['message']
|
|
319
|
-
logger.error(
|
|
320
|
-
'Request: %s failed, status: %s, message: %s' %
|
|
321
|
-
(self.url, response.status_code, error['message']))
|
|
322
|
-
yield DashScopeAPIResponse(
|
|
323
|
-
request_id=request_id,
|
|
324
|
-
status_code=response.status_code,
|
|
325
|
-
output=None,
|
|
326
|
-
code=error['code']
|
|
327
|
-
if 'code' in error else None, # noqa E501
|
|
328
|
-
message=message)
|
|
329
|
-
else:
|
|
330
|
-
msg = response.content
|
|
331
|
-
yield DashScopeAPIResponse(request_id=request_id,
|
|
332
|
-
status_code=response.status_code,
|
|
333
|
-
output=None,
|
|
334
|
-
code='Unknown',
|
|
335
|
-
message=msg.decode('utf-8'))
|
|
277
|
+
yield _handle_http_failed_response(response)
|
|
336
278
|
|
|
337
279
|
def _handle_request(self):
|
|
338
280
|
try:
|
|
@@ -364,6 +306,10 @@ class HttpRequest(AioBaseRequest):
|
|
|
364
306
|
self.method)
|
|
365
307
|
for rsp in self._handle_response(response):
|
|
366
308
|
yield rsp
|
|
367
|
-
except
|
|
309
|
+
except BaseException as e:
|
|
368
310
|
logger.error(e)
|
|
369
|
-
|
|
311
|
+
yield DashScopeAPIResponse(-1,
|
|
312
|
+
'',
|
|
313
|
+
'Unknown',
|
|
314
|
+
message='Error type: %s, message: %s' %
|
|
315
|
+
(type(e), e))
|
|
@@ -154,6 +154,13 @@ class WebSocketRequest(AioBaseRequest):
|
|
|
154
154
|
status_code=code,
|
|
155
155
|
code=code,
|
|
156
156
|
message=msg)
|
|
157
|
+
except BaseException as e:
|
|
158
|
+
logger.exception(e)
|
|
159
|
+
yield DashScopeAPIResponse(request_id='',
|
|
160
|
+
status_code=-1,
|
|
161
|
+
code='Unknown',
|
|
162
|
+
message='Error type: %s, message: %s' %
|
|
163
|
+
(type(e), e))
|
|
157
164
|
|
|
158
165
|
def _to_DashScopeAPIResponse(self, task_id, is_binary, result):
|
|
159
166
|
if is_binary:
|
dashscope/common/utils.py
CHANGED
|
@@ -2,6 +2,8 @@ import asyncio
|
|
|
2
2
|
import json
|
|
3
3
|
import os
|
|
4
4
|
import platform
|
|
5
|
+
import queue
|
|
6
|
+
import threading
|
|
5
7
|
from dataclasses import dataclass
|
|
6
8
|
from http import HTTPStatus
|
|
7
9
|
from typing import Dict
|
|
@@ -75,7 +77,8 @@ def is_url(url: str):
|
|
|
75
77
|
return False
|
|
76
78
|
|
|
77
79
|
|
|
78
|
-
def iter_over_async(ait
|
|
80
|
+
def iter_over_async(ait):
|
|
81
|
+
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
79
82
|
ait = ait.__aiter__()
|
|
80
83
|
|
|
81
84
|
async def get_next():
|
|
@@ -85,18 +88,41 @@ def iter_over_async(ait, loop):
|
|
|
85
88
|
except StopAsyncIteration:
|
|
86
89
|
return True, None
|
|
87
90
|
|
|
91
|
+
def iter_thread(loop, message_queue):
|
|
92
|
+
while True:
|
|
93
|
+
try:
|
|
94
|
+
done, obj = loop.run_until_complete(get_next())
|
|
95
|
+
if done:
|
|
96
|
+
message_queue.put((True, None, None))
|
|
97
|
+
break
|
|
98
|
+
message_queue.put((False, None, obj))
|
|
99
|
+
except BaseException as e: # noqa E722
|
|
100
|
+
logger.exception(e)
|
|
101
|
+
message_queue.put((True, e, None))
|
|
102
|
+
break
|
|
103
|
+
|
|
104
|
+
message_queue = queue.Queue()
|
|
105
|
+
x = threading.Thread(target=iter_thread,
|
|
106
|
+
args=(loop, message_queue),
|
|
107
|
+
name='iter_async_thread')
|
|
108
|
+
x.start()
|
|
88
109
|
while True:
|
|
89
|
-
|
|
90
|
-
if
|
|
91
|
-
|
|
110
|
+
finished, error, obj = message_queue.get()
|
|
111
|
+
if finished:
|
|
112
|
+
if error is not None:
|
|
113
|
+
yield DashScopeAPIResponse(
|
|
114
|
+
-1,
|
|
115
|
+
'',
|
|
116
|
+
'Unknown',
|
|
117
|
+
message='Error type: %s, message: %s' %
|
|
118
|
+
(type(error), error))
|
|
92
119
|
break
|
|
93
|
-
|
|
120
|
+
else:
|
|
121
|
+
yield obj
|
|
94
122
|
|
|
95
123
|
|
|
96
124
|
def async_to_sync(async_generator):
|
|
97
|
-
|
|
98
|
-
asyncio.set_event_loop(loop)
|
|
99
|
-
for message in iter_over_async(async_generator, loop):
|
|
125
|
+
for message in iter_over_async(async_generator):
|
|
100
126
|
yield message
|
|
101
127
|
|
|
102
128
|
|
|
@@ -211,29 +237,46 @@ def _handle_stream(response: requests.Response):
|
|
|
211
237
|
continue # ignore heartbeat...
|
|
212
238
|
|
|
213
239
|
|
|
240
|
+
def _handle_error_message(error, status_code, flattened_output):
|
|
241
|
+
code = None
|
|
242
|
+
msg = ''
|
|
243
|
+
request_id = ''
|
|
244
|
+
if flattened_output:
|
|
245
|
+
error['status_code'] = status_code
|
|
246
|
+
return error
|
|
247
|
+
if 'message' in error:
|
|
248
|
+
msg = error['message']
|
|
249
|
+
if 'msg' in error:
|
|
250
|
+
msg = error['msg']
|
|
251
|
+
if 'code' in error:
|
|
252
|
+
code = error['code']
|
|
253
|
+
if 'request_id' in error:
|
|
254
|
+
request_id = error['request_id']
|
|
255
|
+
return DashScopeAPIResponse(request_id=request_id,
|
|
256
|
+
status_code=status_code,
|
|
257
|
+
code=code,
|
|
258
|
+
message=msg)
|
|
259
|
+
|
|
260
|
+
|
|
214
261
|
def _handle_http_failed_response(
|
|
215
262
|
response: requests.Response,
|
|
216
263
|
flattened_output: bool = False) -> DashScopeAPIResponse:
|
|
217
|
-
msg = ''
|
|
218
|
-
code = None
|
|
219
264
|
request_id = ''
|
|
220
265
|
if 'application/json' in response.headers.get('content-type', ''):
|
|
221
266
|
error = response.json()
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if 'request_id' in error:
|
|
232
|
-
request_id = error['request_id']
|
|
267
|
+
return _handle_error_message(error, response.status_code,
|
|
268
|
+
flattened_output)
|
|
269
|
+
elif SSE_CONTENT_TYPE in response.headers.get('content-type', ''):
|
|
270
|
+
msgs = response.content.decode('utf-8').split('\n')
|
|
271
|
+
for msg in msgs:
|
|
272
|
+
if msg.startswith('data:'):
|
|
273
|
+
error = json.loads(msg.replace('data:', '').strip())
|
|
274
|
+
return _handle_error_message(error, response.status_code,
|
|
275
|
+
flattened_output)
|
|
233
276
|
return DashScopeAPIResponse(request_id=request_id,
|
|
234
277
|
status_code=response.status_code,
|
|
235
|
-
code=
|
|
236
|
-
message=
|
|
278
|
+
code='Unknown',
|
|
279
|
+
message=msgs)
|
|
237
280
|
else:
|
|
238
281
|
msg = response.content.decode('utf-8')
|
|
239
282
|
if flattened_output:
|
|
@@ -244,6 +287,49 @@ def _handle_http_failed_response(
|
|
|
244
287
|
message=msg)
|
|
245
288
|
|
|
246
289
|
|
|
290
|
+
async def _handle_aio_stream(response):
|
|
291
|
+
# TODO define done message.
|
|
292
|
+
is_error = False
|
|
293
|
+
status_code = HTTPStatus.BAD_REQUEST
|
|
294
|
+
async for line in response.content:
|
|
295
|
+
if line:
|
|
296
|
+
line = line.decode('utf8')
|
|
297
|
+
line = line.rstrip('\n').rstrip('\r')
|
|
298
|
+
if line.startswith('event:error'):
|
|
299
|
+
is_error = True
|
|
300
|
+
elif line.startswith('status:'):
|
|
301
|
+
status_code = line[len('status:'):]
|
|
302
|
+
status_code = int(status_code.strip())
|
|
303
|
+
elif line.startswith('data:'):
|
|
304
|
+
line = line[len('data:'):]
|
|
305
|
+
yield (is_error, status_code, line)
|
|
306
|
+
if is_error:
|
|
307
|
+
break
|
|
308
|
+
else:
|
|
309
|
+
continue # ignore heartbeat...
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
async def _handle_aiohttp_failed_response(
|
|
313
|
+
response: requests.Response,
|
|
314
|
+
flattened_output: bool = False) -> DashScopeAPIResponse:
|
|
315
|
+
request_id = ''
|
|
316
|
+
if 'application/json' in response.content_type:
|
|
317
|
+
error = await response.json()
|
|
318
|
+
return _handle_error_message(error, response.status, flattened_output)
|
|
319
|
+
elif SSE_CONTENT_TYPE in response.content_type:
|
|
320
|
+
async for _, _, data in _handle_aio_stream(response):
|
|
321
|
+
error = json.loads(data)
|
|
322
|
+
return _handle_error_message(error, response.status, flattened_output)
|
|
323
|
+
else:
|
|
324
|
+
msg = response.content.decode('utf-8')
|
|
325
|
+
if flattened_output:
|
|
326
|
+
return {'status_code': response.status, 'message': msg}
|
|
327
|
+
return DashScopeAPIResponse(request_id=request_id,
|
|
328
|
+
status_code=response.status,
|
|
329
|
+
code='Unknown',
|
|
330
|
+
message=msg)
|
|
331
|
+
|
|
332
|
+
|
|
247
333
|
def _handle_http_response(response: requests.Response,
|
|
248
334
|
flattened_output: bool = False):
|
|
249
335
|
response = _handle_http_stream_response(response, flattened_output)
|
dashscope/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = '1.19.
|
|
1
|
+
__version__ = '1.19.1'
|
|
@@ -6,7 +6,7 @@ dashscope/files.py,sha256=QgJjwhtn9F548nCA8jD8OvE6aQEj-20hZqJgYXsUdQU,3930
|
|
|
6
6
|
dashscope/finetune.py,sha256=_tflDUvu0KagSoCzLaf0hofpG_P8NU6PylL8CPjVhrA,6243
|
|
7
7
|
dashscope/model.py,sha256=UPOn1qMYFhX-ovXi3BMxZEBk8qOK7WLJOYHMbPZwYBo,1440
|
|
8
8
|
dashscope/models.py,sha256=1-bc-Ue68zurgu_y6RhfFr9uzeQMF5AZq-C32lJGMGU,1224
|
|
9
|
-
dashscope/version.py,sha256=
|
|
9
|
+
dashscope/version.py,sha256=_HUAUi-u8hG8FcFLMpMxSECPn6Si10Hn8M1DGJyxUkQ,23
|
|
10
10
|
dashscope/aigc/__init__.py,sha256=s-MCA87KYiVumYtKtJi5IMN7xelSF6TqEU3s3_7RF-Y,327
|
|
11
11
|
dashscope/aigc/code_generation.py,sha256=KAJVrGp6tiNFBBg64Ovs9RfcP5SrIhrbW3wdA89NKso,10885
|
|
12
12
|
dashscope/aigc/conversation.py,sha256=xRoJlCR-IXHjSdkDrK74a9ut1FJg0FZhTNXZAJC18MA,14231
|
|
@@ -19,8 +19,8 @@ dashscope/api_entities/api_request_data.py,sha256=JUMcfpJjKXEZLCBSFIDpgoaeQYk5uK
|
|
|
19
19
|
dashscope/api_entities/api_request_factory.py,sha256=4p-qxMuvCA0CmUHdH19QaUCaHmlLHAM1X2Jd4YKt5c0,4661
|
|
20
20
|
dashscope/api_entities/base_request.py,sha256=cXUL7xqSV8wBr5d-1kx65AO3IsRR9A_ps6Lok-v-MKM,926
|
|
21
21
|
dashscope/api_entities/dashscope_response.py,sha256=Bp1T7HwVlkOvpMNg-AEjz-BScxhLUXMXlE8ApXTtfhQ,17872
|
|
22
|
-
dashscope/api_entities/http_request.py,sha256=
|
|
23
|
-
dashscope/api_entities/websocket_request.py,sha256=
|
|
22
|
+
dashscope/api_entities/http_request.py,sha256=UeS8_rhApA_FnuPv56B2Ruu1l9O1Bd6NiB9KNboWW50,14011
|
|
23
|
+
dashscope/api_entities/websocket_request.py,sha256=Xr6IJ9WqrIw5ouBQLpgoRSwL1C09jkb4u1EZdxhVQy0,15039
|
|
24
24
|
dashscope/app/__init__.py,sha256=UiN_9i--z84Dw5wUehOh_Tkk_9Gq_td_Kbz1dobBEKg,62
|
|
25
25
|
dashscope/app/application.py,sha256=AegGVsk3dDzYACoYRNNjo3eG-2wrDd0dlOjYHpF0r2Y,7949
|
|
26
26
|
dashscope/app/application_response.py,sha256=AR_dEtfDsS7SqOyUqZmiosdgos1bDnXUvZn7XV_OzAs,6714
|
|
@@ -45,7 +45,7 @@ dashscope/common/env.py,sha256=oQOZW5JyEeTSde394un2lpDJ5RBh4fMU9hBfbtrKKkc,869
|
|
|
45
45
|
dashscope/common/error.py,sha256=Q7GRhniP-7ap4HBpU69frRdKgKLwmH4ySYxCtupsr60,2638
|
|
46
46
|
dashscope/common/logging.py,sha256=ecGxylG3bWES_Xv5-BD6ep4_0Ciu7F6ZPBjiZtu9Jx4,984
|
|
47
47
|
dashscope/common/message_manager.py,sha256=i5149WzDk6nWmdFaHzYx4USXMBeX18GKSI-F4fLwbN0,1097
|
|
48
|
-
dashscope/common/utils.py,sha256=
|
|
48
|
+
dashscope/common/utils.py,sha256=YHs9PvL2BInfzgJazMEDdzOfMzmhudf0J083ieNWXkc,15398
|
|
49
49
|
dashscope/customize/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
50
50
|
dashscope/customize/customize_types.py,sha256=ZtEwSXA4dbjIYr5vgQQNkMuXyC2BNmznGuaF6b7jwr0,4803
|
|
51
51
|
dashscope/customize/deployments.py,sha256=LIkM-hmJHcJZCKV6WJIPSXQ5CAhB5PUxnt5mqKbVbfE,5189
|
|
@@ -80,9 +80,9 @@ dashscope/tokenizers/tokenizer.py,sha256=y6P91qTCYo__pEx_0VHAcj9YECfbUdRqZU1fdGT
|
|
|
80
80
|
dashscope/tokenizers/tokenizer_base.py,sha256=REDhzRyDT13iequ61-a6_KcTy0GFKlihQve5HkyoyRs,656
|
|
81
81
|
dashscope/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
82
82
|
dashscope/utils/oss_utils.py,sha256=fi8-PPsN-iR-iv5k2NS5Z8nlWkpgUhr56FRWm4BDh4A,6984
|
|
83
|
-
dashscope-1.19.
|
|
84
|
-
dashscope-1.19.
|
|
85
|
-
dashscope-1.19.
|
|
86
|
-
dashscope-1.19.
|
|
87
|
-
dashscope-1.19.
|
|
88
|
-
dashscope-1.19.
|
|
83
|
+
dashscope-1.19.1.dist-info/LICENSE,sha256=Izp5L1DF1Mbza6qojkqNNWlE_mYLnr4rmzx2EBF8YFw,11413
|
|
84
|
+
dashscope-1.19.1.dist-info/METADATA,sha256=pURfnZ-PzopFwsIK_l3RiK_ST0qSBNCmHZK2JsEZYic,6609
|
|
85
|
+
dashscope-1.19.1.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
|
|
86
|
+
dashscope-1.19.1.dist-info/entry_points.txt,sha256=raEp5dOuj8whJ7yqZlDM8WQ5p2RfnGrGNo0QLQEnatY,50
|
|
87
|
+
dashscope-1.19.1.dist-info/top_level.txt,sha256=woqavFJK9zas5xTqynmALqOtlafghjsk63Xk86powTU,10
|
|
88
|
+
dashscope-1.19.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|