feat: Enhance placeholder caption generation and formatting
Deploy Development / deploy (push) Successful in 54s
Build Test / pytest-backend (push) Successful in 8s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 16s

- Updated `build_ai_placeholder_caption` in `placeholder_registry.py` to improve the generation of AI context captions by prioritizing descriptions and avoiding redundancy.
- Introduced `format_value_with_d_modifier` in `placeholder_resolver.py` to format values with contextual information, enhancing the clarity of exported placeholder values.
- Modified `export_placeholder_values` in `prompts.py` to utilize the new formatting function, ensuring that exported data includes both raw values and contextual descriptions.
- Added tests for the new formatting function and updated existing tests to ensure accurate caption generation.

These changes improve the contextual relevance of placeholder data and enhance the user experience when interacting with exported values.
This commit is contained in:
Lars committed 2026-04-11 21:47:08 +02:00
1 parent baeddd7c13
commit a9a414b956
4 files changed
+91 -19

No files matched your search

+34 -12
View File
@@ -260,21 +260,34 @@ class PlaceholderRegistry:
def build_ai_placeholder_caption(metadata: PlaceholderMetadata, max_len: int = 400) -> str:
"""
Kurztext für KI-Kontext (z. B. Modifier |d): Bedeutung/Skala, ohne die Rohausgabe zu ersetzen.
Nutzt business_meaning / semantic_contract; bei Scores explizite 0–100-Erläuterung.
Text für |d und Exportfeld ai_caption: zuerst **was** der Platzhalter misst (description),
dann **Einordnung** (business_meaning oder gekürzter semantic_contract).
So ist klar, worauf sich der konkrete Wert bezieht — nicht nur eine „Meta-Bedeutung“.
"""
chunks: List[str] = []
desc = (metadata.description or "").strip()
bm = (metadata.business_meaning or "").strip()
sc = (metadata.semantic_contract or "").strip()
desc = (metadata.description or "").strip()
if bm:
chunks.append(bm)
elif sc:
chunks.append(sc if len(sc) <= max_len else sc[: max_len - 1] + "…")
elif desc:
chunks: List[str] = []
if desc:
chunks.append(desc)
interpret = bm
if not interpret and sc:
interpret = sc if len(sc) <= max_len else sc[: max_len - 1] + "…"
if interpret:
blob = " ".join(chunks).lower()
il = interpret.lower()
# Keine Dublette: gleicher Text oder lange Description bereits in der Interpretation
redundant = il in blob or (
desc
and len(desc) >= 10
and desc.lower() in il
)
if not redundant:
chunks.append(interpret)
if metadata.placeholder_type == PlaceholderType.SCORE:
chunks.append("Skala 0–100: höher = im Modell günstiger / besser abgestimmt.")
@@ -282,9 +295,18 @@ def build_ai_placeholder_caption(metadata: PlaceholderMetadata, max_len: int = 4
if unit and metadata.placeholder_type != PlaceholderType.SCORE:
blob = " ".join(chunks).lower()
u_low = unit.lower()
if u_low not in blob and u_low.replace(" ", "") not in blob.replace(" ", ""):
if u_low not in ("score (0-100)", "0-100", "0–100", "dimensionless"):
chunks.append(f"Technischer Bezug: {unit}.")
# Einheit oft schon in description („… in g (30d)“, „Kalorien“) — nicht doppeln
compact_blob = blob.replace(" ", "").replace("/", "")
compact_u = u_low.replace(" ", "").replace("/", "")
unit_redundant = compact_u in compact_blob or (
"g/day" in u_low and ("g/" in blob or "gramm" in blob or " protein" in blob or " fett" in blob or " kh" in blob)
) or ("kcal" in u_low and ("kcal" in blob or "kalorien" in blob))
if (
not unit_redundant
and u_low not in ("score (0-100)", "0-100", "0–100", "dimensionless")
):
chunks.append(f"Technischer Bezug: {unit}.")
out = " ".join(c for c in chunks if c).strip()
if len(out) > max_len + 120: