Update components/chat.py
Browse files- components/chat.py +67 -164
components/chat.py
CHANGED
|
@@ -1,76 +1,12 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 3 |
from utils.database import verify_vector_store
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
content = str(content)
|
| 8 |
-
|
| 9 |
-
# Remove common technical artifacts
|
| 10 |
-
if "content='" in content:
|
| 11 |
-
content = content.split("content='")[1]
|
| 12 |
-
if "additional_kwargs" in content:
|
| 13 |
-
content = content.split("additional_kwargs")[0]
|
| 14 |
-
|
| 15 |
-
# Clean up any remaining artifacts
|
| 16 |
-
content = content.strip("'")
|
| 17 |
-
content = content.replace('\\n', '\n')
|
| 18 |
-
content = content.replace('\\t', '\t')
|
| 19 |
-
|
| 20 |
-
return content
|
| 21 |
-
|
| 22 |
-
def format_assistant_response(content):
|
| 23 |
-
"""Format the assistant's response into a structured layout."""
|
| 24 |
-
try:
|
| 25 |
-
# Clean the content first
|
| 26 |
-
content = clean_ai_response(content)
|
| 27 |
-
|
| 28 |
-
# Identify sections and structure
|
| 29 |
-
lines = [line.strip() for line in content.split('\n') if line.strip()]
|
| 30 |
-
formatted_sections = []
|
| 31 |
-
current_section = []
|
| 32 |
-
|
| 33 |
-
for line in lines:
|
| 34 |
-
# Handle bullet points and lists
|
| 35 |
-
if line.startswith(('•', '-', '*')):
|
| 36 |
-
line = f"<li>{line.lstrip('•-* ')}</li>"
|
| 37 |
-
if not current_section:
|
| 38 |
-
current_section.append("<ul>")
|
| 39 |
-
current_section.append(line)
|
| 40 |
-
# Handle section headers
|
| 41 |
-
elif line.startswith('**') and line.endswith('**'):
|
| 42 |
-
if current_section:
|
| 43 |
-
if current_section[0] == "<ul>":
|
| 44 |
-
current_section.append("</ul>")
|
| 45 |
-
formatted_sections.extend(current_section)
|
| 46 |
-
current_section = []
|
| 47 |
-
header = line.strip('**')
|
| 48 |
-
formatted_sections.append(f'<h4 class="section-header">{header}</h4>')
|
| 49 |
-
# Regular text
|
| 50 |
-
else:
|
| 51 |
-
if current_section and current_section[0] == "<ul>":
|
| 52 |
-
current_section.append("</ul>")
|
| 53 |
-
formatted_sections.extend(current_section)
|
| 54 |
-
current_section = []
|
| 55 |
-
formatted_sections.append(f'<p>{line}</p>')
|
| 56 |
-
|
| 57 |
-
# Add any remaining content
|
| 58 |
-
if current_section:
|
| 59 |
-
if current_section[0] == "<ul>":
|
| 60 |
-
current_section.append("</ul>")
|
| 61 |
-
formatted_sections.extend(current_section)
|
| 62 |
-
|
| 63 |
-
# Build the final HTML
|
| 64 |
-
formatted_html = f"""
|
| 65 |
-
<div class="response-content">
|
| 66 |
-
{''.join(formatted_sections)}
|
| 67 |
-
</div>
|
| 68 |
-
"""
|
| 69 |
-
|
| 70 |
-
return formatted_html
|
| 71 |
-
except Exception as e:
|
| 72 |
-
st.error(f"Error formatting response: {str(e)}")
|
| 73 |
-
return str(content) # Return raw content if formatting fails
|
| 74 |
|
| 75 |
def display_chat_interface():
|
| 76 |
"""Display modern chat interface with clean formatting."""
|
|
@@ -78,138 +14,105 @@ def display_chat_interface():
|
|
| 78 |
# Add custom CSS for modern chat styling
|
| 79 |
st.markdown("""
|
| 80 |
<style>
|
| 81 |
-
/*
|
| 82 |
.chat-container {
|
| 83 |
max-width: 800px;
|
| 84 |
margin: auto;
|
| 85 |
}
|
| 86 |
|
| 87 |
-
/*
|
| 88 |
.user-message {
|
| 89 |
background-color: #f0f2f6;
|
| 90 |
padding: 1rem;
|
| 91 |
border-radius: 10px;
|
| 92 |
margin: 1rem 0;
|
| 93 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
| 94 |
}
|
| 95 |
|
| 96 |
-
/* Assistant message */
|
| 97 |
.assistant-message {
|
| 98 |
background-color: #ffffff;
|
| 99 |
border: 1px solid #e0e0e0;
|
| 100 |
padding: 1.5rem;
|
| 101 |
border-radius: 10px;
|
| 102 |
margin: 1rem 0;
|
| 103 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
/* Response content */
|
| 107 |
-
.response-content {
|
| 108 |
-
line-height: 1.6;
|
| 109 |
}
|
| 110 |
|
| 111 |
-
/* Section
|
| 112 |
.section-header {
|
|
|
|
| 113 |
color: #0f52ba;
|
| 114 |
-
margin:
|
| 115 |
-
font-size: 1.1rem;
|
| 116 |
-
font-weight: 600;
|
| 117 |
-
border-bottom: 2px solid #e0e0e0;
|
| 118 |
-
padding-bottom: 0.5rem;
|
| 119 |
}
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 126 |
}
|
| 127 |
|
| 128 |
-
|
|
|
|
| 129 |
margin: 0.5rem 0;
|
| 130 |
-
position: relative;
|
| 131 |
-
padding-left: 1rem;
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
.response-content li:before {
|
| 135 |
-
content: "•";
|
| 136 |
-
position: absolute;
|
| 137 |
-
left: -1rem;
|
| 138 |
-
color: #0f52ba;
|
| 139 |
}
|
| 140 |
|
| 141 |
-
/*
|
| 142 |
-
.
|
| 143 |
-
margin:
|
| 144 |
-
color: #2c3e50;
|
| 145 |
-
}
|
| 146 |
-
|
| 147 |
-
/* Source citations */
|
| 148 |
-
.source-citation {
|
| 149 |
-
font-style: italic;
|
| 150 |
-
color: #666;
|
| 151 |
-
border-top: 1px solid #e0e0e0;
|
| 152 |
-
margin-top: 1rem;
|
| 153 |
-
padding-top: 0.5rem;
|
| 154 |
-
font-size: 0.9rem;
|
| 155 |
}
|
| 156 |
</style>
|
| 157 |
""", unsafe_allow_html=True)
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
<
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
| 187 |
with st.spinner("Analyzing..."):
|
| 188 |
-
#
|
| 189 |
-
if not prompt.strip():
|
| 190 |
-
st.warning("Please enter a valid question.")
|
| 191 |
-
return
|
| 192 |
-
|
| 193 |
-
# Create and append human message
|
| 194 |
human_message = HumanMessage(content=prompt)
|
| 195 |
st.session_state.messages.append(human_message)
|
| 196 |
-
|
| 197 |
-
#
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
if response:
|
| 205 |
-
|
|
|
|
| 206 |
st.session_state.messages.append(ai_message)
|
| 207 |
-
st.
|
| 208 |
else:
|
| 209 |
-
st.error("No valid response received
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
st.error(traceback.format_exc())
|
|
|
|
| 1 |
+
# components_chat.py
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 5 |
from utils.database import verify_vector_store
|
| 6 |
+
from threading import Lock
|
| 7 |
|
| 8 |
+
# Create a lock for QA system access
|
| 9 |
+
qa_lock = Lock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def display_chat_interface():
|
| 12 |
"""Display modern chat interface with clean formatting."""
|
|
|
|
| 14 |
# Add custom CSS for modern chat styling
|
| 15 |
st.markdown("""
|
| 16 |
<style>
|
| 17 |
+
/* Clean, modern chat container */
|
| 18 |
.chat-container {
|
| 19 |
max-width: 800px;
|
| 20 |
margin: auto;
|
| 21 |
}
|
| 22 |
|
| 23 |
+
/* Message styling */
|
| 24 |
.user-message {
|
| 25 |
background-color: #f0f2f6;
|
| 26 |
padding: 1rem;
|
| 27 |
border-radius: 10px;
|
| 28 |
margin: 1rem 0;
|
|
|
|
| 29 |
}
|
| 30 |
|
|
|
|
| 31 |
.assistant-message {
|
| 32 |
background-color: #ffffff;
|
| 33 |
border: 1px solid #e0e0e0;
|
| 34 |
padding: 1.5rem;
|
| 35 |
border-radius: 10px;
|
| 36 |
margin: 1rem 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
+
/* Section styling */
|
| 40 |
.section-header {
|
| 41 |
+
font-weight: bold;
|
| 42 |
color: #0f52ba;
|
| 43 |
+
margin-top: 1rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
+
.source-info {
|
| 47 |
+
background-color: #f8f9fa;
|
| 48 |
+
padding: 0.5rem;
|
| 49 |
+
border-left: 3px solid #0f52ba;
|
| 50 |
+
margin-top: 1rem;
|
| 51 |
+
font-size: 0.9rem;
|
| 52 |
}
|
| 53 |
|
| 54 |
+
/* Content sections */
|
| 55 |
+
.content-section {
|
| 56 |
margin: 0.5rem 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
+
/* Clean bullets */
|
| 60 |
+
.clean-bullet {
|
| 61 |
+
margin: 0.3rem 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
}
|
| 63 |
</style>
|
| 64 |
""", unsafe_allow_html=True)
|
| 65 |
|
| 66 |
+
# Check if QA system is initialized
|
| 67 |
+
if 'qa_system' not in st.session_state or st.session_state.qa_system is None:
|
| 68 |
+
st.warning("Please upload documents first to initialize the chat system.")
|
| 69 |
+
return
|
| 70 |
+
|
| 71 |
+
# Initialize chat history
|
| 72 |
+
if 'messages' not in st.session_state:
|
| 73 |
+
st.session_state.messages = []
|
| 74 |
+
|
| 75 |
+
# Display chat history
|
| 76 |
+
for message in st.session_state.messages:
|
| 77 |
+
if isinstance(message, HumanMessage):
|
| 78 |
+
st.markdown(f"""
|
| 79 |
+
<div class="user-message">
|
| 80 |
+
🧑💼 <strong>You:</strong><br>{message.content}
|
| 81 |
+
</div>
|
| 82 |
+
""", unsafe_allow_html=True)
|
| 83 |
+
elif isinstance(message, AIMessage):
|
| 84 |
+
content = str(message.content)
|
| 85 |
+
st.markdown(f"""
|
| 86 |
+
<div class="assistant-message">
|
| 87 |
+
🤖 <strong>Assistant:</strong><br>
|
| 88 |
+
{content}
|
| 89 |
+
</div>
|
| 90 |
+
""", unsafe_allow_html=True)
|
| 91 |
+
|
| 92 |
+
# Chat input at the bottom
|
| 93 |
+
if prompt := st.chat_input("Ask about your documents..."):
|
| 94 |
+
try:
|
| 95 |
with st.spinner("Analyzing..."):
|
| 96 |
+
# Create message objects
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
human_message = HumanMessage(content=prompt)
|
| 98 |
st.session_state.messages.append(human_message)
|
| 99 |
+
|
| 100 |
+
# Use lock to access QA system
|
| 101 |
+
with qa_lock:
|
| 102 |
+
response = st.session_state.qa_system.invoke({
|
| 103 |
+
"input": prompt,
|
| 104 |
+
"chat_history": st.session_state.messages
|
| 105 |
+
})
|
| 106 |
+
|
| 107 |
if response:
|
| 108 |
+
content = str(response)
|
| 109 |
+
ai_message = AIMessage(content=content)
|
| 110 |
st.session_state.messages.append(ai_message)
|
| 111 |
+
st.experimental_rerun()
|
| 112 |
else:
|
| 113 |
+
st.error("No valid response received")
|
| 114 |
|
| 115 |
+
except Exception as e:
|
| 116 |
+
st.error(f"Error: {e}")
|
| 117 |
+
import traceback
|
| 118 |
+
st.error(traceback.format_exc())
|
|
|