Spaces:
Running
Running
update conversation logging
Browse files- src/app.py +32 -7
- src/feedback_utils.py +55 -0
src/app.py
CHANGED
|
@@ -140,6 +140,17 @@ with st.sidebar:
|
|
| 140 |
st.sidebar.caption(f"LLM: {selected_model}")
|
| 141 |
st.sidebar.caption(f"Client: {client_type.upper()}")
|
| 142 |
st.sidebar.caption(f"Session ID: {st.session_state.get('client_key', 'None')[:20]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
# Show title and description
|
| 145 |
st.title("π¬ Depression Assistant Chatbot")
|
|
@@ -255,12 +266,6 @@ for idx, message in enumerate(st.session_state.messages):
|
|
| 255 |
st.error("β Failed to save feedback. Please check your Google Sheets configuration.")
|
| 256 |
else:
|
| 257 |
st.warning("β οΈ Please select at least one rating before submitting feedback.")
|
| 258 |
-
|
| 259 |
-
# Show Google Sheets connection status
|
| 260 |
-
if st.session_state.feedback_manager.is_connected():
|
| 261 |
-
pass
|
| 262 |
-
else:
|
| 263 |
-
st.caption("β οΈ Google Sheets not configured - feedback will not be saved")
|
| 264 |
else:
|
| 265 |
st.success("β
Feedback already submitted for this response")
|
| 266 |
|
|
@@ -315,7 +320,8 @@ if user_input := st.chat_input("Ask me questions about the CANMAT depression gui
|
|
| 315 |
Rag.llm_client = original_client
|
| 316 |
|
| 317 |
t1 = time.perf_counter()
|
| 318 |
-
|
|
|
|
| 319 |
print(f"============== Finish R-A-Generation for Current Query {user_input} ==============")
|
| 320 |
|
| 321 |
# Save the response and sources
|
|
@@ -325,6 +331,25 @@ if user_input := st.chat_input("Ask me questions about the CANMAT depression gui
|
|
| 325 |
message_idx = len(st.session_state.messages) - 1
|
| 326 |
st.session_state.message_sources[message_idx] = results
|
| 327 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
st.rerun()
|
| 329 |
|
| 330 |
except Exception as e:
|
|
|
|
| 140 |
st.sidebar.caption(f"LLM: {selected_model}")
|
| 141 |
st.sidebar.caption(f"Client: {client_type.upper()}")
|
| 142 |
st.sidebar.caption(f"Session ID: {st.session_state.get('client_key', 'None')[:20]}...")
|
| 143 |
+
|
| 144 |
+
# Google Sheets status
|
| 145 |
+
if st.session_state.feedback_manager.is_connected():
|
| 146 |
+
st.sidebar.success("π Google Sheets: Connected")
|
| 147 |
+
# Check if conversation logging is available
|
| 148 |
+
if hasattr(st.session_state.feedback_manager, 'conversation_worksheet') and st.session_state.feedback_manager.conversation_worksheet:
|
| 149 |
+
st.sidebar.success("π Conversation Logging: Active")
|
| 150 |
+
else:
|
| 151 |
+
st.sidebar.warning("π Conversation Logging: Not Available")
|
| 152 |
+
else:
|
| 153 |
+
st.sidebar.error("π Google Sheets: Not Connected")
|
| 154 |
|
| 155 |
# Show title and description
|
| 156 |
st.title("π¬ Depression Assistant Chatbot")
|
|
|
|
| 266 |
st.error("β Failed to save feedback. Please check your Google Sheets configuration.")
|
| 267 |
else:
|
| 268 |
st.warning("β οΈ Please select at least one rating before submitting feedback.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
else:
|
| 270 |
st.success("β
Feedback already submitted for this response")
|
| 271 |
|
|
|
|
| 320 |
Rag.llm_client = original_client
|
| 321 |
|
| 322 |
t1 = time.perf_counter()
|
| 323 |
+
response_time = t1 - t0
|
| 324 |
+
print(f"[Time] Retriever + Generator takes: {response_time:.2f} seconds in total.")
|
| 325 |
print(f"============== Finish R-A-Generation for Current Query {user_input} ==============")
|
| 326 |
|
| 327 |
# Save the response and sources
|
|
|
|
| 331 |
message_idx = len(st.session_state.messages) - 1
|
| 332 |
st.session_state.message_sources[message_idx] = results
|
| 333 |
|
| 334 |
+
# Log conversation to Google Sheets
|
| 335 |
+
try:
|
| 336 |
+
current_embedder = get_current_model_info()["embedder_name"] if get_current_model_info() else "Unknown"
|
| 337 |
+
session_id = st.session_state.get('client_key', 'Unknown')
|
| 338 |
+
|
| 339 |
+
st.session_state.feedback_manager.log_conversation(
|
| 340 |
+
session_id=session_id,
|
| 341 |
+
user_query=user_input,
|
| 342 |
+
ai_response=collected,
|
| 343 |
+
embedder_model=current_embedder,
|
| 344 |
+
llm_model=selected_model,
|
| 345 |
+
temperature=temperature,
|
| 346 |
+
top_p=top_p,
|
| 347 |
+
max_length=max_length,
|
| 348 |
+
response_time=response_time
|
| 349 |
+
)
|
| 350 |
+
except Exception as log_error:
|
| 351 |
+
print(f"Warning: Failed to log conversation to Google Sheets: {log_error}")
|
| 352 |
+
|
| 353 |
st.rerun()
|
| 354 |
|
| 355 |
except Exception as e:
|
src/feedback_utils.py
CHANGED
|
@@ -71,9 +71,30 @@ class FeedbackManager:
|
|
| 71 |
]
|
| 72 |
self.worksheet.append_row(headers)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
st.error(f"β Failed to initialize Google Sheets: {str(e)}")
|
| 76 |
self.client = None
|
|
|
|
| 77 |
|
| 78 |
def save_feedback(self, user_query, ai_response, source_rating, answer_rating,
|
| 79 |
feedback_q1,
|
|
@@ -114,3 +135,37 @@ class FeedbackManager:
|
|
| 114 |
def is_connected(self):
|
| 115 |
"""Check if Google Sheets connection is available"""
|
| 116 |
return self.client is not None and self.worksheet is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
]
|
| 72 |
self.worksheet.append_row(headers)
|
| 73 |
|
| 74 |
+
# Get or create the conversation log worksheet
|
| 75 |
+
try:
|
| 76 |
+
self.conversation_worksheet = self.sheet.worksheet("Conversations")
|
| 77 |
+
except gspread.WorksheetNotFound:
|
| 78 |
+
# Create the worksheet with headers
|
| 79 |
+
self.conversation_worksheet = self.sheet.add_worksheet(title="Conversations", rows="1000", cols="10")
|
| 80 |
+
headers = [
|
| 81 |
+
"Timestamp",
|
| 82 |
+
"Session ID",
|
| 83 |
+
"User Query",
|
| 84 |
+
"AI Response",
|
| 85 |
+
"Embedder Model",
|
| 86 |
+
"LLM Model",
|
| 87 |
+
"Temperature",
|
| 88 |
+
"Top P",
|
| 89 |
+
"Max Length",
|
| 90 |
+
"Response Time (seconds)"
|
| 91 |
+
]
|
| 92 |
+
self.conversation_worksheet.append_row(headers)
|
| 93 |
+
|
| 94 |
except Exception as e:
|
| 95 |
st.error(f"β Failed to initialize Google Sheets: {str(e)}")
|
| 96 |
self.client = None
|
| 97 |
+
self.conversation_worksheet = None
|
| 98 |
|
| 99 |
def save_feedback(self, user_query, ai_response, source_rating, answer_rating,
|
| 100 |
feedback_q1,
|
|
|
|
| 135 |
def is_connected(self):
|
| 136 |
"""Check if Google Sheets connection is available"""
|
| 137 |
return self.client is not None and self.worksheet is not None
|
| 138 |
+
|
| 139 |
+
def log_conversation(self, session_id, user_query, ai_response,
|
| 140 |
+
embedder_model, llm_model, temperature, top_p, max_length, response_time):
|
| 141 |
+
"""Log conversation data to Google Sheets"""
|
| 142 |
+
if not hasattr(self, 'conversation_worksheet') or not self.conversation_worksheet:
|
| 143 |
+
print("β οΈ Google Sheets conversation logging not initialized. Skipping log.")
|
| 144 |
+
return False
|
| 145 |
+
|
| 146 |
+
try:
|
| 147 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 148 |
+
|
| 149 |
+
# Truncate long responses to fit in Google Sheets cells
|
| 150 |
+
user_query_truncated = user_query[:500] + "..." if len(user_query) > 500 else user_query
|
| 151 |
+
ai_response_truncated = ai_response[:1000] + "..." if len(ai_response) > 1000 else ai_response
|
| 152 |
+
|
| 153 |
+
row = [
|
| 154 |
+
timestamp,
|
| 155 |
+
session_id,
|
| 156 |
+
user_query_truncated,
|
| 157 |
+
ai_response_truncated,
|
| 158 |
+
embedder_model,
|
| 159 |
+
llm_model,
|
| 160 |
+
temperature,
|
| 161 |
+
top_p,
|
| 162 |
+
max_length,
|
| 163 |
+
round(response_time, 2)
|
| 164 |
+
]
|
| 165 |
+
|
| 166 |
+
self.conversation_worksheet.append_row(row)
|
| 167 |
+
return True
|
| 168 |
+
|
| 169 |
+
except Exception as e:
|
| 170 |
+
print(f"β Failed to log conversation: {str(e)}")
|
| 171 |
+
return False
|