How to save form output to a Mongo DataBase?

I am attempting to deploy an AI chatbot with a form that would allow me to collect user feedback. Currently, I am able to save the user query and generated chatbot answer to a Mongo DB database, however, when attempting to save the feedback, I am unable to save anything. How could I save the feedback associated with each query and answer pair. Here is my code for the moment:

#init_db is a function to connect to my mongodb database
DB_NAME = "feedback"
COLLECTION_NAME = "ai-chatbot"
feedback_collection = init_db(DB_NAME, COLLECTION_NAME)

def init_messages():
    if 'RAG_messages' not in st.session_state or st.sidebar.button("Eras the conversation"):
        st.session_state.RAG_messages = [{"role": "assistant", "content": "How can I help you?"}]

def save_feedback(question, answer, sources, contexts, feedback_rating, feedback_comment, timestamp):
    feedback_data = {
        "question": question,
        "answer": answer,
        "contexts": contexts,
        "sources": sources,
        "feedback_rating": feedback_rating,
        "feedback_comment": feedback_comment,
        "timestamp": timestamp
    }
    feedback_collection.insert_one(feedback_data)

if is_authenticated():
    st.set_page_config(page_title="AI Chatbot", page_icon="👾")
    st.title("Ask your questions")

    init_messages()

# RAG_qa is a class that represents my RAG chatbot
    RAG_app = RAG_qa()

    for message in st.session_state.RAG_messages:
        st.chat_message(message["role"]).write(message["content"])

    if prompt := st.chat_input("Send a question"):
        st.chat_message("user").write(prompt)
        st.session_state.RAG_messages.append({"role": "user", "content": prompt})

        answer, sources, contexts, urls = RAG_app.retrieval_qa(prompt)

        st.chat_message("assistant").write(answer + '  \n  \n' + sources)
       st.session_state.RAG_messages.append({"role": "assistant", "content": answer + '  \n  \n' + sources})
        st.session_state.RAG_chat_memory_messages = st.session_state.RAG_messages

        with st.form(key='feedback_form'):
            feedback_score = st.radio("Was the answer helpful?", ["Yes", "No"], index=1)
            feedback_comment = st.text_area("Additional comments")
            submitted = st.form_submit_button("Submit Feedback")

            if submitted:
                feedback_rating = 1 if feedback_score == "Yes" else 0
                timestamp = datetime.datetime.now().isoformat()
                save_feedback(
                    question=prompt,
                    answer=answer,
                    urls=urls, 
                    contexts=contexts,
                    feedback_rating=feedback_rating,
                    feedback_comment=feedback_comment,
                    timestamp=timestamp
                )
                st.success("Thank you for your feedback!")
else:
    st.write("Please log in from the homepage to access this page.")

I think you can provide a complete example instead of partial code. We cannot find exceptions through code snippets. Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.