43a0542a11
3-phase pipeline to solve LLM prompt overflow from too many OCR elements:
Phase 1 (generate_skeleton): compressed layout schema → skeleton JRXML
Phase 2 (refine_layout): sampled coordinates → pixel-level position tuning
Phase 3 (map_fields): OCR field names → replace $F{field_N} placeholders
Only triggered when layout_schema.total_rows > 0 on initial_generation intent.
Text requests and all other intents are unaffected (zero behavior change).
57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
"""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",
|
||
"compression": "compression.md",
|
||
"skeleton_generation": "skeleton_generation.md",
|
||
"refine_layout": "refine_layout.md",
|
||
"field_mapping": "field_mapping.md",
|
||
}
|
||
|
||
|
||
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())
|