chainlit 2.7.0__py3-none-any.whl → 2.7.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 chainlit might be problematic. Click here for more details.
- {chainlit-2.7.0.dist-info → chainlit-2.7.1.dist-info}/METADATA +1 -1
- chainlit-2.7.1.dist-info/RECORD +4 -0
- chainlit/__init__.py +0 -207
- chainlit/__main__.py +0 -4
- chainlit/_utils.py +0 -8
- chainlit/action.py +0 -33
- chainlit/auth/__init__.py +0 -95
- chainlit/auth/cookie.py +0 -197
- chainlit/auth/jwt.py +0 -42
- chainlit/cache.py +0 -45
- chainlit/callbacks.py +0 -433
- chainlit/chat_context.py +0 -64
- chainlit/chat_settings.py +0 -34
- chainlit/cli/__init__.py +0 -235
- chainlit/config.py +0 -621
- chainlit/context.py +0 -112
- chainlit/data/__init__.py +0 -111
- chainlit/data/acl.py +0 -19
- chainlit/data/base.py +0 -107
- chainlit/data/chainlit_data_layer.py +0 -687
- chainlit/data/dynamodb.py +0 -616
- chainlit/data/literalai.py +0 -501
- chainlit/data/sql_alchemy.py +0 -741
- chainlit/data/storage_clients/__init__.py +0 -0
- chainlit/data/storage_clients/azure.py +0 -84
- chainlit/data/storage_clients/azure_blob.py +0 -94
- chainlit/data/storage_clients/base.py +0 -28
- chainlit/data/storage_clients/gcs.py +0 -101
- chainlit/data/storage_clients/s3.py +0 -88
- chainlit/data/utils.py +0 -29
- chainlit/discord/__init__.py +0 -6
- chainlit/discord/app.py +0 -364
- chainlit/element.py +0 -454
- chainlit/emitter.py +0 -450
- chainlit/hello.py +0 -12
- chainlit/input_widget.py +0 -182
- chainlit/langchain/__init__.py +0 -6
- chainlit/langchain/callbacks.py +0 -682
- chainlit/langflow/__init__.py +0 -25
- chainlit/llama_index/__init__.py +0 -6
- chainlit/llama_index/callbacks.py +0 -206
- chainlit/logger.py +0 -16
- chainlit/markdown.py +0 -57
- chainlit/mcp.py +0 -99
- chainlit/message.py +0 -619
- chainlit/mistralai/__init__.py +0 -50
- chainlit/oauth_providers.py +0 -835
- chainlit/openai/__init__.py +0 -53
- chainlit/py.typed +0 -0
- chainlit/secret.py +0 -9
- chainlit/semantic_kernel/__init__.py +0 -111
- chainlit/server.py +0 -1616
- chainlit/session.py +0 -304
- chainlit/sidebar.py +0 -55
- chainlit/slack/__init__.py +0 -6
- chainlit/slack/app.py +0 -427
- chainlit/socket.py +0 -381
- chainlit/step.py +0 -490
- chainlit/sync.py +0 -43
- chainlit/teams/__init__.py +0 -6
- chainlit/teams/app.py +0 -348
- chainlit/translations/bn.json +0 -214
- chainlit/translations/el-GR.json +0 -214
- chainlit/translations/en-US.json +0 -214
- chainlit/translations/fr-FR.json +0 -214
- chainlit/translations/gu.json +0 -214
- chainlit/translations/he-IL.json +0 -214
- chainlit/translations/hi.json +0 -214
- chainlit/translations/ja.json +0 -214
- chainlit/translations/kn.json +0 -214
- chainlit/translations/ml.json +0 -214
- chainlit/translations/mr.json +0 -214
- chainlit/translations/nl.json +0 -214
- chainlit/translations/ta.json +0 -214
- chainlit/translations/te.json +0 -214
- chainlit/translations/zh-CN.json +0 -214
- chainlit/translations.py +0 -60
- chainlit/types.py +0 -334
- chainlit/user.py +0 -43
- chainlit/user_session.py +0 -153
- chainlit/utils.py +0 -173
- chainlit/version.py +0 -8
- chainlit-2.7.0.dist-info/RECORD +0 -84
- {chainlit-2.7.0.dist-info → chainlit-2.7.1.dist-info}/WHEEL +0 -0
- {chainlit-2.7.0.dist-info → chainlit-2.7.1.dist-info}/entry_points.txt +0 -0
chainlit/message.py
DELETED
|
@@ -1,619 +0,0 @@
|
|
|
1
|
-
import asyncio
|
|
2
|
-
import json
|
|
3
|
-
import time
|
|
4
|
-
import uuid
|
|
5
|
-
from abc import ABC
|
|
6
|
-
from typing import Dict, List, Optional, Union, cast
|
|
7
|
-
|
|
8
|
-
from literalai.observability.step import MessageStepType
|
|
9
|
-
|
|
10
|
-
from chainlit.action import Action
|
|
11
|
-
from chainlit.chat_context import chat_context
|
|
12
|
-
from chainlit.config import config
|
|
13
|
-
from chainlit.context import context, local_steps
|
|
14
|
-
from chainlit.data import get_data_layer
|
|
15
|
-
from chainlit.element import CustomElement, ElementBased
|
|
16
|
-
from chainlit.logger import logger
|
|
17
|
-
from chainlit.step import StepDict
|
|
18
|
-
from chainlit.types import (
|
|
19
|
-
AskActionResponse,
|
|
20
|
-
AskActionSpec,
|
|
21
|
-
AskElementResponse,
|
|
22
|
-
AskElementSpec,
|
|
23
|
-
AskFileResponse,
|
|
24
|
-
AskFileSpec,
|
|
25
|
-
AskSpec,
|
|
26
|
-
FileDict,
|
|
27
|
-
)
|
|
28
|
-
from chainlit.utils import utc_now
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
class MessageBase(ABC):
|
|
32
|
-
id: str
|
|
33
|
-
thread_id: str
|
|
34
|
-
author: str
|
|
35
|
-
content: str = ""
|
|
36
|
-
type: MessageStepType = "assistant_message"
|
|
37
|
-
streaming = False
|
|
38
|
-
created_at: Union[str, None] = None
|
|
39
|
-
fail_on_persist_error: bool = False
|
|
40
|
-
persisted = False
|
|
41
|
-
is_error = False
|
|
42
|
-
command: Optional[str] = None
|
|
43
|
-
parent_id: Optional[str] = None
|
|
44
|
-
language: Optional[str] = None
|
|
45
|
-
metadata: Optional[Dict] = None
|
|
46
|
-
tags: Optional[List[str]] = None
|
|
47
|
-
wait_for_answer = False
|
|
48
|
-
|
|
49
|
-
def __post_init__(self) -> None:
|
|
50
|
-
self.thread_id = context.session.thread_id
|
|
51
|
-
|
|
52
|
-
previous_steps = local_steps.get() or []
|
|
53
|
-
parent_step = previous_steps[-1] if previous_steps else None
|
|
54
|
-
if parent_step:
|
|
55
|
-
self.parent_id = parent_step.id
|
|
56
|
-
|
|
57
|
-
if not getattr(self, "id", None):
|
|
58
|
-
self.id = str(uuid.uuid4())
|
|
59
|
-
|
|
60
|
-
@classmethod
|
|
61
|
-
def from_dict(self, _dict: StepDict):
|
|
62
|
-
type = _dict.get("type", "assistant_message")
|
|
63
|
-
return Message(
|
|
64
|
-
id=_dict["id"],
|
|
65
|
-
parent_id=_dict.get("parentId"),
|
|
66
|
-
created_at=_dict["createdAt"],
|
|
67
|
-
content=_dict["output"],
|
|
68
|
-
author=_dict.get("name", config.ui.name),
|
|
69
|
-
command=_dict.get("command"),
|
|
70
|
-
type=type, # type: ignore
|
|
71
|
-
language=_dict.get("language"),
|
|
72
|
-
metadata=_dict.get("metadata", {}),
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
def to_dict(self) -> StepDict:
|
|
76
|
-
_dict: StepDict = {
|
|
77
|
-
"id": self.id,
|
|
78
|
-
"threadId": self.thread_id,
|
|
79
|
-
"parentId": self.parent_id,
|
|
80
|
-
"createdAt": self.created_at,
|
|
81
|
-
"command": self.command,
|
|
82
|
-
"start": self.created_at,
|
|
83
|
-
"end": self.created_at,
|
|
84
|
-
"output": self.content,
|
|
85
|
-
"name": self.author,
|
|
86
|
-
"type": self.type,
|
|
87
|
-
"language": self.language,
|
|
88
|
-
"streaming": self.streaming,
|
|
89
|
-
"isError": self.is_error,
|
|
90
|
-
"waitForAnswer": self.wait_for_answer,
|
|
91
|
-
"metadata": self.metadata or {},
|
|
92
|
-
"tags": self.tags,
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return _dict
|
|
96
|
-
|
|
97
|
-
async def update(
|
|
98
|
-
self,
|
|
99
|
-
):
|
|
100
|
-
"""
|
|
101
|
-
Update a message already sent to the UI.
|
|
102
|
-
"""
|
|
103
|
-
|
|
104
|
-
if self.streaming:
|
|
105
|
-
self.streaming = False
|
|
106
|
-
|
|
107
|
-
step_dict = self.to_dict()
|
|
108
|
-
chat_context.add(self)
|
|
109
|
-
|
|
110
|
-
data_layer = get_data_layer()
|
|
111
|
-
if data_layer:
|
|
112
|
-
try:
|
|
113
|
-
asyncio.create_task(data_layer.update_step(step_dict))
|
|
114
|
-
except Exception as e:
|
|
115
|
-
if self.fail_on_persist_error:
|
|
116
|
-
raise e
|
|
117
|
-
logger.error(f"Failed to persist message update: {e!s}")
|
|
118
|
-
|
|
119
|
-
await context.emitter.update_step(step_dict)
|
|
120
|
-
|
|
121
|
-
return True
|
|
122
|
-
|
|
123
|
-
async def remove(self):
|
|
124
|
-
"""
|
|
125
|
-
Remove a message already sent to the UI.
|
|
126
|
-
"""
|
|
127
|
-
chat_context.remove(self)
|
|
128
|
-
step_dict = self.to_dict()
|
|
129
|
-
data_layer = get_data_layer()
|
|
130
|
-
if data_layer:
|
|
131
|
-
try:
|
|
132
|
-
asyncio.create_task(data_layer.delete_step(step_dict["id"]))
|
|
133
|
-
except Exception as e:
|
|
134
|
-
if self.fail_on_persist_error:
|
|
135
|
-
raise e
|
|
136
|
-
logger.error(f"Failed to persist message deletion: {e!s}")
|
|
137
|
-
|
|
138
|
-
await context.emitter.delete_step(step_dict)
|
|
139
|
-
|
|
140
|
-
return True
|
|
141
|
-
|
|
142
|
-
async def _create(self):
|
|
143
|
-
step_dict = self.to_dict()
|
|
144
|
-
data_layer = get_data_layer()
|
|
145
|
-
if data_layer and not self.persisted:
|
|
146
|
-
try:
|
|
147
|
-
asyncio.create_task(data_layer.create_step(step_dict))
|
|
148
|
-
self.persisted = True
|
|
149
|
-
except Exception as e:
|
|
150
|
-
if self.fail_on_persist_error:
|
|
151
|
-
raise e
|
|
152
|
-
logger.error(f"Failed to persist message creation: {e!s}")
|
|
153
|
-
|
|
154
|
-
return step_dict
|
|
155
|
-
|
|
156
|
-
async def send(self):
|
|
157
|
-
if not self.created_at:
|
|
158
|
-
self.created_at = utc_now()
|
|
159
|
-
if self.content is None:
|
|
160
|
-
self.content = ""
|
|
161
|
-
|
|
162
|
-
if config.code.author_rename:
|
|
163
|
-
self.author = await config.code.author_rename(self.author)
|
|
164
|
-
|
|
165
|
-
if self.streaming:
|
|
166
|
-
self.streaming = False
|
|
167
|
-
|
|
168
|
-
step_dict = await self._create()
|
|
169
|
-
chat_context.add(self)
|
|
170
|
-
await context.emitter.send_step(step_dict)
|
|
171
|
-
|
|
172
|
-
return self
|
|
173
|
-
|
|
174
|
-
async def stream_token(self, token: str, is_sequence=False):
|
|
175
|
-
"""
|
|
176
|
-
Sends a token to the UI. This is useful for streaming messages.
|
|
177
|
-
Once all tokens have been streamed, call .send() to end the stream and persist the message if persistence is enabled.
|
|
178
|
-
"""
|
|
179
|
-
if not token:
|
|
180
|
-
return
|
|
181
|
-
|
|
182
|
-
if is_sequence:
|
|
183
|
-
self.content = token
|
|
184
|
-
else:
|
|
185
|
-
self.content += token
|
|
186
|
-
|
|
187
|
-
assert self.id
|
|
188
|
-
|
|
189
|
-
if not self.streaming:
|
|
190
|
-
self.streaming = True
|
|
191
|
-
step_dict = self.to_dict()
|
|
192
|
-
await context.emitter.stream_start(step_dict)
|
|
193
|
-
else:
|
|
194
|
-
await context.emitter.send_token(
|
|
195
|
-
id=self.id, token=token, is_sequence=is_sequence
|
|
196
|
-
)
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
class Message(MessageBase):
|
|
200
|
-
"""
|
|
201
|
-
Send a message to the UI
|
|
202
|
-
|
|
203
|
-
Args:
|
|
204
|
-
content (Union[str, Dict]): The content of the message.
|
|
205
|
-
author (str, optional): The author of the message, this will be used in the UI. Defaults to the assistant name (see config).
|
|
206
|
-
language (str, optional): Language of the code is the content is code. See https://react-code-blocks-rajinwonderland.vercel.app/?path=/story/codeblock--supported-languages for a list of supported languages.
|
|
207
|
-
actions (List[Action], optional): A list of actions to send with the message.
|
|
208
|
-
elements (List[ElementBased], optional): A list of elements to send with the message.
|
|
209
|
-
"""
|
|
210
|
-
|
|
211
|
-
def __init__(
|
|
212
|
-
self,
|
|
213
|
-
content: Union[str, Dict],
|
|
214
|
-
author: Optional[str] = None,
|
|
215
|
-
language: Optional[str] = None,
|
|
216
|
-
actions: Optional[List[Action]] = None,
|
|
217
|
-
elements: Optional[List[ElementBased]] = None,
|
|
218
|
-
type: MessageStepType = "assistant_message",
|
|
219
|
-
metadata: Optional[Dict] = None,
|
|
220
|
-
tags: Optional[List[str]] = None,
|
|
221
|
-
id: Optional[str] = None,
|
|
222
|
-
parent_id: Optional[str] = None,
|
|
223
|
-
command: Optional[str] = None,
|
|
224
|
-
created_at: Union[str, None] = None,
|
|
225
|
-
):
|
|
226
|
-
time.sleep(0.001)
|
|
227
|
-
self.language = language
|
|
228
|
-
if isinstance(content, dict):
|
|
229
|
-
try:
|
|
230
|
-
self.content = json.dumps(content, indent=4, ensure_ascii=False)
|
|
231
|
-
self.language = "json"
|
|
232
|
-
except TypeError:
|
|
233
|
-
self.content = str(content)
|
|
234
|
-
self.language = "text"
|
|
235
|
-
elif isinstance(content, str):
|
|
236
|
-
self.content = content
|
|
237
|
-
else:
|
|
238
|
-
self.content = str(content)
|
|
239
|
-
self.language = "text"
|
|
240
|
-
|
|
241
|
-
if id:
|
|
242
|
-
self.id = str(id)
|
|
243
|
-
|
|
244
|
-
if parent_id:
|
|
245
|
-
self.parent_id = str(parent_id)
|
|
246
|
-
|
|
247
|
-
if command:
|
|
248
|
-
self.command = str(command)
|
|
249
|
-
|
|
250
|
-
if created_at:
|
|
251
|
-
self.created_at = created_at
|
|
252
|
-
|
|
253
|
-
self.metadata = metadata
|
|
254
|
-
self.tags = tags
|
|
255
|
-
|
|
256
|
-
self.author = author or config.ui.name
|
|
257
|
-
self.type = type
|
|
258
|
-
self.actions = actions if actions is not None else []
|
|
259
|
-
self.elements = elements if elements is not None else []
|
|
260
|
-
|
|
261
|
-
super().__post_init__()
|
|
262
|
-
|
|
263
|
-
async def send(self):
|
|
264
|
-
"""
|
|
265
|
-
Send the message to the UI and persist it in the cloud if a project ID is configured.
|
|
266
|
-
Return the ID of the message.
|
|
267
|
-
"""
|
|
268
|
-
await super().send()
|
|
269
|
-
|
|
270
|
-
# Create tasks for all actions and elements
|
|
271
|
-
tasks = [action.send(for_id=self.id) for action in self.actions]
|
|
272
|
-
tasks.extend(element.send(for_id=self.id) for element in self.elements)
|
|
273
|
-
|
|
274
|
-
# Run all tasks concurrently
|
|
275
|
-
await asyncio.gather(*tasks)
|
|
276
|
-
|
|
277
|
-
return self
|
|
278
|
-
|
|
279
|
-
async def update(self):
|
|
280
|
-
"""
|
|
281
|
-
Send the message to the UI and persist it in the cloud if a project ID is configured.
|
|
282
|
-
Return the ID of the message.
|
|
283
|
-
"""
|
|
284
|
-
await super().update()
|
|
285
|
-
|
|
286
|
-
# Update tasks for all actions and elements
|
|
287
|
-
tasks = [
|
|
288
|
-
action.send(for_id=self.id)
|
|
289
|
-
for action in self.actions
|
|
290
|
-
if action.forId is None
|
|
291
|
-
]
|
|
292
|
-
tasks.extend(element.send(for_id=self.id) for element in self.elements)
|
|
293
|
-
|
|
294
|
-
# Run all tasks concurrently
|
|
295
|
-
await asyncio.gather(*tasks)
|
|
296
|
-
|
|
297
|
-
return True
|
|
298
|
-
|
|
299
|
-
async def remove_actions(self):
|
|
300
|
-
for action in self.actions:
|
|
301
|
-
await action.remove()
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
class ErrorMessage(MessageBase):
|
|
305
|
-
"""
|
|
306
|
-
Send an error message to the UI
|
|
307
|
-
If a project ID is configured, the message will be persisted in the cloud.
|
|
308
|
-
|
|
309
|
-
Args:
|
|
310
|
-
content (str): Text displayed above the upload button.
|
|
311
|
-
author (str, optional): The author of the message, this will be used in the UI. Defaults to the assistant name (see config).
|
|
312
|
-
"""
|
|
313
|
-
|
|
314
|
-
def __init__(
|
|
315
|
-
self,
|
|
316
|
-
content: str,
|
|
317
|
-
author: str = config.ui.name,
|
|
318
|
-
fail_on_persist_error: bool = False,
|
|
319
|
-
):
|
|
320
|
-
self.content = content
|
|
321
|
-
self.author = author
|
|
322
|
-
self.type = "assistant_message"
|
|
323
|
-
self.is_error = True
|
|
324
|
-
self.fail_on_persist_error = fail_on_persist_error
|
|
325
|
-
|
|
326
|
-
super().__post_init__()
|
|
327
|
-
|
|
328
|
-
async def send(self):
|
|
329
|
-
"""
|
|
330
|
-
Send the error message to the UI and persist it in the cloud if a project ID is configured.
|
|
331
|
-
Return the ID of the message.
|
|
332
|
-
"""
|
|
333
|
-
return await super().send()
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
class AskMessageBase(MessageBase):
|
|
337
|
-
async def remove(self):
|
|
338
|
-
removed = await super().remove()
|
|
339
|
-
if removed:
|
|
340
|
-
await context.emitter.clear("clear_ask")
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
class AskUserMessage(AskMessageBase):
|
|
344
|
-
"""
|
|
345
|
-
Ask for the user input before continuing.
|
|
346
|
-
If the user does not answer in time (see timeout), a TimeoutError will be raised or None will be returned depending on raise_on_timeout.
|
|
347
|
-
If a project ID is configured, the message will be uploaded to the cloud storage.
|
|
348
|
-
|
|
349
|
-
Args:
|
|
350
|
-
content (str): The content of the prompt.
|
|
351
|
-
author (str, optional): The author of the message, this will be used in the UI. Defaults to the assistant name (see config).
|
|
352
|
-
timeout (int, optional): The number of seconds to wait for an answer before raising a TimeoutError.
|
|
353
|
-
raise_on_timeout (bool, optional): Whether to raise a socketio TimeoutError if the user does not answer in time.
|
|
354
|
-
"""
|
|
355
|
-
|
|
356
|
-
def __init__(
|
|
357
|
-
self,
|
|
358
|
-
content: str,
|
|
359
|
-
author: str = config.ui.name,
|
|
360
|
-
type: MessageStepType = "assistant_message",
|
|
361
|
-
timeout: int = 60,
|
|
362
|
-
raise_on_timeout: bool = False,
|
|
363
|
-
):
|
|
364
|
-
self.content = content
|
|
365
|
-
self.author = author
|
|
366
|
-
self.timeout = timeout
|
|
367
|
-
self.type = type
|
|
368
|
-
self.raise_on_timeout = raise_on_timeout
|
|
369
|
-
|
|
370
|
-
super().__post_init__()
|
|
371
|
-
|
|
372
|
-
async def send(self) -> Union[StepDict, None]:
|
|
373
|
-
"""
|
|
374
|
-
Sends the question to ask to the UI and waits for the reply.
|
|
375
|
-
"""
|
|
376
|
-
if not self.created_at:
|
|
377
|
-
self.created_at = utc_now()
|
|
378
|
-
|
|
379
|
-
if config.code.author_rename:
|
|
380
|
-
self.author = await config.code.author_rename(self.author)
|
|
381
|
-
|
|
382
|
-
if self.streaming:
|
|
383
|
-
self.streaming = False
|
|
384
|
-
|
|
385
|
-
self.wait_for_answer = True
|
|
386
|
-
|
|
387
|
-
step_dict = await self._create()
|
|
388
|
-
|
|
389
|
-
spec = AskSpec(type="text", step_id=step_dict["id"], timeout=self.timeout)
|
|
390
|
-
|
|
391
|
-
res = cast(
|
|
392
|
-
Union[None, StepDict],
|
|
393
|
-
await context.emitter.send_ask_user(step_dict, spec, self.raise_on_timeout),
|
|
394
|
-
)
|
|
395
|
-
|
|
396
|
-
self.wait_for_answer = False
|
|
397
|
-
|
|
398
|
-
return res
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
class AskFileMessage(AskMessageBase):
|
|
402
|
-
"""
|
|
403
|
-
Ask the user to upload a file before continuing.
|
|
404
|
-
If the user does not answer in time (see timeout), a TimeoutError will be raised or None will be returned depending on raise_on_timeout.
|
|
405
|
-
If a project ID is configured, the file will be uploaded to the cloud storage.
|
|
406
|
-
|
|
407
|
-
Args:
|
|
408
|
-
content (str): Text displayed above the upload button.
|
|
409
|
-
accept (Union[List[str], Dict[str, List[str]]]): List of mime type to accept like ["text/csv", "application/pdf"] or a dict like {"text/plain": [".txt", ".py"]}.
|
|
410
|
-
max_size_mb (int, optional): Maximum size per file in MB. Maximum value is 100.
|
|
411
|
-
max_files (int, optional): Maximum number of files to upload. Maximum value is 10.
|
|
412
|
-
author (str, optional): The author of the message, this will be used in the UI. Defaults to the assistant name (see config).
|
|
413
|
-
timeout (int, optional): The number of seconds to wait for an answer before raising a TimeoutError.
|
|
414
|
-
raise_on_timeout (bool, optional): Whether to raise a socketio TimeoutError if the user does not answer in time.
|
|
415
|
-
"""
|
|
416
|
-
|
|
417
|
-
def __init__(
|
|
418
|
-
self,
|
|
419
|
-
content: str,
|
|
420
|
-
accept: Union[List[str], Dict[str, List[str]]],
|
|
421
|
-
max_size_mb=2,
|
|
422
|
-
max_files=1,
|
|
423
|
-
author=config.ui.name,
|
|
424
|
-
type: MessageStepType = "assistant_message",
|
|
425
|
-
timeout=90,
|
|
426
|
-
raise_on_timeout=False,
|
|
427
|
-
):
|
|
428
|
-
self.content = content
|
|
429
|
-
self.max_size_mb = max_size_mb
|
|
430
|
-
self.max_files = max_files
|
|
431
|
-
self.accept = accept
|
|
432
|
-
self.type = type
|
|
433
|
-
self.author = author
|
|
434
|
-
self.timeout = timeout
|
|
435
|
-
self.raise_on_timeout = raise_on_timeout
|
|
436
|
-
|
|
437
|
-
super().__post_init__()
|
|
438
|
-
|
|
439
|
-
async def send(self) -> Union[List[AskFileResponse], None]:
|
|
440
|
-
"""
|
|
441
|
-
Sends the message to request a file from the user to the UI and waits for the reply.
|
|
442
|
-
"""
|
|
443
|
-
if not self.created_at:
|
|
444
|
-
self.created_at = utc_now()
|
|
445
|
-
|
|
446
|
-
if self.streaming:
|
|
447
|
-
self.streaming = False
|
|
448
|
-
|
|
449
|
-
if config.code.author_rename:
|
|
450
|
-
self.author = await config.code.author_rename(self.author)
|
|
451
|
-
|
|
452
|
-
self.wait_for_answer = True
|
|
453
|
-
|
|
454
|
-
step_dict = await self._create()
|
|
455
|
-
|
|
456
|
-
spec = AskFileSpec(
|
|
457
|
-
type="file",
|
|
458
|
-
step_id=step_dict["id"],
|
|
459
|
-
accept=self.accept,
|
|
460
|
-
max_size_mb=self.max_size_mb,
|
|
461
|
-
max_files=self.max_files,
|
|
462
|
-
timeout=self.timeout,
|
|
463
|
-
)
|
|
464
|
-
|
|
465
|
-
res = cast(
|
|
466
|
-
Union[None, List[FileDict]],
|
|
467
|
-
await context.emitter.send_ask_user(step_dict, spec, self.raise_on_timeout),
|
|
468
|
-
)
|
|
469
|
-
|
|
470
|
-
self.wait_for_answer = False
|
|
471
|
-
|
|
472
|
-
if res:
|
|
473
|
-
return [
|
|
474
|
-
AskFileResponse(
|
|
475
|
-
id=r["id"],
|
|
476
|
-
name=r["name"],
|
|
477
|
-
path=str(r["path"]),
|
|
478
|
-
size=r["size"],
|
|
479
|
-
type=r["type"],
|
|
480
|
-
)
|
|
481
|
-
for r in res
|
|
482
|
-
]
|
|
483
|
-
else:
|
|
484
|
-
return None
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
class AskActionMessage(AskMessageBase):
|
|
488
|
-
"""
|
|
489
|
-
Ask the user to select an action before continuing.
|
|
490
|
-
If the user does not answer in time (see timeout), a TimeoutError will be raised or None will be returned depending on raise_on_timeout.
|
|
491
|
-
"""
|
|
492
|
-
|
|
493
|
-
def __init__(
|
|
494
|
-
self,
|
|
495
|
-
content: str,
|
|
496
|
-
actions: List[Action],
|
|
497
|
-
author=config.ui.name,
|
|
498
|
-
timeout=90,
|
|
499
|
-
raise_on_timeout=False,
|
|
500
|
-
):
|
|
501
|
-
self.content = content
|
|
502
|
-
self.actions = actions
|
|
503
|
-
self.author = author
|
|
504
|
-
self.timeout = timeout
|
|
505
|
-
self.raise_on_timeout = raise_on_timeout
|
|
506
|
-
|
|
507
|
-
super().__post_init__()
|
|
508
|
-
|
|
509
|
-
async def send(self) -> Union[AskActionResponse, None]:
|
|
510
|
-
"""
|
|
511
|
-
Sends the question to ask to the UI and waits for the reply
|
|
512
|
-
"""
|
|
513
|
-
if not self.created_at:
|
|
514
|
-
self.created_at = utc_now()
|
|
515
|
-
|
|
516
|
-
if self.streaming:
|
|
517
|
-
self.streaming = False
|
|
518
|
-
|
|
519
|
-
if config.code.author_rename:
|
|
520
|
-
self.author = await config.code.author_rename(self.author)
|
|
521
|
-
|
|
522
|
-
self.wait_for_answer = True
|
|
523
|
-
|
|
524
|
-
step_dict = await self._create()
|
|
525
|
-
|
|
526
|
-
action_keys = []
|
|
527
|
-
|
|
528
|
-
for action in self.actions:
|
|
529
|
-
action_keys.append(action.id)
|
|
530
|
-
await action.send(for_id=str(step_dict["id"]))
|
|
531
|
-
|
|
532
|
-
spec = AskActionSpec(
|
|
533
|
-
type="action",
|
|
534
|
-
step_id=step_dict["id"],
|
|
535
|
-
timeout=self.timeout,
|
|
536
|
-
keys=action_keys,
|
|
537
|
-
)
|
|
538
|
-
|
|
539
|
-
res = cast(
|
|
540
|
-
Union[AskActionResponse, None],
|
|
541
|
-
await context.emitter.send_ask_user(step_dict, spec, self.raise_on_timeout),
|
|
542
|
-
)
|
|
543
|
-
|
|
544
|
-
for action in self.actions:
|
|
545
|
-
await action.remove()
|
|
546
|
-
if res is None:
|
|
547
|
-
self.content = "Timed out: no action was taken"
|
|
548
|
-
else:
|
|
549
|
-
self.content = f"**Selected:** {res['label']}"
|
|
550
|
-
|
|
551
|
-
self.wait_for_answer = False
|
|
552
|
-
|
|
553
|
-
await self.update()
|
|
554
|
-
|
|
555
|
-
return res
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
class AskElementMessage(AskMessageBase):
|
|
559
|
-
"""Ask the user to submit a custom element."""
|
|
560
|
-
|
|
561
|
-
def __init__(
|
|
562
|
-
self,
|
|
563
|
-
content: str,
|
|
564
|
-
element: CustomElement,
|
|
565
|
-
author=config.ui.name,
|
|
566
|
-
timeout=90,
|
|
567
|
-
raise_on_timeout=False,
|
|
568
|
-
):
|
|
569
|
-
self.content = content
|
|
570
|
-
self.element = element
|
|
571
|
-
self.author = author
|
|
572
|
-
self.timeout = timeout
|
|
573
|
-
self.raise_on_timeout = raise_on_timeout
|
|
574
|
-
|
|
575
|
-
super().__post_init__()
|
|
576
|
-
|
|
577
|
-
async def send(self) -> Union[AskElementResponse, None]:
|
|
578
|
-
"""Send the custom element to the UI and wait for the reply."""
|
|
579
|
-
if not self.created_at:
|
|
580
|
-
self.created_at = utc_now()
|
|
581
|
-
|
|
582
|
-
if self.streaming:
|
|
583
|
-
self.streaming = False
|
|
584
|
-
|
|
585
|
-
if config.code.author_rename:
|
|
586
|
-
self.author = await config.code.author_rename(self.author)
|
|
587
|
-
|
|
588
|
-
self.wait_for_answer = True
|
|
589
|
-
|
|
590
|
-
step_dict = await self._create()
|
|
591
|
-
|
|
592
|
-
await self.element.send(for_id=str(step_dict["id"]))
|
|
593
|
-
|
|
594
|
-
spec = AskElementSpec(
|
|
595
|
-
type="element",
|
|
596
|
-
step_id=step_dict["id"],
|
|
597
|
-
timeout=self.timeout,
|
|
598
|
-
element_id=self.element.id,
|
|
599
|
-
)
|
|
600
|
-
|
|
601
|
-
res = cast(
|
|
602
|
-
Union[AskElementResponse, None],
|
|
603
|
-
await context.emitter.send_ask_user(step_dict, spec, self.raise_on_timeout),
|
|
604
|
-
)
|
|
605
|
-
|
|
606
|
-
await self.element.remove()
|
|
607
|
-
|
|
608
|
-
if res is None:
|
|
609
|
-
self.content = "Timed out"
|
|
610
|
-
elif res.get("submitted"):
|
|
611
|
-
self.content = "Thanks for submitting"
|
|
612
|
-
else:
|
|
613
|
-
self.content = "Cancelled"
|
|
614
|
-
|
|
615
|
-
self.wait_for_answer = False
|
|
616
|
-
|
|
617
|
-
await self.update()
|
|
618
|
-
|
|
619
|
-
return res
|
chainlit/mistralai/__init__.py
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import asyncio
|
|
2
|
-
from typing import Union
|
|
3
|
-
|
|
4
|
-
from literalai import ChatGeneration, CompletionGeneration
|
|
5
|
-
|
|
6
|
-
from chainlit.context import get_context
|
|
7
|
-
from chainlit.step import Step
|
|
8
|
-
from chainlit.utils import timestamp_utc
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def instrument_mistralai():
|
|
12
|
-
from literalai.instrumentation.mistralai import instrument_mistralai
|
|
13
|
-
|
|
14
|
-
def on_new_generation(
|
|
15
|
-
generation: Union["ChatGeneration", "CompletionGeneration"], timing
|
|
16
|
-
):
|
|
17
|
-
context = get_context()
|
|
18
|
-
|
|
19
|
-
parent_id = None
|
|
20
|
-
if context.current_step:
|
|
21
|
-
parent_id = context.current_step.id
|
|
22
|
-
|
|
23
|
-
step = Step(
|
|
24
|
-
name=generation.model if generation.model else generation.provider,
|
|
25
|
-
type="llm",
|
|
26
|
-
parent_id=parent_id,
|
|
27
|
-
)
|
|
28
|
-
step.generation = generation
|
|
29
|
-
# Convert start/end time from seconds to milliseconds
|
|
30
|
-
step.start = (
|
|
31
|
-
timestamp_utc(timing.get("start"))
|
|
32
|
-
if timing.get("start", None) is not None
|
|
33
|
-
else None
|
|
34
|
-
)
|
|
35
|
-
step.end = (
|
|
36
|
-
timestamp_utc(timing.get("end"))
|
|
37
|
-
if timing.get("end", None) is not None
|
|
38
|
-
else None
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
if isinstance(generation, ChatGeneration):
|
|
42
|
-
step.input = generation.messages # type: ignore
|
|
43
|
-
step.output = generation.message_completion # type: ignore
|
|
44
|
-
else:
|
|
45
|
-
step.input = generation.prompt # type: ignore
|
|
46
|
-
step.output = generation.completion # type: ignore
|
|
47
|
-
|
|
48
|
-
asyncio.create_task(step.send())
|
|
49
|
-
|
|
50
|
-
instrument_mistralai(None, on_new_generation)
|