Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -171,33 +171,28 @@ def handle_no_answer(response):
|
|
171 |
|
172 |
def ask_bot(query):
|
173 |
# Definiere den standardmäßigen Prompt
|
174 |
-
standard_prompt =
|
175 |
-
"in der der User auch schreibt. Formuliere immer ganze freundliche "
|
176 |
-
"ganze Sätze und biete, wenn möglich, auch mehr Informationen "
|
177 |
-
"(aber nicht mehr als 1 Satz mehr). Wenn der User sehr vage schreibt, "
|
178 |
-
"frage nach - gerade wenn es um Jahre geht, frage nur nach Jahren, "
|
179 |
-
"wenn du zu dem Thema auch Daten zu bestimmten Jahren hast.")
|
180 |
# Kombiniere den standardmäßigen Prompt mit der Benutzeranfrage
|
181 |
-
full_query = standard_prompt +
|
182 |
return full_query
|
183 |
|
184 |
-
|
185 |
-
# Funktion zum sicheren Hinzufügen zur Chat-Historie
|
186 |
def add_to_chat_history(speaker, message, history_key):
|
187 |
if history_key in st.session_state:
|
188 |
-
st.session_state[history_key].append(
|
189 |
-
|
190 |
|
|
|
|
|
|
|
|
|
191 |
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
combined_history += f"{chat[0]}: {chat[1]} "
|
199 |
-
return combined_history + current_query
|
200 |
|
|
|
201 |
|
202 |
def page1():
|
203 |
try:
|
@@ -261,53 +256,50 @@ def page1():
|
|
261 |
if st.button("Was bedeutet die Vorhaltefinanzierung?"):
|
262 |
query = "Was bedeutet die Vorhaltefinanzierung?"
|
263 |
|
|
|
|
|
264 |
if query:
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
"frage nach - gerade wenn es um Jahre geht, frage nur nach Jahren, "
|
271 |
-
"wenn du zu dem Thema auch Daten zu bestimmten Jahren hast.")
|
272 |
-
full_query = standard_prompt + " " + query
|
273 |
-
combined_query = combine_history_with_query(full_query, st.session_state['chat_history_page1'])
|
274 |
-
|
275 |
with st.spinner('Bot is thinking...'):
|
|
|
276 |
chain = load_chatbot()
|
277 |
-
|
|
|
|
|
278 |
with get_openai_callback() as cb:
|
279 |
-
response = chain.run(input_documents=docs, question=
|
280 |
response = handle_no_answer(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
281 |
st.session_state['chat_history_page1'].append(("User", query))
|
282 |
st.session_state['chat_history_page1'].append(("Bot", response))
|
283 |
-
|
284 |
-
# Diagnostic print: Display the entire chat history
|
285 |
-
st.text(f"Current chat history: {st.session_state['chat_history_page1']}")
|
286 |
-
|
287 |
# Display new messages at the bottom
|
288 |
new_messages = st.session_state['chat_history_page1'][-2:]
|
289 |
for chat in new_messages:
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
speaker, message = chat[:2]
|
296 |
-
# No need to extract the status as it is no longer used
|
297 |
-
background_color = "#ffeecf" if speaker == "User" else "#ffeecf"
|
298 |
-
new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{speaker}: {message}</div>", unsafe_allow_html=True)
|
299 |
-
except IndexError as e:
|
300 |
-
st.error(f"Error processing chat entry: {e}")
|
301 |
-
except Exception as e:
|
302 |
-
st.error(f"Unexpected error: {e}")
|
303 |
-
|
304 |
query = ""
|
|
|
|
|
|
|
305 |
|
306 |
-
st.session_state['chat_history_page1'] = [(sender, msg) for sender, msg in st.session_state['chat_history_page1']]
|
307 |
-
|
308 |
except Exception as e:
|
309 |
st.error(f"Upsi, an unexpected error occurred: {e}")
|
310 |
-
|
311 |
|
312 |
|
313 |
|
|
|
171 |
|
172 |
def ask_bot(query):
|
173 |
# Definiere den standardmäßigen Prompt
|
174 |
+
standard_prompt = "Schreibe immer höflich und auf antworte immer in der Sprache in der der User auch schreibt. Formuliere immer ganze freundliche ganze Sätze und biete wenn möglich auch mehr Informationen (aber nicht mehr als 1 Satz mehr). Wenn der User sehr vage schreibt frage nach. Wenn du zu einer bestimmten Frage Daten aus mehreren Jahren hast, frage den User für welche Jahre er sich interessiert und nenne ihm natürlich Möglichkeiten über die Jahre die du hast. "
|
|
|
|
|
|
|
|
|
|
|
175 |
# Kombiniere den standardmäßigen Prompt mit der Benutzeranfrage
|
176 |
+
full_query = standard_prompt + query
|
177 |
return full_query
|
178 |
|
|
|
|
|
179 |
def add_to_chat_history(speaker, message, history_key):
|
180 |
if history_key in st.session_state:
|
181 |
+
st.session_state[history_key].append({"speaker": speaker, "message": message})
|
|
|
182 |
|
183 |
+
def construct_context(chat_history):
|
184 |
+
# Limit the context to the last few interactions (e.g., last 5)
|
185 |
+
limited_history = chat_history[-5:]
|
186 |
+
return " ".join([f"{entry['speaker']}: {entry['message']}" for entry in limited_history])
|
187 |
|
188 |
+
def display_latest_messages(chat_history):
|
189 |
+
latest_messages = chat_history[-2:]
|
190 |
+
for chat_entry in latest_messages:
|
191 |
+
speaker, message = chat_entry['speaker'], chat_entry['message']
|
192 |
+
background_color = "#ffeecf" if speaker == "User" else "#e0ecff"
|
193 |
+
st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{speaker}: {message}</div>", unsafe_allow_html=True)
|
|
|
|
|
194 |
|
195 |
+
|
196 |
|
197 |
def page1():
|
198 |
try:
|
|
|
256 |
if st.button("Was bedeutet die Vorhaltefinanzierung?"):
|
257 |
query = "Was bedeutet die Vorhaltefinanzierung?"
|
258 |
|
259 |
+
|
260 |
+
|
261 |
if query:
|
262 |
+
full_query = ask_bot(query) # Generate full query using ask_bot function
|
263 |
+
|
264 |
+
# Start timing
|
265 |
+
start_time = time.time()
|
266 |
+
|
|
|
|
|
|
|
|
|
|
|
267 |
with st.spinner('Bot is thinking...'):
|
268 |
+
# Load the chatbot model
|
269 |
chain = load_chatbot()
|
270 |
+
|
271 |
+
# Perform the similarity search and get the response
|
272 |
+
docs = VectorStore.similarity_search(query=full_query, k=5)
|
273 |
with get_openai_callback() as cb:
|
274 |
+
response = chain.run(input_documents=docs, question=full_query)
|
275 |
response = handle_no_answer(response)
|
276 |
+
|
277 |
+
# Stop timing
|
278 |
+
end_time = time.time()
|
279 |
+
|
280 |
+
# Calculate duration
|
281 |
+
duration = end_time - start_time
|
282 |
+
st.text(f"Response time: {duration:.2f} seconds")
|
283 |
+
|
284 |
+
# Update chat history after the bot's response
|
285 |
st.session_state['chat_history_page1'].append(("User", query))
|
286 |
st.session_state['chat_history_page1'].append(("Bot", response))
|
287 |
+
|
|
|
|
|
|
|
288 |
# Display new messages at the bottom
|
289 |
new_messages = st.session_state['chat_history_page1'][-2:]
|
290 |
for chat in new_messages:
|
291 |
+
background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
|
292 |
+
new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
|
293 |
+
|
294 |
+
# Clear the input field after the query is made
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
query = ""
|
296 |
+
|
297 |
+
# Mark all messages as old after displaying
|
298 |
+
st.session_state['chat_history_page1'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page1']]
|
299 |
|
|
|
|
|
300 |
except Exception as e:
|
301 |
st.error(f"Upsi, an unexpected error occurred: {e}")
|
302 |
+
# Optionally log the exception details to a file or error tracking service
|
303 |
|
304 |
|
305 |
|