-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanggraph_agent.py
More file actions
1414 lines (1177 loc) · 57.9 KB
/
langgraph_agent.py
File metadata and controls
1414 lines (1177 loc) · 57.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
LangGraph agent for CoreStack MVP (see readme.md)
- Accepts a user query
- Uses Gemini LLM (via LangChain) for intent extraction
- Validates/canonicalizes
- Calls CoreStack API
- Normalizes JSON
- Computes stats
- Formats concise response
- Uses LangGraph StateGraph for node orchestration
"""
import os
import requests
import json
import statistics
import re
from typing import Dict, Any, List
import geopandas as gpd
import rasterio
from rasterio.mask import mask
from shapely.geometry import box
import numpy as np
import tempfile
import numpy as np
from rasterio.mask import mask
from shapely.geometry import Point, box
from langchain_google_genai import ChatGoogleGenerativeAI
from langgraph.graph import StateGraph
from dotenv import load_dotenv
from artifact import ArtifactRegistry
from geospatial_artifact_registry import GeospatialArtifactRegistry
from geospatial_handlers import GeospatialDataHandler
# Initialize both registries for backward compatibility and new features
artifact_registry = ArtifactRegistry() # Legacy registry
geo_artifact_registry = GeospatialArtifactRegistry() # New geospatial registry
import os
DISABLE_REGISTRY = os.getenv('DISABLE_ARTIFACT_REGISTRY') == '1'
if DISABLE_REGISTRY:
# Create dummy registries that do nothing
class DummyRegistry:
def register(self, *args, **kwargs):
return "dummy_id"
def get(self, *args, **kwargs):
return {}
def get_stats(self, *args, **kwargs):
return {"total_artifacts": 0, "by_type": {}}
def register_geospatial_artifact(self, *args, **kwargs):
return "dummy_id"
def get_processing_lineage(self, *args, **kwargs):
return []
artifact_registry = DummyRegistry()
geo_artifact_registry = DummyRegistry()
else:
artifact_registry = ArtifactRegistry()
geo_artifact_registry = GeospatialArtifactRegistry()
load_dotenv()
# Initialising API keys
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
CORE_STACK_API_KEY = os.getenv("CORE_STACK_API_KEY")
print("CORE_STACK_API_KEY is:", CORE_STACK_API_KEY)
class SpatialDataProcessor:
"""Handles raster and vector spatial data processing"""
@staticmethod
def process_raster_url(url: str, bounds=None):
"""Process raster data via WCS"""
try:
# Add coordinate bounds to WCS URL if provided
if bounds:
lon_min, lat_min, lon_max, lat_max = bounds
subset_params = f"&subset=Long({lon_min},{lon_max})&subset=Lat({lat_min},{lat_max})"
if 'subset=' not in url:
url += subset_params
print(f"🌍 Requesting raster data via WCS...")
response = requests.get(url, verify=False, timeout=60)
if response.status_code != 200:
print(f"❌ WCS request failed: {response.status_code}")
return None
content_type = response.headers.get('content-type', '')
print(f"📄 Content type: {content_type}")
# Verify it's actually a raster file
if not any(fmt in content_type.lower() for fmt in ['tiff', 'geotiff', 'image']):
print(f"❌ Not a raster file: {content_type}")
return None
# Process the actual GeoTIFF content
return SpatialDataProcessor._process_raster_content(response.content, bounds)
except Exception as e:
print(f"❌ Raster processing error: {e}")
return None
@staticmethod
def _process_raster_content(content: bytes, bounds=None):
"""Process raster content from response"""
try:
import tempfile
with tempfile.NamedTemporaryFile(suffix='.tif', delete=False) as temp_file:
temp_file.write(content)
temp_file.flush()
with rasterio.open(temp_file.name) as src:
if bounds:
# Clip to bounds if provided
geom = [box(*bounds)]
out_image, out_transform = mask(src, geom, crop=True)
data = out_image[0]
else:
data = src.read(1)
# Compute statistics
valid_data = data[data != src.nodata] if src.nodata is not None else data.flatten()
valid_data = valid_data[~np.isnan(valid_data)]
if len(valid_data) > 0:
return {
'mean': float(np.mean(valid_data)),
'std': float(np.std(valid_data)),
'min': float(np.min(valid_data)),
'max': float(np.max(valid_data)),
'count': len(valid_data),
'bounds': list(src.bounds),
'crs': str(src.crs),
'processing_method': 'direct_raster'
}
# Clean up temp file
import os
os.unlink(temp_file.name)
except Exception as e:
print(f"Error processing raster content: {e}")
return None
@staticmethod
def _convert_to_wms_getmap(url: str, bounds=None):
"""Convert various service URLs to WMS GetMap"""
try:
if bounds:
lon_min, lat_min, lon_max, lat_max = bounds
bbox = f"{lon_min},{lat_min},{lon_max},{lat_max}"
else:
bbox = "75.0,25.0,75.2,25.2" # Default small area
# Basic WMS GetMap parameters
wms_params = {
'SERVICE': 'WMS',
'REQUEST': 'GetMap',
'FORMAT': 'image/tiff',
'WIDTH': '256',
'HEIGHT': '256',
'SRS': 'EPSG:4326',
'BBOX': bbox
}
# Extract base URL and add WMS parameters
base_url = url.split('?')[0]
wms_url = f"{base_url}?" + "&".join([f"{k}={v}" for k, v in wms_params.items()])
# Try to extract layer name from original URL
if 'typeName=' in url:
layer_name = url.split('typeName=')[1].split('&')[0]
wms_url += f"&LAYERS={layer_name}"
return wms_url
except Exception as e:
print(f"Error converting to WMS: {e}")
return None
@staticmethod
def _extract_raster_like_stats_from_vector(gdf: gpd.GeoDataFrame, bounds=None):
"""Extract raster-like statistics from vector data"""
try:
# Get numeric columns
numeric_cols = gdf.select_dtypes(include=[np.number]).columns
if len(numeric_cols) == 0:
return {
'feature_count': len(gdf),
'message': 'Vector data found - no numeric attributes for raster-like analysis',
'processing_method': 'vector_fallback'
}
# Calculate stats for the first numeric column
col = numeric_cols[0]
values = gdf[col].dropna()
if len(values) > 0:
return {
'mean': float(values.mean()),
'std': float(values.std()),
'min': float(values.min()),
'max': float(values.max()),
'count': len(values),
'feature_count': len(gdf),
'analyzed_attribute': col,
'processing_method': 'vector_fallback',
'message': f'Analyzed numeric attribute "{col}" from vector data'
}
except Exception as e:
print(f"Error extracting vector stats: {e}")
return None
@staticmethod
def process_vector_url(url: str, point=None, buffer_km=1.0):
"""Download and process vector data from URL"""
try:
# Download GeoJSON
response = requests.get(url, verify=False)
if response.status_code == 200:
geojson_data = response.json()
if 'features' not in geojson_data or not geojson_data['features']:
return {'feature_count': 0, 'total_area': 0, 'attributes': {}}
gdf = gpd.GeoDataFrame.from_features(geojson_data['features'])
if gdf.empty:
return {'feature_count': 0, 'total_area': 0, 'attributes': {}}
# Set CRS if not present
if gdf.crs is None:
gdf.set_crs(epsg=4326, inplace=True)
if point:
# Filter features near the point
point_geom = Point(point[1], point[0]) # lon, lat
point_buffered = point_geom.buffer(buffer_km * 0.009) # Rough km to degrees
gdf = gdf[gdf.intersects(point_buffered)]
# Extract numerical attributes for analysis
numeric_cols = gdf.select_dtypes(include=[np.number]).columns
results = {
'feature_count': len(gdf),
'total_area': float(gdf.geometry.area.sum()) if not gdf.empty else 0,
'attributes': {}
}
for col in numeric_cols:
if not gdf[col].isna().all():
results['attributes'][col] = {
'mean': float(gdf[col].mean()),
'sum': float(gdf[col].sum()),
'min': float(gdf[col].min()),
'max': float(gdf[col].max())
}
return results
return None
except Exception as e:
print(f"Error processing vector data: {e}")
return None
MOCK_RASTER_URLS = {
# Indian/Rajasthan-specific coordinates (25.31, 75.09)
"elevation": "https://cloud.sdsc.edu/v1/AUTH_opentopography/Raster/SRTM_GL1/SRTM_GL1_srtm_25_04.tif",
"landsat_india": "https://landsat-pds.s3.amazonaws.com/c1/L8/148/041/LC08_L1TP_148041_20170101_20170218_01_T1/LC08_L1TP_148041_20170101_20170218_01_T1_B4.TIF",
"test_raster": "https://github.com/rasterio/rasterio/raw/master/tests/data/RGB.byte.tif"
}
def get_mock_raster_url(layer_name: str, bounds=None):
"""Get a working raster URL for testing - Indian region focused"""
if "elevation" in layer_name.lower() or "dem" in layer_name.lower():
return MOCK_RASTER_URLS["elevation"]
elif "landsat" in layer_name.lower() or "vegetation" in layer_name.lower() or "ndvi" in layer_name.lower():
return MOCK_RASTER_URLS["landsat_india"]
else:
return MOCK_RASTER_URLS["test_raster"]
# --- Node functions ---
def llm_intent_parser(state: Dict[str, Any]) -> Dict[str, Any]:
user_query = state["user_query"]
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
temperature=0,
google_api_key=GEMINI_API_KEY
)
prompt = (
"You are analyzing a geospatial query. Extract and classify the request with high precision. "
"Return ONLY valid JSON, no markdown, code blocks, or explanatory text.\n\n"
"REQUIRED FIELDS (use null if cannot determine):\n"
"- uid: string (MWS identifier like '12_75340', extract if present)\n"
"- latitude: number (decimal degrees, -90 to 90)\n"
"- longitude: number (decimal degrees, -180 to 180)\n"
"- metric_text: string (what user wants to analyze)\n"
"- start_year: number (for temporal queries, extract year only)\n"
"- end_year: number (for temporal queries, extract year only)\n"
"- analysis_type: one of ['timeseries', 'spatial_summary', 'change_detection', 'spatial_trend']\n"
"- data_type_needed: one of ['vector', 'raster', 'both', 'timeseries']\n"
"- spatial_operation: one of ['point_query', 'area_summary', 'buffer_analysis', 'trend_analysis']\n"
"- confidence: number (0.0 to 1.0)\n"
"- clarification_needed: boolean\n"
"- explanation: string (brief reasoning)\n\n"
"CLASSIFICATION RULES:\n"
"1. TIMESERIES queries: contain years/time periods + metrics like 'change', 'trend', 'from X to Y'\n"
" - data_type_needed: 'timeseries'\n"
" - analysis_type: 'timeseries' or 'change_detection'\n\n"
"2. VECTOR queries: spatial features, counts, areas, boundaries\n"
" - Keywords: 'water bodies', 'features', 'area', 'count', 'boundary', 'polygon', 'SOGE', 'drainage', 'aquifer'\n"
" - data_type_needed: 'vector'\n"
" - analysis_type: 'spatial_summary'\n\n"
"3. RASTER queries: continuous data, statistics, values at points\n"
" - Keywords: 'vegetation', 'NDVI', 'elevation', 'slope', 'index', 'average', 'statistics', 'distribution'\n"
" - data_type_needed: 'raster'\n"
" - analysis_type: 'spatial_summary'\n\n"
"4. BUFFER/PROXIMITY queries: 'within X km', 'near', 'around'\n"
" - spatial_operation: 'buffer_analysis'\n\n"
"EDGE CASES:\n"
"- If query has both temporal and spatial elements, prioritize the dominant aspect\n"
"- If unsure between raster/vector, use 'both'\n"
"- Set clarification_needed=true if query is ambiguous\n"
"- Extract coordinates even if mixed with text (e.g., 'near 25.31, 75.09')\n"
"- For 'uid' or 'UID' followed by numbers/underscores, extract as uid\n\n"
f"QUERY: {user_query}\n\n"
"OUTPUT (valid JSON only):"
)
response = llm.invoke(prompt)
print('Gemini raw output:', response.content) # Debug print
content = response.content.strip()
content = re.sub(r"^```json\s*|```$", "", content, flags=re.MULTILINE).strip()
try:
parsed = json.loads(content)
state["parsed"] = parsed
print(f"parsed -> JSON result: {parsed}")
except Exception as e:
state["error"] = f"LLM output not valid JSON: {content}"
# Register artifact
artifact_content = {"user_query": user_query, "parsed": state.get("parsed")}
if state.get("error"):
artifact_content["error"] = state["error"]
artifact_id = artifact_registry.register(
"llm_intent",
artifact_content,
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
return state
def canonicalize_year(year):
try:
y = int(year)
return f"{y}-{y+1}"
except Exception:
return str(year)
def validate(state: Dict[str, Any]) -> Dict[str, Any]:
parsed = state["parsed"]
uid = parsed.get("uid")
start_year = parsed.get("start_year")
end_year = parsed.get("end_year")
metric_text = parsed.get("metric_text")
latitude = parsed.get("latitude")
longitude = parsed.get("longitude")
data_type_needed = parsed.get("data_type_needed", "timeseries")
# Check for location identifier (UID or coordinates)
if not ((uid) or (latitude is not None and longitude is not None)):
state["error"] = "Missing required fields: either UID or latitude/longitude coordinates must be provided"
return state
# Validate coordinates if provided
if latitude is not None:
try:
lat_val = float(latitude)
if not (-90 <= lat_val <= 90):
state["error"] = f"Invalid latitude: {latitude}. Must be between -90 and 90."
return state
except (ValueError, TypeError):
state["error"] = f"Invalid latitude format: {latitude}. Must be a number."
return state
if longitude is not None:
try:
lon_val = float(longitude)
if not (-180 <= lon_val <= 180):
state["error"] = f"Invalid longitude: {longitude}. Must be between -180 and 180."
return state
except (ValueError, TypeError):
state["error"] = f"Invalid longitude format: {longitude}. Must be a number."
return state
# Validate metric text
if not metric_text or not metric_text.strip():
state["error"] = "Missing required field: metric_text (what to analyze)"
return state
# For timeseries queries, years are required
if data_type_needed == "timeseries":
if not start_year or not end_year:
state["error"] = "For timeseries analysis, both start_year and end_year are required"
return state
# Validate and canonicalize years
try:
start_val = int(start_year)
end_val = int(end_year)
if start_val > end_val:
state["error"] = f"Invalid year range: start_year ({start_val}) must be <= end_year ({end_val})"
return state
if start_val < 1900 or end_val > 2030:
state["error"] = f"Invalid year range: years must be between 1900 and 2030"
return state
# Canonicalize years to fiscal year format
parsed["start_year"] = canonicalize_year(start_year)
parsed["end_year"] = canonicalize_year(end_year)
except (ValueError, TypeError):
state["error"] = f"Invalid year format: start_year={start_year}, end_year={end_year}. Must be integers."
return state
# Validate data_type_needed
valid_data_types = ['vector', 'raster', 'both', 'timeseries']
if data_type_needed not in valid_data_types:
state["error"] = f"Invalid data_type_needed: {data_type_needed}. Must be one of {valid_data_types}"
return state
state["parsed"] = parsed
# Register artifact
artifact_content = {"parsed": state.get("parsed")}
if state.get("error"):
artifact_content["error"] = state["error"]
artifact_id = artifact_registry.register(
"validate",
artifact_content,
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
return state
def fetch_mws_data(state: Dict[str, Any]) -> Dict[str, Any]:
"""
Fetches MWS data from the CoreStack API using either UID or lat/long coordinates.
"""
if "error" in state:
return state
base_url = "https://geoserver.core-stack.org/api/v1/"
headers = {"X-API-Key": CORE_STACK_API_KEY}
# Get UID and coordinates from parsed data
uid = state["parsed"].get("uid")
latitude = state["parsed"].get("latitude")
longitude = state["parsed"].get("longitude")
# Handle UID-only or coordinate-only queries
mws_info = {}
if uid and not (latitude and longitude):
# UID provided but no coordinates - get location info from UID
# For now, we'll need coordinates for spatial analysis, so prompt user
state["error"] = f"UID '{uid}' provided but coordinates needed for spatial analysis. Please also provide latitude and longitude."
return state
elif (latitude and longitude) and not uid:
# Coordinates provided but no UID - get UID from coordinates
params_latlon = {"latitude": latitude, "longitude": longitude}
print("CORE_STACK_API_KEY:", CORE_STACK_API_KEY)
print("Headers being sent:", headers)
response_mwsid = requests.get(f"{base_url}get_mwsid_by_latlon/", params=params_latlon, headers=headers)
if response_mwsid.status_code != 200:
state["error"] = f"Could not get UID from coordinates: {response_mwsid.text}"
return state
mws_info = response_mwsid.json()
# Store the UID we got from coordinates
if mws_info.get('uid'):
state["parsed"]["uid"] = mws_info.get('uid')
uid = mws_info.get('uid')
elif uid and latitude and longitude:
# Both provided - use coordinates to get location info
params_latlon = {"latitude": latitude, "longitude": longitude}
response_mwsid = requests.get(f"{base_url}get_mwsid_by_latlon/", params=params_latlon, headers=headers)
if response_mwsid.status_code != 200:
state["error"] = f"Could not get location info: {response_mwsid.text}"
return state
mws_info = response_mwsid.json()
else:
state["error"] = "Either UID or latitude/longitude coordinates must be provided"
return state
print(f"MWS Info: {mws_info}")
# If we have a UID from coordinates but none was provided in query, store it
if not uid and mws_info.get('uid'):
state["parsed"]["uid"] = mws_info.get('uid')
# Step 2: Get full data using location parameters
params_mws_data = {
'state': mws_info.get('State'),
'district': mws_info.get('District'),
'tehsil': mws_info.get('Tehsil'),
'mws_id': mws_info.get('uid')
}
response = requests.get(f"{base_url}get_mws_data/", params=params_mws_data, headers=headers)
print(f"API Response Status: {response.status_code}")
if response.status_code != 200:
state["error"] = f"Step 2 API call failed with status {response.status_code}: {response.text}"
return state
mws_json = response.json()
print(f"API Response keys: {list(mws_json.keys())}")
state["mws_json"] = mws_json
# Register artifact
artifact_content = {"mws_json": state.get("mws_json")}
if state.get("error"):
artifact_content["error"] = state["error"]
artifact_id = artifact_registry.register(
"fetch_mws_data",
artifact_content,
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
return state
print(f"API Response keys: {list(mws_json.keys())}")
state["mws_json"] = mws_json
return state
def normalize_data(state: Dict[str, Any]) -> Dict[str, Any]:
"""
LLM-powered normalizer that intelligently maps metrics to appropriate data blocks and fields.
"""
if "error" in state:
return state
mws_json = state["mws_json"]
metric_text = state["parsed"].get("metric_text", "").lower().strip()
# Use LLM to identify the appropriate data block and field prefix
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
temperature=0,
google_api_key=GEMINI_API_KEY
)
# Create a simplified representation of the data structure
data_structure = {}
for block_name, block_data in mws_json.items():
if not block_data or not isinstance(block_data, list) or not block_data[0]:
continue
# Get a sample of keys from each block
sample_keys = list(block_data[0].keys())[:20] # Limit to first 20 keys for token efficiency
data_structure[block_name] = sample_keys
prompt = (
f"You are a data analyst expert mapping user queries to database fields. "
f"Find the correct data block and field prefix for time-series analysis.\n\n"
f"USER METRIC: '{metric_text}'\n\n"
f"AVAILABLE DATA STRUCTURE:\n{json.dumps(data_structure, indent=2)}\n\n"
f"MAPPING RULES:\n"
f"1. Look for fields with year patterns like 'fieldname_2017-2018', 'fieldname_in_unit_2017-2018'\n"
f"2. Match user metric to field semantics:\n"
f" - 'precipitation' → 'precipitation_in_mm_'\n"
f" - 'cropping intensity' → 'cropping_intensity_'\n"
f" - 'evapotranspiration' → 'et_in_mm_'\n"
f" - 'runoff' → 'runoff_in_mm_'\n"
f" - 'groundwater' → 'g_in_mm_' or 'deltag_in_mm_'\n"
f" - 'well depth' → 'welldepth_in_m_'\n\n"
f"3. Common data blocks:\n"
f" - 'hydrological_annual': water-related metrics\n"
f" - 'terrain': topographic data\n"
f" - 'aquifer_vector': groundwater/aquifer data\n\n"
f"TASK: Return JSON with:\n"
f"- 'block': exact data block name containing the metric\n"
f"- 'key_prefix': field prefix before the year (including trailing underscore)\n"
f"- 'confidence': float 0.0-1.0 indicating match certainty\n\n"
f"VALIDATION:\n"
f"- Ensure the block exists in the provided data structure\n"
f"- Ensure fields with this prefix + year pattern exist\n"
f"- If no good match, set confidence < 0.5\n\n"
f"Example: {{\"block\": \"hydrological_annual\", \"key_prefix\": \"precipitation_in_mm_\", \"confidence\": 0.9}}"
)
try:
response = llm.invoke(prompt)
print("LLM analysis of data structure:", response.content)
# Parse the LLM's response to get block and key_prefix
# Handle both JSON and plain text formats
content = response.content.strip()
if content.startswith("{") and content.endswith("}"):
# It's a JSON response
mapping = json.loads(content)
else:
# Try to extract JSON from text if not already in JSON format
match = re.search(r'\{.*\}', content, re.DOTALL)
if match:
json_str = match.group(0)
mapping = json.loads(json_str)
else:
state["error"] = f"Could not parse LLM response: {content}"
return state
data_block = mapping.get("block")
key_prefix = mapping.get("key_prefix")
if not data_block or not key_prefix:
state["error"] = f"LLM did not provide valid block and key_prefix: {content}"
return state
print(f"LLM selected data block: {data_block}, key prefix: {key_prefix}")
# Extract timeseries data using the LLM-identified block and prefix
rows = []
block_data = mws_json.get(data_block, [{}])[0]
for k, v in block_data.items():
if isinstance(v, (int, float)) and k.startswith(key_prefix):
# Extract year from pattern like 'prefix_2017-2018'
year = k.replace(key_prefix, '')
if year.count("-") == 1: # Validate year format
rows.append({
"year": year,
"value": v,
"source": f"{data_block}.{k}"
})
if not rows:
# Fallback - if LLM suggestion didn't yield data, try some common variations of the key prefix
print(f"No data found with exact prefix. Trying variations of {key_prefix}")
# Try variations like removing underscores, adding/removing "_in_", etc.
variations = [
key_prefix,
key_prefix.replace("_", ""),
re.sub(r'_in_[a-z]+_$', "_", key_prefix),
re.sub(r'_$', "", key_prefix),
key_prefix + "_"
]
for variation in variations:
for k, v in block_data.items():
if isinstance(v, (int, float)) and k.startswith(variation):
# Extract year from pattern
year = k.replace(variation, '')
if year.count("-") == 1: # Validate year format
rows.append({
"year": year,
"value": v,
"source": f"{data_block}.{k}"
})
if rows:
key_prefix = variation
break
if not rows:
state["error"] = f"Could not find time series data for metric: {metric_text}"
return state
print("Available years in timeseries:", [r["year"] for r in rows])
state["timeseries"] = rows
state["metric_block"] = data_block
state["metric_key_prefix"] = key_prefix
# Register artifact using geospatial registry
geospatial_data = GeospatialDataHandler.convert_to_geospatial_model({
"timeseries": rows,
"metric_text": metric_text
})
geo_artifact_id = geo_artifact_registry.register_geospatial_artifact(
artifact_type="normalize_data",
data=geospatial_data,
parent_id=state.get("artifact_id"),
processing_node="normalize_data",
processing_params={"data_block": data_block, "key_prefix": key_prefix}
)
# Also register in legacy registry for compatibility
artifact_content = {"timeseries": state.get("timeseries")}
if state.get("error"):
artifact_content["error"] = state["error"]
artifact_id = artifact_registry.register(
"normalize_data",
artifact_content,
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
state["geo_artifact_id"] = geo_artifact_id
return state
except Exception as e:
state["error"] = f"Error using LLM to analyze data structure: {str(e)}"
# Register artifact with error
artifact_id = artifact_registry.register(
"normalize_data",
{"timeseries": state.get("timeseries"), "error": state.get("error")},
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
return state
def compute_timeseries_stats(state: Dict[str, Any]) -> Dict[str, Any]:
if "error" in state:
return state
timeseries = state["timeseries"]
parsed = state["parsed"]
requested_start_year = parsed["start_year"]
requested_end_year = parsed["end_year"]
ts = sorted([r for r in timeseries if r.get('value') is not None], key=lambda r: r['year'])
if not ts:
state["stats"] = {"error": "No valid timeseries data available"}
return state
year_to_val = {r['year']: float(r['value']) for r in ts}
# Get the closest available years if exact matches are not found
start_year = requested_start_year
end_year = requested_end_year
# Find closest available start year
if requested_start_year not in year_to_val:
# Extract just the first year from each string (e.g. "2017-2018" -> 2017)
req_start = int(requested_start_year.split('-')[0])
available_years = sorted([(int(y.split('-')[0]), y) for y in year_to_val.keys()])
closest_start = min(available_years, key=lambda x: abs(x[0] - req_start))[1]
start_year = closest_start
# Find closest available end year
if requested_end_year not in year_to_val:
# Extract just the first year from each string (e.g. "2017-2018" -> 2017)
req_end = int(requested_end_year.split('-')[0])
available_years = sorted([(int(y.split('-')[0]), y) for y in year_to_val.keys()])
closest_end = min(available_years, key=lambda x: abs(x[0] - req_end))[1]
end_year = closest_end
start_val = year_to_val[start_year]
end_val = year_to_val[end_year]
pct_change = None
if start_val != 0:
pct_change = round((end_val - start_val) / start_val * 100.0, 4)
peak_row = max(ts, key=lambda r: r['value'])
state["stats"] = {
"start_val": start_val,
"end_val": end_val,
"percent_change": pct_change,
"peak_year": peak_row["year"],
"peak_value": peak_row["value"],
"slope": (end_val - start_val) / len(ts) if len(ts) > 1 else 0,
"sources": [peak_row["source"], ts[0]["source"], ts[-1]["source"]],
"actual_start_year": start_year,
"actual_end_year": end_year,
"requested_start_year": requested_start_year,
"requested_end_year": requested_end_year
}
# Register artifact
artifact_content = {"stats": state.get("stats")}
if state.get("error"):
artifact_content["error"] = state["error"]
artifact_id = artifact_registry.register(
"compute_stats",
artifact_content,
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
return state
def format_response(state: Dict[str, Any]) -> Dict[str, Any]:
"""Enhanced response formatter for all data types: timeseries, raster, and vector"""
# Debug the state structure
print("Type of state in format_response:", type(state))
print("State content keys:", list(state.keys()) if isinstance(state, dict) else "Not a dict")
if "error" in state:
state["response"] = state["error"]
# Register artifact
artifact_content = {"response": state.get("response")}
if state.get("error"):
artifact_content["error"] = state["error"]
artifact_id = artifact_registry.register(
"format_response",
artifact_content,
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
return state
parsed = state["parsed"]
# Handle different response types
if "spatial_analysis_results" in state:
# Spatial analysis response
results = state["spatial_analysis_results"]
metric_text = parsed.get("metric_text", "analysis")
# Get location info
location_info = state.get("location_info", {})
if parsed.get("uid"):
location_str = f"UID {parsed['uid']}"
else:
lat = parsed.get("latitude", "unknown")
lon = parsed.get("longitude", "unknown")
location_str = f"Location ({lat}, {lon})"
# Use LLM to intelligently interpret and format the spatial analysis results
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
temperature=0.3, # Slightly creative for better formatting
google_api_key=GEMINI_API_KEY
)
# Prepare the data for LLM interpretation
analysis_data = {
"user_query": state["user_query"],
"metric_requested": metric_text,
"location": location_str,
"spatial_results": results
}
prompt = (
f"You are a geospatial data analyst. The user asked: '{state['user_query']}'\n\n"
f"ANALYSIS RESULTS:\n{json.dumps(analysis_data, indent=2, default=str)}\n\n"
f"INSTRUCTIONS:\n"
f"1. Create a clear, informative response that directly answers the user's question\n"
f"2. Focus on the most relevant insights from the data\n"
f"3. Use appropriate emojis and formatting for readability\n"
f"4. For land use queries: categorize and summarize land use types\n"
f"5. For agricultural queries: focus on farming statistics and areas\n"
f"6. For raster data: explain what the values represent in context\n"
f"7. For vector data: summarize features and key attributes meaningfully\n"
f"8. Keep it concise but informative (aim for 5-10 key points)\n"
f"9. Don't dump raw statistics - interpret what they mean\n"
f"10. Use hectares for areas, include totals and percentages where relevant\n\n"
f"CONTEXT HINTS:\n"
f"- 'doubly_cro' = double cropping areas\n"
f"- 'single_kha' = single cropping (kharif season)\n"
f"- 'built-up' = urban/residential areas\n"
f"- 'tree_fores' = forest cover\n"
f"- 'barrenland' = unused/barren land\n"
f"- 'k_water' = kharif water bodies\n"
f"- 'kr_water' = rabi water bodies\n"
f"- Area values are typically in hectares\n\n"
f"Generate a response that would be helpful to a researcher or policymaker:"
)
try:
llm_response = llm.invoke(prompt)
formatted_response = llm_response.content.strip()
# Clean up any markdown formatting if present
formatted_response = re.sub(r"^```.*\n|```$", "", formatted_response, flags=re.MULTILINE).strip()
state["response"] = formatted_response
except Exception as e:
print(f"Error in LLM formatting: {e}")
# Fallback to simple summary
response_parts = [f"Spatial analysis for {metric_text} at {location_str}:\n"]
for layer_name, result in results.items():
if result['type'] == 'vector':
data = result['data']
response_parts.append(f"• {layer_name}: {data['feature_count']} features analyzed")
elif result['type'] == 'raster':
data = result['data']
response_parts.append(f"• {layer_name}: mean={data['mean']:.2f}, range=[{data['min']:.2f}, {data['max']:.2f}]")
if not results:
response_parts.append("No matching spatial data found for this query.")
state["response"] = "\n".join(response_parts)
# Register artifact
artifact_content = {"response": state.get("response")}
artifact_id = artifact_registry.register(
"format_response",
artifact_content,
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
elif "stats" in state:
# Timeseries analysis response (existing logic)
stats = state["stats"]
uid = parsed.get("uid")
requested_start_year = parsed.get("start_year")
requested_end_year = parsed.get("end_year")
metric_text = parsed.get("metric_text", "value")
# Get actual years used for analysis (may differ from requested if not available)
actual_start_year = stats.get("actual_start_year", requested_start_year)
actual_end_year = stats.get("actual_end_year", requested_end_year)
# Get location identifier (either UID or coordinates)
if uid:
location_str = f"UID {uid}"
else:
lat = parsed.get("latitude")
lon = parsed.get("longitude")
location_str = f"Location ({lat:.5f}, {lon:.5f})" if lat and lon else "Unknown location"
if "error" in stats:
state["response"] = stats["error"]
else:
# Note if we had to use different years than requested
year_note = ""
if (requested_start_year and requested_end_year and
(actual_start_year != requested_start_year or actual_end_year != requested_end_year)):
year_note = f" (Note: Used available years {actual_start_year} to {actual_end_year})"
state["response"] = (
f"{location_str} — {metric_text.title()} changed from {stats['start_val']} ({actual_start_year}) "
f"to a peak of {stats['peak_value']} ({stats['peak_year']}) and is {stats['end_val']} in {actual_end_year}. "
f"Net change {actual_start_year}→{actual_end_year} ≈ {stats['percent_change']}%.{year_note} "
f"Data sources: {', '.join(stats['sources'])}."
)
# Register artifact
artifact_content = {"response": state.get("response")}
if state.get("error"):
artifact_content["error"] = state["error"]
artifact_id = artifact_registry.register(
"format_response",
artifact_content,
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
else:
# Fallback response
state["response"] = "Analysis completed, but no results were generated."
# Register artifact
artifact_id = artifact_registry.register(
"format_response",
{"response": state.get("response")},
parent_id=state.get("artifact_id")
)
state["artifact_id"] = artifact_id
return state
def fetch_spatial_layers(state: Dict[str, Any]) -> Dict[str, Any]:
"""Fetch available spatial layers for the location"""
if "error" in state:
return state
base_url = "https://geoserver.core-stack.org/api/v1/"
headers = {"X-API-Key": CORE_STACK_API_KEY}
# Get location info (same as existing logic)
latitude = state["parsed"].get("latitude")
longitude = state["parsed"].get("longitude")
uid = state["parsed"].get("uid")