Spaces:
Sleeping
Sleeping
Upload engine.py
Browse files
engine.py
CHANGED
|
@@ -171,13 +171,19 @@ def extract_relevant_tables(question, max_tables=4):
|
|
| 171 |
score += 5
|
| 172 |
|
| 173 |
# 5️⃣ Only add if meets minimum threshold (prevents low-quality matches)
|
| 174 |
-
#
|
| 175 |
-
|
|
|
|
|
|
|
| 176 |
matched.append((table, score))
|
| 177 |
|
| 178 |
# Sort by relevance
|
| 179 |
matched.sort(key=lambda x: x[1], reverse=True)
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
return [t[0] for t in matched[:max_tables]]
|
| 182 |
|
| 183 |
|
|
@@ -317,14 +323,20 @@ def is_question_supported(question):
|
|
| 317 |
|
| 318 |
def build_prompt(question):
|
| 319 |
matched = extract_relevant_tables(question)
|
|
|
|
| 320 |
|
| 321 |
if matched:
|
| 322 |
-
schema = {t:
|
| 323 |
else:
|
| 324 |
-
# 🚫 Don't send all 100+ tables! Return a helpful error
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
raise ValueError(
|
| 326 |
-
"I couldn't find any relevant tables for your question
|
| 327 |
-
"
|
|
|
|
| 328 |
)
|
| 329 |
|
| 330 |
prompt = """
|
|
|
|
| 171 |
score += 5
|
| 172 |
|
| 173 |
# 5️⃣ Only add if meets minimum threshold (prevents low-quality matches)
|
| 174 |
+
# Use lower threshold for small schemas (more lenient)
|
| 175 |
+
# Increased threshold from 3 to 4 for better precision, but lower to 2 for small schemas
|
| 176 |
+
threshold = 2 if len(schema) <= 5 else 4
|
| 177 |
+
if score >= threshold:
|
| 178 |
matched.append((table, score))
|
| 179 |
|
| 180 |
# Sort by relevance
|
| 181 |
matched.sort(key=lambda x: x[1], reverse=True)
|
| 182 |
|
| 183 |
+
# If no matches but schema is very small, return all tables (with lower confidence)
|
| 184 |
+
if not matched and len(schema) <= 3:
|
| 185 |
+
return list(schema.keys())[:max_tables]
|
| 186 |
+
|
| 187 |
return [t[0] for t in matched[:max_tables]]
|
| 188 |
|
| 189 |
|
|
|
|
| 323 |
|
| 324 |
def build_prompt(question):
|
| 325 |
matched = extract_relevant_tables(question)
|
| 326 |
+
full_schema = load_ai_schema()
|
| 327 |
|
| 328 |
if matched:
|
| 329 |
+
schema = {t: full_schema[t] for t in matched}
|
| 330 |
else:
|
| 331 |
+
# 🚫 Don't send all 100+ tables! Return a helpful error with available tables
|
| 332 |
+
available_tables = list(full_schema.keys())[:10] # Show first 10 tables
|
| 333 |
+
tables_list = "\n".join(f"- {t}" for t in available_tables)
|
| 334 |
+
if len(full_schema) > 10:
|
| 335 |
+
tables_list += f"\n... and {len(full_schema) - 10} more tables"
|
| 336 |
raise ValueError(
|
| 337 |
+
f"I couldn't find any relevant tables for your question.\n\n"
|
| 338 |
+
f"Available tables:\n{tables_list}\n\n"
|
| 339 |
+
f"Please try mentioning a specific table name or use 'what data' to see all available tables."
|
| 340 |
)
|
| 341 |
|
| 342 |
prompt = """
|