saim1309 commited on
Commit
54fe647
·
verified ·
1 Parent(s): eb656a6

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +110 -3
database.py CHANGED
@@ -92,10 +92,32 @@ def log_question(
92
  except sqlite3.OperationalError as e:
93
  # Fallback for older schema versions (shouldn't happen after migration)
94
  print(f"⚠️ Logging error: {e}. Falling back to basic logging.")
95
- cur.execute("INSERT INTO question_logs (question) VALUES (?)", (question,))
96
-
97
  conn.commit()
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  def get_session_state(session_id: str) -> Dict[str, Any]:
100
  """Get session state from DB"""
101
  with get_db_connection() as conn:
@@ -165,4 +187,89 @@ def update_session_state(session_id: str, preference: str = None, increment_coun
165
  VALUES (?, ?, ?, ?, ?)
166
  """, (session_id, new_pref, new_count, new_clarification, new_knowledge_json))
167
 
168
- conn.commit()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  except sqlite3.OperationalError as e:
93
  # Fallback for older schema versions (shouldn't happen after migration)
94
  print(f"⚠️ Logging error: {e}. Falling back to basic logging.")
 
 
95
  conn.commit()
96
 
97
+ def get_recent_history(session_id: str, limit: int = 5) -> List[Dict[str, str]]:
98
+ """Retrieve recent conversation history for a session from the logs."""
99
+ if not session_id:
100
+ return []
101
+
102
+ with get_db_connection() as conn:
103
+ cur = conn.cursor()
104
+ cur.execute("""
105
+ SELECT question, answer
106
+ FROM question_logs
107
+ WHERE session_id = ?
108
+ AND answer IS NOT NULL
109
+ ORDER BY timestamp DESC
110
+ LIMIT ?
111
+ """, (session_id, limit))
112
+ rows = cur.fetchall()
113
+
114
+ # Reverse to get chronological order
115
+ history = []
116
+ for row in reversed(rows):
117
+ history.append({"role": "user", "content": row['question']})
118
+ history.append({"role": "assistant", "content": row['answer']})
119
+ return history
120
+
121
  def get_session_state(session_id: str) -> Dict[str, Any]:
122
  """Get session state from DB"""
123
  with get_db_connection() as conn:
 
187
  VALUES (?, ?, ?, ?, ?)
188
  """, (session_id, new_pref, new_count, new_clarification, new_knowledge_json))
189
 
190
+ conn.commit()
191
+
192
+ def update_faq_entry(faq_id: int, question: str, answer: str):
193
+ """Update an existing FAQ entry."""
194
+ with get_db_connection() as conn:
195
+ cur = conn.cursor()
196
+ cur.execute(
197
+ "UPDATE faq_entries SET question = ?, answer = ?, embedding = NULL WHERE id = ?",
198
+ (question, answer, faq_id)
199
+ )
200
+ conn.commit()
201
+
202
+ def delete_faq_entry(faq_id: int):
203
+ """Delete an FAQ entry."""
204
+ with get_db_connection() as conn:
205
+ cur = conn.cursor()
206
+ cur.execute("DELETE FROM faq_entries WHERE id = ?", (faq_id,))
207
+ conn.commit()
208
+
209
+ def add_faq_entry(question: str, answer: str):
210
+ """Add a new FAQ entry."""
211
+ with get_db_connection() as conn:
212
+ cur = conn.cursor()
213
+ cur.execute(
214
+ "INSERT INTO faq_entries (question, answer) VALUES (?, ?)",
215
+ (question, answer)
216
+ )
217
+ conn.commit()
218
+
219
+ def bulk_update_faqs(entries: List[Dict[str, str]]):
220
+ """Bulk update FAQs from a list of dictionaries."""
221
+ with get_db_connection() as conn:
222
+ cur = conn.cursor()
223
+ for entry in entries:
224
+ question = entry.get('Question') or entry.get('question')
225
+ answer = entry.get('Answer') or entry.get('answer')
226
+ if question and answer:
227
+ cur.execute(
228
+ "INSERT INTO faq_entries (question, answer) VALUES (?, ?)",
229
+ (question, answer)
230
+ )
231
+ conn.commit()
232
+
233
+ def bulk_update_podcasts(entries: List[Dict[str, str]]):
234
+ """Bulk update Podcasts from a list of dictionaries."""
235
+ with get_db_connection() as conn:
236
+ cur = conn.cursor()
237
+ for entry in entries:
238
+ guest = entry.get('Guest Name') or entry.get('guest_name')
239
+ url = entry.get('YouTube URL') or entry.get('youtube_url')
240
+ summary = entry.get('Summary') or entry.get('summary')
241
+ if guest and url and summary:
242
+ # Format full_text as required by the existing logic
243
+ full_text = f"Guest: {guest}. Summary: {summary}"
244
+ # Store summary in highlight_json as a simple list for compatibility
245
+ h_json = json.dumps([{"summary": summary}])
246
+ cur.execute(
247
+ "INSERT INTO podcast_episodes (guest_name, youtube_url, highlight_json, full_text) VALUES (?, ?, ?, ?)",
248
+ (guest, url, h_json, full_text)
249
+ )
250
+ conn.commit()
251
+
252
+ def fetch_all_podcast_metadata() -> List[Dict[str, Any]]:
253
+ """Fetch all podcast metadata for the admin table."""
254
+ with get_db_connection() as conn:
255
+ cur = conn.cursor()
256
+ cur.execute("SELECT id, guest_name, youtube_url, highlight_json FROM podcast_episodes")
257
+ rows = cur.fetchall()
258
+ results = []
259
+ for row in rows:
260
+ d = dict(row)
261
+ # Try to extract plain summary from JSON for the table
262
+ try:
263
+ h = json.loads(d['highlight_json'])
264
+ d['summary'] = h[0]['summary'] if h and isinstance(h, list) else d['highlight_json']
265
+ except:
266
+ d['summary'] = d['highlight_json']
267
+ results.append(d)
268
+ return results
269
+
270
+ def fetch_all_faq_metadata() -> List[Dict[str, Any]]:
271
+ """Fetch all FAQ metadata for the admin table."""
272
+ with get_db_connection() as conn:
273
+ cur = conn.cursor()
274
+ cur.execute("SELECT id, question, answer FROM faq_entries")
275
+ return [dict(row) for row in cur.fetchall()]