Update src/chat_display.py
Browse files- src/chat_display.py +51 -4
src/chat_display.py
CHANGED
|
@@ -4,9 +4,9 @@ This module handles the chat display components and rendering.
|
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
import os
|
|
|
|
| 7 |
from utils import has_meaningful_content, remove_reasoning_and_sources, clean_explanation, get_image_base64
|
| 8 |
from session_state import get_full_history
|
| 9 |
-
import re
|
| 10 |
|
| 11 |
|
| 12 |
def get_avatars():
|
|
@@ -31,6 +31,50 @@ def get_avatars():
|
|
| 31 |
return user_avatar, assistant_avatar
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def display_chat_history():
|
| 35 |
"""
|
| 36 |
Display the chat history from the database.
|
|
@@ -40,9 +84,6 @@ def display_chat_history():
|
|
| 40 |
# Get full history from database
|
| 41 |
history = get_full_history()
|
| 42 |
|
| 43 |
-
# Show message count for debugging
|
| 44 |
-
# st.caption(f"Displaying {len(history)} messages")
|
| 45 |
-
|
| 46 |
# Display all messages instead of limiting to a fixed number
|
| 47 |
display_history = history
|
| 48 |
|
|
@@ -64,6 +105,12 @@ def display_chat_history():
|
|
| 64 |
cleaned_response = remove_reasoning_and_sources(message["content"])
|
| 65 |
st.markdown(cleaned_response)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# Only display the explanation in an expander if it exists AND has actual content
|
| 68 |
if message.get("explanation") and has_meaningful_content(message.get("explanation")):
|
| 69 |
# Clean up the explanation text
|
|
|
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
import os
|
| 7 |
+
import re
|
| 8 |
from utils import has_meaningful_content, remove_reasoning_and_sources, clean_explanation, get_image_base64
|
| 9 |
from session_state import get_full_history
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
def get_avatars():
|
|
|
|
| 31 |
return user_avatar, assistant_avatar
|
| 32 |
|
| 33 |
|
| 34 |
+
def extract_follow_up_questions(content):
|
| 35 |
+
"""
|
| 36 |
+
Extract follow-up questions from the main content.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
content (str): The message content
|
| 40 |
+
|
| 41 |
+
Returns:
|
| 42 |
+
str: Extracted follow-up questions or empty string if none found
|
| 43 |
+
"""
|
| 44 |
+
# Look for various forms of follow-up questions sections
|
| 45 |
+
patterns = [
|
| 46 |
+
r'(?i)follow[ -]?up questions:?\s*(.*?)(?=\n+\s*(?:#{1,3}|reasoning:|sources:|\Z))',
|
| 47 |
+
r'(?i)#{1,3}\s*follow[ -]?up questions:?\s*(.*?)(?=\n+\s*(?:#{1,3}|reasoning:|sources:|\Z))',
|
| 48 |
+
r'(?i)important questions to ask:?\s*(.*?)(?=\n+\s*(?:#{1,3}|reasoning:|sources:|\Z))',
|
| 49 |
+
r'(?i)clarifying questions:?\s*(.*?)(?=\n+\s*(?:#{1,3}|reasoning:|sources:|\Z))'
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
# Try each pattern until we find a match
|
| 53 |
+
for pattern in patterns:
|
| 54 |
+
follow_up_match = re.search(pattern, content, re.DOTALL)
|
| 55 |
+
if follow_up_match:
|
| 56 |
+
follow_up_text = follow_up_match.group(1).strip()
|
| 57 |
+
# Format the questions into a numbered list
|
| 58 |
+
questions = []
|
| 59 |
+
lines = follow_up_text.split('\n')
|
| 60 |
+
question_num = 1
|
| 61 |
+
|
| 62 |
+
for line in lines:
|
| 63 |
+
# Check if this is a question line (starts with a number or bullet)
|
| 64 |
+
question_match = re.match(r'^\s*(?:\d+\.|\-|\•|\*)\s*(.*)', line.strip())
|
| 65 |
+
if question_match and line.strip():
|
| 66 |
+
questions.append(f"{question_num}. {question_match.group(1).strip()}")
|
| 67 |
+
question_num += 1
|
| 68 |
+
elif line.strip():
|
| 69 |
+
# If not formatted as a list but has content
|
| 70 |
+
questions.append(f"{question_num}. {line.strip()}")
|
| 71 |
+
question_num += 1
|
| 72 |
+
|
| 73 |
+
return "\n".join(questions)
|
| 74 |
+
|
| 75 |
+
return ""
|
| 76 |
+
|
| 77 |
+
|
| 78 |
def display_chat_history():
|
| 79 |
"""
|
| 80 |
Display the chat history from the database.
|
|
|
|
| 84 |
# Get full history from database
|
| 85 |
history = get_full_history()
|
| 86 |
|
|
|
|
|
|
|
|
|
|
| 87 |
# Display all messages instead of limiting to a fixed number
|
| 88 |
display_history = history
|
| 89 |
|
|
|
|
| 105 |
cleaned_response = remove_reasoning_and_sources(message["content"])
|
| 106 |
st.markdown(cleaned_response)
|
| 107 |
|
| 108 |
+
# Extract follow-up questions
|
| 109 |
+
follow_up_questions = extract_follow_up_questions(message["content"])
|
| 110 |
+
if follow_up_questions:
|
| 111 |
+
with st.expander("Show Follow-up Questions"):
|
| 112 |
+
st.markdown(follow_up_questions)
|
| 113 |
+
|
| 114 |
# Only display the explanation in an expander if it exists AND has actual content
|
| 115 |
if message.get("explanation") and has_meaningful_content(message.get("explanation")):
|
| 116 |
# Clean up the explanation text
|