User Session
The user session is designed to persist data in memory through the life cycle of a chat session. Each user session is unique to a user and a given chat session.
Why use the user session?
Let’s say you want to keep track of each chat session message count.
A naive implementation might look like this:
This example is for illustrative purposes only. It is not recommended to use this code in production.
At first glance, this code seems to work. However, it has a major flaw. If two users are chatting with the bot at the same time, both users will increment the same counter
.
This is where the user session comes in. Let’s rewrite the above example using the user session:
User Session Default Values
By default, Chainlit stores chat session related data in the user session.
The following keys are reserved for chat session related data:
The session id.
Only set if you are enabled Authentication. Contains the user object of the user that started this chat session.
Only relevant if you are using the Chat Profiles feature. Contains the chat profile selected by this user.
Only relevant if you are using the Chat Settings feature. Contains the chat settings given by this user.
Only relevant if you are using the user_env config. Contains the environment variables given by this user.