File size: 1,251 Bytes
55b0a04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import yaml
from pathlib import Path
from typing import Dict, Any
import os


# Global prompts dictionary
PROMPTS = {}


def load_prompts(yaml_file="prompts.yaml"):
    """Load prompt templates from the YAML file or use defaults if file doesn't exist."""
    global PROMPTS
    yaml_path = Path(yaml_file)

    if yaml_path.exists():
        try:
            with open(yaml_path, "r") as f:
                prompts = yaml.safe_load(f)
            print(f"Successfully loaded prompts from {yaml_file}")
            PROMPTS = prompts
            return prompts
        except Exception as e:
            print(f"Error loading prompts from file: {e}")

    else:
        print(f"Prompts file {yaml_file} not found, using default prompts")
        raise FileNotFoundError(f"Prompts file {yaml_file} not found")



def format_prompt(prompt_template, **kwargs):
    """Format a prompt template with the provided kwargs."""
    try:
        return prompt_template.format(**kwargs)
    except KeyError as e:
        print(f"Missing key in prompt template: {e}")
        return prompt_template
    except Exception as e:
        print(f"Error formatting prompt: {e}")
        return prompt_template


# Initialize prompts when module is imported
load_prompts()