|
| 1 | +from enum import Enum, auto |
| 2 | +from pathlib import Path |
| 3 | +from string import Template |
| 4 | + |
| 5 | + |
| 6 | +class PromptType(Enum): |
| 7 | + """Enumeration of all available prompt types.""" |
| 8 | + |
| 9 | + CONTENT_ANALYZER = auto() |
| 10 | + PLANNER = auto() |
| 11 | + SCRIPT_GENERATOR = auto() |
| 12 | + EDITOR = auto() |
| 13 | + USER_PROXY = auto() |
| 14 | + USER_MESSAGE = auto() |
| 15 | + MANAGER = auto() |
| 16 | + |
| 17 | + |
| 18 | +class PromptManager: |
| 19 | + """ |
| 20 | + Manages loading and caching of system prompts for the neuralnoise application. |
| 21 | +
|
| 22 | + This class provides a centralized way to load and access prompts from the prompts directory. |
| 23 | + It loads all prompts during initialization and provides methods to access and substitute |
| 24 | + variables in the prompts. |
| 25 | + """ |
| 26 | + |
| 27 | + _instance = None |
| 28 | + |
| 29 | + def __new__(cls, *args, **kwargs): |
| 30 | + """Implement singleton pattern to ensure only one instance exists.""" |
| 31 | + if cls._instance is None: |
| 32 | + cls._instance = super(PromptManager, cls).__new__(cls) |
| 33 | + return cls._instance |
| 34 | + |
| 35 | + def __init__(self, prompts_dir: Path | None = None, language: str = "en"): |
| 36 | + """ |
| 37 | + Initialize the PromptManager with the prompts directory and language. |
| 38 | +
|
| 39 | + Args: |
| 40 | + prompts_dir: Directory containing prompt files. If None, uses the default package prompts. |
| 41 | + language: Language code for prompt templates. |
| 42 | + """ |
| 43 | + # Skip initialization if already initialized |
| 44 | + if hasattr(self, "_initialized") and self._initialized: |
| 45 | + return |
| 46 | + |
| 47 | + self.language = language |
| 48 | + |
| 49 | + # Set prompts directory |
| 50 | + if prompts_dir is None: |
| 51 | + from neuralnoise.utils import package_root |
| 52 | + |
| 53 | + self.prompts_dir = package_root / "prompts" |
| 54 | + else: |
| 55 | + self.prompts_dir = Path(prompts_dir) |
| 56 | + |
| 57 | + # Map of PromptType to file paths |
| 58 | + self.prompt_files = { |
| 59 | + PromptType.CONTENT_ANALYZER: self.prompts_dir |
| 60 | + / "content_analyzer.system.xml", |
| 61 | + PromptType.PLANNER: self.prompts_dir / "planner.system.xml", |
| 62 | + PromptType.SCRIPT_GENERATOR: self.prompts_dir |
| 63 | + / "script_generation.system.xml", |
| 64 | + PromptType.EDITOR: self.prompts_dir / "editor.system.xml", |
| 65 | + PromptType.USER_PROXY: self.prompts_dir / "user_proxy.system.xml", |
| 66 | + PromptType.USER_MESSAGE: self.prompts_dir / "user_proxy.message.xml", |
| 67 | + PromptType.MANAGER: self.prompts_dir / "manager.system.xml", |
| 68 | + } |
| 69 | + |
| 70 | + # Load all prompts |
| 71 | + self.prompts: dict[PromptType, str] = {} |
| 72 | + self._load_all_prompts() |
| 73 | + |
| 74 | + self._initialized = True |
| 75 | + |
| 76 | + def _load_all_prompts(self) -> None: |
| 77 | + """Load all prompts from the prompts directory.""" |
| 78 | + for prompt_type, file_path in self.prompt_files.items(): |
| 79 | + self.prompts[prompt_type] = self._load_prompt_file(file_path) |
| 80 | + |
| 81 | + def _load_prompt_file(self, path: Path) -> str: |
| 82 | + """ |
| 83 | + Load a prompt from a file. |
| 84 | +
|
| 85 | + Args: |
| 86 | + path: Path to the prompt file. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + The content of the prompt file or an empty string if the file doesn't exist. |
| 90 | + """ |
| 91 | + if not path.exists(): |
| 92 | + return "" |
| 93 | + |
| 94 | + with open(path, "r", encoding="utf-8") as f: |
| 95 | + content = f.read() |
| 96 | + |
| 97 | + return content |
| 98 | + |
| 99 | + def get_prompt(self, prompt_type: PromptType, **kwargs) -> str: |
| 100 | + """ |
| 101 | + Get a prompt with variables substituted. |
| 102 | +
|
| 103 | + Args: |
| 104 | + prompt_type: Type of prompt to get. |
| 105 | + **kwargs: Variables to substitute in the prompt. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + The prompt with variables substituted. |
| 109 | + """ |
| 110 | + content = self.prompts.get(prompt_type, "") |
| 111 | + |
| 112 | + # Always include language in kwargs if not provided |
| 113 | + if "language" not in kwargs: |
| 114 | + kwargs["language"] = self.language |
| 115 | + |
| 116 | + if content and kwargs: |
| 117 | + template = Template(content) |
| 118 | + content = template.safe_substitute(kwargs) |
| 119 | + |
| 120 | + return content |
| 121 | + |
| 122 | + def update_prompt(self, prompt_type: PromptType, **common_kwargs) -> None: |
| 123 | + """ |
| 124 | + Update a prompt with common variables substituted. |
| 125 | +
|
| 126 | + Args: |
| 127 | + **common_kwargs: Common variables to substitute in all prompts. |
| 128 | +
|
| 129 | + Returns: |
| 130 | + Dictionary mapping prompt names to prompt content. |
| 131 | + """ |
| 132 | + self.prompts[prompt_type] = self.get_prompt(prompt_type, **common_kwargs) |
| 133 | + |
| 134 | + def update_prompts(self, **common_kwargs) -> None: |
| 135 | + """ |
| 136 | + Update all prompts with common variables substituted. |
| 137 | +
|
| 138 | + Args: |
| 139 | + **common_kwargs: Common variables to substitute in all prompts. |
| 140 | +
|
| 141 | + Returns: |
| 142 | + Dictionary mapping prompt names to prompt content. |
| 143 | + """ |
| 144 | + for prompt_type in PromptType: |
| 145 | + self.prompts[prompt_type] = self.get_prompt(prompt_type, **common_kwargs) |
0 commit comments