bhavika24 commited on
Commit
c540963
·
verified ·
1 Parent(s): 36a91ff

Update engine.py

Browse files
Files changed (1) hide show
  1. engine.py +10 -41
engine.py CHANGED
@@ -279,10 +279,8 @@ def process_question(question):
279
  global LAST_PROMPT_TYPE, LAST_SUGGESTED_DATE
280
 
281
  q = question.strip().lower()
282
-
283
- # -------------------------------
284
  # Patient summary intent
285
- # -------------------------------
286
  if is_patient_summary_question(q):
287
  return {
288
  "status": "ok",
@@ -292,18 +290,16 @@ def process_question(question):
292
  "note": None
293
  }
294
 
295
- # -------------------------------
296
- # WHY follow-up handling
297
- # -------------------------------
298
  if is_why_question(q) and LAST_PROMPT_TYPE == "NO_DATA":
299
- year = LAST_SUGGESTED_DATE[:4] if LAST_SUGGESTED_DATE else "the latest available year"
300
  return {
301
  "status": "ok",
302
  "message": (
303
- f"I suggested **{year}** because that’s the most recent year "
304
  f"for which data exists in the system.\n\n"
305
- "Your database doesn’t contain newer records yet.\n\n"
306
- "You can explore:\n"
 
307
  "• Data from 2021\n"
308
  "• Trends over time\n"
309
  "• Patient summaries"
@@ -311,9 +307,7 @@ def process_question(question):
311
  "data": []
312
  }
313
 
314
- # -------------------------------
315
- # YES / confirmation handling
316
- # -------------------------------
317
  if is_confirmation(q) and LAST_PROMPT_TYPE == "NO_DATA":
318
  return {
319
  "status": "ok",
@@ -328,19 +322,9 @@ def process_question(question):
328
  "data": []
329
  }
330
 
331
- # -------------------------------
332
- # Normalize question
333
- # -------------------------------
334
  question = correct_spelling(question)
335
  question = normalize_time_question(question)
336
 
337
- # Reset state once user asks a fresh question
338
- LAST_PROMPT_TYPE = None
339
- LAST_SUGGESTED_DATE = None
340
-
341
- # -------------------------------
342
- # Metadata queries
343
- # -------------------------------
344
  if any(x in question for x in ["what data", "what tables", "which data"]):
345
  return {
346
  "status": "ok",
@@ -348,9 +332,6 @@ def process_question(question):
348
  "data": []
349
  }
350
 
351
- # -------------------------------
352
- # Unsupported questions
353
- # -------------------------------
354
  unsupported = get_unsupported_reason(question)
355
  if unsupported:
356
  return {
@@ -366,9 +347,6 @@ def process_question(question):
366
  "data": []
367
  }
368
 
369
- # -------------------------------
370
- # LLM → SQL
371
- # -------------------------------
372
  sql = call_llm(build_prompt(question))
373
 
374
  if sql == "NOT_ANSWERABLE":
@@ -381,13 +359,9 @@ def process_question(question):
381
  sql = validate_sql(sanitize_sql(sql))
382
  cols, rows = run_query(sql)
383
 
384
- # -------------------------------
385
- # No data (aggregate case)
386
- # -------------------------------
387
  if is_aggregate_only_query(sql) and not has_underlying_data(sql):
388
  LAST_PROMPT_TYPE = "NO_DATA"
389
  LAST_SUGGESTED_DATE = get_latest_data_date()
390
-
391
  return {
392
  "status": "ok",
393
  "message": friendly("No data is available for that time period."),
@@ -396,13 +370,9 @@ def process_question(question):
396
  "sql": None
397
  }
398
 
399
- # -------------------------------
400
- # Empty result set
401
- # -------------------------------
402
  if not rows:
403
  LAST_PROMPT_TYPE = "NO_DATA"
404
  LAST_SUGGESTED_DATE = get_latest_data_date()
405
-
406
  return {
407
  "status": "ok",
408
  "message": friendly("No records found."),
@@ -410,13 +380,12 @@ def process_question(question):
410
  "data": []
411
  }
412
 
413
- # -------------------------------
414
- # Successful response
415
- # -------------------------------
416
  return {
417
  "status": "ok",
418
  "sql": sql,
419
  "columns": cols,
420
  "data": rows
421
  }
422
-
 
279
  global LAST_PROMPT_TYPE, LAST_SUGGESTED_DATE
280
 
281
  q = question.strip().lower()
282
+
 
283
  # Patient summary intent
 
284
  if is_patient_summary_question(q):
285
  return {
286
  "status": "ok",
 
290
  "note": None
291
  }
292
 
293
+ # Handle WHY questions
 
 
294
  if is_why_question(q) and LAST_PROMPT_TYPE == "NO_DATA":
 
295
  return {
296
  "status": "ok",
297
  "message": (
298
+ f"I suggested **{LAST_SUGGESTED_DATE[:4]}** because that’s the most recent year "
299
  f"for which data exists in the system.\n\n"
300
+ "Your database doesn’t contain newer records, so questions like "
301
+ "“today” or “this year” can’t be answered yet.\n\n"
302
+ "You can still explore:\n"
303
  "• Data from 2021\n"
304
  "• Trends over time\n"
305
  "• Patient summaries"
 
307
  "data": []
308
  }
309
 
310
+ # Handle confirmations
 
 
311
  if is_confirmation(q) and LAST_PROMPT_TYPE == "NO_DATA":
312
  return {
313
  "status": "ok",
 
322
  "data": []
323
  }
324
 
 
 
 
325
  question = correct_spelling(question)
326
  question = normalize_time_question(question)
327
 
 
 
 
 
 
 
 
328
  if any(x in question for x in ["what data", "what tables", "which data"]):
329
  return {
330
  "status": "ok",
 
332
  "data": []
333
  }
334
 
 
 
 
335
  unsupported = get_unsupported_reason(question)
336
  if unsupported:
337
  return {
 
347
  "data": []
348
  }
349
 
 
 
 
350
  sql = call_llm(build_prompt(question))
351
 
352
  if sql == "NOT_ANSWERABLE":
 
359
  sql = validate_sql(sanitize_sql(sql))
360
  cols, rows = run_query(sql)
361
 
 
 
 
362
  if is_aggregate_only_query(sql) and not has_underlying_data(sql):
363
  LAST_PROMPT_TYPE = "NO_DATA"
364
  LAST_SUGGESTED_DATE = get_latest_data_date()
 
365
  return {
366
  "status": "ok",
367
  "message": friendly("No data is available for that time period."),
 
370
  "sql": None
371
  }
372
 
 
 
 
373
  if not rows:
374
  LAST_PROMPT_TYPE = "NO_DATA"
375
  LAST_SUGGESTED_DATE = get_latest_data_date()
 
376
  return {
377
  "status": "ok",
378
  "message": friendly("No records found."),
 
380
  "data": []
381
  }
382
 
383
+ LAST_PROMPT_TYPE = None
384
+ LAST_SUGGESTED_DATE = None
385
+
386
  return {
387
  "status": "ok",
388
  "sql": sql,
389
  "columns": cols,
390
  "data": rows
391
  }