Skip to content

Commit d15bf14

Browse files
author
zhansheng.lzs
committed
refactor: add deprecation warnings to Threads class without changing logic
- Restore original threads.py from fedf9f8 - Add only deprecation warnings, no logic changes - Keep all super() calls unchanged (get, update, delete) - Add warnings to: create, retrieve, update, delete, create_and_run - All tests pass (5/5)
1 parent 5124df4 commit d15bf14

1 file changed

Lines changed: 60 additions & 17 deletions

File tree

dashscope/threads/threads.py

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright (c) Alibaba, Inc. and its affiliates.
33

44
import warnings
5-
from typing import Dict, List
5+
from typing import Dict, List, Optional
66

77
from dashscope.assistants.assistant_types import DeleteResponse
88
from dashscope.client.base_api import (
@@ -12,11 +12,10 @@
1212
UpdateMixin,
1313
)
1414
from dashscope.common.error import InputRequired
15-
from dashscope.threads.thread_types import Thread
15+
from dashscope.threads.thread_types import Run, Thread
1616

1717
__all__ = ["Threads"]
1818

19-
# Deprecation warning message
2019
_DEPRECATION_MSG = (
2120
"The Assistants API (dashscope.threads) is deprecated and will be "
2221
"removed in a future release. Please migrate to the Responses API. "
@@ -186,13 +185,12 @@ def update( # type: ignore[override]
186185
api_key: str = None,
187186
**kwargs,
188187
) -> Thread:
189-
"""Update the thread.
188+
"""Update thread information.
190189
191190
Args:
192-
thread_id (str): The target thread.
191+
thread_id (str): The thread id.
193192
metadata (Dict, optional):
194-
The key-value information associate with thread. Defaults to
195-
None.
193+
The thread key-value information. Defaults to None.
196194
workspace (str, optional):
197195
The DashScope workspace id. Defaults to None.
198196
api_key (str, optional): Your DashScope api key. Defaults to None.
@@ -207,37 +205,36 @@ def update( # type: ignore[override]
207205
)
208206
if not thread_id:
209207
raise InputRequired("thread_id is required!")
210-
data = {"thread_id": thread_id}
211-
if metadata:
212-
data["metadata"] = metadata
213-
response = super().call(
214-
data=data,
208+
response = super().update(
209+
thread_id,
210+
json={"metadata": metadata},
215211
api_key=api_key,
216-
flattened_output=True,
217212
workspace=workspace,
213+
flattened_output=True,
214+
method="post",
218215
**kwargs,
219216
)
220217
return Thread(**response)
221218

222219
@classmethod
223220
def delete( # type: ignore[override]
224221
cls,
225-
thread_id: str,
222+
thread_id,
226223
*,
227224
workspace: str = None,
228225
api_key: str = None,
229226
**kwargs,
230227
) -> DeleteResponse:
231-
"""Delete the thread.
228+
"""Delete thread.
232229
233230
Args:
234-
thread_id (str): The target thread.
231+
thread_id (str): The thread id to delete.
235232
workspace (str, optional):
236233
The DashScope workspace id. Defaults to None.
237234
api_key (str, optional): Your DashScope api key. Defaults to None.
238235
239236
Returns:
240-
DeleteResponse: The delete response.
237+
AssistantsDeleteResponse: The deleted information.
241238
"""
242239
warnings.warn(
243240
_DEPRECATION_MSG,
@@ -254,3 +251,49 @@ def delete( # type: ignore[override]
254251
**kwargs,
255252
)
256253
return DeleteResponse(**response)
254+
255+
@classmethod
256+
def create_and_run(
257+
cls,
258+
*,
259+
assistant_id: str,
260+
thread: Optional[Dict] = None,
261+
model: Optional[str] = None,
262+
instructions: Optional[str] = None,
263+
additional_instructions: Optional[str] = None,
264+
tools: Optional[List[Dict]] = None,
265+
metadata: Optional[Dict] = None,
266+
workspace: str = None,
267+
api_key: str = None,
268+
**kwargs,
269+
) -> Run:
270+
warnings.warn(
271+
_DEPRECATION_MSG,
272+
category=DeprecationWarning,
273+
stacklevel=2,
274+
)
275+
if not assistant_id:
276+
raise InputRequired("assistant_id is required")
277+
data = {"assistant_id": assistant_id}
278+
if thread:
279+
data["thread"] = thread
280+
if model:
281+
data["model"] = model
282+
if instructions:
283+
data["instructions"] = instructions
284+
if additional_instructions:
285+
data["additional_instructions"] = additional_instructions
286+
if tools:
287+
data["tools"] = tools
288+
if metadata:
289+
data["metadata"] = metadata
290+
291+
response = super().call(
292+
data=data,
293+
path="threads/runs",
294+
api_key=api_key,
295+
flattened_output=True,
296+
workspace=workspace,
297+
**kwargs,
298+
)
299+
return Run(**response)

0 commit comments

Comments
 (0)