Spaces:
Sleeping
Sleeping
Upload engine.py
Browse files
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 |
-
"""
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
| 305 |
return None
|
| 306 |
|
| 307 |
|
| 308 |
-
|
|
|
|
| 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
|