trubrics / streamlit-feedback

Collect user feedback from within your Streamlit app
MIT License
129 stars 14 forks source link

Unable to record the user input and store in variable #18

Open bpkapkar opened 8 months ago

bpkapkar commented 8 months ago

I have made changes to the code, but it seems that there is an issue when using it with st.chat_input. The example code works correctly without it, but when integrated with st.chat_input, it returns None as the value. I have reviewed the modified code to identify any mistakes, but the problem persists. As I need to store user input in a variable and check actions accordingly. below is the sample code import streamlit as st from streamlit_feedback import streamlit_feedback

def _submit_feedback(user_response, emoji=None): st.toast(f"Feedback submitted: {user_response}", icon=emoji) return user_response.update({"some metadata": 123})

def chatbot_thumbs_app(streamlit_feedback, debug=False): st.title("πŸ’¬ Chatbot") openai_api_key="sadd" if "messages" not in st.session_state: st.session_state["messages"] = [ {"role": "assistant", "content": "How can I help you?"} ]

messages = st.session_state.messages

for n, msg in enumerate(messages):
    st.chat_message(msg["role"]).write(msg["content"])

    if msg["role"] == "assistant" and n > 1:
        feedback_key = f"feedback_{int(n/2)}"

        if feedback_key not in st.session_state:
            st.session_state[feedback_key] = None

        test=streamlit_feedback(
            feedback_type="thumbs",
            optional_text_label="Please provide extra information",
            on_submit=_submit_feedback,
            key=feedback_key,
        )
        print(test)

if prompt := st.chat_input():
    messages.append({"role": "user", "content": prompt})
    st.chat_message("user").write(prompt)

    if debug:
        st.session_state["response"] = "dummy response"
    else:
        if not openai_api_key:
            st.info("Please add your OpenAI API key to continue.")
            st.stop()
        else:
            pass
        response = "test"
        st.session_state["response"] = "test"
    with st.chat_message("assistant"):
        messages.append(
            {"role": "assistant", "content": st.session_state["response"]}
        )
        st.write(st.session_state["response"])
        st.rerun()

chatbot_thumbs_app(streamlit_feedback)

image

jeffkayne commented 8 months ago

The component returns None if the app is rerun without interacting with the component. I tried your code on my side and everything seems to work correctly to me?

bpkapkar commented 8 months ago

Thanks @jeffkayne for looking into it and answering it. In addition to it I am able to get return multiple value when user interacts with thumbs. one another challenge I am finding is , we are unable to return 2 values from the function given in below function and give key value error or even we reply user_response that's look like complete dictionary that doesn't work like key value function and gets key error

def _submit_feedback(user_response, emoji=None): st.toast(f"Feedback submitted: {user_response}", icon=emoji) print(user_response) print(user_response["text"]) return user_response["score"], user_response["text"]

One more query how to identify the which thumbs has been clicked from series of thumbs that is available chat app so we can record the corresponding message to that thumbs reaction. How do we use feedback_key in this .

jeffkayne commented 8 months ago

the on_submit= funcion shouldn't return anything. It is a handler function that can do some action (e.g. save the response to a db) after submitting. To get the user_response, you should simply use the return variable of the component. In your example, that is the test variable

bpkapkar commented 8 months ago

Thanks @jeffkayne for looking into my query, to clarify more detail what I am trying to achieve. I am trying to record multiple data points example , score and text. Below is the example when I am passing the function

def _submit_feedback(user_response, emoji=None): st.toast(f"Feedback submitted: {user_response}", icon=emoji)

I am able to get the response πŸ‘

this works fine.

def _submit_feedback(user_response, emoji=None): st.toast(f"Feedback submitted: {user_response}", icon=emoji) return user_response["score"], user_response["text"] and modifying the function

reply1,reply2=streamlit_feedback( feedback_type="thumbs", optional_text_label="Please provide extra information", on_submit=_submit_feedback, key=feedback_key, )

We get the error
image

reply1,reply2=streamlit_feedback(

TypeError: cannot unpack non-iterable NoneType object

def _submit_feedback(user_response, emoji=None): st.toast(f"Feedback submitted: {user_response}", icon=emoji) return user_response

reply1=streamlit_feedback( feedback_type="thumbs", optional_text_label="Please provide extra information", on_submit=_submit_feedback, key=feedback_key, ) print(reply1["score"])

We get the error print(reply1["score"]) TypeError: 'NoneType' object is not subscriptable

As I was looking to record the multiple data points I was facing this challenge as well . I found there might some code changes required in base library or I need to do correct things

jeffkayne commented 8 months ago

To store multiple data points, you need to reset your feedback component by using a dynamic key.

Here is some code that stores multiple feedback results in a session state variable:

import streamlit as st
from streamlit_feedback import streamlit_feedback

def handle_feedback(user_response):
    st.toast("βœ”οΈ Feedback received!")

if "feedback" not in st.session_state:
    st.session_state.feedback = []

if "feedback_key" not in st.session_state:
    st.session_state.feedback_key = 0

if st.button("Refresh feedback component"):
    st.session_state.feedback_key += 1  # overwrite feedback component

result = streamlit_feedback(
    feedback_type="thumbs",
    align="flex-start",
    on_submit=handle_feedback,
    key=f"feedback_{st.session_state.feedback_key}",
)

if result:
    st.session_state.feedback.append(result)

# view session state
st.session_state.feedback