-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathprompts.py
78 lines (60 loc) · 2.56 KB
/
prompts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Prompt management for codegate."""
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, Optional, Union
import yaml
from .exceptions import ConfigurationError
@dataclass
class PromptConfig:
"""Configuration for system prompts."""
prompts: Dict[str, str] = field(default_factory=dict)
def __getattr__(self, name: str) -> str:
"""Allow attribute-style access to prompts."""
if name in self.prompts:
return self.prompts[name]
raise AttributeError(f"No prompt named '{name}' found")
@classmethod
def from_file(cls, prompt_path: Union[str, Path]) -> "PromptConfig":
"""Load prompts from a YAML file.
Args:
prompt_path: Path to the YAML prompts file
Returns:
PromptConfig: Prompts configuration instance
Raises:
ConfigurationError: If the file cannot be read or parsed
"""
try:
with open(prompt_path, "r") as f:
prompt_data = yaml.safe_load(f)
if not isinstance(prompt_data, dict):
raise ConfigurationError("Prompts file must contain a YAML dictionary")
def validate_prompts(data, parent_key=""):
"""Recursively validate prompt values."""
for key, value in data.items():
full_key = f"{parent_key}.{key}" if parent_key else key
if isinstance(value, dict):
validate_prompts(value, full_key) # Recurse into nested dictionaries
elif not isinstance(value, str):
raise ConfigurationError(
f"Prompt '{full_key}' must be a string, got {type(value)}"
)
# Validate the entire structure
validate_prompts(prompt_data)
return cls(prompts=prompt_data)
except yaml.YAMLError as e:
raise ConfigurationError(f"Failed to parse prompts file: {e}")
except OSError as e:
raise ConfigurationError(f"Failed to read prompts file: {e}")
@classmethod
def load(cls, prompt_path: Optional[Union[str, Path]] = None) -> "PromptConfig":
"""Load prompts with optional file override.
Args:
prompt_path: Optional path to prompts file
Returns:
PromptConfig: Resolved prompts configuration
Raises:
ConfigurationError: If configuration is invalid
"""
if prompt_path:
return cls.from_file(prompt_path)
return cls()