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.
- dashscope/__init__.py +61 -14
- dashscope/aigc/__init__.py +10 -3
- dashscope/aigc/chat_completion.py +282 -0
- dashscope/aigc/code_generation.py +145 -0
- dashscope/aigc/conversation.py +71 -12
- dashscope/aigc/generation.py +288 -16
- dashscope/aigc/image_synthesis.py +473 -31
- dashscope/aigc/multimodal_conversation.py +299 -14
- dashscope/aigc/video_synthesis.py +610 -0
- dashscope/api_entities/aiohttp_request.py +8 -5
- dashscope/api_entities/api_request_data.py +4 -2
- dashscope/api_entities/api_request_factory.py +68 -20
- dashscope/api_entities/base_request.py +20 -3
- dashscope/api_entities/chat_completion_types.py +344 -0
- dashscope/api_entities/dashscope_response.py +243 -15
- dashscope/api_entities/encryption.py +179 -0
- dashscope/api_entities/http_request.py +216 -62
- dashscope/api_entities/websocket_request.py +43 -34
- dashscope/app/__init__.py +5 -0
- dashscope/app/application.py +203 -0
- dashscope/app/application_response.py +246 -0
- dashscope/assistants/__init__.py +16 -0
- dashscope/assistants/assistant_types.py +175 -0
- dashscope/assistants/assistants.py +311 -0
- dashscope/assistants/files.py +197 -0
- dashscope/audio/__init__.py +4 -2
- dashscope/audio/asr/__init__.py +17 -1
- dashscope/audio/asr/asr_phrase_manager.py +203 -0
- dashscope/audio/asr/recognition.py +167 -27
- dashscope/audio/asr/transcription.py +107 -14
- dashscope/audio/asr/translation_recognizer.py +1006 -0
- dashscope/audio/asr/vocabulary.py +177 -0
- dashscope/audio/qwen_asr/__init__.py +7 -0
- dashscope/audio/qwen_asr/qwen_transcription.py +189 -0
- dashscope/audio/qwen_omni/__init__.py +11 -0
- dashscope/audio/qwen_omni/omni_realtime.py +524 -0
- dashscope/audio/qwen_tts/__init__.py +5 -0
- dashscope/audio/qwen_tts/speech_synthesizer.py +77 -0
- dashscope/audio/qwen_tts_realtime/__init__.py +10 -0
- dashscope/audio/qwen_tts_realtime/qwen_tts_realtime.py +355 -0
- dashscope/audio/tts/__init__.py +2 -0
- dashscope/audio/tts/speech_synthesizer.py +5 -0
- dashscope/audio/tts_v2/__init__.py +12 -0
- dashscope/audio/tts_v2/enrollment.py +179 -0
- dashscope/audio/tts_v2/speech_synthesizer.py +886 -0
- dashscope/cli.py +157 -37
- dashscope/client/base_api.py +652 -87
- dashscope/common/api_key.py +2 -0
- dashscope/common/base_type.py +135 -0
- dashscope/common/constants.py +13 -16
- dashscope/common/env.py +2 -0
- dashscope/common/error.py +58 -22
- dashscope/common/logging.py +2 -0
- dashscope/common/message_manager.py +2 -0
- dashscope/common/utils.py +276 -46
- dashscope/customize/__init__.py +0 -0
- dashscope/customize/customize_types.py +192 -0
- dashscope/customize/deployments.py +146 -0
- dashscope/customize/finetunes.py +234 -0
- dashscope/embeddings/__init__.py +5 -1
- dashscope/embeddings/batch_text_embedding.py +208 -0
- dashscope/embeddings/batch_text_embedding_response.py +65 -0
- dashscope/embeddings/multimodal_embedding.py +118 -10
- dashscope/embeddings/text_embedding.py +13 -1
- dashscope/{file.py → files.py} +19 -4
- dashscope/io/input_output.py +2 -0
- dashscope/model.py +11 -2
- dashscope/models.py +43 -0
- dashscope/multimodal/__init__.py +20 -0
- dashscope/multimodal/dialog_state.py +56 -0
- dashscope/multimodal/multimodal_constants.py +28 -0
- dashscope/multimodal/multimodal_dialog.py +648 -0
- dashscope/multimodal/multimodal_request_params.py +313 -0
- dashscope/multimodal/tingwu/__init__.py +10 -0
- dashscope/multimodal/tingwu/tingwu.py +80 -0
- dashscope/multimodal/tingwu/tingwu_realtime.py +579 -0
- dashscope/nlp/__init__.py +0 -0
- dashscope/nlp/understanding.py +64 -0
- dashscope/protocol/websocket.py +3 -0
- dashscope/rerank/__init__.py +0 -0
- dashscope/rerank/text_rerank.py +69 -0
- dashscope/resources/qwen.tiktoken +151643 -0
- dashscope/threads/__init__.py +26 -0
- dashscope/threads/messages/__init__.py +0 -0
- dashscope/threads/messages/files.py +113 -0
- dashscope/threads/messages/messages.py +220 -0
- dashscope/threads/runs/__init__.py +0 -0
- dashscope/threads/runs/runs.py +501 -0
- dashscope/threads/runs/steps.py +112 -0
- dashscope/threads/thread_types.py +665 -0
- dashscope/threads/threads.py +212 -0
- dashscope/tokenizers/__init__.py +7 -0
- dashscope/tokenizers/qwen_tokenizer.py +111 -0
- dashscope/tokenizers/tokenization.py +125 -0
- dashscope/tokenizers/tokenizer.py +45 -0
- dashscope/tokenizers/tokenizer_base.py +32 -0
- dashscope/utils/__init__.py +0 -0
- dashscope/utils/message_utils.py +838 -0
- dashscope/utils/oss_utils.py +243 -0
- dashscope/utils/param_utils.py +29 -0
- dashscope/version.py +3 -1
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/METADATA +53 -50
- dashscope-1.25.6.dist-info/RECORD +112 -0
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/WHEEL +1 -1
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/entry_points.txt +0 -1
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info/licenses}/LICENSE +2 -4
- dashscope/deployment.py +0 -129
- dashscope/finetune.py +0 -149
- dashscope-1.8.0.dist-info/RECORD +0 -49
- {dashscope-1.8.0.dist-info → dashscope-1.25.6.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from http import HTTPStatus
|
|
5
|
+
from typing import Dict, List, Optional
|
|
6
|
+
|
|
7
|
+
from dashscope.client.base_api import (CancelMixin, CreateMixin,
|
|
8
|
+
GetStatusMixin, ListObjectMixin,
|
|
9
|
+
UpdateMixin)
|
|
10
|
+
from dashscope.common.error import (AssistantError, InputRequired,
|
|
11
|
+
TimeoutException)
|
|
12
|
+
from dashscope.common.logging import logger
|
|
13
|
+
from dashscope.threads.thread_types import (Run, RunList, RunStep,
|
|
14
|
+
RunStepDelta, Thread,
|
|
15
|
+
ThreadMessage, ThreadMessageDelta)
|
|
16
|
+
|
|
17
|
+
__all__ = ['Runs']
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Runs(CreateMixin, CancelMixin, ListObjectMixin, GetStatusMixin,
|
|
21
|
+
UpdateMixin):
|
|
22
|
+
SUB_PATH = 'RUNS' # useless
|
|
23
|
+
|
|
24
|
+
@classmethod
|
|
25
|
+
def create_thread_and_run(cls,
|
|
26
|
+
*,
|
|
27
|
+
assistant_id: str,
|
|
28
|
+
thread: Optional[Dict] = None,
|
|
29
|
+
model: Optional[str] = None,
|
|
30
|
+
instructions: Optional[str] = None,
|
|
31
|
+
additional_instructions: Optional[str] = None,
|
|
32
|
+
tools: Optional[List[Dict]] = None,
|
|
33
|
+
stream: Optional[bool] = False,
|
|
34
|
+
metadata: Optional[Dict] = None,
|
|
35
|
+
workspace: str = None,
|
|
36
|
+
extra_body: Optional[Dict] = None,
|
|
37
|
+
api_key: str = None,
|
|
38
|
+
**kwargs) -> Run:
|
|
39
|
+
if not assistant_id:
|
|
40
|
+
raise InputRequired('assistant_id is required')
|
|
41
|
+
data = {'assistant_id': assistant_id}
|
|
42
|
+
if thread:
|
|
43
|
+
data['thread'] = thread
|
|
44
|
+
if model:
|
|
45
|
+
data['model'] = model
|
|
46
|
+
if instructions:
|
|
47
|
+
data['instructions'] = instructions
|
|
48
|
+
if additional_instructions:
|
|
49
|
+
data['additional_instructions'] = additional_instructions
|
|
50
|
+
if tools:
|
|
51
|
+
data['tools'] = tools
|
|
52
|
+
if metadata:
|
|
53
|
+
data['metadata'] = metadata
|
|
54
|
+
data['stream'] = stream
|
|
55
|
+
if extra_body is not None and extra_body:
|
|
56
|
+
data = {**data, **extra_body}
|
|
57
|
+
|
|
58
|
+
response = super().call(data=data,
|
|
59
|
+
path='threads/runs',
|
|
60
|
+
api_key=api_key,
|
|
61
|
+
flattened_output=True,
|
|
62
|
+
stream=stream,
|
|
63
|
+
workspace=workspace,
|
|
64
|
+
**kwargs)
|
|
65
|
+
if stream:
|
|
66
|
+
return ((event_type, cls.convert_stream_object(event_type, item))
|
|
67
|
+
for event_type, item in response)
|
|
68
|
+
else:
|
|
69
|
+
return Run(**response)
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def create(cls,
|
|
73
|
+
thread_id: str,
|
|
74
|
+
*,
|
|
75
|
+
assistant_id: str,
|
|
76
|
+
model: Optional[str] = None,
|
|
77
|
+
instructions: Optional[str] = None,
|
|
78
|
+
additional_instructions: Optional[str] = None,
|
|
79
|
+
tools: Optional[List[Dict]] = None,
|
|
80
|
+
metadata: Optional[Dict] = None,
|
|
81
|
+
stream: Optional[bool] = False,
|
|
82
|
+
workspace: str = None,
|
|
83
|
+
extra_body: Optional[Dict] = None,
|
|
84
|
+
api_key: str = None,
|
|
85
|
+
top_p: Optional[float] = None,
|
|
86
|
+
top_k: Optional[int] = None,
|
|
87
|
+
temperature: Optional[float] = None,
|
|
88
|
+
max_tokens: Optional[int] = None,
|
|
89
|
+
**kwargs) -> Run:
|
|
90
|
+
"""Create a run.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
thread_id (str): The thread to run.
|
|
94
|
+
assistant_id (str): The assistant id to run.
|
|
95
|
+
model (str): The model to use.
|
|
96
|
+
instructions (str, optional): The system instructions this assistant uses. Defaults to None.
|
|
97
|
+
additional_instructions (Optional[str], optional): Appends additional
|
|
98
|
+
instructions at the end of the instructions for the run. This is
|
|
99
|
+
useful for modifying the behavior on a per-run basis without
|
|
100
|
+
overriding other instructions.. Defaults to None.
|
|
101
|
+
tools (Optional[str], optional): List of tools to use.. Defaults to [].
|
|
102
|
+
metadata (Dict, optional): Custom key-value pairs associate with run. Defaults to None.
|
|
103
|
+
workspace (str): The DashScope workspace id.
|
|
104
|
+
api_key (str, optional): The DashScope workspace id. Defaults to None.
|
|
105
|
+
|
|
106
|
+
Raises:
|
|
107
|
+
InputRequired: thread and assistant is required.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
Run: The `Run` object.
|
|
111
|
+
"""
|
|
112
|
+
if not thread_id or not assistant_id:
|
|
113
|
+
raise InputRequired('thread_id and assistant_id is required')
|
|
114
|
+
data = {'assistant_id': assistant_id}
|
|
115
|
+
if model:
|
|
116
|
+
data['model'] = model
|
|
117
|
+
if instructions:
|
|
118
|
+
data['instructions'] = instructions
|
|
119
|
+
if additional_instructions:
|
|
120
|
+
data['additional_instructions'] = additional_instructions
|
|
121
|
+
if tools:
|
|
122
|
+
data['tools'] = tools
|
|
123
|
+
if metadata:
|
|
124
|
+
data['metadata'] = metadata
|
|
125
|
+
data['stream'] = stream
|
|
126
|
+
if extra_body is not None and extra_body:
|
|
127
|
+
data = {**data, **extra_body}
|
|
128
|
+
|
|
129
|
+
if top_p is not None:
|
|
130
|
+
data['top_p'] = top_p
|
|
131
|
+
if top_k is not None:
|
|
132
|
+
data['top_k'] = top_k
|
|
133
|
+
if temperature is not None:
|
|
134
|
+
data['temperature'] = temperature
|
|
135
|
+
if max_tokens is not None:
|
|
136
|
+
data['max_tokens'] = max_tokens
|
|
137
|
+
|
|
138
|
+
response = super().call(data=data,
|
|
139
|
+
path=f'threads/{thread_id}/runs',
|
|
140
|
+
api_key=api_key,
|
|
141
|
+
flattened_output=True,
|
|
142
|
+
stream=stream,
|
|
143
|
+
workspace=workspace,
|
|
144
|
+
**kwargs)
|
|
145
|
+
if stream:
|
|
146
|
+
return ((event_type, cls.convert_stream_object(event_type, item))
|
|
147
|
+
for event_type, item in response)
|
|
148
|
+
else:
|
|
149
|
+
return Run(**response)
|
|
150
|
+
|
|
151
|
+
@classmethod
|
|
152
|
+
def convert_stream_object(cls, event, item):
|
|
153
|
+
event_object_map = {
|
|
154
|
+
'thread.created': Thread,
|
|
155
|
+
'thread.run.created': Run,
|
|
156
|
+
'thread.run.queued': Run,
|
|
157
|
+
'thread.run.in_progress': Run,
|
|
158
|
+
'thread.run.requires_action': Run,
|
|
159
|
+
'thread.run.completed': Run,
|
|
160
|
+
'thread.run.failed': Run,
|
|
161
|
+
'thread.run.cancelled': Run,
|
|
162
|
+
'thread.run.expired': Run,
|
|
163
|
+
'thread.run.step.created': RunStep,
|
|
164
|
+
'thread.run.step.in_progress': RunStep,
|
|
165
|
+
'thread.run.step.delta': RunStepDelta,
|
|
166
|
+
'thread.run.step.completed': RunStep,
|
|
167
|
+
'thread.run.step.failed': RunStep,
|
|
168
|
+
'thread.run.step.cancelled': RunStep,
|
|
169
|
+
'thread.run.step.expired': RunStep,
|
|
170
|
+
'thread.message.created': ThreadMessage,
|
|
171
|
+
'thread.message.in_progress': ThreadMessage,
|
|
172
|
+
'thread.message.delta': ThreadMessageDelta,
|
|
173
|
+
'thread.message.completed': ThreadMessage,
|
|
174
|
+
'thread.message.incomplete': ThreadMessage,
|
|
175
|
+
'error': AssistantError,
|
|
176
|
+
}
|
|
177
|
+
if (event in event_object_map):
|
|
178
|
+
return event_object_map[event](**item)
|
|
179
|
+
else:
|
|
180
|
+
return item
|
|
181
|
+
|
|
182
|
+
@classmethod
|
|
183
|
+
def call(cls,
|
|
184
|
+
thread_id: str,
|
|
185
|
+
*,
|
|
186
|
+
assistant_id: str,
|
|
187
|
+
model: Optional[str] = None,
|
|
188
|
+
instructions: Optional[str] = None,
|
|
189
|
+
additional_instructions: Optional[str] = None,
|
|
190
|
+
tools: Optional[List[Dict]] = None,
|
|
191
|
+
stream: Optional[bool] = False,
|
|
192
|
+
metadata: Optional[Dict] = None,
|
|
193
|
+
workspace: str = None,
|
|
194
|
+
extra_body: Optional[Dict] = None,
|
|
195
|
+
api_key: str = None,
|
|
196
|
+
top_p: Optional[float] = None,
|
|
197
|
+
top_k: Optional[int] = None,
|
|
198
|
+
temperature: Optional[float] = None,
|
|
199
|
+
max_tokens: Optional[int] = None,
|
|
200
|
+
**kwargs) -> Run:
|
|
201
|
+
"""Create a run.
|
|
202
|
+
|
|
203
|
+
Args:
|
|
204
|
+
thread_id (str): The thread to run.
|
|
205
|
+
assistant_id (str): The assistant id to run.
|
|
206
|
+
model (str): The model to use.
|
|
207
|
+
instructions (str, optional): The system instructions this assistant uses. Defaults to None.
|
|
208
|
+
additional_instructions (Optional[str], optional): Appends additional
|
|
209
|
+
instructions at the end of the instructions for the run. This is
|
|
210
|
+
useful for modifying the behavior on a per-run basis without
|
|
211
|
+
overriding other instructions.. Defaults to None.
|
|
212
|
+
tools (Optional[str], optional): List of tools to use.. Defaults to [].
|
|
213
|
+
metadata (Dict, optional): Custom key-value pairs associate with run. Defaults to None.
|
|
214
|
+
workspace (str): The DashScope workspace id.
|
|
215
|
+
api_key (str, optional): The DashScope workspace id. Defaults to None.
|
|
216
|
+
|
|
217
|
+
Raises:
|
|
218
|
+
InputRequired: thread and assistant is required.
|
|
219
|
+
|
|
220
|
+
Returns:
|
|
221
|
+
Run: The `Run` object.
|
|
222
|
+
"""
|
|
223
|
+
return cls.create(thread_id,
|
|
224
|
+
assistant_id=assistant_id,
|
|
225
|
+
model=model,
|
|
226
|
+
instructions=instructions,
|
|
227
|
+
additional_instructions=additional_instructions,
|
|
228
|
+
tools=tools,
|
|
229
|
+
stream=stream,
|
|
230
|
+
metadata=metadata,
|
|
231
|
+
workspace=workspace,
|
|
232
|
+
extra_body=extra_body,
|
|
233
|
+
api_key=api_key,
|
|
234
|
+
top_p=top_p,
|
|
235
|
+
top_k=top_k,
|
|
236
|
+
temperature=temperature,
|
|
237
|
+
max_tokens=max_tokens,
|
|
238
|
+
**kwargs)
|
|
239
|
+
|
|
240
|
+
@classmethod
|
|
241
|
+
def list(cls,
|
|
242
|
+
thread_id: str,
|
|
243
|
+
*,
|
|
244
|
+
limit: int = None,
|
|
245
|
+
order: str = None,
|
|
246
|
+
after: str = None,
|
|
247
|
+
before: str = None,
|
|
248
|
+
workspace: str = None,
|
|
249
|
+
api_key: str = None,
|
|
250
|
+
**kwargs) -> RunList:
|
|
251
|
+
"""List `Run`.
|
|
252
|
+
|
|
253
|
+
Args:
|
|
254
|
+
thread_id (str): The thread id.
|
|
255
|
+
limit (int, optional): How many assistant to retrieve. Defaults to None.
|
|
256
|
+
order (str, optional): Sort order by created_at. Defaults to None.
|
|
257
|
+
after (str, optional): Assistant id after. Defaults to None.
|
|
258
|
+
before (str, optional): Assistant id before. Defaults to None.
|
|
259
|
+
workspace (str, optional): The DashScope workspace id. Defaults to None.
|
|
260
|
+
api_key (str, optional): Your DashScope api key. Defaults to None.
|
|
261
|
+
|
|
262
|
+
Returns:
|
|
263
|
+
RunList: The list of runs.
|
|
264
|
+
"""
|
|
265
|
+
if not thread_id:
|
|
266
|
+
raise InputRequired('thread_id is required!')
|
|
267
|
+
response = super().list(limit=limit,
|
|
268
|
+
order=order,
|
|
269
|
+
after=after,
|
|
270
|
+
before=before,
|
|
271
|
+
path=f'threads/{thread_id}/runs',
|
|
272
|
+
workspace=workspace,
|
|
273
|
+
api_key=api_key,
|
|
274
|
+
flattened_output=True,
|
|
275
|
+
**kwargs)
|
|
276
|
+
return RunList(**response)
|
|
277
|
+
|
|
278
|
+
@classmethod
|
|
279
|
+
def retrieve(cls,
|
|
280
|
+
run_id: str,
|
|
281
|
+
*,
|
|
282
|
+
thread_id: str,
|
|
283
|
+
workspace: str = None,
|
|
284
|
+
api_key: str = None,
|
|
285
|
+
**kwargs) -> Run:
|
|
286
|
+
"""Retrieve the `Run`.
|
|
287
|
+
|
|
288
|
+
Args:
|
|
289
|
+
thread_id (str): The thread id.
|
|
290
|
+
run_id (str): The run id.
|
|
291
|
+
workspace (str): The dashscope workspace id.
|
|
292
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
293
|
+
|
|
294
|
+
Returns:
|
|
295
|
+
Run: The `Run` object.
|
|
296
|
+
"""
|
|
297
|
+
if not thread_id or not run_id:
|
|
298
|
+
raise InputRequired('thread_id and run_id are required!')
|
|
299
|
+
response = super().get(run_id,
|
|
300
|
+
path=f'threads/{thread_id}/runs/{run_id}',
|
|
301
|
+
workspace=workspace,
|
|
302
|
+
api_key=api_key,
|
|
303
|
+
flattened_output=True,
|
|
304
|
+
**kwargs)
|
|
305
|
+
return Run(**response)
|
|
306
|
+
|
|
307
|
+
@classmethod
|
|
308
|
+
def get(cls,
|
|
309
|
+
run_id: str,
|
|
310
|
+
*,
|
|
311
|
+
thread_id: str,
|
|
312
|
+
workspace: str = None,
|
|
313
|
+
api_key: str = None,
|
|
314
|
+
**kwargs) -> Run:
|
|
315
|
+
"""Retrieve the `Run`.
|
|
316
|
+
|
|
317
|
+
Args:
|
|
318
|
+
thread_id (str): The thread id.
|
|
319
|
+
run_id (str): The run id.
|
|
320
|
+
workspace (str): The dashscope workspace id.
|
|
321
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
322
|
+
|
|
323
|
+
Returns:
|
|
324
|
+
Run: The `Run` object.
|
|
325
|
+
"""
|
|
326
|
+
return cls.retrieve(run_id,
|
|
327
|
+
thread_id=thread_id,
|
|
328
|
+
workspace=workspace,
|
|
329
|
+
api_key=api_key,
|
|
330
|
+
**kwargs)
|
|
331
|
+
|
|
332
|
+
@classmethod
|
|
333
|
+
def submit_tool_outputs(cls,
|
|
334
|
+
run_id: str,
|
|
335
|
+
*,
|
|
336
|
+
thread_id: str,
|
|
337
|
+
tool_outputs: List[Dict],
|
|
338
|
+
stream: Optional[bool] = False,
|
|
339
|
+
workspace: str = None,
|
|
340
|
+
extra_body: Optional[Dict] = None,
|
|
341
|
+
api_key: str = None,
|
|
342
|
+
**kwargs) -> Run:
|
|
343
|
+
"""_summary_
|
|
344
|
+
|
|
345
|
+
Args:
|
|
346
|
+
thread_id (str): The thread id.
|
|
347
|
+
run_id (str): The run id.
|
|
348
|
+
tool_outputs (List[Dict]): The tool outputs.
|
|
349
|
+
workspace (str): The dashscope workspace id.
|
|
350
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
351
|
+
|
|
352
|
+
Raises:
|
|
353
|
+
InputRequired: The tool output thread id run id
|
|
354
|
+
are required.
|
|
355
|
+
|
|
356
|
+
Returns:
|
|
357
|
+
Run: The 'Run`.
|
|
358
|
+
"""
|
|
359
|
+
if not tool_outputs:
|
|
360
|
+
raise InputRequired('tool_outputs is required!')
|
|
361
|
+
if not thread_id or not run_id:
|
|
362
|
+
raise InputRequired('thread_id and run_id are required!')
|
|
363
|
+
|
|
364
|
+
data = {'tool_outputs': tool_outputs}
|
|
365
|
+
data['stream'] = stream
|
|
366
|
+
if extra_body is not None and extra_body:
|
|
367
|
+
data = {**data, **extra_body}
|
|
368
|
+
|
|
369
|
+
response = super().call(
|
|
370
|
+
data,
|
|
371
|
+
path=f'threads/{thread_id}/runs/{run_id}/submit_tool_outputs',
|
|
372
|
+
workspace=workspace,
|
|
373
|
+
api_key=api_key,
|
|
374
|
+
stream=stream,
|
|
375
|
+
flattened_output=True,
|
|
376
|
+
**kwargs)
|
|
377
|
+
if stream:
|
|
378
|
+
return ((event_type, cls.convert_stream_object(event_type, item))
|
|
379
|
+
for event_type, item in response)
|
|
380
|
+
else:
|
|
381
|
+
return Run(**response)
|
|
382
|
+
|
|
383
|
+
@classmethod
|
|
384
|
+
def wait(cls,
|
|
385
|
+
run_id: str,
|
|
386
|
+
*,
|
|
387
|
+
thread_id: str,
|
|
388
|
+
timeout_seconds: float = float('inf'),
|
|
389
|
+
workspace: str = None,
|
|
390
|
+
api_key: str = None,
|
|
391
|
+
**kwargs) -> Run:
|
|
392
|
+
"""Wait for run to complete.
|
|
393
|
+
|
|
394
|
+
Args:
|
|
395
|
+
thread_id (str): The thread id.
|
|
396
|
+
run_id (str): The run id.
|
|
397
|
+
timeout_seconds (int): The timeout seconds. Defaults inf.
|
|
398
|
+
workspace (str): The dashscope workspace id.
|
|
399
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
400
|
+
|
|
401
|
+
Returns:
|
|
402
|
+
Run: The run final status.
|
|
403
|
+
"""
|
|
404
|
+
if not run_id or not thread_id:
|
|
405
|
+
raise InputRequired('run_id and thread_id are required!')
|
|
406
|
+
start_time = time.perf_counter()
|
|
407
|
+
while True:
|
|
408
|
+
run = cls.get(run_id,
|
|
409
|
+
thread_id=thread_id,
|
|
410
|
+
workspace=workspace,
|
|
411
|
+
api_key=api_key)
|
|
412
|
+
if run.status_code == HTTPStatus.OK:
|
|
413
|
+
if hasattr(run, 'status'):
|
|
414
|
+
if run.status in [
|
|
415
|
+
'cancelled', 'failed', 'completed', 'expired',
|
|
416
|
+
'requires_action'
|
|
417
|
+
]:
|
|
418
|
+
break
|
|
419
|
+
else:
|
|
420
|
+
time_eclipsed = time.perf_counter() - start_time
|
|
421
|
+
if time_eclipsed > timeout_seconds:
|
|
422
|
+
raise TimeoutException('Wait run complete timeout')
|
|
423
|
+
time.sleep(1)
|
|
424
|
+
else:
|
|
425
|
+
logger.error('run has no status')
|
|
426
|
+
break
|
|
427
|
+
else:
|
|
428
|
+
logger.error(
|
|
429
|
+
'Get run thread_id: %s, run_id: %s failed, message: %s' %
|
|
430
|
+
(thread_id, run_id, run.message))
|
|
431
|
+
break
|
|
432
|
+
return run
|
|
433
|
+
|
|
434
|
+
@classmethod
|
|
435
|
+
def update(cls,
|
|
436
|
+
run_id: str,
|
|
437
|
+
*,
|
|
438
|
+
thread_id: str,
|
|
439
|
+
metadata: Optional[Dict] = None,
|
|
440
|
+
workspace: str = None,
|
|
441
|
+
api_key: str = None,
|
|
442
|
+
**kwargs) -> Run:
|
|
443
|
+
"""Create a run.
|
|
444
|
+
|
|
445
|
+
Args:
|
|
446
|
+
thread_id (str): The thread of the run id to be updated.
|
|
447
|
+
run_id (str): The run id to update.
|
|
448
|
+
model (str): The model to use.
|
|
449
|
+
metadata (Dict, optional): Custom key-value pairs associate with run. Defaults to None.
|
|
450
|
+
workspace (str): The DashScope workspace id.
|
|
451
|
+
api_key (str, optional): The DashScope workspace id. Defaults to None.
|
|
452
|
+
|
|
453
|
+
Raises:
|
|
454
|
+
InputRequired: thread id and run is required.
|
|
455
|
+
|
|
456
|
+
Returns:
|
|
457
|
+
Run: The `Run` object.
|
|
458
|
+
"""
|
|
459
|
+
if not thread_id or not run_id:
|
|
460
|
+
raise InputRequired('thread id and run id are required!')
|
|
461
|
+
response = super().update(run_id,
|
|
462
|
+
json={'metadata': metadata},
|
|
463
|
+
path='threads/%s/runs/%s' %
|
|
464
|
+
(thread_id, run_id),
|
|
465
|
+
api_key=api_key,
|
|
466
|
+
workspace=workspace,
|
|
467
|
+
flattened_output=True,
|
|
468
|
+
method='post',
|
|
469
|
+
**kwargs)
|
|
470
|
+
return Run(**response)
|
|
471
|
+
|
|
472
|
+
@classmethod
|
|
473
|
+
def cancel(cls,
|
|
474
|
+
run_id: str,
|
|
475
|
+
*,
|
|
476
|
+
thread_id: str,
|
|
477
|
+
workspace: str = None,
|
|
478
|
+
api_key: str = None,
|
|
479
|
+
**kwargs) -> Run:
|
|
480
|
+
"""Cancel the `Run`.
|
|
481
|
+
|
|
482
|
+
Args:
|
|
483
|
+
thread_id (str): The thread id.
|
|
484
|
+
run_id (str): The run id.
|
|
485
|
+
workspace (str): The dashscope workspace id.
|
|
486
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
487
|
+
|
|
488
|
+
Returns:
|
|
489
|
+
Run: The `Run` object.
|
|
490
|
+
"""
|
|
491
|
+
if not thread_id or not run_id:
|
|
492
|
+
raise InputRequired('thread id and run id are required!')
|
|
493
|
+
response = super().cancel(run_id,
|
|
494
|
+
path='threads/%s/runs/%s/cancel' %
|
|
495
|
+
(thread_id, run_id),
|
|
496
|
+
api_key=api_key,
|
|
497
|
+
workspace=workspace,
|
|
498
|
+
flattened_output=True,
|
|
499
|
+
**kwargs)
|
|
500
|
+
|
|
501
|
+
return Run(**response)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
2
|
+
|
|
3
|
+
from dashscope.client.base_api import GetStatusMixin, ListObjectMixin
|
|
4
|
+
from dashscope.common.error import InputRequired
|
|
5
|
+
from dashscope.threads.thread_types import RunStep, RunStepList
|
|
6
|
+
|
|
7
|
+
__all__ = ['Steps']
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Steps(ListObjectMixin, GetStatusMixin):
|
|
11
|
+
SUB_PATH = 'RUNS' # useless
|
|
12
|
+
|
|
13
|
+
@classmethod
|
|
14
|
+
def list(cls,
|
|
15
|
+
run_id: str,
|
|
16
|
+
*,
|
|
17
|
+
thread_id: str,
|
|
18
|
+
limit: int = None,
|
|
19
|
+
order: str = None,
|
|
20
|
+
after: str = None,
|
|
21
|
+
before: str = None,
|
|
22
|
+
workspace: str = None,
|
|
23
|
+
api_key: str = None,
|
|
24
|
+
**kwargs) -> RunStepList:
|
|
25
|
+
"""List `RunStep` of `Run`.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
thread_id (str): The thread id.
|
|
29
|
+
run_id (str): The run id.
|
|
30
|
+
limit (int, optional): How many assistant to retrieve. Defaults to None.
|
|
31
|
+
order (str, optional): Sort order by created_at. Defaults to None.
|
|
32
|
+
after (str, optional): Assistant id after. Defaults to None.
|
|
33
|
+
before (str, optional): Assistant id before. Defaults to None.
|
|
34
|
+
workspace (str, optional): The DashScope workspace id. Defaults to None.
|
|
35
|
+
api_key (str, optional): Your DashScope api key. Defaults to None.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
RunList: The list of runs.
|
|
39
|
+
"""
|
|
40
|
+
if not run_id:
|
|
41
|
+
raise InputRequired('run_id is required!')
|
|
42
|
+
response = super().list(
|
|
43
|
+
limit=limit,
|
|
44
|
+
order=order,
|
|
45
|
+
after=after,
|
|
46
|
+
before=before,
|
|
47
|
+
path=f'threads/{thread_id}/runs/{run_id}/steps',
|
|
48
|
+
workspace=workspace,
|
|
49
|
+
api_key=api_key,
|
|
50
|
+
flattened_output=True,
|
|
51
|
+
**kwargs)
|
|
52
|
+
return RunStepList(**response)
|
|
53
|
+
|
|
54
|
+
@classmethod
|
|
55
|
+
def retrieve(cls,
|
|
56
|
+
step_id: str,
|
|
57
|
+
*,
|
|
58
|
+
thread_id: str,
|
|
59
|
+
run_id: str,
|
|
60
|
+
workspace: str = None,
|
|
61
|
+
api_key: str = None,
|
|
62
|
+
**kwargs) -> RunStep:
|
|
63
|
+
"""Retrieve the `RunStep`.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
thread_id (str): The thread id.
|
|
67
|
+
run_id (str): The run id.
|
|
68
|
+
step_id (str): The step id.
|
|
69
|
+
workspace (str): The dashscope workspace id.
|
|
70
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
RunStep: The `RunStep` object.
|
|
74
|
+
"""
|
|
75
|
+
if not thread_id or not run_id or not step_id:
|
|
76
|
+
raise InputRequired('thread_id, run_id and step_id are required!')
|
|
77
|
+
response = super().get(
|
|
78
|
+
run_id,
|
|
79
|
+
path=f'threads/{thread_id}/runs/{run_id}/steps/{step_id}',
|
|
80
|
+
workspace=workspace,
|
|
81
|
+
api_key=api_key,
|
|
82
|
+
flattened_output=True,
|
|
83
|
+
**kwargs)
|
|
84
|
+
return RunStep(**response)
|
|
85
|
+
|
|
86
|
+
@classmethod
|
|
87
|
+
def get(cls,
|
|
88
|
+
step_id: str,
|
|
89
|
+
*,
|
|
90
|
+
thread_id: str,
|
|
91
|
+
run_id: str,
|
|
92
|
+
workspace: str = None,
|
|
93
|
+
api_key: str = None,
|
|
94
|
+
**kwargs) -> RunStep:
|
|
95
|
+
"""Retrieve the `RunStep`.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
thread_id (str): The thread id.
|
|
99
|
+
run_id (str): The run id.
|
|
100
|
+
step_id (str): The step id.
|
|
101
|
+
workspace (str): The dashscope workspace id.
|
|
102
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
RunStep: The `RunStep` object.
|
|
106
|
+
"""
|
|
107
|
+
return cls.retrieve(thread_id,
|
|
108
|
+
run_id,
|
|
109
|
+
step_id,
|
|
110
|
+
workspace=workspace,
|
|
111
|
+
api_key=api_key,
|
|
112
|
+
**kwargs)
|