cryogenic22 commited on
Commit
15941ed
·
verified ·
1 Parent(s): 84a3737

Update components/chat.py

Browse files
Files changed (1) hide show
  1. components/chat.py +21 -9
components/chat.py CHANGED
@@ -1,6 +1,7 @@
1
  # components/chat.py
2
  import streamlit as st
3
- from langchain_core.messages import HumanMessage, AIMessage
 
4
 
5
  def display_chat_interface():
6
  """Display chat interface component"""
@@ -8,30 +9,41 @@ def display_chat_interface():
8
 
9
  # Initialize chat history if not exists
10
  if 'chat_history' not in st.session_state:
11
- st.session_state.chat_history = []
 
 
12
 
13
  # Display chat history
14
  for message in st.session_state.chat_history:
15
  if isinstance(message, HumanMessage):
16
  st.markdown(f"🧑‍💼 **You**: {message.content}")
17
- else:
18
  st.markdown(f"🤖 **Assistant**: {message.content}")
 
19
 
20
  # Chat input
21
  if prompt := st.chat_input("Ask about the RFPs..."):
22
  try:
23
  with st.spinner("Analyzing documents..."):
24
- # Use the correct input format
 
 
 
 
 
 
 
25
  result = st.session_state.qa_system({
26
- "input": prompt, # Changed from "question" to "input"
 
27
  })
28
 
29
  if result and 'answer' in result:
30
- # Add messages to chat history
31
- st.session_state.chat_history.append(HumanMessage(content=prompt))
32
- st.session_state.chat_history.append(AIMessage(content=result['answer']))
33
 
34
- # Force refresh to show new messages
35
  st.rerun()
36
  else:
37
  st.error("No valid response received from the QA system")
 
1
  # components/chat.py
2
  import streamlit as st
3
+ from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
4
+ from utils.database import format_chat_history # Import the formatting function
5
 
6
  def display_chat_interface():
7
  """Display chat interface component"""
 
9
 
10
  # Initialize chat history if not exists
11
  if 'chat_history' not in st.session_state:
12
+ st.session_state.chat_history = [
13
+ SystemMessage(content="Chat started. You can ask questions about the RFP documents.")
14
+ ]
15
 
16
  # Display chat history
17
  for message in st.session_state.chat_history:
18
  if isinstance(message, HumanMessage):
19
  st.markdown(f"🧑‍💼 **You**: {message.content}")
20
+ elif isinstance(message, AIMessage):
21
  st.markdown(f"🤖 **Assistant**: {message.content}")
22
+ # Don't display system messages in the UI
23
 
24
  # Chat input
25
  if prompt := st.chat_input("Ask about the RFPs..."):
26
  try:
27
  with st.spinner("Analyzing documents..."):
28
+ # Add the new message to chat history
29
+ current_msg = HumanMessage(content=prompt)
30
+ st.session_state.chat_history.append(current_msg)
31
+
32
+ # Format the chat history properly
33
+ formatted_history = format_chat_history(st.session_state.chat_history[:-1])
34
+
35
+ # Make the query with properly formatted input
36
  result = st.session_state.qa_system({
37
+ "input": prompt,
38
+ "chat_history": formatted_history
39
  })
40
 
41
  if result and 'answer' in result:
42
+ # Add AI response to chat history
43
+ ai_msg = AIMessage(content=result['answer'])
44
+ st.session_state.chat_history.append(ai_msg)
45
 
46
+ # Rerun to update the display
47
  st.rerun()
48
  else:
49
  st.error("No valid response received from the QA system")