Spaces:
Running
Running
Commit ·
2188936
1
Parent(s): dddc990
feat : add deblur prompt
Browse files- services/agents.py +32 -3
services/agents.py
CHANGED
|
@@ -190,6 +190,22 @@ def deblur_image_from_url(
|
|
| 190 |
return json.dumps({
|
| 191 |
"error": f"圖片處理失敗。錯誤訊息: {e}"
|
| 192 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
# ==========================
|
| 195 |
# LangChain 代理人設定
|
|
@@ -197,9 +213,10 @@ def deblur_image_from_url(
|
|
| 197 |
|
| 198 |
# 結合所有定義的工具
|
| 199 |
tools = [
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
| 203 |
]
|
| 204 |
|
| 205 |
# 建立 LLM 模型實例 (使用 LangChain 的 ChatGoogleGenerativeAI)
|
|
@@ -212,6 +229,18 @@ llm = ChatGoogleGenerativeAI(
|
|
| 212 |
# ✅ 建立 Prompt (新版語法)
|
| 213 |
sys_prompt = """
|
| 214 |
你是一個強大的圖像生成、圖像去模糊與問答助理,可以根據用戶的請求使用提供的工具。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
### 核心輸出規範
|
| 216 |
* **結果呈現**:當你執行以下任一圖像處理工具成功後,你最終的回答 output **必須包含該 URL 的完整資訊**:
|
| 217 |
* `generate_and_upload_image`
|
|
|
|
| 190 |
return json.dumps({
|
| 191 |
"error": f"圖片處理失敗。錯誤訊息: {e}"
|
| 192 |
})
|
| 193 |
+
|
| 194 |
+
# ------------------------------
|
| 195 |
+
# 1️⃣ 意圖分類工具
|
| 196 |
+
# ------------------------------
|
| 197 |
+
@tool
|
| 198 |
+
def classify_intent(user_input: str) -> str:
|
| 199 |
+
"""
|
| 200 |
+
判斷使用者輸入意圖:
|
| 201 |
+
- "deblur" -> 去模糊圖片
|
| 202 |
+
- "qa" -> 一般問題或分析圖片
|
| 203 |
+
"""
|
| 204 |
+
deblur_keywords = ["去模糊", "清晰", "修復", "模糊", "deblur"]
|
| 205 |
+
if any(k in user_input.lower() for k in deblur_keywords):
|
| 206 |
+
return "deblur"
|
| 207 |
+
else:
|
| 208 |
+
return "qa"
|
| 209 |
|
| 210 |
# ==========================
|
| 211 |
# LangChain 代理人設定
|
|
|
|
| 213 |
|
| 214 |
# 結合所有定義的工具
|
| 215 |
tools = [
|
| 216 |
+
classify_intent, # 意圖分類
|
| 217 |
+
generate_and_upload_image, # 生成圖片
|
| 218 |
+
analyze_image_with_text, # 分析圖片
|
| 219 |
+
deblur_image_from_url # 去模糊圖片
|
| 220 |
]
|
| 221 |
|
| 222 |
# 建立 LLM 模型實例 (使用 LangChain 的 ChatGoogleGenerativeAI)
|
|
|
|
| 229 |
# ✅ 建立 Prompt (新版語法)
|
| 230 |
sys_prompt = """
|
| 231 |
你是一個強大的圖像生成、圖像去模糊與問答助理,可以根據用戶的請求使用提供的工具。
|
| 232 |
+
1. classify_intent(user_input) -> 判斷使用者意圖
|
| 233 |
+
2. deblur_image_from_url(file_url, user_text) -> 去模糊圖片
|
| 234 |
+
3. analyze_image_with_text(image_path, user_text) -> 分析圖片
|
| 235 |
+
4. generate_and_upload_image(prompt) -> 生成圖片
|
| 236 |
+
|
| 237 |
+
流程:
|
| 238 |
+
- 先呼叫 classify_intent 判斷使用者意圖
|
| 239 |
+
- 若返回 'deblur',則使用 deblur_image_from_url
|
| 240 |
+
- 若返回 'qa',則使用 analyze_image_with_text 或其他工具
|
| 241 |
+
|
| 242 |
+
當輸入文字明顯與圖片去模糊相關,請使用 deblur_image_from_url。
|
| 243 |
+
當輸入是問題或文字描述,請使用 analyze_image_with_text。
|
| 244 |
### 核心輸出規範
|
| 245 |
* **結果呈現**:當你執行以下任一圖像處理工具成功後,你最終的回答 output **必須包含該 URL 的完整資訊**:
|
| 246 |
* `generate_and_upload_image`
|