bhavika24 commited on
Commit
2c656c5
·
verified ·
1 Parent(s): 875a655

Upload engine.py

Browse files
Files changed (1) hide show
  1. engine.py +32 -27
engine.py CHANGED
@@ -5,8 +5,9 @@ from openai import OpenAI
5
  from difflib import get_close_matches
6
  from datetime import datetime
7
 
8
- TRANSCRIPT = []
9
 
 
10
  def log_interaction(user_q, sql=None, result=None, error=None):
11
  TRANSCRIPT.append({
12
  "timestamp": datetime.utcnow().isoformat(),
@@ -93,7 +94,7 @@ def correct_spelling(q):
93
  # =========================
94
  import json
95
  from functools import lru_cache
96
- def col_desc(desc):
97
  """Safely extract column description from metadata."""
98
  if isinstance(desc, dict):
99
  return desc.get("description", "")
@@ -102,6 +103,7 @@ def col_desc(desc):
102
 
103
  @lru_cache(maxsize=1)
104
  def load_ai_schema():
 
105
  """Load schema from metadata JSON file with error handling."""
106
  try:
107
  with open("metadata.json", "r") as f:
@@ -241,7 +243,7 @@ def extract_relevant_tables(question, max_tables=4):
241
  # HUMAN SCHEMA DESCRIPTION
242
  # =========================
243
 
244
- def describe_schema(max_tables=10):
245
  schema = load_ai_schema()
246
  total_tables = len(schema)
247
 
@@ -279,33 +281,36 @@ def describe_schema(max_tables=10):
279
  # =========================
280
 
281
  def get_latest_data_date():
282
- """Get the latest data date by checking tables with date columns."""
283
- schema = load_ai_schema()
284
-
285
- # Common date column names to check
286
- date_columns = ["date", "start_date", "end_date", "admission_date", "admittime", "dischtime", "created_at", "updated_at"]
287
-
288
- # Try to find a table with a date column
289
- for table_name in schema.keys():
290
- columns = schema[table_name].get("columns", {})
291
-
292
- # Check if table has any date-like column
293
- for col_name in columns.keys():
294
- col_lower = col_name.lower()
295
- if any(date_col in col_lower for date_col in date_columns):
296
- try:
297
- result = conn.execute(
298
- f"SELECT MAX({col_name}) FROM {table_name}"
299
- ).fetchone()
300
- if result and result[0]:
301
- return result[0]
302
- except (sqlite3.Error, sqlite3.OperationalError):
303
- continue # Try next table/column
304
-
 
 
305
  return None
306
 
307
 
308
- def normalize_time_question(q):
 
309
  latest = get_latest_data_date()
310
  if not latest:
311
  return q
 
5
  from difflib import get_close_matches
6
  from datetime import datetime
7
 
8
+ TRANSCRIPT = [] #memory log
9
 
10
+ #store interaction in transcript
11
  def log_interaction(user_q, sql=None, result=None, error=None):
12
  TRANSCRIPT.append({
13
  "timestamp": datetime.utcnow().isoformat(),
 
94
  # =========================
95
  import json
96
  from functools import lru_cache
97
+ def col_desc(desc):#extract description
98
  """Safely extract column description from metadata."""
99
  if isinstance(desc, dict):
100
  return desc.get("description", "")
 
103
 
104
  @lru_cache(maxsize=1)
105
  def load_ai_schema():
106
+ #load metadata
107
  """Load schema from metadata JSON file with error handling."""
108
  try:
109
  with open("metadata.json", "r") as f:
 
243
  # HUMAN SCHEMA DESCRIPTION
244
  # =========================
245
 
246
+ def describe_schema(max_tables=10):#what data you have or which table exist
247
  schema = load_ai_schema()
248
  total_tables = len(schema)
249
 
 
281
  # =========================
282
 
283
  def get_latest_data_date():
284
+ """
285
+ Returns the most meaningful 'latest date' for the system.
286
+ Priority:
287
+ 1. admissions.admittime
288
+ 2. icustays.intime
289
+ 3. chartevents.charttime
290
+ """
291
+
292
+ checks = [
293
+ ("admissions", "admittime"),
294
+ ("icustays", "intime"),
295
+ ("chartevents", "charttime"),
296
+ ]
297
+
298
+ for table, column in checks:
299
+ try:
300
+ result = conn.execute(
301
+ f"SELECT MAX({column}) FROM {table}"
302
+ ).fetchone()
303
+
304
+ if result and result[0]:
305
+ return result[0]
306
+ except Exception:
307
+ continue
308
+
309
  return None
310
 
311
 
312
+
313
+ def normalize_time_question(q):#total-actual date
314
  latest = get_latest_data_date()
315
  if not latest:
316
  return q