diff --git a/backend/app/services/geo_assistant_service.py b/backend/app/services/geo_assistant_service.py index 19fc8821..28ab2f5e 100644 --- a/backend/app/services/geo_assistant_service.py +++ b/backend/app/services/geo_assistant_service.py @@ -171,6 +171,15 @@ class GeoAssistantService: return round(value, 4) 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 def normalize_plain_text(answer: str) -> str: lines: list[str] = [] @@ -418,6 +427,7 @@ class GeoAssistantService: "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]] = [] for metric in metrics: if not isinstance(metric, dict): @@ -433,6 +443,8 @@ class GeoAssistantService: is_estimate=bool(metric.get("is_estimate")), ) context_metrics.append(item) + if id(metric) not in model_metric_ids: + continue serialized_metrics.append(item.model_dump(mode="json")) serialized_metrics[-1]["value"] = self.rounded_context_value(item.value, item.unit) serialized_metrics[-1]["measurement_quality"] = ( diff --git a/backend/tests/test_sprint202_temporal_metrics_and_ollama.py b/backend/tests/test_sprint202_temporal_metrics_and_ollama.py index 71c0eb00..5b5f500e 100644 --- a/backend/tests/test_sprint202_temporal_metrics_and_ollama.py +++ b/backend/tests/test_sprint202_temporal_metrics_and_ollama.py @@ -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 +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: answer = GeoAssistantService.normalize_plain_text("**Bevolking**\n* 36.783 inwoners\n`Bron: Statbel`")