2026-05-19 15:02:53 +08:00
|
|
|
|
"""Prompt 加载器:从 prompts/ 目录加载 .md 文件。
|
|
|
|
|
|
|
|
|
|
|
|
支持热重载 — 每次调用都从磁盘读取,修改 prompt 文件无需重启应用。
|
|
|
|
|
|
|
|
|
|
|
|
用法:
|
|
|
|
|
|
from prompts.loader import load_prompt
|
|
|
|
|
|
prompt = load_prompt("intent_classify").format(has_report="是", user_input="...")
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
_PROMPTS_DIR = Path(__file__).resolve().parent
|
|
|
|
|
|
|
|
|
|
|
|
# 文件名 → 变量名 映射
|
|
|
|
|
|
_NAME_MAP = {
|
|
|
|
|
|
"intent_classify": "intent_classify.md",
|
|
|
|
|
|
"consult": "consult.md",
|
|
|
|
|
|
"initial_generation": "initial_generation.md",
|
|
|
|
|
|
"modification": "modification.md",
|
|
|
|
|
|
"correction": "correction.md",
|
|
|
|
|
|
"explain_error": "explain_error.md",
|
2026-05-21 08:34:32 +08:00
|
|
|
|
"compression": "compression.md",
|
|
|
|
|
|
"skeleton_generation": "skeleton_generation.md",
|
|
|
|
|
|
"refine_layout": "refine_layout.md",
|
|
|
|
|
|
"field_mapping": "field_mapping.md",
|
2026-05-19 15:02:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_prompt(name: str) -> str:
|
|
|
|
|
|
"""从 prompts/{name}.md 加载 prompt 模板(每次从磁盘读取,支持热重载)。
|
|
|
|
|
|
|
|
|
|
|
|
返回的字符串包含 Python .format() 占位符,调用方负责填充。
|
|
|
|
|
|
"""
|
|
|
|
|
|
filename = _NAME_MAP.get(name)
|
|
|
|
|
|
if not filename:
|
|
|
|
|
|
raise ValueError(f"未知 prompt: {name},可选值: {list(_NAME_MAP.keys())}")
|
|
|
|
|
|
|
|
|
|
|
|
filepath = _PROMPTS_DIR / filename
|
|
|
|
|
|
if not filepath.exists():
|
|
|
|
|
|
raise FileNotFoundError(f"Prompt 文件不存在: {filepath}")
|
|
|
|
|
|
|
|
|
|
|
|
text = filepath.read_text(encoding="utf-8").strip()
|
|
|
|
|
|
|
|
|
|
|
|
# 去掉可能存在的 markdown frontmatter(--- 包裹的元数据)
|
|
|
|
|
|
if text.startswith("---"):
|
|
|
|
|
|
end = text.find("---", 3)
|
|
|
|
|
|
if end != -1:
|
|
|
|
|
|
text = text[end + 3:].strip()
|
|
|
|
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def list_prompts() -> list[str]:
|
|
|
|
|
|
"""列出所有可用的 prompt 名称。"""
|
|
|
|
|
|
return sorted(_NAME_MAP.keys())
|