File size: 1,943 Bytes
ada8cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import streamlit as st
from app import rag_query, process_feedback


st.title("RAG Chatbot")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for i, message in enumerate(st.session_state.messages):
    with st.chat_message(message["role"]):
        st.markdown(message["content"])
        if message["role"] == "assistant":
                col1, col2 = st.columns([1,15])
                with col1:
                    if st.button("πŸ‘", key=f"thumbs_up_{i}"):
                        process_feedback(st.session_state.messages[i-1]["content"], message["content"], True)
                with col2:
                    if st.button("πŸ‘Ž", key=f"thumbs_down_{i}"):
                        process_feedback(st.session_state.messages[i-1]["content"], message["content"], False)
                   # st.session_state.messages.pop()  # Remove the last assistant message
                    #st.rerun()  # Rerun the app to regenerate the response

# React to user input
if prompt := st.chat_input("What is your question?"):
    # Display user message in chat message container
    st.chat_message("user").markdown(prompt)
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})

    response = rag_query(prompt)
    
    # Display assistant response in chat message container
    with st.chat_message("assistant"):
        st.markdown(response)
    # Add assistant response to chat history
    st.session_state.messages.append({"role": "assistant", "content": response})

    # Rerun the app to display the feedback buttons
    st.experimental_rerun()

# Sidebar for additional controls
with st.sidebar:
    st.header("Options")
    if st.button("Clear Chat History"):
        st.session_state.messages = []
        st.experimental_rerun()