fix(rag): transform SPARQL results to match frontend metadata format for map coordinates
- Convert flat SPARQL results {lat, lon} to nested {metadata: {latitude, longitude}}
- Parse string coordinates to float values
- Add city/country/institution_type from template slots
- Enables ChatMapPanel to render map markers correctly
This commit is contained in:
parent
14be18e7c4
commit
ce66a294e5
1 changed files with 37 additions and 1 deletions
|
|
@ -3047,10 +3047,46 @@ async def dspy_query(request: DSPyQueryRequest) -> DSPyQueryResponse:
|
|||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
bindings = data.get("results", {}).get("bindings", [])
|
||||
sparql_results = [
|
||||
raw_results = [
|
||||
{k: v.get("value") for k, v in binding.items()}
|
||||
for binding in bindings
|
||||
]
|
||||
|
||||
# Transform SPARQL results to match frontend expected format
|
||||
# Frontend expects: {name, website, metadata: {latitude, longitude, city, ...}}
|
||||
# SPARQL returns: {name, website, lat, lon, city, ...}
|
||||
sparql_results = []
|
||||
for row in raw_results:
|
||||
# Parse lat/lon to float if present
|
||||
lat = None
|
||||
lon = None
|
||||
if row.get("lat"):
|
||||
try:
|
||||
lat = float(row["lat"])
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
if row.get("lon"):
|
||||
try:
|
||||
lon = float(row["lon"])
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
transformed = {
|
||||
"name": row.get("name"),
|
||||
"website": row.get("website"),
|
||||
"metadata": {
|
||||
"latitude": lat,
|
||||
"longitude": lon,
|
||||
"city": row.get("city") or template_result.slots.get("city"),
|
||||
"country": row.get("country") or template_result.slots.get("country"),
|
||||
"region": row.get("region") or template_result.slots.get("region"),
|
||||
"institution_type": row.get("type") or template_result.slots.get("institution_type"),
|
||||
},
|
||||
"scores": {"combined": 1.0}, # SPARQL results are exact matches
|
||||
}
|
||||
sparql_results.append(transformed)
|
||||
|
||||
logger.debug(f"[FACTUAL-QUERY] Transformed {len(sparql_results)} results, {sum(1 for r in sparql_results if r['metadata']['latitude'])} with coordinates")
|
||||
else:
|
||||
sparql_error = f"SPARQL returned {response.status_code}"
|
||||
else:
|
||||
|
|
|
|||
Loading…
Reference in a new issue