How to build a survey chatbot with streamlit_chat

I am a noob here; I am building a simple question-answering chatbot using streamlit. I have a list of questions {Q1, Q2, … Qn} that I want to ask a user and collect the user’s response in the database. For each user’s response to each question, I also have one follow-up question (FQ1 that I want to ask further and collect responses. The follow-up question is generated on the fly through a hugging face model.

In essence, here is the following workflow I want to be able to see in the chat history:

Bot: Q1
User Response: [text input]
Bot: FQ1 (follow-up question generated on the fly through a model)
User Response: [text input]

Bot: Q2
User Response: [text input]
Bot: FQ2 (follow-up question generated on the fly)
User Response: [text input]
| |
| |
Bot: Q5
User Response: [text input]
Bot: FQ5 (follow-up question generated on the fly)
User Response:

For now, if we assume the questions and follow-up questions are pre-defined lists, can someone please help provide a vanilla code to solve the above? There are multiple examples of streamlit chat components, but none of them deal with asking a pre-defined set of questions (something like a survey bot). Having a bunch of issues while playing through session variables. Any help is appreciated.

you should be able to iterate over the questions and the replies stored in the session state (so that they won’t be lost) and for each message display the response and question using the streamlit_chat.message

if 'responses' not in st.session_state.keys():
        st.session_state.questions = ['q1', 'q2', ...]
        st.session_state.responses = []
message(st.session_state.questions[0]) # first question

for response, question in zip(st.session_state.responses, st.session_state.questions[1:]):
    message(response, is_user = True)
    message(question)

new_response = st.text_input("response:")

let me know if this is helpful, mention me if you have any follow-up questions

1 Like

Thank you, this is helpful. This is a workable code if anyone is interested.

import streamlit as st

from streamlit_chat import message

def on_input_change():
    user_input = st.session_state.user_input
    st.session_state.responses.append(user_input)

def on_btn_click():
    del st.session_state['questions']
    del st.session_state['responses']

st.session_state.setdefault('questions', [])

st.title("Survey QA Bot")
questions_list = ['question 1', 'question 2', 'question 3']

if 'responses' not in st.session_state.keys():
    st.session_state.questions.extend(questions_list)
    st.session_state.responses = []

chat_placeholder = st.empty()
st.button("Clear message", on_click=on_btn_click)

message(st.session_state.questions[0]) 

with st.container():
    for response, question in zip(st.session_state.responses, st.session_state.questions[1:]):
        message(response, is_user = True)
        message(question)


with st.container():
    st.text_input("User Response:", on_change=on_input_change, key="user_input")

hi i get buttons in my response from chatbot…but how i will display and give as a input to chatbot
please i need sample coding