Hi,
I am trying to implement the feedback option for Chat response using streamlit_feedback library. I can get the “thumbs” or “faces” bottom displayed. However, i can’t capture the user selected feedback. Below is my sample code.
I am seeking two helps as below.
- How to capture the response of the feedback selected by the user ?
- Right now below code display the feedback only for last response, how to keep the feedback button for all past responses also? So that if user want to go back and give feedback for any of the past response, it should be possible.
Any suggestion here would be very helpful for me.
import streamlit as st
from streamlit_feedback import streamlit_feedback
from langchain.schema import (SystemMessage, HumanMessage, AIMessage)
if "messages" not in st.session_state:
st.session_state.messages = [
AIMessage(content= f"\n \n Welcome! How can I assist you today?")
]
if "response" not in st.session_state:
st.session_state["response"] = ""
if "feedback_key" not in st.session_state:
st.session_state.feedback_key = 0
def _submit_feedback(user_response, emoji=None):
st.toast(f"Feedback submitted: {user_response}", icon=emoji)
return user_response.update({"some metadata": 123})
# Display chat history
messages = st.session_state.get("messages", [])
for message in messages:
if isinstance(message, AIMessage):
with st.chat_message("assistant", avatar='🦙'):
st.markdown(message.content)
elif isinstance(message, HumanMessage):
with st.chat_message("user",avatar= "🦖"):
st.markdown(message.content)
if user_question :=st.chat_input("Input your question! ",):
with st.chat_message("user",avatar= "🦖"):
st.markdown(user_question)
st.session_state.messages.append(HumanMessage(content=user_question))
with st.chat_message("assistant",avatar='🦙'):
st.markdown("LLM Response")
st.session_state.feedback_key += 1 # overwrite feedback component
st.session_state.messages.append(AIMessage(content=f"LLM Response"))
# with st.form('form'):
score_mappings = {
"thumbs": {"👍": 1, "👎": 0},
"faces": {"😀": 1, "🙂": 0.75, "😐": 0.5, "🙁": 0.25, "😞": 0},
}
feedback = streamlit_feedback(
feedback_type="faces",
# optional_text_label="[Optional] Please provide an explanation",
)
if feedback:
st.write(":orange[Component output:]")
st.write(feedback)