Spaces:
Running
Running
Update pages/chatbot.py
Browse files- pages/chatbot.py +46 -48
pages/chatbot.py
CHANGED
@@ -1,49 +1,47 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from app import rag_query, process_feedback
|
3 |
-
|
4 |
-
|
5 |
-
st.title("RAG Chatbot")
|
6 |
-
|
7 |
-
# Initialize
|
8 |
-
if "messages" not in st.session_state:
|
9 |
-
st.session_state.messages = []
|
10 |
-
|
11 |
-
#
|
12 |
-
for i, message in enumerate(st.session_state.messages):
|
13 |
-
with st.chat_message(message["role"]):
|
14 |
-
st.markdown(message["content"])
|
15 |
-
if message["role"] == "assistant":
|
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 |
-
if st.button("Clear Chat History"):
|
48 |
-
st.session_state.messages = []
|
49 |
st.experimental_rerun()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from app import rag_query, process_feedback
|
3 |
+
|
4 |
+
|
5 |
+
st.title("🛡️ RAG Chatbot MB Ageas Life 🛡️")
|
6 |
+
|
7 |
+
# Initialize session history
|
8 |
+
if "messages" not in st.session_state:
|
9 |
+
st.session_state.messages = []
|
10 |
+
|
11 |
+
# Hiển thị lại tin nhắn cũ
|
12 |
+
for i, message in enumerate(st.session_state.messages):
|
13 |
+
with st.chat_message(message["role"]):
|
14 |
+
st.markdown(message["content"])
|
15 |
+
if message["role"] == "assistant":
|
16 |
+
col1, col2 = st.columns([1, 15])
|
17 |
+
with col1:
|
18 |
+
if st.button("👍", key=f"thumbs_up_{i}"):
|
19 |
+
process_feedback(st.session_state.messages[i-1]["content"], message["content"], True)
|
20 |
+
with col2:
|
21 |
+
if st.button("👎", key=f"thumbs_down_{i}"):
|
22 |
+
process_feedback(st.session_state.messages[i-1]["content"], message["content"], False)
|
23 |
+
|
24 |
+
# Nhận input người dùng
|
25 |
+
if prompt := st.chat_input("Ask me any question related to MBAL"):
|
26 |
+
# Hiển thị tin nhắn người dùng
|
27 |
+
st.chat_message("user").markdown(prompt)
|
28 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
29 |
+
memory.chat_memory.add_user_message(prompt)
|
30 |
+
|
31 |
+
# Gọi hàm RAG để trả lời
|
32 |
+
response = rag_query(prompt)
|
33 |
+
|
34 |
+
# Hiển thị tin nhắn chatbot
|
35 |
+
with st.chat_message("assistant"):
|
36 |
+
st.markdown(response)
|
37 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
38 |
+
memory.chat_memory.add_ai_message(response)
|
39 |
+
|
40 |
+
st.rerun()
|
41 |
+
|
42 |
+
# Sidebar for additional controls
|
43 |
+
with st.sidebar:
|
44 |
+
st.header("Options")
|
45 |
+
if st.button("Clear Chat History"):
|
46 |
+
st.session_state.messages = []
|
|
|
|
|
47 |
st.experimental_rerun()
|