ngcanh commited on
Commit
a20f30f
·
verified ·
1 Parent(s): 3c7d3dd

Update pages/chatbot.py

Browse files
Files changed (1) hide show
  1. 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 chat history
8
- if "messages" not in st.session_state:
9
- st.session_state.messages = []
10
-
11
- # Display chat messages from history on app rerun
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
- # st.session_state.messages.pop() # Remove the last assistant message
24
- #st.rerun() # Rerun the app to regenerate the response
25
-
26
- # React to user input
27
- if prompt := st.chat_input("What is your question?"):
28
- # Display user message in chat message container
29
- st.chat_message("user").markdown(prompt)
30
- # Add user message to chat history
31
- st.session_state.messages.append({"role": "user", "content": prompt})
32
-
33
- response = rag_query(prompt)
34
-
35
- # Display assistant response in chat message container
36
- with st.chat_message("assistant"):
37
- st.markdown(response)
38
- # Add assistant response to chat history
39
- st.session_state.messages.append({"role": "assistant", "content": response})
40
-
41
- # Rerun the app to display the feedback buttons
42
- st.experimental_rerun()
43
-
44
- # Sidebar for additional controls
45
- with st.sidebar:
46
- st.header("Options")
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
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()