Serie: RAG AI
python
49 righe
· Aggiornato 2026-05-08
prompts.py
RAG_AI/src/prompts.py
"""集中管理 Prompt 模板。
把 prompt 從邏輯中抽離,未來要做 A/B test、版本管理、或本地化都更容易。
"""
from langchain_core.prompts import ChatPromptTemplate
# 主 RAG Prompt:強調「不知道就說不知道」、要求引用來源
RAG_SYSTEM_PROMPT = """你是一位專業的客服助理,根據以下「文件內容」回答使用者問題。
回答規則:
1. 答案必須**完全基於**提供的文件內容,不要編造任何文件中沒有的事實。
2. 如果文件無法回答此問題,請明確回答「根據現有資料無法回答此問題」,不要硬猜。
3. 在答案最後以「來源:」格式標明引用的檔名與頁碼。
4. 用繁體中文回答,語氣專業、簡潔、友善。
文件內容:
{context}"""
RAG_PROMPT = ChatPromptTemplate.from_messages(
[
("system", RAG_SYSTEM_PROMPT),
("human", "{question}"),
]
)
# HyDE:讓 LLM 先「假裝知道答案」以提升檢索品質(教材第 12.4 節)
HYDE_PROMPT = ChatPromptTemplate.from_template(
"請假裝你是專家,針對以下問題寫出一段約 100 字的答案。即使你不確定也請寫出最可能的答案。\n\n"
"問題:{question}\n\n"
"答案:"
)
# 文件相關性評分:Self-RAG 用(教材第 15.6 節)
GRADE_DOCUMENT_PROMPT = ChatPromptTemplate.from_template(
"問題:{question}\n\n文件片段:{document}\n\n"
"這份文件片段是否有助於回答問題?只回 'yes' 或 'no',不要解釋。"
)
# Multi-Query 改寫:把單一問題改寫成多個變體(教材第 12.3 節)
MULTI_QUERY_PROMPT = ChatPromptTemplate.from_template(
"你是 AI 語言模型助理。針對使用者的原始問題,產生 3 個不同角度但意思相近的問題版本,"
"幫助從向量資料庫中檢索到更全面的資訊。\n"
"請直接列出 3 個問題,每行一個,不要有編號或多餘文字。\n\n"
"原始問題:{question}"
)
Articoli correlati
RAG AI
python
Aggiornato 2026-05-08
main.py
main.py — python source code from the RAG AI learning materials (RAG_AI/main.py).
Leggi l'articolo →
RAG AI
python
Aggiornato 2026-05-08
__init__.py
__init__.py — python source code from the RAG AI learning materials (RAG_AI/src/__init__.py).
Leggi l'articolo →
RAG AI
python
Aggiornato 2026-05-08
app.py
app.py — python source code from the RAG AI learning materials (RAG_AI/src/app.py).
Leggi l'articolo →
RAG AI
python
Aggiornato 2026-05-08
config.py
config.py — python source code from the RAG AI learning materials (RAG_AI/src/config.py).
Leggi l'articolo →
RAG AI
python
Aggiornato 2026-05-08
evaluate.py
evaluate.py — python source code from the RAG AI learning materials (RAG_AI/src/evaluate.py).
Leggi l'articolo →
RAG AI
python
Aggiornato 2026-05-08
ingest.py
ingest.py — python source code from the RAG AI learning materials (RAG_AI/src/ingest.py).
Leggi l'articolo →