File size: 7,339 Bytes
cbc00ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# -*- coding: utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.

from typing import Dict, List, Optional

from dashscope.assistants.assistant_types import DeleteResponse
from dashscope.client.base_api import (
    CreateMixin,
    DeleteMixin,
    GetStatusMixin,
    UpdateMixin,
)
from dashscope.common.error import InputRequired
from dashscope.threads.thread_types import Run, Thread

__all__ = ["Threads"]


class Threads(CreateMixin, DeleteMixin, GetStatusMixin, UpdateMixin):
    SUB_PATH = "threads"

    @classmethod
    def call(  # type: ignore[override]
        cls,
        *,
        messages: List[Dict] = None,
        metadata: Dict = None,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Thread:
        """Create a thread.

        Args:
            messages (List[Dict], optional):
                List of messages to start thread. Defaults to None.
            metadata (Dict, optional):
                The key-value information associate with thread. Defaults to
                None.
            workspace (str, optional):
                The DashScope workspace id. Defaults to None.
            api_key (str, optional): Your DashScope api key. Defaults to None.

        Returns:
            Thread: The thread object.
        """
        return cls.create(
            messages=messages,
            metadata=metadata,
            workspace=workspace,
            api_key=api_key,
            **kwargs,
        )

    @classmethod
    def create(
        cls,
        *,
        messages: List[Dict] = None,
        metadata: Dict = None,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Thread:
        """Create a thread.

        Args:
            messages (List[Dict], optional):
                List of messages to start thread. Defaults to None.
            metadata (Dict, optional):
                The key-value information associate with thread. Defaults to
                None.
            workspace (str, optional):
                The DashScope workspace id. Defaults to None.
            api_key (str, optional): Your DashScope api key. Defaults to None.

        Returns:
            Thread: The thread object.
        """
        data = {}
        if messages:
            data["messages"] = messages
        if metadata:
            data["metadata"] = metadata
        response = super().call(
            data=data if data else "",
            api_key=api_key,
            flattened_output=True,
            workspace=workspace,
            **kwargs,
        )
        return Thread(**response)

    @classmethod
    def get(  # type: ignore[override]
        cls,
        thread_id: str,
        *,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Thread:
        """Retrieve the thread.

        Args:
            thread_id (str): The target thread.
            workspace (str, optional):
                The DashScope workspace id. Defaults to None.
            api_key (str, optional): Your DashScope api key. Defaults to None.

        Returns:
            Thread: The `Thread` information.
        """
        return cls.retrieve(
            thread_id,
            workspace=workspace,
            api_key=api_key,
            **kwargs,
        )

    @classmethod
    def retrieve(
        cls,
        thread_id: str,
        *,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Thread:
        """Retrieve the thread.

        Args:
            thread_id (str): The target thread.
            workspace (str, optional):
                The DashScope workspace id. Defaults to None.
            api_key (str, optional): Your DashScope api key. Defaults to None.

        Returns:
            Thread: The `Thread` information.
        """
        if not thread_id:
            raise InputRequired("thread_id is required!")
        response = super().get(
            thread_id,
            api_key=api_key,
            flattened_output=True,
            workspace=workspace,
            **kwargs,
        )
        return Thread(**response)

    @classmethod
    def update(  # type: ignore[override]
        cls,
        thread_id: str,
        *,
        metadata: Dict = None,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Thread:
        """Update thread information.

        Args:
            thread_id (str): The thread id.
            metadata (Dict, optional):
                The thread key-value information. Defaults to None.
            workspace (str, optional):
                The DashScope workspace id. Defaults to None.
            api_key (str, optional): Your DashScope api key. Defaults to None.

        Returns:
            Thread: The `Thread` information.
        """
        if not thread_id:
            raise InputRequired("thread_id is required!")
        response = super().update(
            thread_id,
            json={"metadata": metadata},
            api_key=api_key,
            workspace=workspace,
            flattened_output=True,
            method="post",
            **kwargs,
        )
        return Thread(**response)

    @classmethod
    def delete(  # type: ignore[override]
        cls,
        thread_id,
        *,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> DeleteResponse:
        """Delete thread.

        Args:
            thread_id (str): The thread id to delete.
            workspace (str, optional):
                The DashScope workspace id. Defaults to None.
            api_key (str, optional): Your DashScope api key. Defaults to None.

        Returns:
            AssistantsDeleteResponse: The deleted information.
        """
        if not thread_id:
            raise InputRequired("thread_id is required!")
        response = super().delete(
            thread_id,
            api_key=api_key,
            workspace=workspace,
            flattened_output=True,
            **kwargs,
        )
        return DeleteResponse(**response)

    @classmethod
    def create_and_run(
        cls,
        *,
        assistant_id: str,
        thread: Optional[Dict] = None,
        model: Optional[str] = None,
        instructions: Optional[str] = None,
        additional_instructions: Optional[str] = None,
        tools: Optional[List[Dict]] = None,
        metadata: Optional[Dict] = None,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Run:
        if not assistant_id:
            raise InputRequired("assistant_id is required")
        data = {"assistant_id": assistant_id}
        if thread:
            data["thread"] = thread
        if model:
            data["model"] = model
        if instructions:
            data["instructions"] = instructions
        if additional_instructions:
            data["additional_instructions"] = additional_instructions
        if tools:
            data["tools"] = tools
        if metadata:
            data["metadata"] = metadata

        response = super().call(
            data=data,
            path="threads/runs",
            api_key=api_key,
            flattened_output=True,
            workspace=workspace,
            **kwargs,
        )
        return Run(**response)