32 lines
1014 B
Python
32 lines
1014 B
Python
import os
|
|
from typing import Any, Dict, Optional
|
|
|
|
import yaml
|
|
|
|
|
|
class PromptManager:
|
|
"""提示词配置管理器"""
|
|
|
|
def __init__(self, config_path: Optional[str] = None):
|
|
root_dir = os.path.dirname(os.path.dirname(__file__))
|
|
self._config_path = config_path or os.path.join(root_dir, "config", "prompts.yaml")
|
|
self._data: Dict[str, Any] = {}
|
|
self.reload()
|
|
|
|
def reload(self) -> None:
|
|
"""重新加载提示词配置"""
|
|
with open(self._config_path, "r", encoding="utf-8") as f:
|
|
self._data = yaml.safe_load(f) or {}
|
|
|
|
def get(self, group: str, name: str, default: str = "") -> str:
|
|
"""获取指定提示词"""
|
|
return str(self._data.get(group, {}).get(name, default))
|
|
|
|
def list_groups(self) -> list[str]:
|
|
"""列出所有分组"""
|
|
return list(self._data.keys())
|
|
|
|
def list_prompts(self, group: str) -> list[str]:
|
|
"""列出分组内提示词"""
|
|
return list(self._data.get(group, {}).keys())
|