cryogenic22 commited on
Commit
4534bd6
·
verified ·
1 Parent(s): 04d1254

Update components/chat.py

Browse files
Files changed (1) hide show
  1. components/chat.py +32 -14
components/chat.py CHANGED
@@ -13,24 +13,42 @@ from utils.response_formatter import EnhancedResponseFormatter, display_enhanced
13
  qa_lock = Lock()
14
 
15
  def display_chat_interface():
16
- """
17
- Display a modern chat interface with error handling and clean formatting.
18
- Manages conversation state and handles document context.
19
- """
20
- # Verify QA system initialization
21
- if not _verify_chat_ready():
22
- return
23
-
24
- # Initialize chat history if needed
25
- if 'messages' not in st.session_state:
26
  st.session_state.messages = []
27
 
28
- # Display existing chat history
29
- _display_chat_history()
 
 
 
 
 
 
30
 
31
- # Handle new user input
32
  if prompt := st.chat_input("Ask about your documents..."):
33
- _process_user_message(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  def _verify_chat_ready() -> bool:
36
  """Check if the chat system is properly initialized."""
 
13
  qa_lock = Lock()
14
 
15
  def display_chat_interface():
16
+ """Display chat interface with proper formatting."""
17
+ if not st.session_state.get('messages'):
 
 
 
 
 
 
 
 
18
  st.session_state.messages = []
19
 
20
+ # Display chat messages
21
+ for message in st.session_state.messages:
22
+ if isinstance(message, HumanMessage):
23
+ with st.chat_message("user"):
24
+ st.write(message.content)
25
+ elif isinstance(message, AIMessage):
26
+ with st.chat_message("assistant"):
27
+ display_enhanced_response(message.content, message.additional_kwargs.get('sources'))
28
 
29
+ # Chat input
30
  if prompt := st.chat_input("Ask about your documents..."):
31
+ with st.chat_message("user"):
32
+ st.write(prompt)
33
+ st.session_state.messages.append(HumanMessage(content=prompt))
34
+
35
+ with st.chat_message("assistant"):
36
+ with st.spinner("Analyzing documents..."):
37
+ response = st.session_state.qa_system.invoke({
38
+ "input": prompt,
39
+ "chat_history": st.session_state.messages
40
+ })
41
+
42
+ if response:
43
+ ai_message = AIMessage(
44
+ content=str(response),
45
+ additional_kwargs={'sources': response.metadata.get('sources', [])}
46
+ )
47
+ st.session_state.messages.append(ai_message)
48
+ display_enhanced_response(
49
+ str(response),
50
+ response.metadata.get('sources', [])
51
+ )
52
 
53
  def _verify_chat_ready() -> bool:
54
  """Check if the chat system is properly initialized."""