Files
more_dots/services/core/prompt_manager.py
T
2026-03-24 18:07:22 +08:00

43 lines
1.4 KiB
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(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())
_GLOBAL_PROMPT_MANAGER: Optional[PromptManager] = None
def get_prompt_manager(config_path: Optional[str] = None) -> PromptManager:
"""获取全局 PromptManager(单例)"""
global _GLOBAL_PROMPT_MANAGER
if _GLOBAL_PROMPT_MANAGER is None:
_GLOBAL_PROMPT_MANAGER = PromptManager(config_path=config_path)
return _GLOBAL_PROMPT_MANAGER