neuralleap commited on
Commit
ab76045
·
verified ·
1 Parent(s): a429775

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -12
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
- # Only update the chat title after at least 2 assistant responses
398
- # This gives enough context to identify the subject of the conversation
399
- if len(messages) >= 4: # At least 2 user messages and 2 assistant responses
400
- current_title = st.session_state.conversations[current_id]["title"]
401
- if current_title.startswith("New chat"):
402
- # Get the content of the conversation to identify the subject
403
- conversation_content = " ".join([msg["content"][:100] for msg in messages[:4]])
404
- # Create a brief title based on the conversation content
405
- new_title = prompt[:25] + "..." if len(prompt) > 25 else prompt
406
- st.session_state.conversations[current_id]["title"] = new_title
 
 
 
 
 
 
 
 
 
 
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)