Update components/chat.py
Browse files- components/chat.py +26 -0
components/chat.py
CHANGED
|
@@ -6,6 +6,7 @@ from utils.database import verify_vector_store
|
|
| 6 |
from threading import Lock
|
| 7 |
from typing import Optional
|
| 8 |
import traceback
|
|
|
|
| 9 |
|
| 10 |
# Create a lock for QA system access
|
| 11 |
qa_lock = Lock()
|
|
@@ -37,6 +38,31 @@ def _verify_chat_ready() -> bool:
|
|
| 37 |
return False
|
| 38 |
return True
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def _display_chat_history():
|
| 41 |
"""Display all messages in the chat history."""
|
| 42 |
for message in st.session_state.messages:
|
|
|
|
| 6 |
from threading import Lock
|
| 7 |
from typing import Optional
|
| 8 |
import traceback
|
| 9 |
+
from utils.response_formatter import ResponseFormatter, display_formatted_response
|
| 10 |
|
| 11 |
# Create a lock for QA system access
|
| 12 |
qa_lock = Lock()
|
|
|
|
| 38 |
return False
|
| 39 |
return True
|
| 40 |
|
| 41 |
+
def _process_user_message(prompt: str):
|
| 42 |
+
"""Process a new user message and generate AI response."""
|
| 43 |
+
try:
|
| 44 |
+
with st.spinner("Analyzing..."):
|
| 45 |
+
human_message = HumanMessage(content=prompt)
|
| 46 |
+
st.session_state.messages.append(human_message)
|
| 47 |
+
with st.chat_message("user"):
|
| 48 |
+
st.write(prompt)
|
| 49 |
+
|
| 50 |
+
with qa_lock:
|
| 51 |
+
response = st.session_state.qa_system.invoke({
|
| 52 |
+
"input": prompt,
|
| 53 |
+
"chat_history": st.session_state.messages
|
| 54 |
+
})
|
| 55 |
+
|
| 56 |
+
if response:
|
| 57 |
+
# Format the response
|
| 58 |
+
ai_message = AIMessage(content=str(response))
|
| 59 |
+
st.session_state.messages.append(ai_message)
|
| 60 |
+
with st.chat_message("assistant"):
|
| 61 |
+
display_formatted_response(
|
| 62 |
+
str(response),
|
| 63 |
+
metadata=getattr(response, 'metadata', None)
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
def _display_chat_history():
|
| 67 |
"""Display all messages in the chat history."""
|
| 68 |
for message in st.session_state.messages:
|