Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
6.24k stars 799 forks source link

Problem with user session variables #71

Closed sebderhy closed 1 year ago

sebderhy commented 1 year ago

Seems like it's impossible to create user session variables.

This code snippet:

from chainlit import user_session
user_session.set("chat_history", "a b c")
print(f"Chat History: {user_session.get('chat_history')}")

produces: Chat History: None

willydouhard commented 1 year ago

You have to be in a chainlit context to use user session. Meaning calling that code in on_chat_start or langchain_factory or action_callback. Any context will do.

The reason is that in this context Chainlit knows which websocket session is running and is able to store information for this session. Outside of the context there is not way to know which websocket session (if any) is active.

sebderhy commented 1 year ago

Sorry, but it is still a bit unclear to me how to do that... I've tried running this code

@cl.on_chat_start
def init_session():
    user_session.set('chat_history', [])
    print("chat_history: ", user_session.get('chat_history'))
    user_session.set('path_out', create_output_path())
    print("path_out: ", user_session.get('path_out'))

But I see that this code is being called several times when launching the app.

willydouhard commented 1 year ago

This is the right way to use the user_session.

Do you have multiple tabs/windows of the chainlit app opened? If that is the case each tab/window is going to connect and trigger on_chat_start. Each will have its own user session.

sebderhy commented 1 year ago

Ah great, indeed, I had a few unused tabs opened. I didn't know that they could get reactivated like this. Thanks for the insight!