feat: enhance normalization of metric values for improved handling
- Updated `_normalize_metric_value_for_read` to compact numeric strings and ensure consistent formatting for string data types. - Enhanced `normalize_prompt_number` to handle numeric strings and non-finite float values effectively. - Improved unit tests to validate the new normalization behavior for session metrics and scalar formatting.
This commit is contained in:
1 parent
178534e9eb
commit
7676897fda
4 files changed
+64
-10
No files matched your search
@@ -4,10 +4,14 @@ Kompakte Zahlen- und JSON-Aufbereitung für KI-Platzhalter (Token sparen).
|
||||
- Floats: sinnvolle Nachkommastellen je nach Größenordnung (kleine Werte <0,1 mehr Präzision).
|
||||
- ≥10 meist ganzzahlig; Prozent/Verhältnisse über denselben Mechanismus lesbar.
|
||||
- Rekursiv auf dict/list-Strukturen vor json.dumps in _safe_json anwendbar.
|
||||
|
||||
Hinweis: numpy.float64 und numerische Strings (DB/API) sind keine ``float``-Instanzen —
|
||||
diese werden explizit mit float() normalisiert.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
import re
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
|
||||
@@ -37,22 +41,45 @@ def compact_float_for_prompt(x: float) -> float | int:
|
||||
|
||||
|
||||
def normalize_prompt_number(x: Any) -> Any:
|
||||
"""int/Decimal/float kompakt; Rest unverändert."""
|
||||
"""int/Decimal/float kompakt; numpy-Scalars; numerische Strings; sonst unverändert."""
|
||||
if x is None:
|
||||
return None
|
||||
if isinstance(x, bool):
|
||||
return x
|
||||
if isinstance(x, int) and not isinstance(x, bool):
|
||||
return x
|
||||
if isinstance(x, str):
|
||||
s = x.strip()
|
||||
if not s:
|
||||
return x
|
||||
try:
|
||||
if re.fullmatch(r"-?\d+", s):
|
||||
return int(s)
|
||||
xf = float(s)
|
||||
except ValueError:
|
||||
return x
|
||||
if not math.isfinite(xf):
|
||||
return x
|
||||
return compact_float_for_prompt(xf)
|
||||
if isinstance(x, Decimal):
|
||||
try:
|
||||
xf = float(x)
|
||||
except Exception:
|
||||
return x
|
||||
if not math.isfinite(xf):
|
||||
return x
|
||||
return compact_float_for_prompt(xf)
|
||||
if isinstance(x, float):
|
||||
if not math.isfinite(x):
|
||||
return x
|
||||
return compact_float_for_prompt(x)
|
||||
return x
|
||||
try:
|
||||
xf = float(x)
|
||||
except (TypeError, ValueError):
|
||||
return x
|
||||
if not math.isfinite(xf):
|
||||
return x
|
||||
return compact_float_for_prompt(xf)
|
||||
|
||||
|
||||
def compact_json_payload_for_prompts(obj: Any) -> Any:
|
||||
@@ -73,25 +100,23 @@ def compact_json_payload_for_prompts(obj: Any) -> Any:
|
||||
def format_scalar_for_prompt_text(x: Any) -> str:
|
||||
"""
|
||||
Kurzdarstellung für Text-Platzhalter (activity_detail, Tabellen, …).
|
||||
Nutzt dieselbe Komprimierung wie JSON (normalize_prompt_number).
|
||||
Alle Zahlenpfade über normalize_prompt_number; Ausgabe kurz (%g, keine Float-Schweife).
|
||||
"""
|
||||
if x is None:
|
||||
return "—"
|
||||
if isinstance(x, bool):
|
||||
return "ja" if x else "nein"
|
||||
if isinstance(x, str):
|
||||
return x
|
||||
n = normalize_prompt_number(x)
|
||||
if isinstance(n, bool):
|
||||
return "ja" if n else "nein"
|
||||
if isinstance(n, str):
|
||||
return n
|
||||
if isinstance(n, int) and not isinstance(n, bool):
|
||||
return str(n)
|
||||
if isinstance(n, float):
|
||||
if not math.isfinite(n):
|
||||
return str(n)
|
||||
if abs(n - round(n)) < 1e-9:
|
||||
return str(int(round(n)))
|
||||
return str(n)
|
||||
return "%g" % n
|
||||
return str(n)
|
||||
|
||||
|
||||
@@ -121,7 +146,7 @@ def session_metrics_list_to_key_value_compact(metrics: list[Any] | None) -> dict
|
||||
elif dt == "boolean":
|
||||
out[str(k)] = bool(v)
|
||||
elif dt == "string":
|
||||
out[str(k)] = str(v)
|
||||
out[str(k)] = normalize_prompt_number(v)
|
||||
else:
|
||||
out[str(k)] = normalize_prompt_number(v)
|
||||
return out
|
||||
Reference in new issue
Block a user