geomind-ai 1.0.0__py3-none-any.whl → 1.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- geomind/__init__.py +11 -11
- geomind/agent.py +441 -445
- geomind/cli.py +125 -125
- geomind/config.py +55 -55
- geomind/tools/__init__.py +27 -27
- geomind/tools/geocoding.py +108 -108
- geomind/tools/processing.py +351 -349
- geomind/tools/stac_search.py +234 -231
- {geomind_ai-1.0.0.dist-info → geomind_ai-1.0.2.dist-info}/METADATA +78 -85
- geomind_ai-1.0.2.dist-info/RECORD +14 -0
- {geomind_ai-1.0.0.dist-info → geomind_ai-1.0.2.dist-info}/licenses/LICENSE +21 -21
- geomind_ai-1.0.0.dist-info/RECORD +0 -14
- {geomind_ai-1.0.0.dist-info → geomind_ai-1.0.2.dist-info}/WHEEL +0 -0
- {geomind_ai-1.0.0.dist-info → geomind_ai-1.0.2.dist-info}/entry_points.txt +0 -0
- {geomind_ai-1.0.0.dist-info → geomind_ai-1.0.2.dist-info}/top_level.txt +0 -0
geomind/agent.py
CHANGED
|
@@ -1,445 +1,441 @@
|
|
|
1
|
-
"""
|
|
2
|
-
GeoMind Agent - Main agent class powered by OpenRouter.
|
|
3
|
-
|
|
4
|
-
This module implements the AI agent that can understand natural language
|
|
5
|
-
queries about satellite imagery and execute the appropriate tools.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
import json
|
|
9
|
-
|
|
10
|
-
from
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
from .
|
|
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
|
-
self.provider = "openrouter"
|
|
247
|
-
self.api_key =
|
|
248
|
-
self.model_name = model or OPENROUTER_MODEL
|
|
249
|
-
self.base_url = OPENROUTER_API_URL
|
|
250
|
-
|
|
251
|
-
if not self.api_key:
|
|
252
|
-
raise ValueError(
|
|
253
|
-
"OpenRouter API key required.\n"
|
|
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
|
-
return
|
|
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
|
-
print("
|
|
403
|
-
print("
|
|
404
|
-
print("=" * 60)
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
print(
|
|
414
|
-
sys.exit(1)
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
if user_input.lower()
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
if __name__ == "__main__":
|
|
445
|
-
main()
|
|
1
|
+
"""
|
|
2
|
+
GeoMind Agent - Main agent class powered by OpenRouter.
|
|
3
|
+
|
|
4
|
+
This module implements the AI agent that can understand natural language
|
|
5
|
+
queries about satellite imagery and execute the appropriate tools.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
import re
|
|
10
|
+
from typing import Optional, Callable, Any
|
|
11
|
+
from datetime import datetime
|
|
12
|
+
|
|
13
|
+
from openai import OpenAI
|
|
14
|
+
|
|
15
|
+
from .config import OPENROUTER_API_KEY, OPENROUTER_API_URL, OPENROUTER_MODEL
|
|
16
|
+
from .tools import (
|
|
17
|
+
geocode_location,
|
|
18
|
+
get_bbox_from_location,
|
|
19
|
+
search_imagery,
|
|
20
|
+
get_item_details,
|
|
21
|
+
list_recent_imagery,
|
|
22
|
+
create_rgb_composite,
|
|
23
|
+
calculate_ndvi,
|
|
24
|
+
get_band_statistics,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# Map tool names to functions
|
|
29
|
+
TOOL_FUNCTIONS = {
|
|
30
|
+
"geocode_location": geocode_location,
|
|
31
|
+
"get_bbox_from_location": get_bbox_from_location,
|
|
32
|
+
"search_imagery": search_imagery,
|
|
33
|
+
"list_recent_imagery": list_recent_imagery,
|
|
34
|
+
"get_item_details": get_item_details,
|
|
35
|
+
"create_rgb_composite": create_rgb_composite,
|
|
36
|
+
"calculate_ndvi": calculate_ndvi,
|
|
37
|
+
"get_band_statistics": get_band_statistics,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
# Tool definitions for the LLM
|
|
41
|
+
TOOLS = [
|
|
42
|
+
{
|
|
43
|
+
"type": "function",
|
|
44
|
+
"function": {
|
|
45
|
+
"name": "geocode_location",
|
|
46
|
+
"description": "Convert a place name to geographic coordinates (latitude, longitude). Use this when you need to find coordinates for a location.",
|
|
47
|
+
"parameters": {
|
|
48
|
+
"type": "object",
|
|
49
|
+
"properties": {
|
|
50
|
+
"place_name": {
|
|
51
|
+
"type": "string",
|
|
52
|
+
"description": "The name of the place to geocode (e.g., 'New York City', 'Paris, France')",
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"required": ["place_name"],
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"type": "function",
|
|
61
|
+
"function": {
|
|
62
|
+
"name": "get_bbox_from_location",
|
|
63
|
+
"description": "Get a bounding box for a location, suitable for searching satellite imagery.",
|
|
64
|
+
"parameters": {
|
|
65
|
+
"type": "object",
|
|
66
|
+
"properties": {
|
|
67
|
+
"place_name": {
|
|
68
|
+
"type": "string",
|
|
69
|
+
"description": "The name of the place",
|
|
70
|
+
},
|
|
71
|
+
"buffer_km": {
|
|
72
|
+
"type": "number",
|
|
73
|
+
"description": "Buffer distance in kilometers (default: 10)",
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
"required": ["place_name"],
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"type": "function",
|
|
82
|
+
"function": {
|
|
83
|
+
"name": "search_imagery",
|
|
84
|
+
"description": "Search for Sentinel-2 satellite imagery in the EOPF catalog. Returns available scenes.",
|
|
85
|
+
"parameters": {
|
|
86
|
+
"type": "object",
|
|
87
|
+
"properties": {
|
|
88
|
+
"bbox": {
|
|
89
|
+
"type": "array",
|
|
90
|
+
"items": {"type": "number"},
|
|
91
|
+
"description": "Bounding box as [min_lon, min_lat, max_lon, max_lat]",
|
|
92
|
+
},
|
|
93
|
+
"start_date": {
|
|
94
|
+
"type": "string",
|
|
95
|
+
"description": "Start date in YYYY-MM-DD format",
|
|
96
|
+
},
|
|
97
|
+
"end_date": {
|
|
98
|
+
"type": "string",
|
|
99
|
+
"description": "End date in YYYY-MM-DD format",
|
|
100
|
+
},
|
|
101
|
+
"max_cloud_cover": {
|
|
102
|
+
"type": "number",
|
|
103
|
+
"description": "Maximum cloud cover percentage (0-100)",
|
|
104
|
+
},
|
|
105
|
+
"max_items": {
|
|
106
|
+
"type": "integer",
|
|
107
|
+
"description": "Maximum number of results",
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
"required": [],
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"type": "function",
|
|
116
|
+
"function": {
|
|
117
|
+
"name": "list_recent_imagery",
|
|
118
|
+
"description": "List recent Sentinel-2 imagery for a location. Combines geocoding and search.",
|
|
119
|
+
"parameters": {
|
|
120
|
+
"type": "object",
|
|
121
|
+
"properties": {
|
|
122
|
+
"location_name": {
|
|
123
|
+
"type": "string",
|
|
124
|
+
"description": "Name of the location to search",
|
|
125
|
+
},
|
|
126
|
+
"days": {
|
|
127
|
+
"type": "integer",
|
|
128
|
+
"description": "Number of days to look back (default: 7)",
|
|
129
|
+
},
|
|
130
|
+
"max_cloud_cover": {
|
|
131
|
+
"type": "number",
|
|
132
|
+
"description": "Maximum cloud cover percentage",
|
|
133
|
+
},
|
|
134
|
+
"max_items": {
|
|
135
|
+
"type": "integer",
|
|
136
|
+
"description": "Maximum number of results",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
"required": [],
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
"type": "function",
|
|
145
|
+
"function": {
|
|
146
|
+
"name": "get_item_details",
|
|
147
|
+
"description": "Get detailed information about a specific Sentinel-2 scene by its ID.",
|
|
148
|
+
"parameters": {
|
|
149
|
+
"type": "object",
|
|
150
|
+
"properties": {
|
|
151
|
+
"item_id": {"type": "string", "description": "The STAC item ID"}
|
|
152
|
+
},
|
|
153
|
+
"required": ["item_id"],
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
"type": "function",
|
|
159
|
+
"function": {
|
|
160
|
+
"name": "create_rgb_composite",
|
|
161
|
+
"description": "Create an RGB true-color composite image from Sentinel-2 data.",
|
|
162
|
+
"parameters": {
|
|
163
|
+
"type": "object",
|
|
164
|
+
"properties": {
|
|
165
|
+
"zarr_url": {
|
|
166
|
+
"type": "string",
|
|
167
|
+
"description": "URL to the SR_10m Zarr asset from a STAC item",
|
|
168
|
+
},
|
|
169
|
+
"output_path": {
|
|
170
|
+
"type": "string",
|
|
171
|
+
"description": "Optional path to save the output image",
|
|
172
|
+
},
|
|
173
|
+
"subset_size": {
|
|
174
|
+
"type": "integer",
|
|
175
|
+
"description": "Size to subset the image (default: 1000 pixels)",
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
"required": ["zarr_url"],
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
"type": "function",
|
|
184
|
+
"function": {
|
|
185
|
+
"name": "calculate_ndvi",
|
|
186
|
+
"description": "Calculate NDVI (vegetation index) from Sentinel-2 data.",
|
|
187
|
+
"parameters": {
|
|
188
|
+
"type": "object",
|
|
189
|
+
"properties": {
|
|
190
|
+
"zarr_url": {
|
|
191
|
+
"type": "string",
|
|
192
|
+
"description": "URL to the SR_10m Zarr asset",
|
|
193
|
+
},
|
|
194
|
+
"output_path": {
|
|
195
|
+
"type": "string",
|
|
196
|
+
"description": "Optional path to save the NDVI image",
|
|
197
|
+
},
|
|
198
|
+
"subset_size": {
|
|
199
|
+
"type": "integer",
|
|
200
|
+
"description": "Size to subset the image",
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
"required": ["zarr_url"],
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
"type": "function",
|
|
209
|
+
"function": {
|
|
210
|
+
"name": "get_band_statistics",
|
|
211
|
+
"description": "Get statistics (min, max, mean) for spectral bands.",
|
|
212
|
+
"parameters": {
|
|
213
|
+
"type": "object",
|
|
214
|
+
"properties": {
|
|
215
|
+
"zarr_url": {
|
|
216
|
+
"type": "string",
|
|
217
|
+
"description": "URL to the Zarr asset",
|
|
218
|
+
},
|
|
219
|
+
"bands": {
|
|
220
|
+
"type": "array",
|
|
221
|
+
"items": {"type": "string"},
|
|
222
|
+
"description": "List of band names to analyze",
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
"required": ["zarr_url"],
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
]
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
class GeoMindAgent:
|
|
233
|
+
"""
|
|
234
|
+
GeoMind - An AI agent for geospatial analysis with Sentinel-2 imagery.
|
|
235
|
+
|
|
236
|
+
Uses OpenRouter API for access to multiple AI models.
|
|
237
|
+
"""
|
|
238
|
+
|
|
239
|
+
def __init__(self, model: Optional[str] = None):
|
|
240
|
+
"""
|
|
241
|
+
Initialize the GeoMind agent.
|
|
242
|
+
|
|
243
|
+
Args:
|
|
244
|
+
model: Model name (default: xiaomi/mimo-v2-flash:free)
|
|
245
|
+
"""
|
|
246
|
+
self.provider = "openrouter"
|
|
247
|
+
self.api_key = OPENROUTER_API_KEY
|
|
248
|
+
self.model_name = model or OPENROUTER_MODEL
|
|
249
|
+
self.base_url = OPENROUTER_API_URL
|
|
250
|
+
|
|
251
|
+
if not self.api_key:
|
|
252
|
+
raise ValueError(
|
|
253
|
+
"OpenRouter API key required. Set OPENROUTER_API_KEY in .env file.\n"
|
|
254
|
+
"Get your API key at: https://openrouter.ai/settings/keys"
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
print(f"🚀 GeoMind Agent initialized with {self.model_name} (OpenRouter)")
|
|
258
|
+
print(f" API URL: {self.base_url}")
|
|
259
|
+
|
|
260
|
+
# Create OpenAI-compatible client
|
|
261
|
+
self.client = OpenAI(base_url=self.base_url, api_key=self.api_key)
|
|
262
|
+
|
|
263
|
+
# Chat history
|
|
264
|
+
self.history = []
|
|
265
|
+
|
|
266
|
+
# Add system message
|
|
267
|
+
self.system_prompt = self._get_system_prompt()
|
|
268
|
+
|
|
269
|
+
def _get_system_prompt(self) -> str:
|
|
270
|
+
"""Get the system prompt for the agent."""
|
|
271
|
+
return f"""You are GeoMind, an expert AI assistant specialized in geospatial analysis
|
|
272
|
+
and satellite imagery. You help users find, analyze, and visualize Sentinel-2 satellite data
|
|
273
|
+
from the EOPF (ESA Earth Observation Processing Framework) catalog.
|
|
274
|
+
|
|
275
|
+
Your capabilities include:
|
|
276
|
+
1. **Search**: Find Sentinel-2 L2A imagery by location, date, and cloud cover
|
|
277
|
+
2. **Geocoding**: Convert place names to coordinates for searching
|
|
278
|
+
3. **Visualization**: Create RGB composites and NDVI maps from imagery
|
|
279
|
+
4. **Analysis**: Calculate spectral indices and band statistics
|
|
280
|
+
|
|
281
|
+
Key information:
|
|
282
|
+
- Data source: EOPF STAC API (https://stac.core.eopf.eodc.eu)
|
|
283
|
+
- Satellite: Sentinel-2 (L2A surface reflectance products)
|
|
284
|
+
- Bands available: B01-B12 at 10m, 20m, or 60m resolution
|
|
285
|
+
- Current date: {datetime.now().strftime('%Y-%m-%d')}
|
|
286
|
+
|
|
287
|
+
When users ask for imagery:
|
|
288
|
+
1. First use get_bbox_from_location or list_recent_imagery to search
|
|
289
|
+
2. Present the results clearly with key metadata
|
|
290
|
+
3. Offer to create visualizations if data is found
|
|
291
|
+
|
|
292
|
+
Always explain what you're doing and interpret results in a helpful way."""
|
|
293
|
+
|
|
294
|
+
def _execute_function(self, name: str, args: dict) -> dict:
|
|
295
|
+
"""Execute a function call and return the result."""
|
|
296
|
+
print(f" 🔧 Executing: {name}({args})")
|
|
297
|
+
|
|
298
|
+
if name not in TOOL_FUNCTIONS:
|
|
299
|
+
return {"error": f"Unknown function: {name}"}
|
|
300
|
+
|
|
301
|
+
try:
|
|
302
|
+
result = TOOL_FUNCTIONS[name](**args)
|
|
303
|
+
return result
|
|
304
|
+
except Exception as e:
|
|
305
|
+
return {"error": str(e)}
|
|
306
|
+
|
|
307
|
+
def chat(self, message: str, verbose: bool = True) -> str:
|
|
308
|
+
"""
|
|
309
|
+
Send a message to the agent and get a response.
|
|
310
|
+
"""
|
|
311
|
+
if verbose:
|
|
312
|
+
print(f"\n💬 User: {message}")
|
|
313
|
+
print("🤔 Processing...")
|
|
314
|
+
|
|
315
|
+
# Add user message to history
|
|
316
|
+
self.history.append({"role": "user", "content": message})
|
|
317
|
+
|
|
318
|
+
# Build messages with system prompt
|
|
319
|
+
messages = [{"role": "system", "content": self.system_prompt}] + self.history
|
|
320
|
+
|
|
321
|
+
max_iterations = 10
|
|
322
|
+
iteration = 0
|
|
323
|
+
|
|
324
|
+
while iteration < max_iterations:
|
|
325
|
+
iteration += 1
|
|
326
|
+
|
|
327
|
+
# Call the model
|
|
328
|
+
response = self.client.chat.completions.create(
|
|
329
|
+
model=self.model_name,
|
|
330
|
+
messages=messages,
|
|
331
|
+
tools=TOOLS,
|
|
332
|
+
tool_choice="auto",
|
|
333
|
+
max_tokens=4096,
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
assistant_message = response.choices[0].message
|
|
337
|
+
|
|
338
|
+
# Check if there are tool calls
|
|
339
|
+
if assistant_message.tool_calls:
|
|
340
|
+
# Add assistant message with tool calls to messages
|
|
341
|
+
messages.append(
|
|
342
|
+
{
|
|
343
|
+
"role": "assistant",
|
|
344
|
+
"content": assistant_message.content or "",
|
|
345
|
+
"tool_calls": [
|
|
346
|
+
{
|
|
347
|
+
"id": tc.id,
|
|
348
|
+
"type": "function",
|
|
349
|
+
"function": {
|
|
350
|
+
"name": tc.function.name,
|
|
351
|
+
"arguments": tc.function.arguments,
|
|
352
|
+
},
|
|
353
|
+
}
|
|
354
|
+
for tc in assistant_message.tool_calls
|
|
355
|
+
],
|
|
356
|
+
}
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
# Execute each tool call
|
|
360
|
+
for tool_call in assistant_message.tool_calls:
|
|
361
|
+
func_name = tool_call.function.name
|
|
362
|
+
func_args = json.loads(tool_call.function.arguments)
|
|
363
|
+
|
|
364
|
+
result = self._execute_function(func_name, func_args)
|
|
365
|
+
|
|
366
|
+
# Add tool result to messages
|
|
367
|
+
messages.append(
|
|
368
|
+
{
|
|
369
|
+
"role": "tool",
|
|
370
|
+
"tool_call_id": tool_call.id,
|
|
371
|
+
"content": json.dumps(result, default=str),
|
|
372
|
+
}
|
|
373
|
+
)
|
|
374
|
+
else:
|
|
375
|
+
# No tool calls, we have a final response
|
|
376
|
+
final_text = assistant_message.content or ""
|
|
377
|
+
|
|
378
|
+
# Add to history
|
|
379
|
+
self.history.append({"role": "assistant", "content": final_text})
|
|
380
|
+
|
|
381
|
+
if verbose:
|
|
382
|
+
print(f"\n🌍 GeoMind: {final_text}")
|
|
383
|
+
|
|
384
|
+
return final_text
|
|
385
|
+
|
|
386
|
+
return "Max iterations reached."
|
|
387
|
+
|
|
388
|
+
def reset(self):
|
|
389
|
+
"""Reset the chat session."""
|
|
390
|
+
self.history = []
|
|
391
|
+
print("🔄 Chat session reset")
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def main(model: Optional[str] = None):
|
|
395
|
+
"""Main entry point for CLI usage."""
|
|
396
|
+
import sys
|
|
397
|
+
|
|
398
|
+
print("=" * 60)
|
|
399
|
+
print("🌍 GeoMind - Geospatial AI Agent")
|
|
400
|
+
print("=" * 60)
|
|
401
|
+
print("Powered by OpenRouter | Sentinel-2 Imagery")
|
|
402
|
+
print("Type 'quit' or 'exit' to end the session")
|
|
403
|
+
print("Type 'reset' to start a new conversation")
|
|
404
|
+
print("=" * 60)
|
|
405
|
+
|
|
406
|
+
try:
|
|
407
|
+
agent = GeoMindAgent(model=model)
|
|
408
|
+
except ValueError as e:
|
|
409
|
+
print(f"\n❌ Error: {e}")
|
|
410
|
+
sys.exit(1)
|
|
411
|
+
except Exception as e:
|
|
412
|
+
print(f"\n❌ Error: {e}")
|
|
413
|
+
print("\nPlease check your API key and internet connection.")
|
|
414
|
+
sys.exit(1)
|
|
415
|
+
|
|
416
|
+
while True:
|
|
417
|
+
try:
|
|
418
|
+
user_input = input("\n💬 You: ").strip()
|
|
419
|
+
|
|
420
|
+
if not user_input:
|
|
421
|
+
continue
|
|
422
|
+
|
|
423
|
+
if user_input.lower() in ["quit", "exit", "q"]:
|
|
424
|
+
print("\n👋 Goodbye!")
|
|
425
|
+
break
|
|
426
|
+
|
|
427
|
+
if user_input.lower() == "reset":
|
|
428
|
+
agent.reset()
|
|
429
|
+
continue
|
|
430
|
+
|
|
431
|
+
agent.chat(user_input)
|
|
432
|
+
|
|
433
|
+
except KeyboardInterrupt:
|
|
434
|
+
print("\n\n👋 Goodbye!")
|
|
435
|
+
break
|
|
436
|
+
except Exception as e:
|
|
437
|
+
print(f"\n❌ Error: {e}")
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
if __name__ == "__main__":
|
|
441
|
+
main()
|