Closed sebderhy closed 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.
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.
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.
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!
Seems like it's impossible to create user session variables.
This code snippet:
produces:
Chat History: None