feat: Refine placeholder resolution with enhanced modifiers support
- Updated `resolve_placeholders` in `prompt_executor.py` to support combined modifiers for placeholders, allowing for more flexible output formats. - Enhanced `build_ai_placeholder_caption` in `placeholder_registry.py` to clarify the generation of AI context captions, focusing on descriptions and explanations. - Introduced new helper functions in `placeholder_resolver.py` to streamline the retrieval of descriptions and explanations for placeholders. - Modified tests to cover new functionality, ensuring accurate behavior for combined modifiers and improved placeholder resolution. These changes enhance the usability and clarity of placeholder outputs, providing users with richer contextual information.
This commit is contained in:
1 parent
a9a414b956
commit
4868e44882
5 files changed
+151
-91
No files matched your search
@@ -49,25 +49,46 @@ from data_layer.health_metrics import (
|
||||
|
||||
from placeholder_registry import build_ai_placeholder_caption, get_registry
|
||||
|
||||
# {{key}} oder {{key|d}} — Modifier d hängt KI-Kontext (ai_caption) an
|
||||
# {{key|d}} — nur description anhängen; {{key|x}} — nur Erklärung (ai_caption / Registry)
|
||||
_PLACEHOLDER_TOKEN_RE = re.compile(
|
||||
r"\{\{\s*([a-zA-Z0-9_]+)(?:\s*\|\s*([a-zA-Z0-9_,\s]+))?\s*\}\}"
|
||||
)
|
||||
|
||||
|
||||
def _ai_caption_for_placeholder_key(key: str) -> Optional[str]:
|
||||
meta = get_registry().get(key)
|
||||
if meta:
|
||||
return build_ai_placeholder_caption(meta)
|
||||
def get_catalog_row_for_key(
|
||||
catalog: Optional[Dict[str, List[Dict[str, Any]]]], key: str
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
"""Katalogzeile zu Platzhalter-Key (key ohne {{}})."""
|
||||
if not catalog:
|
||||
return None
|
||||
for items in catalog.values():
|
||||
for item in items:
|
||||
raw = item.get("key") or ""
|
||||
ik = str(raw).replace("{{", "").replace("}}", "").strip()
|
||||
if ik == key:
|
||||
return item
|
||||
return None
|
||||
|
||||
|
||||
def _description_for_registry_key(key: str) -> str:
|
||||
meta = get_registry().get(key)
|
||||
if not meta:
|
||||
return ""
|
||||
return (meta.description or "").strip()
|
||||
|
||||
|
||||
def _explain_for_registry_key(key: str) -> str:
|
||||
meta = get_registry().get(key)
|
||||
if not meta:
|
||||
return ""
|
||||
return build_ai_placeholder_caption(meta).strip()
|
||||
|
||||
|
||||
def format_value_with_d_modifier(value: str, catalog_row: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Entspricht der Prompt-Ersetzung bei {{key|d}}: „Wert — Kontext“.
|
||||
Kontext: ai_caption aus dem Katalog, sonst description (wie prompt_executor).
|
||||
Vorschau für Export wie {{key|d}}: „Wert — description“ (kein ai_caption).
|
||||
"""
|
||||
cap = (catalog_row.get("ai_caption") or catalog_row.get("description") or "").strip()
|
||||
cap = (catalog_row.get("description") or "").strip()
|
||||
if cap:
|
||||
return f"{value} — {cap}"
|
||||
return str(value)
|
||||
@@ -1615,7 +1636,9 @@ def resolve_placeholders(template: str, profile_id: str) -> str:
|
||||
|
||||
Unterstützt Modifier wie bei der Prompt-Pipeline:
|
||||
- {{fat_avg}} — nur Wert
|
||||
- {{fat_avg|d}} — Wert plus KI-Kontext (business_meaning / semantic_contract aus Registry)
|
||||
- {{fat_avg|d}} — Wert — description (kurz, token-sparend)
|
||||
- {{fat_avg|x}} — nur Erklärung (business_meaning / semantic_contract, ggf. Score-Skala), ohne Wert
|
||||
- {{fat_avg|d,x}} — Wert — description — Erklärung
|
||||
|
||||
Args:
|
||||
template: Prompt template with placeholders
|
||||
@@ -1637,11 +1660,27 @@ def resolve_placeholders(template: str, profile_id: str) -> str:
|
||||
value = str(resolver(profile_id))
|
||||
except Exception:
|
||||
return f"[Fehler: {ph}]"
|
||||
if "d" in mods:
|
||||
cap = _ai_caption_for_placeholder_key(key)
|
||||
if cap:
|
||||
value = f"{value} — {cap}"
|
||||
return value
|
||||
|
||||
want_d = "d" in mods
|
||||
want_x = "x" in mods
|
||||
|
||||
if want_x and not want_d:
|
||||
expl = _explain_for_registry_key(key)
|
||||
return expl if expl else ""
|
||||
|
||||
if not want_d and not want_x:
|
||||
return value
|
||||
|
||||
parts: List[str] = [value]
|
||||
if want_d:
|
||||
desc = _description_for_registry_key(key)
|
||||
if desc:
|
||||
parts.append(desc)
|
||||
if want_x:
|
||||
expl = _explain_for_registry_key(key)
|
||||
if expl:
|
||||
parts.append(expl)
|
||||
return " — ".join(parts)
|
||||
|
||||
return _PLACEHOLDER_TOKEN_RE.sub(_repl, template)
|
||||
|
||||
@@ -1846,7 +1885,7 @@ def get_placeholder_catalog(profile_id: str) -> Dict[str, List[Dict[str, str]]]:
|
||||
'key': key,
|
||||
'description': description,
|
||||
'example': str(example),
|
||||
'ai_caption': description,
|
||||
'ai_caption': '',
|
||||
})
|
||||
|
||||
# Add ALL remaining placeholders from PLACEHOLDER_MAP that aren't categorized yet
|
||||
|
||||
Reference in new issue
Block a user