Nada commited on
Commit
d25a98e
·
1 Parent(s): b9d799b
Files changed (1) hide show
  1. chatbot.py +21 -11
chatbot.py CHANGED
@@ -818,19 +818,29 @@ Would you like to connect with a professional now, or would you prefer to keep t
818
 
819
  # Generate a follow-up question if the response is too short
820
  if len(response_text.split()) < 20 and not response_text.endswith('?'):
821
- follow_up_prompt = f"""Based on the conversation so far:
 
822
  {chr(10).join([f"{msg['role']}: {msg['text']}" for msg in conversation_history[-3:]])}
823
 
824
- Generate a thoughtful follow-up question that:
825
- 1. Shows you're actively listening
826
- 2. Encourages deeper exploration
827
- 3. Maintains therapeutic rapport
828
- 4. Is open-ended and non-judgmental
829
-
830
- Respond with just the question."""
831
-
832
- follow_up = self.llm.invoke(follow_up_prompt)
833
- response_text += f"\n\n{follow_up}"
 
 
 
 
 
 
 
 
 
834
 
835
  # assistant response -> conversation history
836
  assistant_message = Message(
 
818
 
819
  # Generate a follow-up question if the response is too short
820
  if len(response_text.split()) < 20 and not response_text.endswith('?'):
821
+ follow_up_prompt = f"""
822
+ Recent conversation:
823
  {chr(10).join([f"{msg['role']}: {msg['text']}" for msg in conversation_history[-3:]])}
824
 
825
+ Now, write a single empathetic and open-ended question to encourage the user to share more.
826
+ Respond with just the question, no explanation.
827
+ """
828
+ follow_up = self.llm.invoke(follow_up_prompt).strip()
829
+ # Clean and extract only the actual question (first sentence ending with '?')
830
+ matches = re.findall(r'([^\n.?!]*\?)', follow_up)
831
+ if matches:
832
+ question = matches[0].strip()
833
+ else:
834
+ question = follow_up.strip().split('\n')[0]
835
+ # If the main response is very short, return just the question
836
+ if len(response_text.split()) < 5:
837
+ response_text = question
838
+ else:
839
+ response_text = f"{response_text}\n\n{question}"
840
+
841
+ # Final post-processing: remove any LLM commentary that may have leaked in
842
+ response_text = response_text.strip()
843
+ response_text = re.sub(r"(Your response|This response).*", "", response_text, flags=re.IGNORECASE).strip()
844
 
845
  # assistant response -> conversation history
846
  assistant_message = Message(