-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
104 lines (78 loc) · 3.28 KB
/
main.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import streamlit as st
from deepseek_api import generate_response, set_api_key
def initialize_session_state():
if "messages" not in st.session_state:
st.session_state.messages = []
def set_api_key_in_sidebar():
st.sidebar.header("Configuration")
api_key = st.sidebar.text_input("Enter your DeepSeek API key:",
type="password")
if api_key:
set_api_key(api_key)
else:
st.sidebar.warning(
"Please enter your DeepSeek API key to use the chatbot.")
return api_key
def toggle_chain_of_thought():
return st.sidebar.checkbox("Enable Chain of Thought", value=False)
def display_chat_history():
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
def get_user_input(api_key_present):
if not api_key_present:
st.error("Please enter your DeepSeek API key in the sidebar.")
return None
return st.chat_input("What would you like to know?")
def add_message(role, content):
st.session_state.messages.append({"role": role, "content": content})
def generate_and_display_response(prompt, use_cot):
add_message("user", prompt)
with st.chat_message("user"):
st.markdown(prompt)
add_message("assistant", "") # Placeholder for assistant's message
with st.chat_message("assistant") as assistant_message:
message_placeholder = st.empty()
full_response = ""
reasoning_steps = []
try:
if use_cot:
with st.spinner("Thinking..."):
for response in generate_response(prompt, use_cot):
if response.startswith("Step"):
reasoning_steps.append(response)
else:
full_response += response
message_placeholder.markdown(full_response + "▌")
else:
with st.spinner("Generating response..."):
for response in generate_response(prompt, use_cot):
full_response += response
message_placeholder.markdown(full_response + "▌")
except Exception as e:
full_response = "😕 Sorry, I encountered an error while generating the response."
message_placeholder.markdown(full_response)
st.error(f"Error: {e}")
return
message_placeholder.markdown(full_response)
st.session_state.messages[-1][
"content"] = full_response # Update the placeholder message
if use_cot and reasoning_steps:
with st.expander("Show reasoning steps"):
for step in reasoning_steps:
st.markdown(step)
def main():
st.set_page_config(page_title="DeepSeek Chatbot",
page_icon="🤖",
layout="wide")
st.title("🤖 DeepSeek Chatbot with Chain of Thought")
initialize_session_state()
api_key = set_api_key_in_sidebar()
use_cot = toggle_chain_of_thought()
st.write("---")
display_chat_history()
prompt = get_user_input(api_key_present=bool(api_key))
if prompt:
generate_and_display_response(prompt, use_cot)
if __name__ == "__main__":
main()