File size: 871 Bytes
fa529c6
267e635
 
 
fa529c6
 
 
267e635
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa529c6
267e635
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
import yaml
from typing import Dict, Any, Optional
from openai import OpenAI
from dataclasses import dataclass

@dataclass
class PromptTemplate:
    """A simple prompt template that handles parameter substitution and generation settings."""
    template: str
    parameters: Dict[str, Any]

    def format(self, **kwargs) -> str:
        return self.template.format(**kwargs)

class PromptLoader:
    """Loads prompts and their parameters from a YAML file."""

    @staticmethod
    def load_prompts(yaml_path: str) -> Dict[str, PromptTemplate]:
        with open(yaml_path, 'r') as f:
            data = yaml.safe_load(f)

        prompts = {}
        for name, content in data.items():
            prompts[name] = PromptTemplate(
                template=content['template'],
                parameters=content.get('parameters', {})
            )
        return prompts