Go for detabase.
You can also choose others.
Messages database
Require your users to register and login with username and password. After the login, you now have the username.
You can use this username to create a message database detabase file.
deta = Deta(st.secrets["data_key"])
db = deta.Base(f"{username}_message_db")
For every user prompt you can save it as a dict.
if prompt := st.chat_input('your message'):
db.put({"role": "user", "content": prompt})
In detabase, it needs a key for every entry. But if you did not specify, detabase will add it automatically.
The fields or header of that detabase may not be enough because we need a topic. This topic is needed by the ai, so that it can reply with relevance.
And so before chatting, we will require the users to define the topic. You can be clever about this, most apps just let the users press the new topic button, and try to manage behind the scene. What I will be presenting is basic.
st.text_input('New Topic', ...)
The user will write a topic and we will use it in the detabase.
We will revise our detabase entry. Let’s add a “topic” dict key.
if prompt := st.chat_input('your message'):
db.put({"role": "user", "content": prompt, 'topic': st.session_state.topic})
# Get, show and save response.
with st.chat_message("assistant"):
# Get response
response = generate_response(prompt)
db.put({"role": "assistant", "content": response, 'topic': st.session_state.topic})
You can chat under the topic soccer and it will be saved in detabase.
Topics are saved and you can select them later to continue the conversation.
The detabase entries are just a list of dict. If the user selected a different topic in the selectbox, you can retrieve the data in detabase and filter the topic your are interested with. You can use this filtered topic as input to the ai, to make the ai responses relevant to the topic.
The data in detabase can be easily converted to pandas dataframe. You can allow the users to download csv, json, or excel file formats.
Deta python sdk is a good reference.
Users account
For your users account, register, login, etc. management you can use detabase too.
deta = Deta(st.secrets["data_key"])
db = deta.Base("users")
To register create a form to input username and password.
if submitted:
db.put({"username": username, "password": pw_hasher(password), "key": username})
usernames must be unique, now we include the key with value username. Detabase will not accept if there is already a username that exists in the detabase.
You can use argon2 to hash the password. This is the argon2 in pypi.
To verify login, just load the db, and check the username and the associated password using this typical code.
>>> from argon2 import PasswordHasher
>>> ph = PasswordHasher()
>>> hash = ph.hash("correct horse battery staple")
>>> hash
'$argon2id$v=19$m=65536,t=3,p=4$MIIRqgvgQbgj220jfp0MPA$YfwJSVjtjSU0zzV/P3S9nnQ/USre2wvJMjfCIjrTQbg'
>>> ph.verify(hash, "correct horse battery staple")
True
>>> ph.check_needs_rehash(hash)
False
>>> ph.verify(hash, "Tr0ub4dor&3")
Traceback (most recent call last):
...
argon2.exceptions.VerifyMismatchError: The password does not match the supplied hash