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})