cryogenic22 commited on
Commit
0120103
·
verified ·
1 Parent(s): cc2b215

Update components/chat.py

Browse files
Files changed (1) hide show
  1. components/chat.py +72 -49
components/chat.py CHANGED
@@ -4,29 +4,71 @@ from utils.database import verify_vector_store
4
 
5
  def clean_response_content(response_content):
6
  """Clean the response content of any technical metadata."""
7
- # Convert to string if it's an AIMessage
8
- response_content = str(response_content)
9
 
10
- # Remove the content= prefix if it exists
11
- if "content='" in response_content:
12
- response_content = response_content.split("content='")[1]
13
-
14
- # Remove response metadata
15
- if "response_metadata=" in response_content:
16
- response_content = response_content.split("response_metadata=")[0]
17
-
18
- # Remove any trailing quotes or metadata artifacts
19
- response_content = response_content.rstrip("' ")
20
-
21
- # Replace escaped newlines with actual newlines
22
- response_content = response_content.replace('\\n', '\n')
23
-
24
- return response_content
25
 
26
  def display_chat_interface():
27
  """Display modern chat interface with clean formatting."""
28
 
29
- # [Previous CSS styling remains the same...]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Check if QA system is initialized
32
  if 'qa_system' not in st.session_state or st.session_state.qa_system is None:
@@ -46,32 +88,13 @@ def display_chat_interface():
46
  </div>
47
  """, unsafe_allow_html=True)
48
  elif isinstance(message, AIMessage):
49
- # Clean and structure the response
50
- response_content = clean_response_content(message.content)
51
-
52
- # Format the content into sections
53
- formatted_content = ""
54
- sections = response_content.split('\n\n')
55
-
56
- for section in sections:
57
- if section.startswith('Summary:'):
58
- formatted_content += f'<div class="summary-section">{section}</div>'
59
- elif section.startswith('Key Points:'):
60
- formatted_content += '<div class="key-points">'
61
- formatted_content += f'<div class="section-header">{section.split(":")[0]}:</div>'
62
- points = [p.strip() for p in section.split('\n')[1:] if p.strip()]
63
- for point in points:
64
- formatted_content += f'<div class="key-point-item">{point.lstrip("- ")}</div>'
65
- formatted_content += '</div>'
66
- elif section.startswith('Source'):
67
- formatted_content += f'<div class="source-info">{section}</div>'
68
- else:
69
- formatted_content += f'<div class="content-section">{section}</div>'
70
 
71
  st.markdown(f"""
72
  <div class="assistant-message">
73
  🤖 <strong>Assistant:</strong><br>
74
- {formatted_content}
75
  </div>
76
  """, unsafe_allow_html=True)
77
 
@@ -84,16 +107,15 @@ def display_chat_interface():
84
  st.session_state.messages.append(human_message)
85
 
86
  # Get response
87
- response = st.session_state.qa_system.invoke(
88
- {
89
- "input": prompt,
90
- "chat_history": st.session_state.messages
91
- } # Missing comma here!
92
- )
93
 
94
  if response:
95
- response_str = str(response)
96
- ai_message = AIMessage(content=response_str)
 
97
  st.session_state.messages.append(ai_message)
98
  st.rerun()
99
  else:
@@ -102,4 +124,5 @@ def display_chat_interface():
102
  except Exception as e:
103
  st.error(f"Error: {e}")
104
  import traceback
105
- st.error(traceback.format_exc())
 
 
4
 
5
  def clean_response_content(response_content):
6
  """Clean the response content of any technical metadata."""
7
+ # Convert the response to string first
8
+ content = str(response_content)
9
 
10
+ # Clean up the content
11
+ if "content='" in content:
12
+ content = content.split("content='")[1].split("'")[0]
13
+ elif 'content="' in content:
14
+ content = content.split('content="')[1].split('"')[0]
15
+
16
+ return content.replace('\\n', '\n')
 
 
 
 
 
 
 
 
17
 
18
  def display_chat_interface():
19
  """Display modern chat interface with clean formatting."""
20
 
21
+ # Add custom CSS for modern chat styling
22
+ st.markdown("""
23
+ <style>
24
+ /* Clean, modern chat container */
25
+ .chat-container {
26
+ max-width: 800px;
27
+ margin: auto;
28
+ }
29
+
30
+ /* Message styling */
31
+ .user-message {
32
+ background-color: #f0f2f6;
33
+ padding: 1rem;
34
+ border-radius: 10px;
35
+ margin: 1rem 0;
36
+ }
37
+
38
+ .assistant-message {
39
+ background-color: #ffffff;
40
+ border: 1px solid #e0e0e0;
41
+ padding: 1.5rem;
42
+ border-radius: 10px;
43
+ margin: 1rem 0;
44
+ }
45
+
46
+ /* Section styling */
47
+ .section-header {
48
+ font-weight: bold;
49
+ color: #0f52ba;
50
+ margin-top: 1rem;
51
+ }
52
+
53
+ .source-info {
54
+ background-color: #f8f9fa;
55
+ padding: 0.5rem;
56
+ border-left: 3px solid #0f52ba;
57
+ margin-top: 1rem;
58
+ font-size: 0.9rem;
59
+ }
60
+
61
+ /* Content sections */
62
+ .content-section {
63
+ margin: 0.5rem 0;
64
+ }
65
+
66
+ /* Clean bullets */
67
+ .clean-bullet {
68
+ margin: 0.3rem 0;
69
+ }
70
+ </style>
71
+ """, unsafe_allow_html=True)
72
 
73
  # Check if QA system is initialized
74
  if 'qa_system' not in st.session_state or st.session_state.qa_system is None:
 
88
  </div>
89
  """, unsafe_allow_html=True)
90
  elif isinstance(message, AIMessage):
91
+ # Get the content and clean it
92
+ content = clean_response_content(message.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  st.markdown(f"""
95
  <div class="assistant-message">
96
  🤖 <strong>Assistant:</strong><br>
97
+ {content}
98
  </div>
99
  """, unsafe_allow_html=True)
100
 
 
107
  st.session_state.messages.append(human_message)
108
 
109
  # Get response
110
+ response = st.session_state.qa_system.invoke({
111
+ "input": prompt,
112
+ "chat_history": st.session_state.messages
113
+ })
 
 
114
 
115
  if response:
116
+ # Clean the response before creating the message
117
+ cleaned_response = clean_response_content(response)
118
+ ai_message = AIMessage(content=cleaned_response)
119
  st.session_state.messages.append(ai_message)
120
  st.rerun()
121
  else:
 
124
  except Exception as e:
125
  st.error(f"Error: {e}")
126
  import traceback
127
+ st.error(traceback.format_exc())
128
+