Paul720810 commited on
Commit
89729e6
·
verified ·
1 Parent(s): e07167f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -21
app.py CHANGED
@@ -696,47 +696,56 @@ class TextToSQLSystem:
696
 
697
  def _build_prompt(self, user_q: str, examples: List[Dict]) -> str:
698
  """
699
- 建立結構更清晰的提示詞,明確區分上下文和最終指令,避免模型混淆。
700
  """
701
  relevant_tables = self._identify_relevant_tables(user_q)
702
 
703
  # 1. 提供背景上下文 (Context)
704
- # 這部分告訴模型它的角色和它擁有的參考資料。
705
  system_context = "You are an expert AI assistant that generates SQLite queries based on a database schema and a user's question."
706
  schema_str = self._format_relevant_schema(relevant_tables)
707
 
708
  # 2. 提供一個清晰的範例 (Few-shot Example)
709
- # 這為模型設定了期望的輸入輸出格式。
710
  ex_str = ""
 
711
  if examples:
712
- best_example = examples
 
 
 
 
713
  # 使用 [QUESTION] 和 [SQL] 標籤來強化結構
714
  ex_str = f"Here is an example of how to answer:\n[QUESTION]: {best_example['question']}\n[SQL]:\n```sql\n{best_example['sql']}\n```\n\n---\n\n"
715
 
716
  # 3. 給出最終的、不可混淆的指令 (Final, Imperative Instruction)
717
- # 這部分是提示詞的結尾,告訴模型「現在開始做這個」。
718
  final_task_instruction = f"""Now, based on the schema and the new user question below, generate a single, valid SQLite query.
719
- **CRITICAL RULES TO FOLLOW:**
720
- - You **must** only use the tables and columns provided in the schema above.
721
- - You **must** use SQLite syntax (e.g., use `strftime('%Y', date_column)` for years).
722
- - You **must** output **nothing** else, only the SQL query inside a single ```sql code block.
723
-
724
- [QUESTION]: {user_q}
725
- [SQL]:
726
- ```sql
727
- """
 
728
 
729
  # 4. 組合最終的 Prompt
730
  prompt = f"""{system_context}
731
- {schema_str}
732
- {ex_str}
733
- {final_task_instruction}
734
- """
 
735
 
736
- # 限制總長度 (這個邏輯保持不變)
 
737
  if len(prompt) > 1500:
738
- prompt = prompt[:1500] + "...\n" + final_task_instruction
739
-
 
 
 
 
740
  return prompt
741
 
742
 
 
696
 
697
  def _build_prompt(self, user_q: str, examples: List[Dict]) -> str:
698
  """
699
+ 建立結構更清晰的提示詞,明確區分上下文和最終指令,避免模型混淆。(已修正 TypeError)
700
  """
701
  relevant_tables = self._identify_relevant_tables(user_q)
702
 
703
  # 1. 提供背景上下文 (Context)
 
704
  system_context = "You are an expert AI assistant that generates SQLite queries based on a database schema and a user's question."
705
  schema_str = self._format_relevant_schema(relevant_tables)
706
 
707
  # 2. 提供一個清晰的範例 (Few-shot Example)
 
708
  ex_str = ""
709
+ # 檢查 examples 列表是否為空,避免出錯
710
  if examples:
711
+ # --- START: 這裡是修正的地方 ---
712
+ # 從列表中取出第一個元素 (它是一個字典)
713
+ best_example = examples[0]
714
+ # --- END: 修正完成 ---
715
+
716
  # 使用 [QUESTION] 和 [SQL] 標籤來強化結構
717
  ex_str = f"Here is an example of how to answer:\n[QUESTION]: {best_example['question']}\n[SQL]:\n```sql\n{best_example['sql']}\n```\n\n---\n\n"
718
 
719
  # 3. 給出最終的、不可混淆的指令 (Final, Imperative Instruction)
 
720
  final_task_instruction = f"""Now, based on the schema and the new user question below, generate a single, valid SQLite query.
721
+
722
+ **CRITICAL RULES TO FOLLOW:**
723
+ - You **must** only use the tables and columns provided in the schema above.
724
+ - You **must** use SQLite syntax (e.g., use `strftime('%Y', date_column)` for years).
725
+ - You **must** output **nothing** else, only the SQL query inside a single ```sql code block.
726
+
727
+ [QUESTION]: {user_q}
728
+ [SQL]:
729
+ ```sql
730
+ """
731
 
732
  # 4. 組合最終的 Prompt
733
  prompt = f"""{system_context}
734
+
735
+ {schema_str}
736
+ {ex_str}
737
+ {final_task_instruction}
738
+ """
739
 
740
+ # 5. 限制總長度 (這個邏輯保持不變)
741
+ # 確保在截斷時,最後的指令部分是完整的
742
  if len(prompt) > 1500:
743
+ # 找到 final_task_instruction prompt 中的起始位置
744
+ instruction_start_index = prompt.find("Now, based on the schema")
745
+ # 保留 schema 和一部分範例,然後接上完整的最終指令
746
+ allowed_context_len = 1500 - len(final_task_instruction)
747
+ prompt = prompt[:allowed_context_len] + "...\n\n" + final_task_instruction
748
+
749
  return prompt
750
 
751