chainlit 0.7.700__py3-none-any.whl → 1.0.0rc1__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/__init__.py +32 -23
- chainlit/auth.py +9 -10
- chainlit/cli/__init__.py +1 -2
- chainlit/config.py +13 -12
- chainlit/context.py +7 -3
- chainlit/data/__init__.py +375 -9
- chainlit/data/acl.py +6 -5
- chainlit/element.py +86 -123
- chainlit/emitter.py +117 -50
- chainlit/frontend/dist/assets/{index-71698725.js → index-6aee009a.js} +118 -292
- chainlit/frontend/dist/assets/{react-plotly-2c0acdf0.js → react-plotly-2f07c02a.js} +1 -1
- chainlit/frontend/dist/index.html +1 -1
- chainlit/haystack/callbacks.py +45 -43
- chainlit/hello.py +1 -1
- chainlit/langchain/callbacks.py +132 -120
- chainlit/llama_index/callbacks.py +68 -48
- chainlit/message.py +179 -207
- chainlit/oauth_providers.py +39 -34
- chainlit/playground/provider.py +44 -30
- chainlit/playground/providers/anthropic.py +4 -4
- chainlit/playground/providers/huggingface.py +2 -2
- chainlit/playground/providers/langchain.py +8 -10
- chainlit/playground/providers/openai.py +19 -13
- chainlit/server.py +155 -99
- chainlit/session.py +109 -40
- chainlit/socket.py +47 -36
- chainlit/step.py +393 -0
- chainlit/types.py +78 -21
- chainlit/user.py +32 -0
- chainlit/user_session.py +1 -5
- {chainlit-0.7.700.dist-info → chainlit-1.0.0rc1.dist-info}/METADATA +12 -31
- chainlit-1.0.0rc1.dist-info/RECORD +60 -0
- chainlit/client/base.py +0 -169
- chainlit/client/cloud.py +0 -502
- chainlit/prompt.py +0 -40
- chainlit-0.7.700.dist-info/RECORD +0 -61
- {chainlit-0.7.700.dist-info → chainlit-1.0.0rc1.dist-info}/WHEEL +0 -0
- {chainlit-0.7.700.dist-info → chainlit-1.0.0rc1.dist-info}/entry_points.txt +0 -0
chainlit/__init__.py
CHANGED
|
@@ -5,7 +5,7 @@ from dotenv import load_dotenv
|
|
|
5
5
|
env_found = load_dotenv(dotenv_path=os.path.join(os.getcwd(), ".env"))
|
|
6
6
|
|
|
7
7
|
import asyncio
|
|
8
|
-
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
|
|
8
|
+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
|
|
9
9
|
|
|
10
10
|
from starlette.datastructures import Headers
|
|
11
11
|
|
|
@@ -21,8 +21,8 @@ import chainlit.input_widget as input_widget
|
|
|
21
21
|
from chainlit.action import Action
|
|
22
22
|
from chainlit.cache import cache
|
|
23
23
|
from chainlit.chat_settings import ChatSettings
|
|
24
|
-
from chainlit.client.base import AppUser, ConversationDict, PersistedAppUser
|
|
25
24
|
from chainlit.config import config
|
|
25
|
+
from chainlit.context import context
|
|
26
26
|
from chainlit.element import (
|
|
27
27
|
Audio,
|
|
28
28
|
Avatar,
|
|
@@ -46,31 +46,34 @@ from chainlit.message import (
|
|
|
46
46
|
Message,
|
|
47
47
|
)
|
|
48
48
|
from chainlit.oauth_providers import get_configured_oauth_providers
|
|
49
|
+
from chainlit.step import Step, step
|
|
49
50
|
from chainlit.sync import make_async, run_sync
|
|
50
51
|
from chainlit.telemetry import trace
|
|
51
|
-
from chainlit.types import ChatProfile,
|
|
52
|
+
from chainlit.types import ChatProfile, ThreadDict
|
|
53
|
+
from chainlit.user import PersistedUser, User
|
|
52
54
|
from chainlit.user_session import user_session
|
|
53
55
|
from chainlit.utils import make_module_getattr, wrap_user_function
|
|
54
56
|
from chainlit.version import __version__
|
|
57
|
+
from chainlit_client import ChatGeneration, CompletionGeneration, GenerationMessage
|
|
55
58
|
|
|
56
59
|
if env_found:
|
|
57
60
|
logger.info("Loaded .env file")
|
|
58
61
|
|
|
59
62
|
|
|
60
63
|
@trace
|
|
61
|
-
def password_auth_callback(func: Callable[[str, str], Optional[
|
|
64
|
+
def password_auth_callback(func: Callable[[str, str], Optional[User]]) -> Callable:
|
|
62
65
|
"""
|
|
63
66
|
Framework agnostic decorator to authenticate the user.
|
|
64
67
|
|
|
65
68
|
Args:
|
|
66
|
-
func (Callable[[str, str], Optional[
|
|
69
|
+
func (Callable[[str, str], Optional[User]]): The authentication callback to execute. Takes the email and password as parameters.
|
|
67
70
|
|
|
68
71
|
Example:
|
|
69
72
|
@cl.password_auth_callback
|
|
70
|
-
async def password_auth_callback(username: str, password: str) -> Optional[
|
|
73
|
+
async def password_auth_callback(username: str, password: str) -> Optional[User]:
|
|
71
74
|
|
|
72
75
|
Returns:
|
|
73
|
-
Callable[[str, str], Optional[
|
|
76
|
+
Callable[[str, str], Optional[User]]: The decorated authentication callback.
|
|
74
77
|
"""
|
|
75
78
|
|
|
76
79
|
config.code.password_auth_callback = wrap_user_function(func)
|
|
@@ -78,19 +81,19 @@ def password_auth_callback(func: Callable[[str, str], Optional[AppUser]]) -> Cal
|
|
|
78
81
|
|
|
79
82
|
|
|
80
83
|
@trace
|
|
81
|
-
def header_auth_callback(func: Callable[[Headers], Optional[
|
|
84
|
+
def header_auth_callback(func: Callable[[Headers], Optional[User]]) -> Callable:
|
|
82
85
|
"""
|
|
83
86
|
Framework agnostic decorator to authenticate the user via a header
|
|
84
87
|
|
|
85
88
|
Args:
|
|
86
|
-
func (Callable[[Headers], Optional[
|
|
89
|
+
func (Callable[[Headers], Optional[User]]): The authentication callback to execute.
|
|
87
90
|
|
|
88
91
|
Example:
|
|
89
92
|
@cl.header_auth_callback
|
|
90
|
-
async def header_auth_callback(headers: Headers) -> Optional[
|
|
93
|
+
async def header_auth_callback(headers: Headers) -> Optional[User]:
|
|
91
94
|
|
|
92
95
|
Returns:
|
|
93
|
-
Callable[[Headers], Optional[
|
|
96
|
+
Callable[[Headers], Optional[User]]: The decorated authentication callback.
|
|
94
97
|
"""
|
|
95
98
|
|
|
96
99
|
config.code.header_auth_callback = wrap_user_function(func)
|
|
@@ -99,20 +102,20 @@ def header_auth_callback(func: Callable[[Headers], Optional[AppUser]]) -> Callab
|
|
|
99
102
|
|
|
100
103
|
@trace
|
|
101
104
|
def oauth_callback(
|
|
102
|
-
func: Callable[[str, str, Dict[str, str],
|
|
105
|
+
func: Callable[[str, str, Dict[str, str], User], Optional[User]]
|
|
103
106
|
) -> Callable:
|
|
104
107
|
"""
|
|
105
108
|
Framework agnostic decorator to authenticate the user via oauth
|
|
106
109
|
|
|
107
110
|
Args:
|
|
108
|
-
func (Callable[[str, str, Dict[str, str],
|
|
111
|
+
func (Callable[[str, str, Dict[str, str], User], Optional[User]]): The authentication callback to execute.
|
|
109
112
|
|
|
110
113
|
Example:
|
|
111
114
|
@cl.oauth_callback
|
|
112
|
-
async def oauth_callback(provider_id: str, token: str, raw_user_data: Dict[str, str], default_app_user:
|
|
115
|
+
async def oauth_callback(provider_id: str, token: str, raw_user_data: Dict[str, str], default_app_user: User) -> Optional[User]:
|
|
113
116
|
|
|
114
117
|
Returns:
|
|
115
|
-
Callable[[str, str, Dict[str, str],
|
|
118
|
+
Callable[[str, str, Dict[str, str], User], Optional[User]]: The decorated authentication callback.
|
|
116
119
|
"""
|
|
117
120
|
|
|
118
121
|
if len(get_configured_oauth_providers()) == 0:
|
|
@@ -158,7 +161,7 @@ def on_chat_start(func: Callable) -> Callable:
|
|
|
158
161
|
|
|
159
162
|
|
|
160
163
|
@trace
|
|
161
|
-
def on_chat_resume(func: Callable[[
|
|
164
|
+
def on_chat_resume(func: Callable[[ThreadDict], Any]) -> Callable:
|
|
162
165
|
"""
|
|
163
166
|
Hook to react to resume websocket connection event.
|
|
164
167
|
|
|
@@ -175,16 +178,16 @@ def on_chat_resume(func: Callable[[ConversationDict], Any]) -> Callable:
|
|
|
175
178
|
|
|
176
179
|
@trace
|
|
177
180
|
def set_chat_profiles(
|
|
178
|
-
func: Callable[[Optional["
|
|
181
|
+
func: Callable[[Optional["User"]], List["ChatProfile"]]
|
|
179
182
|
) -> Callable:
|
|
180
183
|
"""
|
|
181
|
-
Programmatic declaration of the available chat profiles (can depend on the
|
|
184
|
+
Programmatic declaration of the available chat profiles (can depend on the User from the session if authentication is setup).
|
|
182
185
|
|
|
183
186
|
Args:
|
|
184
|
-
func (Callable[[Optional["
|
|
187
|
+
func (Callable[[Optional["User"]], List["ChatProfile"]]): The function declaring the chat profiles.
|
|
185
188
|
|
|
186
189
|
Returns:
|
|
187
|
-
Callable[[Optional["
|
|
190
|
+
Callable[[Optional["User"]], List["ChatProfile"]]: The decorated function.
|
|
188
191
|
"""
|
|
189
192
|
|
|
190
193
|
config.code.set_chat_profiles = wrap_user_function(func)
|
|
@@ -225,7 +228,7 @@ def author_rename(func: Callable[[str], str]) -> Callable[[str], str]:
|
|
|
225
228
|
@trace
|
|
226
229
|
def on_stop(func: Callable) -> Callable:
|
|
227
230
|
"""
|
|
228
|
-
Hook to react to the user stopping a
|
|
231
|
+
Hook to react to the user stopping a thread.
|
|
229
232
|
|
|
230
233
|
Args:
|
|
231
234
|
func (Callable[[], Any]): The stop hook to execute.
|
|
@@ -291,8 +294,8 @@ __getattr__ = make_module_getattr(
|
|
|
291
294
|
__all__ = [
|
|
292
295
|
"user_session",
|
|
293
296
|
"Action",
|
|
294
|
-
"
|
|
295
|
-
"
|
|
297
|
+
"User",
|
|
298
|
+
"PersistedUser",
|
|
296
299
|
"Audio",
|
|
297
300
|
"Pdf",
|
|
298
301
|
"Plotly",
|
|
@@ -312,6 +315,11 @@ __all__ = [
|
|
|
312
315
|
"AskUserMessage",
|
|
313
316
|
"AskActionMessage",
|
|
314
317
|
"AskFileMessage",
|
|
318
|
+
"Step",
|
|
319
|
+
"step",
|
|
320
|
+
"ChatGeneration",
|
|
321
|
+
"CompletionGeneration",
|
|
322
|
+
"GenerationMessage",
|
|
315
323
|
"on_chat_start",
|
|
316
324
|
"on_chat_end",
|
|
317
325
|
"on_chat_resume",
|
|
@@ -325,6 +333,7 @@ __all__ = [
|
|
|
325
333
|
"run_sync",
|
|
326
334
|
"make_async",
|
|
327
335
|
"cache",
|
|
336
|
+
"context",
|
|
328
337
|
"LangchainCallbackHandler",
|
|
329
338
|
"AsyncLangchainCallbackHandler",
|
|
330
339
|
"LlamaIndexCallbackHandler",
|
chainlit/auth.py
CHANGED
|
@@ -3,10 +3,10 @@ from datetime import datetime, timedelta
|
|
|
3
3
|
from typing import Any, Dict
|
|
4
4
|
|
|
5
5
|
import jwt
|
|
6
|
-
from chainlit.client.cloud import AppUser
|
|
7
6
|
from chainlit.config import config
|
|
8
|
-
from chainlit.data import
|
|
7
|
+
from chainlit.data import get_data_layer
|
|
9
8
|
from chainlit.oauth_providers import get_configured_oauth_providers
|
|
9
|
+
from chainlit.user import User
|
|
10
10
|
from fastapi import Depends, HTTPException
|
|
11
11
|
from fastapi.security import OAuth2PasswordBearer
|
|
12
12
|
|
|
@@ -47,7 +47,7 @@ def get_configuration():
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
|
|
50
|
-
def create_jwt(data:
|
|
50
|
+
def create_jwt(data: User) -> str:
|
|
51
51
|
to_encode = data.to_dict() # type: Dict[str, Any]
|
|
52
52
|
to_encode.update(
|
|
53
53
|
{
|
|
@@ -67,21 +67,20 @@ async def authenticate_user(token: str = Depends(reuseable_oauth)):
|
|
|
67
67
|
options={"verify_signature": True},
|
|
68
68
|
)
|
|
69
69
|
del dict["exp"]
|
|
70
|
-
|
|
70
|
+
user = User(**dict)
|
|
71
71
|
except Exception as e:
|
|
72
72
|
raise HTTPException(status_code=401, detail="Invalid authentication token")
|
|
73
|
-
|
|
74
|
-
if chainlit_client:
|
|
73
|
+
if data_layer := get_data_layer():
|
|
75
74
|
try:
|
|
76
|
-
|
|
75
|
+
persisted_user = await data_layer.get_user(user.identifier)
|
|
77
76
|
except Exception as e:
|
|
78
77
|
raise HTTPException(status_code=500, detail=str(e))
|
|
79
|
-
if
|
|
78
|
+
if persisted_user == None:
|
|
80
79
|
raise HTTPException(status_code=401, detail="User does not exist")
|
|
81
80
|
|
|
82
|
-
return
|
|
81
|
+
return persisted_user
|
|
83
82
|
else:
|
|
84
|
-
return
|
|
83
|
+
return user
|
|
85
84
|
|
|
86
85
|
|
|
87
86
|
async def get_current_user(token: str = Depends(reuseable_oauth)):
|
chainlit/cli/__init__.py
CHANGED
|
@@ -21,7 +21,7 @@ from chainlit.config import (
|
|
|
21
21
|
from chainlit.logger import logger
|
|
22
22
|
from chainlit.markdown import init_markdown
|
|
23
23
|
from chainlit.secret import random_secret
|
|
24
|
-
from chainlit.server import app,
|
|
24
|
+
from chainlit.server import app, register_wildcard_route_handler
|
|
25
25
|
from chainlit.telemetry import trace_event
|
|
26
26
|
|
|
27
27
|
|
|
@@ -73,7 +73,6 @@ def run_chainlit(target: str):
|
|
|
73
73
|
host=host,
|
|
74
74
|
port=port,
|
|
75
75
|
log_level=log_level,
|
|
76
|
-
ws_max_size=max_message_size,
|
|
77
76
|
ws_per_message_deflate=ws_per_message_deflate,
|
|
78
77
|
)
|
|
79
78
|
server = uvicorn.Server(config)
|
chainlit/config.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import os
|
|
2
2
|
import sys
|
|
3
3
|
from importlib import util
|
|
4
|
+
from pathlib import Path
|
|
4
5
|
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
|
|
5
6
|
|
|
6
7
|
import tomli
|
|
@@ -12,8 +13,8 @@ from starlette.datastructures import Headers
|
|
|
12
13
|
|
|
13
14
|
if TYPE_CHECKING:
|
|
14
15
|
from chainlit.action import Action
|
|
15
|
-
from chainlit.
|
|
16
|
-
from chainlit.
|
|
16
|
+
from chainlit.types import ChatProfile, ThreadDict
|
|
17
|
+
from chainlit.user import User
|
|
17
18
|
|
|
18
19
|
|
|
19
20
|
BACKEND_ROOT = os.path.dirname(__file__)
|
|
@@ -23,6 +24,10 @@ PACKAGE_ROOT = os.path.dirname(os.path.dirname(BACKEND_ROOT))
|
|
|
23
24
|
# Get the directory the script is running from
|
|
24
25
|
APP_ROOT = os.getcwd()
|
|
25
26
|
|
|
27
|
+
# Create the directory to store the uploaded files
|
|
28
|
+
FILES_DIRECTORY = Path(APP_ROOT) / ".files"
|
|
29
|
+
FILES_DIRECTORY.mkdir(exist_ok=True)
|
|
30
|
+
|
|
26
31
|
config_dir = os.path.join(APP_ROOT, ".chainlit")
|
|
27
32
|
config_file = os.path.join(config_dir, "config.toml")
|
|
28
33
|
|
|
@@ -66,7 +71,7 @@ multi_modal = true
|
|
|
66
71
|
# Name of the app and chatbot.
|
|
67
72
|
name = "Chatbot"
|
|
68
73
|
|
|
69
|
-
# Show the readme while the
|
|
74
|
+
# Show the readme while the thread is empty.
|
|
70
75
|
show_readme_as_default = true
|
|
71
76
|
|
|
72
77
|
# Description of the app and chatbot. This is used for HTML tags.
|
|
@@ -190,20 +195,20 @@ class CodeSettings:
|
|
|
190
195
|
# Module object loaded from the module_name
|
|
191
196
|
module: Any = None
|
|
192
197
|
# Bunch of callbacks defined by the developer
|
|
193
|
-
password_auth_callback: Optional[Callable[[str, str], Optional["
|
|
194
|
-
header_auth_callback: Optional[Callable[[Headers], Optional["
|
|
198
|
+
password_auth_callback: Optional[Callable[[str, str], Optional["User"]]] = None
|
|
199
|
+
header_auth_callback: Optional[Callable[[Headers], Optional["User"]]] = None
|
|
195
200
|
oauth_callback: Optional[
|
|
196
|
-
Callable[[str, str, Dict[str, str], "
|
|
201
|
+
Callable[[str, str, Dict[str, str], "User"], Optional["User"]]
|
|
197
202
|
] = None
|
|
198
203
|
on_stop: Optional[Callable[[], Any]] = None
|
|
199
204
|
on_chat_start: Optional[Callable[[], Any]] = None
|
|
200
205
|
on_chat_end: Optional[Callable[[], Any]] = None
|
|
201
|
-
on_chat_resume: Optional[Callable[["
|
|
206
|
+
on_chat_resume: Optional[Callable[["ThreadDict"], Any]] = None
|
|
202
207
|
on_message: Optional[Callable[[str], Any]] = None
|
|
203
208
|
author_rename: Optional[Callable[[str], str]] = None
|
|
204
209
|
on_settings_update: Optional[Callable[[Dict[str, Any]], Any]] = None
|
|
205
210
|
set_chat_profiles: Optional[
|
|
206
|
-
Callable[[Optional["
|
|
211
|
+
Callable[[Optional["User"]], List["ChatProfile"]]
|
|
207
212
|
] = None
|
|
208
213
|
|
|
209
214
|
|
|
@@ -229,8 +234,6 @@ class ChainlitConfig:
|
|
|
229
234
|
root = APP_ROOT
|
|
230
235
|
# Chainlit server URL. Used only for cloud features
|
|
231
236
|
chainlit_server: str
|
|
232
|
-
# Whether or not a chainlit api key has been provided
|
|
233
|
-
data_persistence: bool
|
|
234
237
|
# The url of the deployed app. Only set if the app is deployed.
|
|
235
238
|
chainlit_prod_url = chainlit_prod_url
|
|
236
239
|
|
|
@@ -341,11 +344,9 @@ def load_config():
|
|
|
341
344
|
settings = load_settings()
|
|
342
345
|
|
|
343
346
|
chainlit_server = os.environ.get("CHAINLIT_SERVER", "https://cloud.chainlit.io")
|
|
344
|
-
data_persistence = "CHAINLIT_API_KEY" in os.environ
|
|
345
347
|
|
|
346
348
|
config = ChainlitConfig(
|
|
347
349
|
chainlit_server=chainlit_server,
|
|
348
|
-
data_persistence=data_persistence,
|
|
349
350
|
chainlit_prod_url=chainlit_prod_url,
|
|
350
351
|
run=RunSettings(),
|
|
351
352
|
**settings,
|
chainlit/context.py
CHANGED
|
@@ -7,9 +7,8 @@ from chainlit.session import HTTPSession, WebsocketSession
|
|
|
7
7
|
from lazify import LazyProxy
|
|
8
8
|
|
|
9
9
|
if TYPE_CHECKING:
|
|
10
|
-
from chainlit.client.cloud import AppUser, PersistedAppUser
|
|
11
10
|
from chainlit.emitter import BaseChainlitEmitter
|
|
12
|
-
from chainlit.
|
|
11
|
+
from chainlit.user import PersistedUser, User
|
|
13
12
|
|
|
14
13
|
|
|
15
14
|
class ChainlitContextException(Exception):
|
|
@@ -22,6 +21,11 @@ class ChainlitContext:
|
|
|
22
21
|
emitter: "BaseChainlitEmitter"
|
|
23
22
|
session: Union["HTTPSession", "WebsocketSession"]
|
|
24
23
|
|
|
24
|
+
@property
|
|
25
|
+
def current_step(self):
|
|
26
|
+
if self.session.active_steps:
|
|
27
|
+
return self.session.active_steps[-1]
|
|
28
|
+
|
|
25
29
|
def __init__(self, session: Union["HTTPSession", "WebsocketSession"]):
|
|
26
30
|
from chainlit.emitter import BaseChainlitEmitter, ChainlitEmitter
|
|
27
31
|
|
|
@@ -47,7 +51,7 @@ def init_ws_context(session_or_sid: Union[WebsocketSession, str]) -> ChainlitCon
|
|
|
47
51
|
|
|
48
52
|
|
|
49
53
|
def init_http_context(
|
|
50
|
-
user: Optional[Union["
|
|
54
|
+
user: Optional[Union["User", "PersistedUser"]] = None,
|
|
51
55
|
auth_token: Optional[str] = None,
|
|
52
56
|
user_env: Optional[Dict[str, str]] = None,
|
|
53
57
|
) -> ChainlitContext:
|