meeting_memory/providers/prompt_loader.py

40 lines
1.3 KiB
Python
Raw Normal View History

2026-06-24 07:05:19 +00:00
from __future__ import annotations
from pathlib import Path
def load_prompt(name: str, *, language: str = "zh") -> dict[str, str]:
path = Path(__file__).resolve().parent.parent / "prompt" / language / f"{name}.yaml"
if not path.exists():
return {"system": "你是一个会议知识库维护 Agent。"}
text = path.read_text(encoding="utf-8-sig")
return _parse_yaml_sections(text)
def _parse_yaml_sections(text: str) -> dict[str, str]:
sections: dict[str, str] = {}
current_key: str | None = None
current_lines: list[str] = []
for line in text.splitlines():
if line and not line[0].isspace() and line.rstrip().endswith(": |"):
if current_key is not None:
sections[current_key] = "\n".join(current_lines).strip()
current_key = line.split(":")[0].strip()
current_lines = []
elif current_key is not None:
if line.startswith(" "):
current_lines.append(line[2:])
elif line.strip() == "":
current_lines.append("")
else:
current_lines.append(line)
if current_key is not None:
sections[current_key] = "\n".join(current_lines).strip()
if "system" not in sections:
sections["system"] = text.strip()
return sections