Spaces:
Sleeping
Sleeping
Commit ·
353f65a
1
Parent(s): 2308eeb
fix: include sample values in SQL schema summary for better query generation
Browse filesThe LLM was generating SQL queries with wrong column values (e.g., guessing
country name format) because the schema summary only showed column names and
types. Now includes up to 3 sample values per column so the LLM can see
actual data format (e.g., Country: "China", "United States", "Brazil").
Before: - table (N rows): Country (TEXT), Year (INTEGER)
After: - table (N rows, from PTS/data.xlsx):
Country (TEXT) e.g. "China", "United States"
Year (INTEGER) e.g. "1976", "2000"
This fixes the issue where asking "PTS scores for China" would route to SQL
but generate queries that returned no results.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- src/sql_retriever.py +15 -5
src/sql_retriever.py
CHANGED
|
@@ -96,18 +96,28 @@ def _lookup_source_file(table_name: str, schema: dict) -> str:
|
|
| 96 |
def build_schema_summary(schema: dict) -> str:
|
| 97 |
"""Build a compact schema summary for injection into the QU prompt.
|
| 98 |
|
|
|
|
|
|
|
|
|
|
| 99 |
Format:
|
| 100 |
Available SQL tables:
|
| 101 |
-
- table_name (N rows
|
|
|
|
|
|
|
| 102 |
"""
|
| 103 |
if not schema:
|
| 104 |
return ""
|
| 105 |
|
| 106 |
lines = ["Available SQL tables:"]
|
| 107 |
for table_name, info in schema.items():
|
| 108 |
-
cols = ", ".join(
|
| 109 |
-
f"{c['name']} ({c['type']})" for c in info.get("columns", [])
|
| 110 |
-
)
|
| 111 |
row_count = info.get("row_count", 0)
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
return "\n".join(lines)
|
|
|
|
| 96 |
def build_schema_summary(schema: dict) -> str:
|
| 97 |
"""Build a compact schema summary for injection into the QU prompt.
|
| 98 |
|
| 99 |
+
Includes sample values so the LLM knows what data looks like
|
| 100 |
+
(e.g., country names, value formats) for correct WHERE clauses.
|
| 101 |
+
|
| 102 |
Format:
|
| 103 |
Available SQL tables:
|
| 104 |
+
- table_name (N rows, from source_file):
|
| 105 |
+
col1 (TYPE) e.g. "val1", "val2"
|
| 106 |
+
col2 (TYPE) e.g. "val1", "val2"
|
| 107 |
"""
|
| 108 |
if not schema:
|
| 109 |
return ""
|
| 110 |
|
| 111 |
lines = ["Available SQL tables:"]
|
| 112 |
for table_name, info in schema.items():
|
|
|
|
|
|
|
|
|
|
| 113 |
row_count = info.get("row_count", 0)
|
| 114 |
+
source = info.get("source_file", "")
|
| 115 |
+
lines.append(f"\n- {table_name} ({row_count} rows, from {source}):")
|
| 116 |
+
for c in info.get("columns", []):
|
| 117 |
+
samples = c.get("sample", [])
|
| 118 |
+
sample_str = ""
|
| 119 |
+
if samples:
|
| 120 |
+
quoted = ", ".join(f'"{s}"' for s in samples[:3])
|
| 121 |
+
sample_str = f" e.g. {quoted}"
|
| 122 |
+
lines.append(f" {c['name']} ({c['type']}){sample_str}")
|
| 123 |
return "\n".join(lines)
|