Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -414,6 +414,48 @@ class TextToSQLSystem:
|
|
| 414 |
|
| 415 |
return relevant_tables[:3] # 最多返回3個相關表格
|
| 416 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
def _validate_and_fix_sql(self, sql: str, question: str) -> str:
|
| 418 |
"""
|
| 419 |
(V10 / 組別查詢版)
|
|
|
|
| 414 |
|
| 415 |
return relevant_tables[:3] # 最多返回3個相關表格
|
| 416 |
|
| 417 |
+
# 請將這整個函數複製到您的 TextToSQLSystem class 內部
|
| 418 |
+
|
| 419 |
+
def _format_relevant_schema(self, table_names: List[str]) -> str:
|
| 420 |
+
"""
|
| 421 |
+
生成一個簡化的、不易被模型錯誤模仿的 Schema 字符串。
|
| 422 |
+
"""
|
| 423 |
+
if not self.schema:
|
| 424 |
+
return "No schema available.\n"
|
| 425 |
+
|
| 426 |
+
actual_table_names_map = {name.lower(): name for name in self.schema.keys()}
|
| 427 |
+
real_table_names = []
|
| 428 |
+
for table in table_names:
|
| 429 |
+
actual_name = actual_table_names_map.get(table.lower())
|
| 430 |
+
if actual_name:
|
| 431 |
+
real_table_names.append(actual_name)
|
| 432 |
+
elif table in self.schema:
|
| 433 |
+
real_table_names.append(table)
|
| 434 |
+
|
| 435 |
+
if not real_table_names:
|
| 436 |
+
self._log("未識別到相關表格,使用預設核心表格。", "WARNING")
|
| 437 |
+
real_table_names = ['TSR53SampleDescription', 'JobTimeline', 'JobsInProgress']
|
| 438 |
+
|
| 439 |
+
formatted = ""
|
| 440 |
+
for table in real_table_names:
|
| 441 |
+
if table in self.schema:
|
| 442 |
+
# 使用簡單的 "Table: ..." 和 "Columns: ..." 格式
|
| 443 |
+
formatted += f"Table: {table}\n"
|
| 444 |
+
cols_str = []
|
| 445 |
+
# 只顯示前 10 個關鍵欄位
|
| 446 |
+
for col in self.schema[table][:10]:
|
| 447 |
+
col_name = col['name']
|
| 448 |
+
col_type = col['type']
|
| 449 |
+
col_desc = col.get('description', '').replace('\n', ' ')
|
| 450 |
+
# 將描述信息放在括號裡
|
| 451 |
+
if col_desc:
|
| 452 |
+
cols_str.append(f"{col_name} ({col_type}, {col_desc})")
|
| 453 |
+
else:
|
| 454 |
+
cols_str.append(f"{col_name} ({col_type})")
|
| 455 |
+
formatted += f"Columns: {', '.join(cols_str)}\n\n"
|
| 456 |
+
|
| 457 |
+
return formatted.strip()
|
| 458 |
+
|
| 459 |
def _validate_and_fix_sql(self, sql: str, question: str) -> str:
|
| 460 |
"""
|
| 461 |
(V10 / 組別查詢版)
|