I am creating a chatbot using Streamlit. I want to capture user feedback after the AI submits a response. I have modified the Echo-bot example to match the structure of my chatbot. However, when I actually fill out the form, and hit the submit button, nothing is getting captured. What am I doing wrong here?
import streamlit as st
from time import sleep
st.title("Echo Bot")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# React to user input
if prompt := st.chat_input("What is up?"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
response = f"Echo: {prompt}"
# Display assistant response in chat message container
with st.chat_message("assistant"), st.spinner():
sleep(5)
st.markdown(response)
with st.form(key=f"{len(st.session_state.messages)}_key"):
stars = st.feedback(options="stars")
extra_feedback = st.text_input(label="[Optional] Please provide additional details")
clicked = st.form_submit_button('Save feedback')
if clicked:
print(stars)
print(extra_feedback)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
Because forms make it so that widgets don’t automatically affect the variables they return immediately, this is the pattern I normally recommend for doing something with form input:
def process_form():
input1 = st.session_state["key1"]
input2 = st.session_state["key2"]
# do something with the inputs
with st.form("example"):
st.text_input("Foo", key="key1")
st.text_input("Bar", key="key2")
st.form_submit_button("Submit", on_click=process_form)
In the case of your code:
import streamlit as st
from time import sleep
st.title("Echo Bot")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
def display_feedback():
stars = st.session_state.get("stars")
extra_feedback = st.session_state.get("extra_feedback")
print(stars)
print(extra_feedback)
# React to user input
if prompt := st.chat_input("What is up?"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
response = f"Echo: {prompt}"
# Display assistant response in chat message container
with st.chat_message("assistant"), st.spinner():
sleep(.5)
st.markdown(response)
with st.form(key=f"{len(st.session_state.messages)}_key"):
stars = st.feedback(options="stars", key=f"stars")
extra_feedback = st.text_input(
label="[Optional] Please provide additional details",
key=f"extra_feedback",
)
st.form_submit_button("Save feedback", on_click=display_feedback)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.