How to capture the streamlit-feedback output?

149 views Asked by At

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.

1

There are 1 answers

0
TheHungryCub On

Check this :

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

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

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