Paul720810 commited on
Commit
b8fc056
·
verified ·
1 Parent(s): 3d52e16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -1
app.py CHANGED
@@ -637,7 +637,7 @@ SELECT
637
  }
638
 
639
  # 動作意圖:count / list
640
- if any(kw in q_lower for kw in ['幾份', '多少', '數量', '總數', 'how many', 'count']):
641
  intents['action'] = 'count'
642
  if ("測試項目" in q) or ("test item" in q_lower):
643
  sql['select'].append("COUNT(jip.ItemCode) AS item_count")
@@ -715,6 +715,14 @@ SELECT
715
  # 第二層:解析 AI 輸出並修正
716
  self._log("未觸發任何模板,嘗試解析並修正 AI 輸出…")
717
  parsed_sql = parse_sql_from_response(raw_response)
 
 
 
 
 
 
 
 
718
  if not parsed_sql:
719
  self._log(f"❌ 未能從模型回應中解析出任何 SQL。原始回應: {raw_response}", "ERROR")
720
  return None, f"無法解析SQL。原始回應:\n{raw_response}"
 
637
  }
638
 
639
  # 動作意圖:count / list
640
+ if any(kw in q_lower for kw in ['幾份', '份數', '份数', '多少', '數量', '總數', 'how many', 'count']):
641
  intents['action'] = 'count'
642
  if ("測試項目" in q) or ("test item" in q_lower):
643
  sql['select'].append("COUNT(jip.ItemCode) AS item_count")
 
715
  # 第二層:解析 AI 輸出並修正
716
  self._log("未觸發任何模板,嘗試解析並修正 AI 輸出…")
717
  parsed_sql = parse_sql_from_response(raw_response)
718
+ if not parsed_sql:
719
+ # 嘗試救援:模型可能省略了開頭的 SELECT(因為 Prompt 已種子 SELECT)
720
+ resp = (raw_response or '').strip()
721
+ if resp and not resp.upper().startswith('SELECT') and re.search(r'\bFROM\b', resp, re.IGNORECASE):
722
+ self._log("嘗試自動補上 SELECT 以修復不完整輸出", "INFO")
723
+ salvage_sql = 'SELECT ' + resp
724
+ parsed_sql = parse_sql_from_response(salvage_sql) or salvage_sql
725
+
726
  if not parsed_sql:
727
  self._log(f"❌ 未能從模型回應中解析出任何 SQL。原始回應: {raw_response}", "ERROR")
728
  return None, f"無法解析SQL。原始回應:\n{raw_response}"