chainlit 1.1.404__py3-none-any.whl → 1.2.0__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.

Files changed (52) hide show
  1. chainlit/__init__.py +53 -305
  2. chainlit/_utils.py +8 -0
  3. chainlit/callbacks.py +308 -0
  4. chainlit/config.py +55 -29
  5. chainlit/copilot/dist/index.js +510 -629
  6. chainlit/data/__init__.py +6 -521
  7. chainlit/data/base.py +121 -0
  8. chainlit/data/dynamodb.py +2 -5
  9. chainlit/data/literalai.py +395 -0
  10. chainlit/data/sql_alchemy.py +10 -9
  11. chainlit/data/storage_clients.py +69 -15
  12. chainlit/data/utils.py +29 -0
  13. chainlit/frontend/dist/assets/{DailyMotion-e665b444.js → DailyMotion-05f4fe48.js} +1 -1
  14. chainlit/frontend/dist/assets/{Facebook-5207db92.js → Facebook-f25411d1.js} +1 -1
  15. chainlit/frontend/dist/assets/{FilePlayer-86937d6e.js → FilePlayer-40ff3414.js} +1 -1
  16. chainlit/frontend/dist/assets/{Kaltura-c96622c1.js → Kaltura-6cbf3897.js} +1 -1
  17. chainlit/frontend/dist/assets/{Mixcloud-57ae3e32.js → Mixcloud-34e7c912.js} +1 -1
  18. chainlit/frontend/dist/assets/{Mux-20373920.js → Mux-8aaff6ac.js} +1 -1
  19. chainlit/frontend/dist/assets/{Preview-c68c0613.js → Preview-2d3bf558.js} +1 -1
  20. chainlit/frontend/dist/assets/{SoundCloud-8a9e3eae.js → SoundCloud-b835f90f.js} +1 -1
  21. chainlit/frontend/dist/assets/{Streamable-1ed099af.js → Streamable-1293e4f3.js} +1 -1
  22. chainlit/frontend/dist/assets/{Twitch-6820039f.js → Twitch-c69660cd.js} +1 -1
  23. chainlit/frontend/dist/assets/{Vidyard-d39ab91d.js → Vidyard-43bda599.js} +1 -1
  24. chainlit/frontend/dist/assets/{Vimeo-017cd9a7.js → Vimeo-54150039.js} +1 -1
  25. chainlit/frontend/dist/assets/{Wistia-a509d9f2.js → Wistia-aa3c721b.js} +1 -1
  26. chainlit/frontend/dist/assets/{YouTube-42dfd82f.js → YouTube-dd0f3cc2.js} +1 -1
  27. chainlit/frontend/dist/assets/index-cf48bedd.js +729 -0
  28. chainlit/frontend/dist/assets/react-plotly-f52a41eb.js +3484 -0
  29. chainlit/frontend/dist/index.html +1 -1
  30. chainlit/langchain/callbacks.py +6 -1
  31. chainlit/llama_index/callbacks.py +20 -4
  32. chainlit/markdown.py +15 -9
  33. chainlit/message.py +0 -1
  34. chainlit/server.py +90 -36
  35. chainlit/session.py +4 -1
  36. chainlit/translations/bn.json +231 -0
  37. chainlit/translations/gu.json +231 -0
  38. chainlit/translations/he-IL.json +231 -0
  39. chainlit/translations/hi.json +231 -0
  40. chainlit/translations/kn.json +231 -0
  41. chainlit/translations/ml.json +231 -0
  42. chainlit/translations/mr.json +231 -0
  43. chainlit/translations/ta.json +231 -0
  44. chainlit/translations/te.json +231 -0
  45. chainlit/utils.py +1 -1
  46. {chainlit-1.1.404.dist-info → chainlit-1.2.0.dist-info}/METADATA +3 -3
  47. chainlit-1.2.0.dist-info/RECORD +96 -0
  48. chainlit/frontend/dist/assets/index-30df9b2b.js +0 -730
  49. chainlit/frontend/dist/assets/react-plotly-5bb34118.js +0 -3602
  50. chainlit-1.1.404.dist-info/RECORD +0 -82
  51. {chainlit-1.1.404.dist-info → chainlit-1.2.0.dist-info}/WHEEL +0 -0
  52. {chainlit-1.1.404.dist-info → chainlit-1.2.0.dist-info}/entry_points.txt +0 -0
chainlit/__init__.py CHANGED
@@ -1,33 +1,24 @@
1
- import inspect
2
1
  import os
3
2
 
4
3
  from dotenv import load_dotenv
5
4
 
5
+ # ruff: noqa: E402
6
+ # Keep this here to ensure imports have environment available.
6
7
  env_found = load_dotenv(dotenv_path=os.path.join(os.getcwd(), ".env"))
7
8
 
8
- import asyncio
9
- from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
9
+ from chainlit.logger import logger
10
10
 
11
- from fastapi import Request, Response
12
- from pydantic.dataclasses import dataclass
13
- from starlette.datastructures import Headers
11
+ if env_found:
12
+ logger.info("Loaded .env file")
14
13
 
15
- if TYPE_CHECKING:
16
- from chainlit.haystack.callbacks import HaystackAgentCallbackHandler
17
- from chainlit.langchain.callbacks import (
18
- LangchainCallbackHandler,
19
- AsyncLangchainCallbackHandler,
20
- )
21
- from chainlit.llama_index.callbacks import LlamaIndexCallbackHandler
22
- from chainlit.openai import instrument_openai
23
- from chainlit.mistralai import instrument_mistralai
14
+ import asyncio
15
+ from typing import TYPE_CHECKING, Any, Dict
24
16
 
25
17
  import chainlit.input_widget as input_widget
26
18
  from chainlit.action import Action
27
19
  from chainlit.cache import cache
28
20
  from chainlit.chat_context import chat_context
29
21
  from chainlit.chat_settings import ChatSettings
30
- from chainlit.config import config
31
22
  from chainlit.context import context
32
23
  from chainlit.element import (
33
24
  Audio,
@@ -43,7 +34,6 @@ from chainlit.element import (
43
34
  Text,
44
35
  Video,
45
36
  )
46
- from chainlit.logger import logger
47
37
  from chainlit.message import (
48
38
  AskActionMessage,
49
39
  AskFileMessage,
@@ -51,303 +41,44 @@ from chainlit.message import (
51
41
  ErrorMessage,
52
42
  Message,
53
43
  )
54
- from chainlit.oauth_providers import get_configured_oauth_providers
55
44
  from chainlit.step import Step, step
56
45
  from chainlit.sync import make_async, run_sync
57
- from chainlit.telemetry import trace
58
- from chainlit.types import AudioChunk, ChatProfile, Starter, ThreadDict
46
+ from chainlit.types import AudioChunk, ChatProfile, Starter
59
47
  from chainlit.user import PersistedUser, User
60
48
  from chainlit.user_session import user_session
61
- from chainlit.utils import make_module_getattr, wrap_user_function
49
+ from chainlit.utils import make_module_getattr
62
50
  from chainlit.version import __version__
63
51
  from literalai import ChatGeneration, CompletionGeneration, GenerationMessage
52
+ from pydantic.dataclasses import dataclass
64
53
 
65
- if env_found:
66
- logger.info("Loaded .env file")
67
-
68
-
69
- @trace
70
- def password_auth_callback(func: Callable[[str, str], Optional[User]]) -> Callable:
71
- """
72
- Framework agnostic decorator to authenticate the user.
73
-
74
- Args:
75
- func (Callable[[str, str], Optional[User]]): The authentication callback to execute. Takes the email and password as parameters.
76
-
77
- Example:
78
- @cl.password_auth_callback
79
- async def password_auth_callback(username: str, password: str) -> Optional[User]:
80
-
81
- Returns:
82
- Callable[[str, str], Optional[User]]: The decorated authentication callback.
83
- """
84
-
85
- config.code.password_auth_callback = wrap_user_function(func)
86
- return func
87
-
88
-
89
- @trace
90
- def header_auth_callback(func: Callable[[Headers], Optional[User]]) -> Callable:
91
- """
92
- Framework agnostic decorator to authenticate the user via a header
93
-
94
- Args:
95
- func (Callable[[Headers], Optional[User]]): The authentication callback to execute.
96
-
97
- Example:
98
- @cl.header_auth_callback
99
- async def header_auth_callback(headers: Headers) -> Optional[User]:
100
-
101
- Returns:
102
- Callable[[Headers], Optional[User]]: The decorated authentication callback.
103
- """
104
-
105
- config.code.header_auth_callback = wrap_user_function(func)
106
- return func
107
-
108
-
109
- @trace
110
- def oauth_callback(
111
- func: Callable[[str, str, Dict[str, str], User], Optional[User]]
112
- ) -> Callable:
113
- """
114
- Framework agnostic decorator to authenticate the user via oauth
115
-
116
- Args:
117
- func (Callable[[str, str, Dict[str, str], User], Optional[User]]): The authentication callback to execute.
118
-
119
- Example:
120
- @cl.oauth_callback
121
- async def oauth_callback(provider_id: str, token: str, raw_user_data: Dict[str, str], default_app_user: User, id_token: Optional[str]) -> Optional[User]:
122
-
123
- Returns:
124
- Callable[[str, str, Dict[str, str], User], Optional[User]]: The decorated authentication callback.
125
- """
126
-
127
- if len(get_configured_oauth_providers()) == 0:
128
- raise ValueError(
129
- "You must set the environment variable for at least one oauth provider to use oauth authentication."
130
- )
131
-
132
- config.code.oauth_callback = wrap_user_function(func)
133
- return func
134
-
135
-
136
- @trace
137
- def on_logout(func: Callable[[Request, Response], Any]) -> Callable:
138
- """
139
- Function called when the user logs out.
140
- Takes the FastAPI request and response as parameters.
141
- """
142
-
143
- config.code.on_logout = wrap_user_function(func)
144
- return func
145
-
146
-
147
- @trace
148
- def on_message(func: Callable) -> Callable:
149
- """
150
- Framework agnostic decorator to react to messages coming from the UI.
151
- The decorated function is called every time a new message is received.
152
-
153
- Args:
154
- func (Callable[[Message], Any]): The function to be called when a new message is received. Takes a cl.Message.
155
-
156
- Returns:
157
- Callable[[str], Any]: The decorated on_message function.
158
- """
159
-
160
- async def with_parent_id(message: Message):
161
- async with Step(name="on_message", type="run", parent_id=message.id) as s:
162
- s.input = message.content
163
- if len(inspect.signature(func).parameters) > 0:
164
- await func(message)
165
- else:
166
- await func()
167
-
168
- config.code.on_message = wrap_user_function(with_parent_id)
169
- return func
170
-
171
-
172
- @trace
173
- def on_chat_start(func: Callable) -> Callable:
174
- """
175
- Hook to react to the user websocket connection event.
176
-
177
- Args:
178
- func (Callable[], Any]): The connection hook to execute.
179
-
180
- Returns:
181
- Callable[], Any]: The decorated hook.
182
- """
183
-
184
- config.code.on_chat_start = wrap_user_function(
185
- step(func, name="on_chat_start", type="run"), with_task=True
186
- )
187
- return func
188
-
189
-
190
- @trace
191
- def on_chat_resume(func: Callable[[ThreadDict], Any]) -> Callable:
192
- """
193
- Hook to react to resume websocket connection event.
194
-
195
- Args:
196
- func (Callable[], Any]): The connection hook to execute.
197
-
198
- Returns:
199
- Callable[], Any]: The decorated hook.
200
- """
201
-
202
- config.code.on_chat_resume = wrap_user_function(func, with_task=True)
203
- return func
204
-
205
-
206
- @trace
207
- def set_chat_profiles(
208
- func: Callable[[Optional["User"]], List["ChatProfile"]]
209
- ) -> Callable:
210
- """
211
- Programmatic declaration of the available chat profiles (can depend on the User from the session if authentication is setup).
212
-
213
- Args:
214
- func (Callable[[Optional["User"]], List["ChatProfile"]]): The function declaring the chat profiles.
215
-
216
- Returns:
217
- Callable[[Optional["User"]], List["ChatProfile"]]: The decorated function.
218
- """
219
-
220
- config.code.set_chat_profiles = wrap_user_function(func)
221
- return func
222
-
223
-
224
- @trace
225
- def set_starters(func: Callable[[Optional["User"]], List["Starter"]]) -> Callable:
226
- """
227
- Programmatic declaration of the available starter (can depend on the User from the session if authentication is setup).
228
-
229
- Args:
230
- func (Callable[[Optional["User"]], List["Starter"]]): The function declaring the starters.
231
-
232
- Returns:
233
- Callable[[Optional["User"]], List["Starter"]]: The decorated function.
234
- """
235
-
236
- config.code.set_starters = wrap_user_function(func)
237
- return func
238
-
239
-
240
- @trace
241
- def on_chat_end(func: Callable) -> Callable:
242
- """
243
- Hook to react to the user websocket disconnect event.
244
-
245
- Args:
246
- func (Callable[], Any]): The disconnect hook to execute.
247
-
248
- Returns:
249
- Callable[], Any]: The decorated hook.
250
- """
251
-
252
- config.code.on_chat_end = wrap_user_function(func, with_task=True)
253
- return func
254
-
255
-
256
- @trace
257
- def on_audio_chunk(func: Callable) -> Callable:
258
- """
259
- Hook to react to the audio chunks being sent.
260
-
261
- Args:
262
- chunk (AudioChunk): The audio chunk being sent.
263
-
264
- Returns:
265
- Callable[], Any]: The decorated hook.
266
- """
267
-
268
- config.code.on_audio_chunk = wrap_user_function(func, with_task=False)
269
- return func
270
-
271
-
272
- @trace
273
- def on_audio_end(func: Callable) -> Callable:
274
- """
275
- Hook to react to the audio stream ending. This is called after the last audio chunk is sent.
276
-
277
- Args:
278
- elements ([List[Element]): The files that were uploaded before starting the audio stream (if any).
279
-
280
- Returns:
281
- Callable[], Any]: The decorated hook.
282
- """
54
+ from .callbacks import (
55
+ action_callback,
56
+ author_rename,
57
+ header_auth_callback,
58
+ oauth_callback,
59
+ on_audio_chunk,
60
+ on_audio_end,
61
+ on_chat_end,
62
+ on_chat_resume,
63
+ on_chat_start,
64
+ on_logout,
65
+ on_message,
66
+ on_settings_update,
67
+ on_stop,
68
+ password_auth_callback,
69
+ set_chat_profiles,
70
+ set_starters,
71
+ )
283
72
 
284
- config.code.on_audio_end = wrap_user_function(
285
- step(func, name="on_audio_end", type="run"), with_task=True
73
+ if TYPE_CHECKING:
74
+ from chainlit.haystack.callbacks import HaystackAgentCallbackHandler
75
+ from chainlit.langchain.callbacks import (
76
+ AsyncLangchainCallbackHandler,
77
+ LangchainCallbackHandler,
286
78
  )
287
- return func
288
-
289
-
290
- @trace
291
- def author_rename(func: Callable[[str], str]) -> Callable[[str], str]:
292
- """
293
- Useful to rename the author of message to display more friendly author names in the UI.
294
- Args:
295
- func (Callable[[str], str]): The function to be called to rename an author. Takes the original author name as parameter.
296
-
297
- Returns:
298
- Callable[[Any, str], Any]: The decorated function.
299
- """
300
-
301
- config.code.author_rename = wrap_user_function(func)
302
- return func
303
-
304
-
305
- @trace
306
- def on_stop(func: Callable) -> Callable:
307
- """
308
- Hook to react to the user stopping a thread.
309
-
310
- Args:
311
- func (Callable[[], Any]): The stop hook to execute.
312
-
313
- Returns:
314
- Callable[[], Any]: The decorated stop hook.
315
- """
316
-
317
- config.code.on_stop = wrap_user_function(func)
318
- return func
319
-
320
-
321
- def action_callback(name: str) -> Callable:
322
- """
323
- Callback to call when an action is clicked in the UI.
324
-
325
- Args:
326
- func (Callable[[Action], Any]): The action callback to execute. First parameter is the action.
327
- """
328
-
329
- def decorator(func: Callable[[Action], Any]):
330
- config.code.action_callbacks[name] = wrap_user_function(func, with_task=True)
331
- return func
332
-
333
- return decorator
334
-
335
-
336
- def on_settings_update(
337
- func: Callable[[Dict[str, Any]], Any]
338
- ) -> Callable[[Dict[str, Any]], Any]:
339
- """
340
- Hook to react to the user changing any settings.
341
-
342
- Args:
343
- func (Callable[], Any]): The hook to execute after settings were changed.
344
-
345
- Returns:
346
- Callable[], Any]: The decorated hook.
347
- """
348
-
349
- config.code.on_settings_update = wrap_user_function(func, with_task=True)
350
- return func
79
+ from chainlit.llama_index.callbacks import LlamaIndexCallbackHandler
80
+ from chainlit.mistralai import instrument_mistralai
81
+ from chainlit.openai import instrument_openai
351
82
 
352
83
 
353
84
  def sleep(duration: int):
@@ -380,6 +111,7 @@ __getattr__ = make_module_getattr(
380
111
  )
381
112
 
382
113
  __all__ = [
114
+ "__version__",
383
115
  "ChatProfile",
384
116
  "Starter",
385
117
  "user_session",
@@ -434,6 +166,22 @@ __all__ = [
434
166
  "HaystackAgentCallbackHandler",
435
167
  "instrument_openai",
436
168
  "instrument_mistralai",
169
+ "password_auth_callback",
170
+ "header_auth_callback",
171
+ "oauth_callback",
172
+ "on_logout",
173
+ "on_message",
174
+ "on_chat_start",
175
+ "on_chat_resume",
176
+ "set_chat_profiles",
177
+ "set_starters",
178
+ "on_chat_end",
179
+ "on_audio_chunk",
180
+ "on_audio_end",
181
+ "author_rename",
182
+ "on_stop",
183
+ "action_callback",
184
+ "on_settings_update",
437
185
  ]
438
186
 
439
187
 
chainlit/_utils.py ADDED
@@ -0,0 +1,8 @@
1
+ """Util functions which are explicitly not part of the public API."""
2
+
3
+ from pathlib import Path
4
+
5
+
6
+ def is_path_inside(child_path: Path, parent_path: Path) -> bool:
7
+ """Check if the child path is inside the parent path."""
8
+ return parent_path.resolve() in child_path.resolve().parents