Pluto0616 commited on
Commit
029e309
·
verified ·
1 Parent(s): c14389a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ if "messages" not in st.session_state.keys():
2
+ st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
3
+
4
+ # Display or clear chat messages
5
+ for message in st.session_state.messages:
6
+ with st.chat_message(message["role"]):
7
+ st.write(message["content"])
8
+
9
+ def clear_chat_history():
10
+ st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
11
+
12
+ st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
13
+
14
+ # Function for generating LLaMA2 response
15
+ def generate_llama_index_response(prompt_input):
16
+ return greet2(prompt_input)
17
+
18
+ # User-provided prompt
19
+ if prompt := st.chat_input():
20
+ st.session_state.messages.append({"role": "user", "content": prompt})
21
+ with st.chat_message("user"):
22
+ st.write(prompt)
23
+
24
+ # Gegenerate_llama_index_response last message is not from assistant
25
+ if st.session_state.messages[-1]["role"] != "assistant":
26
+ with st.chat_message("assistant"):
27
+ with st.spinner("Thinking..."):
28
+ response = generate_llama_index_response(prompt)
29
+ placeholder = st.empty()
30
+ placeholder.markdown(response)
31
+ message = {"role": "assistant", "content": response}
32
+ st.session_state.messages.append(message)