Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -134,49 +134,49 @@ if 'DEEPSEEK_API_KEY' in st.session_state:
|
|
134 |
for message in st.session_state.chat_history:
|
135 |
with st.chat_message(message["role"]):
|
136 |
st.write(message["content"])
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
with st.spinner("Thinking..."):
|
152 |
-
try:
|
153 |
-
# Retrieve relevant context from vector database
|
154 |
-
context = retrieve_context(user_input, k=k_documents)
|
155 |
-
|
156 |
-
# Prepare chat history for DeepSeek API
|
157 |
-
system_prompt = "You are a helpful assistant with access to a knowledge base."
|
158 |
-
if context:
|
159 |
-
system_prompt += f"\n\nRelevant information from knowledge base:\n{context}\n\nUse this information to answer the user's question. If the information doesn't contain the answer, just say that you don't know based on the available information."
|
160 |
-
|
161 |
-
messages = [{"role": "system", "content": system_prompt}]
|
162 |
-
for msg in st.session_state.chat_history:
|
163 |
-
messages.append({"role": msg["role"], "content": msg["content"]})
|
164 |
-
|
165 |
-
# Call DeepSeek API
|
166 |
-
response = st.session_state.client.chat.completions.create(
|
167 |
-
model="deepseek-chat",
|
168 |
-
messages=messages,
|
169 |
-
stream=False
|
170 |
-
)
|
171 |
-
|
172 |
-
assistant_response = response.choices[0].message.content
|
173 |
-
st.write(assistant_response)
|
174 |
-
|
175 |
-
# Add assistant response to chat history
|
176 |
-
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
177 |
-
|
178 |
-
except Exception as e:
|
179 |
-
st.error(f"Error: {str(e)}")
|
180 |
|
181 |
# Sidebar with info
|
182 |
with st.sidebar:
|
|
|
134 |
for message in st.session_state.chat_history:
|
135 |
with st.chat_message(message["role"]):
|
136 |
st.write(message["content"])
|
137 |
+
|
138 |
+
# Move chat_input outside of any container
|
139 |
+
user_input = st.chat_input("Type your message here...")
|
140 |
+
|
141 |
+
if user_input:
|
142 |
+
# Add user message to chat history
|
143 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
144 |
+
|
145 |
+
# Display user message
|
146 |
+
with st.chat_message("user"):
|
147 |
+
st.write(user_input)
|
148 |
+
|
149 |
+
# Get model response
|
150 |
+
with st.chat_message("assistant"):
|
151 |
+
with st.spinner("Thinking..."):
|
152 |
+
try:
|
153 |
+
# Retrieve relevant context from vector database
|
154 |
+
context = retrieve_context(user_input, k=k_documents)
|
155 |
+
|
156 |
+
# Prepare chat history for DeepSeek API
|
157 |
+
system_prompt = "You are a helpful assistant with access to a knowledge base."
|
158 |
+
if context:
|
159 |
+
system_prompt += f"\n\nRelevant information from knowledge base:\n{context}\n\nUse this information to answer the user's question. If the information doesn't contain the answer, just say that you don't know based on the available information."
|
160 |
+
|
161 |
+
messages = [{"role": "system", "content": system_prompt}]
|
162 |
+
for msg in st.session_state.chat_history:
|
163 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
164 |
+
|
165 |
+
# Call DeepSeek API
|
166 |
+
response = st.session_state.client.chat.completions.create(
|
167 |
+
model="deepseek-chat",
|
168 |
+
messages=messages,
|
169 |
+
stream=False
|
170 |
+
)
|
171 |
+
|
172 |
+
assistant_response = response.choices[0].message.content
|
173 |
+
st.write(assistant_response)
|
174 |
+
|
175 |
+
# Add assistant response to chat history
|
176 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
177 |
|
178 |
+
except Exception as e:
|
179 |
+
st.error(f"Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
# Sidebar with info
|
182 |
with st.sidebar:
|