pythonstreamlitfeedback

How to capture the streamlit-feedback output?


I am facing issue while capturing the feedback given from the front end. Below is my code.

import streamlit as st
from streamlit_feedback import streamlit_feedback


def display_answer():
    for i in st.session_state.chat_history:
        with st.chat_message("human"):
            st.write(i["question"])
        with st.chat_message("ai"):
            st.write(i["answer"])


def create_answer(question):
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = []

    message_id = len(st.session_state.chat_history)

    st.session_state.chat_history.append(
        {
            "question": question,
            "answer": f"{question}_Answer",
            "message_id": message_id,
        }
    )


if question := st.chat_input(placeholder="Ask your question here .... !!!!"):
    create_answer(question)
    display_answer()

    if feedback := streamlit_feedback(feedback_type="thumbs", align="flex-start"):
        print(feedback)

currently in my code when i ask question in input, i can see the question and answer pair in UI. Also i can see the feedback buttons. But when i click on any of the button, the UI becomes blank and history will be gone.

i am completely blank now. if my understanding is correct, when i click on any of the feedback button, i should see the feedback printed in terminal.


Solution

  • This resolved my issue.

    import streamlit as st
    from streamlit_feedback import streamlit_feedback
    
    
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = []
    
    
    def display_answer():
        for i in st.session_state.chat_history:
            with st.chat_message("human"):
                st.write(i["question"])
            with st.chat_message("ai"):
                st.write(i["answer"])
    
            # If there is no feedback show N/A
            if "feedback" in i:
                st.write(f"Feedback: {i['feedback']}")
            else:
                st.write("Feedback: N/A")
    
    def create_answer(question):
        if "chat_history" not in st.session_state:
            st.session_state.chat_history = []
    
        message_id = len(st.session_state.chat_history)
    
        st.session_state.chat_history.append({
            "question": question,
            "answer": f"{question}_Answer",
            "message_id": message_id,
        })
    
    
    def fbcb():
        message_id = len(st.session_state.chat_history) - 1
        if message_id >= 0:
            st.session_state.chat_history[message_id]["feedback"] = st.session_state.fb_k
        display_answer()
    
    
    if question := st.chat_input(placeholder="Ask your question here .... !!!!"):
        create_answer(question)
        display_answer()
    
        with st.form('form'):
            streamlit_feedback(feedback_type="thumbs", align="flex-start", key='fb_k')
            st.form_submit_button('Save feedback', on_click=fbcb)```