32 lines
1022 B
Python
32 lines
1022 B
Python
"""Prompt loader + renderer.
|
|
|
|
Every prompt — system or user — lives as a `.txt` file in this directory.
|
|
Two ways to consume them:
|
|
|
|
- `load(name)` returns the file's text unchanged. Use for system prompts
|
|
(and any user prompt that has no placeholders).
|
|
- `render(name, **vars)` loads then runs `str.format(**vars)` on it. Use
|
|
for user prompts that interpolate question/SQL/rows/findings/etc.
|
|
Placeholders are standard Python format syntax: `{name}`. Literal braces
|
|
must be doubled: `{{` / `}}`.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
PROMPTS_DIR = Path(__file__).resolve().parent
|
|
|
|
|
|
@lru_cache(maxsize=None)
|
|
def load(name: str) -> str:
|
|
"""Read the named prompt file (no substitution)."""
|
|
return (PROMPTS_DIR / f"{name}.txt").read_text(encoding="utf-8").strip()
|
|
|
|
|
|
def render(name: str, /, **vars: Any) -> str:
|
|
"""Load a prompt and substitute `{placeholder}` values from kwargs."""
|
|
return load(name).format(**vars)
|