Update app.py
Browse files
app.py
CHANGED
@@ -136,7 +136,8 @@ if "current_conversation_id" not in st.session_state:
|
|
136 |
st.session_state.current_conversation_id = new_id
|
137 |
st.session_state.conversations[new_id] = {
|
138 |
"title": f"New chat {datetime.now().strftime('%H:%M')}",
|
139 |
-
"messages": []
|
|
|
140 |
}
|
141 |
|
142 |
# Default Model as o1
|
@@ -249,7 +250,8 @@ def create_new_chat():
|
|
249 |
st.session_state.current_conversation_id = new_id
|
250 |
st.session_state.conversations[new_id] = {
|
251 |
"title": f"New chat {datetime.now().strftime('%H:%M')}",
|
252 |
-
"messages": []
|
|
|
253 |
}
|
254 |
|
255 |
# Functions for system prompt editing
|
@@ -394,16 +396,26 @@ st.markdown('</div>', unsafe_allow_html=True)
|
|
394 |
st.markdown('</div>', unsafe_allow_html=True)
|
395 |
|
396 |
if prompt:
|
397 |
-
#
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
st.session_state.conversations[current_id]["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
407 |
|
408 |
# Add user message to conversation
|
409 |
messages.append({"role": "user", "content": prompt})
|
@@ -444,6 +456,16 @@ if prompt:
|
|
444 |
# Add the response to conversation history
|
445 |
messages.append({"role": "assistant", "content": full_response})
|
446 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
447 |
except Exception as e:
|
448 |
error_msg = f"Error: {str(e)}"
|
449 |
message_placeholder.markdown(error_msg)
|
|
|
136 |
st.session_state.current_conversation_id = new_id
|
137 |
st.session_state.conversations[new_id] = {
|
138 |
"title": f"New chat {datetime.now().strftime('%H:%M')}",
|
139 |
+
"messages": [],
|
140 |
+
"should_update_title": False
|
141 |
}
|
142 |
|
143 |
# Default Model as o1
|
|
|
250 |
st.session_state.current_conversation_id = new_id
|
251 |
st.session_state.conversations[new_id] = {
|
252 |
"title": f"New chat {datetime.now().strftime('%H:%M')}",
|
253 |
+
"messages": [],
|
254 |
+
"should_update_title": False
|
255 |
}
|
256 |
|
257 |
# Functions for system prompt editing
|
|
|
396 |
st.markdown('</div>', unsafe_allow_html=True)
|
397 |
|
398 |
if prompt:
|
399 |
+
# Check if this is the first message
|
400 |
+
if len(messages) == 0:
|
401 |
+
# Check if the first prompt is just a greeting
|
402 |
+
greetings = ["hello", "hi", "hey", "greetings", "good morning", "good afternoon", "good evening", "howdy"]
|
403 |
+
is_greeting = any(greeting in prompt.lower() for greeting in greetings)
|
404 |
+
|
405 |
+
# Keep "New chat" if it's a greeting, otherwise change immediately after first response
|
406 |
+
if not is_greeting:
|
407 |
+
# We'll update the title after getting first response
|
408 |
+
st.session_state.conversations[current_id]["should_update_title"] = True
|
409 |
+
|
410 |
+
# Update title after 2nd or 3rd user message if still has default title
|
411 |
+
elif len(messages) >= 3 and st.session_state.conversations[current_id]["title"].startswith("New chat"):
|
412 |
+
# Create a short 3-4 word summary
|
413 |
+
words = prompt.split()
|
414 |
+
if len(words) > 4:
|
415 |
+
short_title = " ".join(words[:4]) + "..."
|
416 |
+
else:
|
417 |
+
short_title = prompt
|
418 |
+
st.session_state.conversations[current_id]["title"] = short_title[:30]
|
419 |
|
420 |
# Add user message to conversation
|
421 |
messages.append({"role": "user", "content": prompt})
|
|
|
456 |
# Add the response to conversation history
|
457 |
messages.append({"role": "assistant", "content": full_response})
|
458 |
|
459 |
+
# Check if we should update the title after first response
|
460 |
+
if len(messages) == 2 and st.session_state.conversations[current_id].get("should_update_title", False):
|
461 |
+
# Extract a short title from the conversation context
|
462 |
+
combined_text = prompt + " " + full_response
|
463 |
+
words = combined_text.split()
|
464 |
+
title_words = words[:4] if len(words) > 4 else words
|
465 |
+
new_title = " ".join(title_words)
|
466 |
+
st.session_state.conversations[current_id]["title"] = new_title[:30]
|
467 |
+
st.session_state.conversations[current_id]["should_update_title"] = False
|
468 |
+
|
469 |
except Exception as e:
|
470 |
error_msg = f"Error: {str(e)}"
|
471 |
message_placeholder.markdown(error_msg)
|