Files
agent_jrxml/prompts/loader.py
T
panda 70614dff5e feat: comprehensive v2 upgrade — streaming, error KB, file upload, layout analysis
Major changes:
- Streaming: LLM统一 _BaseLLM 接口 (invoke + stream), generate/modify/correct
  节点使用 get_stream_writer() 实现逐字输出, UI 节点平铺展开自动折叠
- Prompt外部化: 7个prompt拆分到 prompts/*.md, loader.py 支持热重载
- 错误自增长: backend/error_kb.py — 指纹去重 + ChromaDB持久化,
  correct_jrxml→validate 通过时自动入库, retrieve同时搜索错误KB
- 文件上传: backend/file_parser.py — PDF/DOCX/图片/文本解析,
  侧边栏多文件上传, 文本自动注入下一条消息
- A4模板识别: backend/layout_analyzer.py — 三种模式(完整A4/行片段修改/行片段新建),
  PaddleOCR元素提取 + 行分组 + JRXML section匹配
- 会话历史下载: jrxml_versions版本追踪 + 侧边栏历史版本下载按钮
- 预览修复: route_after_save跳过预览/导出意图的验证循环
- Ctrl+C修复: JS注入拦截Streamlit裸c键清缓存

Docs: CLAUDE.md (完整项目文档), ROADMAP.md (改进路线图)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-19 15:02:53 +08:00

54 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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",
}
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())