init
This commit is contained in:
29
backend/prompts.py
Normal file
29
backend/prompts.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""Template-Lader: templates/<Name>.md, Platzhalter {name}, Datei-Hash je Call
|
||||
(Ledger — uncommittete Prompt-Änderungen bleiben nachvollziehbar)."""
|
||||
|
||||
import hashlib
|
||||
from functools import lru_cache
|
||||
|
||||
from config import TEMPLATES_DIR
|
||||
|
||||
|
||||
@lru_cache(maxsize=None)
|
||||
def _lade(name: str) -> tuple[str, str]:
|
||||
text = (TEMPLATES_DIR / f"{name}.md").read_text(encoding="utf-8")
|
||||
return text, hashlib.sha256(text.encode()).hexdigest()[:12]
|
||||
|
||||
|
||||
def render(name: str, **werte) -> tuple[str, str]:
|
||||
"""→ (prompt, template_hash). format_map, damit geschweifte Klammern in
|
||||
JSON-Beispielen doppelt geschrieben werden ({{ }})."""
|
||||
text, h = _lade(name)
|
||||
return text.format_map(_Sicher(werte)), h
|
||||
|
||||
|
||||
class _Sicher(dict):
|
||||
def __missing__(self, key):
|
||||
raise KeyError(f"Template-Platzhalter fehlt: {key}")
|
||||
|
||||
|
||||
def cache_leeren() -> None:
|
||||
_lade.cache_clear()
|
||||
Reference in New Issue
Block a user