Keep assistant prompts focused on semantic metrics
This commit is contained in:
@@ -171,6 +171,15 @@ class GeoAssistantService:
|
|||||||
return round(value, 4)
|
return round(value, 4)
|
||||||
return round(value, 2)
|
return round(value, 2)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def model_context_metrics(metrics: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||||
|
meaningful_metrics = [
|
||||||
|
metric
|
||||||
|
for metric in metrics
|
||||||
|
if str(metric.get("metric_unit") or "").casefold().strip() not in {"objecten", "features"}
|
||||||
|
]
|
||||||
|
return meaningful_metrics or metrics
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def normalize_plain_text(answer: str) -> str:
|
def normalize_plain_text(answer: str) -> str:
|
||||||
lines: list[str] = []
|
lines: list[str] = []
|
||||||
@@ -418,6 +427,7 @@ class GeoAssistantService:
|
|||||||
"is_estimate": summary.get("is_estimate", False),
|
"is_estimate": summary.get("is_estimate", False),
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
model_metric_ids = {id(metric) for metric in self.model_context_metrics(metrics)}
|
||||||
serialized_metrics: list[dict[str, Any]] = []
|
serialized_metrics: list[dict[str, Any]] = []
|
||||||
for metric in metrics:
|
for metric in metrics:
|
||||||
if not isinstance(metric, dict):
|
if not isinstance(metric, dict):
|
||||||
@@ -433,6 +443,8 @@ class GeoAssistantService:
|
|||||||
is_estimate=bool(metric.get("is_estimate")),
|
is_estimate=bool(metric.get("is_estimate")),
|
||||||
)
|
)
|
||||||
context_metrics.append(item)
|
context_metrics.append(item)
|
||||||
|
if id(metric) not in model_metric_ids:
|
||||||
|
continue
|
||||||
serialized_metrics.append(item.model_dump(mode="json"))
|
serialized_metrics.append(item.model_dump(mode="json"))
|
||||||
serialized_metrics[-1]["value"] = self.rounded_context_value(item.value, item.unit)
|
serialized_metrics[-1]["value"] = self.rounded_context_value(item.value, item.unit)
|
||||||
serialized_metrics[-1]["measurement_quality"] = (
|
serialized_metrics[-1]["measurement_quality"] = (
|
||||||
|
|||||||
@@ -202,6 +202,21 @@ def test_geo_assistant_rounds_prompt_values_by_semantic_unit(value: float, unit:
|
|||||||
assert GeoAssistantService.rounded_context_value(value, unit) == expected
|
assert GeoAssistantService.rounded_context_value(value, unit) == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_geo_assistant_omits_supporting_object_count_from_richer_model_context() -> None:
|
||||||
|
metrics = [
|
||||||
|
{"metric_label": "Bodemkaartoppervlakte", "metric_value": 11_448.35, "metric_unit": "ha"},
|
||||||
|
{"metric_label": "Bodemkaartvlakken", "metric_value": 1_159, "metric_unit": "objecten"},
|
||||||
|
]
|
||||||
|
|
||||||
|
assert GeoAssistantService.model_context_metrics(metrics) == metrics[:1]
|
||||||
|
|
||||||
|
|
||||||
|
def test_geo_assistant_keeps_object_count_when_it_is_the_only_metric() -> None:
|
||||||
|
metrics = [{"metric_label": "Objecten", "metric_value": 12, "metric_unit": "objecten"}]
|
||||||
|
|
||||||
|
assert GeoAssistantService.model_context_metrics(metrics) == metrics
|
||||||
|
|
||||||
|
|
||||||
def test_geo_assistant_normalizes_model_markdown_for_plain_text_renderer() -> None:
|
def test_geo_assistant_normalizes_model_markdown_for_plain_text_renderer() -> None:
|
||||||
answer = GeoAssistantService.normalize_plain_text("**Bevolking**\n* 36.783 inwoners\n`Bron: Statbel`")
|
answer = GeoAssistantService.normalize_plain_text("**Bevolking**\n* 36.783 inwoners\n`Bron: Statbel`")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user