AG4DP-Example-Chatbot / prompt_template.py
harpreetsahota's picture
Update prompt_template.py
267e635 verified
raw
history blame
871 Bytes
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