For all the developers out there who have built chatbots with Streamlit Cloud, how did you build your database?

Hello, I was making a chatbot app using streamlit cloud. And I want to save some datas while using this function.

There are two datas I want to save-

  1. Basic user informations
    It will be saved after doing sign up, and includes ID and PW.

  2. Chat messages
    I want my app’s users can download their previous chat messages. And it is also used for various way such as analyzing their feelings.

I think the first one might not be needed to be updated that frequently, but the second one might be.
And I think both of them are really simple so I don’t think I need to build a big structure of it(just two tables would be okay for doing the job).

But because I am still newbie and learning programming, I really don’t know which database should I look at and what to choose.
Can you give me some ideas?

In case it helps, I know there are some “Login” components that you can find as Custom Components people have already written. Maybe they save to a Database and you can sort of build on top of that, and extend it to persist the messages. Even if not you can see how some people built the logins themselves.

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.

image

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.