File size: 11,050 Bytes
4851501 |
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 |
# LLM Integration & Prompt Engineering
Deep dive into GeoQuery's LLM integration, prompt system, and AI capabilities.
---
## Overview
GeoQuery uses **Google Gemini 2.0 Flash** for all AI capabilities:
- Intent detection
- Text-to-SQL generation
- Natural language explanations
- Layer naming and styling decisions
**Key Feature**: Thinking mode for transparency into reasoning process.
---
## LLMGateway Service
**File**: `backend/core/llm_gateway.py`
Central interface to Gemini API with streaming support.
### Initialization
```python
from google import genai
from google.genai import types
class LLMGateway:
def __init__(self):
api_key = os.getenv("GEMINI_API_KEY")
self.client = genai.Client(api_key=api_key)
self.model = "gemini-2.0-flash-exp"
```
### Configuration
**Model**: `gemini-2.0-flash-exp`
- **Speed**: Fast responses (~1s for SQL)
- **Quality**: High accuracy for structured output
- **Thinking Mode**: Shows reasoning process
- **JSON Output**: Structured responses
**Parameters**:
```python
config = types.GenerateContentConfig(
temperature=1, # Creative but consistent
response_mime_type="application/json", # For structured outputs
thinking_config=types.ThinkingConfig(
mode=types.ThinkingMode.THINKING # Enable reasoning
)
)
```
---
## Prompt System
All prompts centralized in `backend/core/prompts.py`.
### 1. System Instruction
**Prompt**: `SYSTEM_INSTRUCTION`
Sets overall context and capabilities:
```
You are GeoQuery, an advanced Territorial Intelligence Agent capable of
analyzing diverse geographic datasets.
## Your Capabilities
- Dynamic Metadata Catalog (not fixed schema)
- Spatial Analysis with PostGIS/DuckDB functions
- Visual outputs (maps, charts)
## Output Guidelines
1. Be Data-Driven: Base answers on SQL query results
2. Be Visual: Use choropleth maps, point maps, charts
3. Be Transparent: Explain reasoning, cite sources
...
```
**Purpose**: Establishes AI persona and core behavior
---
### 2. Intent Detection
**Prompt**: `INTENT_DETECTION_PROMPT`
Classifies user queries into categories.
```
Analyze this user query and determine the best output type.
User Query: "{user_query}"
THINK STEP BY STEP:
1. What is the user asking for?
2. Does this require geographic visualization?
3. Does this require a chart/graph?
4. Is this a general question?
Respond with ONLY ONE of these exact words:
- GENERAL_CHAT
- DATA_QUERY
- MAP_REQUEST
- SPATIAL_OP
- STAT_QUERY
Key rules:
- "color by", "compare regions" → MAP_REQUEST
- "create a chart" → STAT_QUERY
- Questions about data availability → GENERAL_CHAT
```
**Examples**:
| Query | Thinking | Intent |
|-------|----------|--------|
| "Show me hospitals" | User wants to SEE on map | `MAP_REQUEST` |
| "How many provinces?" | Numerical answer, no viz needed | `DATA_QUERY` |
| "Create bar chart of districts" | Explicitly requests chart | `STAT_QUERY` |
| "Subtract Chiriquí from Panama" | Geometric operation | `SPATIAL_OP` |
| "What data do you have?" | General question | `GENERAL_CHAT` |
---
### 3. SQL Generation
**Prompt**: `SQL_GENERATION_PROMPT`
Converts natural language to DuckDB SQL.
```
You are a DuckDB SQL expert for geographic data analysis.
{table_schema}
### CRITICAL - Data Availability:
✅ You may ONLY query the tables listed above.
❌ Do NOT invent table names or columns.
If requested data is NOT available, return:
-- ERROR: DATA_UNAVAILABLE
-- Requested: [what user asked for]
-- Available: [list tables you DO have]
### User Request: "{user_query}"
### Rules:
1. Return ONLY the SQL query
2. Use DuckDB syntax (ILIKE for case-insensitive)
3. ALWAYS include 'geom' for map visualization
4. For "top N", use ORDER BY ... DESC LIMIT N
5. Do NOT add LIMIT unless explicitly requested
6. NEVER invent columns that don't exist
### Special Dataset - Population:
- Use `kontur_population` (H3 hexagons)
- Columns: population, geom
- Large dataset (33K hexagons) - use LIMIT 40000
...
Generate SQL:
```
**Key Features**:
- **Error Prevention**: Explicit instructions to avoid hallucinating tables
- **Spatial Functions**: Guides use of ST_Intersects, ST_Within, etc.
- **Data Unavailable Handling**: Returns special marker instead of invalid SQL
**Examples**:
Input: "Show hospitals in David"
```sql
SELECT name, amenity, geom
FROM panama_healthsites_geojson
WHERE amenity = 'hospital'
AND ST_Intersects(geom, (SELECT geom FROM pan_admin2 WHERE adm2_name = 'David'))
```
Input: "Population density in Veraguas"
```sql
SELECT population, geom
FROM kontur_population
WHERE ST_Intersects(geom, (SELECT geom FROM pan_admin1 WHERE adm1_name = 'Veraguas'))
LIMIT 5000
```
---
### 4. Spatial SQL
**Prompt**: `SPATIAL_SQL_PROMPT`
For geometric operations (difference, intersection, buffer, etc.).
```
You are a GIS expert using DuckDB Spatial.
Available Data:
{layer_context}
User Request: "{user_query}"
Rules:
1. Return ONLY SQL query
2. Use DuckDB Spatial functions:
- ST_Difference, ST_Intersection, ST_Union
- ST_Buffer, ST_Within, ST_Contains
3. The geometry column is named 'geom'
4. Use EXACT table names shown above
5. IMPORTANT: For aggregate geometries (ST_Union), use CTE pattern:
CORRECT:
WITH layer_b_union AS (SELECT ST_Union(geom) as geom FROM layer_b)
SELECT a.*, ST_Difference(a.geom, b.geom) as geom
FROM layer_a a, layer_b_union b
WRONG:
SELECT ST_Difference(geom, (SELECT ST_Union(geom) FROM layer_b))
FROM layer_a
```
**Example**:
Input: "Subtract protected areas from Chiriquí province"
```sql
WITH protected_union AS (
SELECT ST_Union(geom) as geom FROM stri_protected_areas_2025
)
SELECT
p.adm1_name,
ST_Difference(p.geom, pa.geom) as geom
FROM pan_admin1 p, protected_union pa
WHERE p.adm1_name = 'Chiriquí'
```
---
### 5. Layer Naming
**Prompt**: `LAYER_NAME_PROMPT`
Generates descriptive name, emoji, and point style for map layers.
```
User Request: "{user_query}"
SQL Query: "{sql_query}"
Rules:
1. Return JSON with: name, emoji, pointStyle
2. "name": Short descriptive (1-4 words)
3. "emoji": Single emoji for data content
4. "pointStyle": How to render points
- "icon": Small/medium POI (<500 points)
- "circle": Large point datasets (>500 points)
- null: Polygon data (use choropleth)
Examples:
{"name": "Hospitals in David", "emoji": "🏥", "pointStyle": "icon"}
{"name": "Population Density", "emoji": "👥", "pointStyle": null}
{"name": "Traffic Intersections", "emoji": "🚦", "pointStyle": "circle"}
```
**Decision Logic**:
- Hospitals, schools, parks → icon
- Intersections, sensors (large datasets) → circle
- H3 hexagons, admin boundaries → null (polygon rendering)
---
### 6. Explanation
**Prompt**: `EXPLANATION_PROMPT`
Generates natural language explanation of results.
```
Explain the results of this data query to the user.
User Question: "{user_query}"
SQL Query: {sql_query}
Data Result Summary: {data_summary}
Instructions:
1. Keep response concise
2. Only describe ACTUAL data returned
3. Cite data source
4. Speak as GeoQuery
Example citation:
"Source: Administrative boundary data from HDX/INEC, 2021"
```
**Features**:
- **Factual**: Only describes what was actually found
- **Contextual**: Relates results to user's question
- **Transparent**: Cites data sources
---
### 7. SQL Correction
**Prompt**: `SQL_CORRECTION_PROMPT`
Repairs failed SQL queries.
```
Your previous query failed. Fix it.
### Error Message:
{error_message}
### Failed SQL:
{incorrect_sql}
### User Request:
"{user_query}"
### Database Schema:
{schema_context}
Rules:
1. Fix the error described in the message
2. Return ONLY the valid SQL query
3. Keep query logic consistent with User Request
```
**Common Fixes**:
- Column ambiguity → Add table aliases
- Missing column → Use correct column name
- Syntax error → Fix DuckDB syntax
---
## Streaming Implementation
### Thinking + Content Streaming
```python
async def stream_sql_generation(self, query: str, schema: str):
config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
mode=types.ThinkingMode.THINKING
)
)
response = await asyncio.to_thread(
self.client.models.generate_content_stream,
model=self.model,
contents=query_prompt,
config=config
)
async for chunk in response:
if hasattr(chunk, 'thought'):
yield {"type": "thought", "text": chunk.thought.text}
if hasattr(chunk, 'text'):
yield {"type": "content", "text": chunk.text}
```
**Frontend receives**:
```json
{"type": "thought", "text": "I need to find hospitals in the David district..."}
{"type": "content", "text": "SELECT name, geom FROM ..."}
```
---
## Error Handling
### 1. Data Unavailable
```sql
-- ERROR: DATA_UNAVAILABLE
-- Requested: crime statistics
-- Available: hospitals, schools, admin boundaries
```
→ System detects marker and returns helpful error
### 2. SQL Execution Error
```
Error: column "hospitals" does not exist
```
→ Send to `correct_sql()` → LLM fixes → Retry
### 3. Rate Limiting
```python
try:
response = await self.client.models.generate_content(...)
except Exception as e:
if "rate limit" in str(e).lower():
await asyncio.sleep(1)
# Retry
```
---
## Performance Optimizations
### Caching
**Not currently implemented**, but recommended:
```python
from functools import lru_cache
@lru_cache(maxsize=100)
async def cached_sql_generation(query_hash: str):
...
```
### Token Management
- **Minimize Context**: Only send relevant table schemas
- **Semantic Search**: Pre-filter to top 15 tables
- **Batch Requests**: Combine multiple LLM calls where possible
---
## Prompt Engineering Best Practices
### 1. Be Explicit
❌ "Generate SQL for this query"
✅ "Generate DuckDB SQL with spatial functions. Include 'geom' column. Use ILIKE for text matching."
### 2. Provide Examples
```
Example:
Input: "hospitals in Panama"
Output: SELECT name, geom FROM panama_healthsites_geojson WHERE amenity='hospital'
```
### 3. Use Constraints
```
Rules:
- Return ONLY SQL (no markdown, no explanation)
- Use EXACT table names from schema
- DO NOT invent columns
```
### 4. Handle Edge Cases
```
If data not available, return:
-- ERROR: DATA_UNAVAILABLE
```
### 5. Structure Output
```
Return valid JSON:
{"name": "...", "emoji": "..."}
```
---
## Testing
### Manual Testing
```python
llm = LLMGateway()
# Test intent detection
intent = await llm.detect_intent("Show me hospitals", [])
print(intent) # Should be "MAP_REQUEST"
# Test SQL generation
sql = await llm.generate_analytical_sql("hospitals in David", schema, [])
print(sql) # Should be valid SELECT query
```
### Prompt Iteration
1. **Test with real queries**
2. **Analyze failures**
3. **Update prompt**
4. **Re-test**
---
## Next Steps
- **Core Services**: [CORE_SERVICES.md](CORE_SERVICES.md)
- **Data Flow**: [../DATA_FLOW.md](../DATA_FLOW.md)
- **API Reference**: [API_ENDPOINTS.md](API_ENDPOINTS.md)
|