Spaces:
Running
Running
Song
commited on
Commit
·
9853009
1
Parent(s):
1741c30
hi
Browse files
app.py
CHANGED
@@ -66,8 +66,8 @@ SECTION_WEIGHTS = {
|
|
66 |
"用法及用量": 1.0,
|
67 |
"病人使用須知": 1.0,
|
68 |
"儲存條件": 1.0,
|
69 |
-
"警語及注意事項":
|
70 |
-
"禁忌": 1.
|
71 |
"副作用": 1.0,
|
72 |
"藥物交互作用": 1.0,
|
73 |
"其他": 1.0,
|
@@ -388,7 +388,7 @@ class RagPipeline:
|
|
388 |
all_reranked_results.sort(key=lambda x: x['rerank_score'], reverse=True)
|
389 |
|
390 |
context = self._build_context(all_reranked_results, LLM_MODEL_CONFIG["max_context_chars"])
|
391 |
-
prompt = self._make_prompt(q_orig, context)
|
392 |
messages = [
|
393 |
{"role": "system", "content": "你是嚴謹的台灣藥師。"},
|
394 |
{"role": "user", "content": prompt}
|
@@ -455,8 +455,30 @@ class RagPipeline:
|
|
455 |
return self._llm_call([{"role": "user", "content": prompt}])
|
456 |
|
457 |
def _adjust_section_weights(self, intents: List[str]) -> Dict[str, float]:
|
|
|
458 |
weights = SECTION_WEIGHTS.copy()
|
459 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
460 |
return weights
|
461 |
|
462 |
def _semantic_search(self, index, query: str, top_k: int, embedding_model) -> Tuple[List[int], List[float]]:
|
@@ -505,7 +527,13 @@ class RagPipeline:
|
|
505 |
processed_chunks.add(text)
|
506 |
return context.strip()
|
507 |
|
508 |
-
def _make_prompt(self, query: str, context: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
509 |
return f"""
|
510 |
你是一位專業且謹慎的台灣藥師。請嚴格根據「參考資料」回答使用者問題,使用繁體中文。
|
511 |
|
@@ -513,6 +541,7 @@ class RagPipeline:
|
|
513 |
1) 完全依據參考資料,不得捏造或引用外部知識。
|
514 |
2) 以清楚的段落或條列回覆,不要使用 Markdown 符號(如 *, -, #)。
|
515 |
3) 如果資料不足,請回覆:「根據提供的資料,無法回答此問題。」
|
|
|
516 |
|
517 |
參考資料:
|
518 |
---
|
|
|
66 |
"用法及用量": 1.0,
|
67 |
"病人使用須知": 1.0,
|
68 |
"儲存條件": 1.0,
|
69 |
+
"警語及注意事項": 2.0, # 提高權重
|
70 |
+
"禁忌": 1.5,
|
71 |
"副作用": 1.0,
|
72 |
"藥物交互作用": 1.0,
|
73 |
"其他": 1.0,
|
|
|
388 |
all_reranked_results.sort(key=lambda x: x['rerank_score'], reverse=True)
|
389 |
|
390 |
context = self._build_context(all_reranked_results, LLM_MODEL_CONFIG["max_context_chars"])
|
391 |
+
prompt = self._make_prompt(q_orig, context, intents)
|
392 |
messages = [
|
393 |
{"role": "system", "content": "你是嚴謹的台灣藥師。"},
|
394 |
{"role": "user", "content": prompt}
|
|
|
455 |
return self._llm_call([{"role": "user", "content": prompt}])
|
456 |
|
457 |
def _adjust_section_weights(self, intents: List[str]) -> Dict[str, float]:
|
458 |
+
"""根據意圖調整各仿單章節的檢索權重"""
|
459 |
weights = SECTION_WEIGHTS.copy()
|
460 |
+
|
461 |
+
# 如果沒有偵測到意圖,則使用預設權重
|
462 |
+
if not intents:
|
463 |
+
return weights
|
464 |
+
|
465 |
+
intent = intents[0] # 只考慮第一個意圖
|
466 |
+
|
467 |
+
if intent in ["操作 (Administration)", "劑型相關 (Dosage Form Concerns)"]:
|
468 |
+
weights["用法及用量"] *= 1.5
|
469 |
+
weights["病人使用須知"] *= 2.0
|
470 |
+
elif intent == "保存/攜帶 (Storage & Handling)":
|
471 |
+
weights["儲存條件"] *= 2.0
|
472 |
+
elif intent == "副作用/異常 (Side Effects / Issues)":
|
473 |
+
# 依據您的建議,特別強化「警語」
|
474 |
+
weights["警語及注意事項"] *= 3.0
|
475 |
+
weights["副作用"] *= 1.5
|
476 |
+
elif intent == "時間/併用 (Timing & Interaction)":
|
477 |
+
weights["用法及用量"] *= 1.5
|
478 |
+
weights["藥物交互作用"] *= 2.0
|
479 |
+
elif intent == "劑量調整 (Dosage Adjustment)":
|
480 |
+
weights["用法及用量"] *= 2.0
|
481 |
+
|
482 |
return weights
|
483 |
|
484 |
def _semantic_search(self, index, query: str, top_k: int, embedding_model) -> Tuple[List[int], List[float]]:
|
|
|
527 |
processed_chunks.add(text)
|
528 |
return context.strip()
|
529 |
|
530 |
+
def _make_prompt(self, query: str, context: str, intents: List[str]) -> str:
|
531 |
+
|
532 |
+
# 根據意圖,加入特別提醒 LLM 的指令
|
533 |
+
additional_instruction = ""
|
534 |
+
if "劑量調整 (Dosage Adjustment)" in intents or "時間/併用 (Timing & Interaction)" in intents:
|
535 |
+
additional_instruction = "在回答用藥劑量和時間時,務必提醒使用者,醫師開立的藥袋醫囑優先於仿單的一般建議。"
|
536 |
+
|
537 |
return f"""
|
538 |
你是一位專業且謹慎的台灣藥師。請嚴格根據「參考資料」回答使用者問題,使用繁體中文。
|
539 |
|
|
|
541 |
1) 完全依據參考資料,不得捏造或引用外部知識。
|
542 |
2) 以清楚的段落或條列回覆,不要使用 Markdown 符號(如 *, -, #)。
|
543 |
3) 如果資料不足,請回覆:「根據提供的資料,無法回答此問題。」
|
544 |
+
4) {additional_instruction}
|
545 |
|
546 |
參考資料:
|
547 |
---
|