Spaces:
Sleeping
Sleeping
edit prompts
#1
by
Rene0119 - opened
app.py
CHANGED
|
@@ -54,7 +54,10 @@ configuration = Configuration(access_token=CHANNEL_ACCESS_TOKEN)
|
|
| 54 |
parser = WebhookParser(CHANNEL_SECRET)
|
| 55 |
handler = WebhookHandler(CHANNEL_SECRET)
|
| 56 |
|
|
|
|
|
|
|
| 57 |
genai.configure(api_key=GOOGLE_API_KEY)
|
|
|
|
| 58 |
chat = genai.GenerativeModel(model_name="gemini-1.5-flash")
|
| 59 |
|
| 60 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -259,12 +262,12 @@ def callback():
|
|
| 259 |
|
| 260 |
# AI 問答
|
| 261 |
if user_input.startswith("AI "):
|
| 262 |
-
prompt = "
|
| 263 |
try:
|
| 264 |
response = chat.generate_content(prompt)
|
| 265 |
reply_text = response.text
|
| 266 |
except Exception as e:
|
| 267 |
-
reply_text = f"
|
| 268 |
|
| 269 |
reply_request = ReplyMessageRequest(
|
| 270 |
reply_token=event.reply_token,
|
|
@@ -331,7 +334,7 @@ def callback():
|
|
| 331 |
ai_resp = chat.generate_content(prompt)
|
| 332 |
side_effects = ai_resp.text.strip()
|
| 333 |
except Exception as e:
|
| 334 |
-
side_effects = f"
|
| 335 |
reply_text = (
|
| 336 |
f"🔹 中文品名:{zh_name}\n"
|
| 337 |
f"📌 英文品名:{en_name}\n"
|
|
@@ -352,7 +355,7 @@ def callback():
|
|
| 352 |
ai_resp = chat.generate_content(prompt)
|
| 353 |
reply_text = ai_resp.text
|
| 354 |
except Exception as e:
|
| 355 |
-
reply_text = f"
|
| 356 |
|
| 357 |
except Exception as e:
|
| 358 |
reply_text = f"⚠️ 查詢資料時發生錯誤:{str(e)}"
|
|
@@ -445,17 +448,22 @@ def callback():
|
|
| 445 |
filename = os.path.basename(tf.name)
|
| 446 |
image = Image.open(tf.name)
|
| 447 |
|
| 448 |
-
|
| 449 |
"請根據這張圖片判斷藥品資訊,若圖片無法判斷適應症或副作用,請根據藥品名稱推測並補充,"
|
| 450 |
"請用繁體中文回答,不要使用 Markdown 格式或 `*` 符號,不要提到你是 AI。"
|
| 451 |
"只要條列資料本身,不要加任何說明、警語或強調語句,也不要加**:\n"
|
| 452 |
"🔹 中文品名:\n"
|
| 453 |
"📌 英文品名:\n"
|
| 454 |
"📄 適應症:\n"
|
| 455 |
-
"⚠️ 副作用:(請用
|
| 456 |
)
|
| 457 |
-
|
| 458 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
|
| 460 |
reply_request = ReplyMessageRequest(
|
| 461 |
reply_token=event.reply_token,
|
|
|
|
| 54 |
parser = WebhookParser(CHANNEL_SECRET)
|
| 55 |
handler = WebhookHandler(CHANNEL_SECRET)
|
| 56 |
|
| 57 |
+
# --- 初始化 Google AI ---
|
| 58 |
+
|
| 59 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 60 |
+
text_system_prompt = "你是一個專業的中文藥物安全衛教AI,運行於Linebot平台,負責為台灣用戶提供用藥查詢、衛教提醒、藥品辨識與互動諮詢。所有回應必須以繁體中文呈現,語氣需保持專業、中立、清晰,嚴禁使用非正式語彙或網路用語。你的回答僅限於台灣現行合法藥品、常見用藥安全及一般衛教知識,絕不涉及診斷、處方或違法用途。遇重要藥品資訊或警語時,務必標示資料來源(如衛福部、健保署或官方藥物資料庫);無法查證時,需說明資訊有限並提醒用戶諮詢藥師。遇到模糊、非藥物相關、或疑似緊急情境(如中毒、嚴重過敏),請直接回覆:「請儘速就醫或聯絡藥師,Linebot無法提供緊急醫療協助。」回答時,優先給出簡明結論,再補充必要說明,遇複雜內容可分點陳述,藥品名稱、注意事項及用法用量需明顯標註。若用戶詢問非本功能範圍問題,請回覆:「本Linebot僅提供藥物安全與衛生教育資訊。」並簡要列舉可查詢主題(如用藥禁忌、藥物交互作用、藥品保存方式等)。所有資訊僅反映截至2025年6月之官方資料,若遇新藥、召回或重大警訊,應提醒用戶查閱衛福部或官方藥事機構。"
|
| 61 |
chat = genai.GenerativeModel(model_name="gemini-1.5-flash")
|
| 62 |
|
| 63 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 262 |
|
| 263 |
# AI 問答
|
| 264 |
if user_input.startswith("AI "):
|
| 265 |
+
prompt = text_system_prompt + "\n" + user_input[3:].strip()
|
| 266 |
try:
|
| 267 |
response = chat.generate_content(prompt)
|
| 268 |
reply_text = response.text
|
| 269 |
except Exception as e:
|
| 270 |
+
reply_text = f"系統繁忙,請稍後再試:{e}"
|
| 271 |
|
| 272 |
reply_request = ReplyMessageRequest(
|
| 273 |
reply_token=event.reply_token,
|
|
|
|
| 334 |
ai_resp = chat.generate_content(prompt)
|
| 335 |
side_effects = ai_resp.text.strip()
|
| 336 |
except Exception as e:
|
| 337 |
+
side_effects = f"系統繁忙,請稍後再試:{e}"
|
| 338 |
reply_text = (
|
| 339 |
f"🔹 中文品名:{zh_name}\n"
|
| 340 |
f"📌 英文品名:{en_name}\n"
|
|
|
|
| 355 |
ai_resp = chat.generate_content(prompt)
|
| 356 |
reply_text = ai_resp.text
|
| 357 |
except Exception as e:
|
| 358 |
+
reply_text = f"系統繁忙,請稍後再試:{e}"
|
| 359 |
|
| 360 |
except Exception as e:
|
| 361 |
reply_text = f"⚠️ 查詢資料時發生錯誤:{str(e)}"
|
|
|
|
| 448 |
filename = os.path.basename(tf.name)
|
| 449 |
image = Image.open(tf.name)
|
| 450 |
|
| 451 |
+
image_specific_prompt_text = (
|
| 452 |
"請根據這張圖片判斷藥品資訊,若圖片無法判斷適應症或副作用,請根據藥品名稱推測並補充,"
|
| 453 |
"請用繁體中文回答,不要使用 Markdown 格式或 `*` 符號,不要提到你是 AI。"
|
| 454 |
"只要條列資料本身,不要加任何說明、警語或強調語句,也不要加**:\n"
|
| 455 |
"🔹 中文品名:\n"
|
| 456 |
"📌 英文品名:\n"
|
| 457 |
"📄 適應症:\n"
|
| 458 |
+
"⚠️ 副作用:(請用-開頭條列,不要用*)"
|
| 459 |
)
|
| 460 |
+
# 結合全局系統提示
|
| 461 |
+
full_image_prompt = f"{text_system_prompt}\n\n{image_specific_prompt_text}"
|
| 462 |
+
try:
|
| 463 |
+
response = chat.generate_content([image, full_image_prompt])
|
| 464 |
+
description = response.text
|
| 465 |
+
except Exception as e:
|
| 466 |
+
description = f"系統繁忙,請稍後再試:{e}"
|
| 467 |
|
| 468 |
reply_request = ReplyMessageRequest(
|
| 469 |
reply_token=event.reply_token,
|