Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,7 +61,7 @@ class TextToSQLSystem:
|
|
| 61 |
self.llm = Llama(
|
| 62 |
model_path=model_path,
|
| 63 |
n_ctx=4096,
|
| 64 |
-
n_threads=
|
| 65 |
verbose=False
|
| 66 |
)
|
| 67 |
self._log(f"✅ 已載入 GGUF 模型: {GGUF_FILENAME}")
|
|
@@ -121,20 +121,29 @@ class TextToSQLSystem:
|
|
| 121 |
|
| 122 |
def _build_prompt(self, user_q: str, examples: List[Dict]) -> str:
|
| 123 |
system_instruction = (
|
| 124 |
-
"
|
| 125 |
-
"
|
|
|
|
|
|
|
|
|
|
| 126 |
)
|
| 127 |
schema_str = self._format_schema_for_prompt()
|
| 128 |
ex_str = "--- 範例 ---\n"
|
| 129 |
for i, ex in enumerate(examples, 1):
|
| 130 |
ex_str += f"範例 {i} 問題: {ex['question']}\nSQL:\n```sql\n{ex['sql']}\n```\n\n"
|
| 131 |
-
return f"{system_instruction}\n\n{schema_str}\n{ex_str}\n--- 使用者問題 ---\n{user_q}"
|
| 132 |
|
| 133 |
def huggingface_api_call(self, prompt: str) -> str:
|
| 134 |
try:
|
| 135 |
self._log("🧠 使用 GGUF 模型生成 SQL...")
|
| 136 |
-
output = self.llm(
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
return text
|
| 139 |
except Exception as e:
|
| 140 |
self._log(f"❌ 生成失敗: {e}", "ERROR")
|
|
|
|
| 61 |
self.llm = Llama(
|
| 62 |
model_path=model_path,
|
| 63 |
n_ctx=4096,
|
| 64 |
+
n_threads=os.cpu_count(),
|
| 65 |
verbose=False
|
| 66 |
)
|
| 67 |
self._log(f"✅ 已載入 GGUF 模型: {GGUF_FILENAME}")
|
|
|
|
| 121 |
|
| 122 |
def _build_prompt(self, user_q: str, examples: List[Dict]) -> str:
|
| 123 |
system_instruction = (
|
| 124 |
+
"你是一位頂尖的 SQLite 專家。\n"
|
| 125 |
+
"請嚴格遵守以下規則:\n"
|
| 126 |
+
"1. 僅輸出最終的 SQL 語法,不要包含解釋或自然語言。\n"
|
| 127 |
+
"2. 必須使用 ```sql 開頭 和 ``` 結尾包住查詢。\n"
|
| 128 |
+
"3. 查詢必須能直接在 SQLite 執行。\n"
|
| 129 |
)
|
| 130 |
schema_str = self._format_schema_for_prompt()
|
| 131 |
ex_str = "--- 範例 ---\n"
|
| 132 |
for i, ex in enumerate(examples, 1):
|
| 133 |
ex_str += f"範例 {i} 問題: {ex['question']}\nSQL:\n```sql\n{ex['sql']}\n```\n\n"
|
| 134 |
+
return f"{system_instruction}\n\n{schema_str}\n{ex_str}\n--- 使用者問題 ---\n請根據以上資訊,生成 SQL 查詢:\n\"{user_q}\""
|
| 135 |
|
| 136 |
def huggingface_api_call(self, prompt: str) -> str:
|
| 137 |
try:
|
| 138 |
self._log("🧠 使用 GGUF 模型生成 SQL...")
|
| 139 |
+
output = self.llm(
|
| 140 |
+
prompt,
|
| 141 |
+
max_tokens=256,
|
| 142 |
+
temperature=0.2,
|
| 143 |
+
stop=["</s>", "```"],
|
| 144 |
+
echo=False
|
| 145 |
+
)
|
| 146 |
+
text = output["choices"][0]["text"].strip()
|
| 147 |
return text
|
| 148 |
except Exception as e:
|
| 149 |
self._log(f"❌ 生成失敗: {e}", "ERROR")
|