|
| 1 | +# SQLite Agent Extension Usage Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The SQLite Agent extension enables autonomous AI agents within SQLite databases. It requires both [sqlite-mcp](https://github.com/sqliteai/sqlite-mcp) for external tool access and [sqlite-ai](https://github.com/sqliteai/sqlite-ai) for LLM inference and embeddings. |
| 6 | + |
| 7 | +Optionally, [sqlite-vector](https://github.com/sqliteai/sqlite-vector) can be loaded to enable automatic vector embeddings and semantic search capabilities when using table extraction mode. |
| 8 | + |
| 9 | +## Quick Start |
| 10 | + |
| 11 | +### 1. Load Extensions |
| 12 | + |
| 13 | +The agent extension requires both sqlite-mcp and sqlite-ai: |
| 14 | + |
| 15 | +```sql |
| 16 | +-- Load required extensions (in order) |
| 17 | +.load ../sqlite-mcp/dist/mcp |
| 18 | +.load ../sqlite-ai/dist/ai |
| 19 | +.load ./dist/agent |
| 20 | + |
| 21 | +-- Optional: Load sqlite-vector for automatic embeddings and semantic search |
| 22 | +.load ../sqlite-vector/dist/vector |
| 23 | + |
| 24 | +-- Verify versions |
| 25 | +SELECT mcp_version(); -- 0.1.4 |
| 26 | +SELECT agent_version(); -- 0.1.0 |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. Initialize |
| 30 | + |
| 31 | +Load an AI model and connect to an MCP server: |
| 32 | + |
| 33 | +```sql |
| 34 | +-- Load AI model (required) |
| 35 | +SELECT llm_model_load('/path/to/model.gguf', 'gpu_layers=99'); |
| 36 | + |
| 37 | +-- Connect to MCP server (required) |
| 38 | +SELECT mcp_connect('http://localhost:8000/mcp'); |
| 39 | +``` |
| 40 | + |
| 41 | +### 3. Run Agent |
| 42 | + |
| 43 | +```sql |
| 44 | +-- Simple text response |
| 45 | +SELECT agent_run('Find affordable apartments in Rome', 5); |
| 46 | + |
| 47 | +-- Table extraction with auto-features |
| 48 | +CREATE TABLE listings (id INTEGER, title TEXT, price REAL, embedding BLOB); |
| 49 | +SELECT agent_run('Find apartments in Rome', 'listings', 8); |
| 50 | +``` |
| 51 | + |
| 52 | +## Two Modes of Operation |
| 53 | + |
| 54 | +### MODE 1: Text Response |
| 55 | + |
| 56 | +Returns agent's text answer after using tools. |
| 57 | + |
| 58 | +**Use when:** |
| 59 | +- You want a conversational response |
| 60 | +- You need quick answers without data storage |
| 61 | +- You're prototyping or exploring |
| 62 | + |
| 63 | +**Example:** |
| 64 | +```sql |
| 65 | +SELECT agent_run( |
| 66 | + 'Find 3 affordable apartments in Rome with AC under 100 EUR', |
| 67 | + 8 |
| 68 | +); |
| 69 | +``` |
| 70 | + |
| 71 | +**Response:** |
| 72 | +``` |
| 73 | +I found several options: |
| 74 | +1. Bright Studio - €85/night, AC, WiFi |
| 75 | +2. Cozy Room in Trastevere - €72/night, AC, WiFi |
| 76 | +3. Modern Apartment - €95/night, AC, WiFi, Parking |
| 77 | +``` |
| 78 | + |
| 79 | +### MODE 2: Table Extraction |
| 80 | + |
| 81 | +Fetches data and populates a database table. |
| 82 | + |
| 83 | +**Use when:** |
| 84 | +- You want to store structured data |
| 85 | +- You need semantic search capabilities |
| 86 | +- You're building a data-driven application |
| 87 | + |
| 88 | +**Example:** |
| 89 | +```sql |
| 90 | +CREATE TABLE listings ( |
| 91 | + id INTEGER PRIMARY KEY, |
| 92 | + title TEXT, |
| 93 | + location TEXT, |
| 94 | + price REAL, |
| 95 | + amenities TEXT, |
| 96 | + embedding BLOB |
| 97 | +); |
| 98 | + |
| 99 | +SELECT agent_run( |
| 100 | + 'Find affordable apartments in Rome under 100 EUR', |
| 101 | + 'listings', |
| 102 | + 8 |
| 103 | +); |
| 104 | +-- Returns: "Inserted 5 rows into listings" |
| 105 | +``` |
| 106 | + |
| 107 | +## Auto-Features in Table Mode |
| 108 | + |
| 109 | +When using table extraction mode, the agent automatically: |
| 110 | + |
| 111 | +### 1. Schema Inspection |
| 112 | +Inspects table columns to understand what data to fetch: |
| 113 | +```sql |
| 114 | +CREATE TABLE listings ( |
| 115 | + id INTEGER, |
| 116 | + title TEXT, |
| 117 | + price REAL, |
| 118 | + location TEXT |
| 119 | +); |
| 120 | + |
| 121 | +-- Agent knows to fetch: id, title, price, location |
| 122 | +``` |
| 123 | + |
| 124 | +### 2. Structured Extraction |
| 125 | +Uses LLM to extract data matching your schema: |
| 126 | +```sql |
| 127 | +-- Agent extracts structured JSON matching columns |
| 128 | +-- Handles type conversion automatically |
| 129 | +``` |
| 130 | + |
| 131 | +### 3. Auto-Embeddings (Requires sqlite-vector) |
| 132 | +Generates embeddings for BLOB columns named `*_embedding`: |
| 133 | +```sql |
| 134 | +CREATE TABLE listings ( |
| 135 | + title TEXT, |
| 136 | + description TEXT, |
| 137 | + title_embedding BLOB, -- Auto-embedded from 'title' |
| 138 | + description_embedding BLOB -- Auto-embedded from 'description' |
| 139 | +); |
| 140 | + |
| 141 | +SELECT agent_run('Find apartments', 'listings', 8); |
| 142 | +-- Automatically generates embeddings if sqlite-vector is loaded! |
| 143 | +``` |
| 144 | + |
| 145 | +### 4. Auto-Vector Index (Requires sqlite-vector) |
| 146 | +Initializes vector search indices: |
| 147 | +```sql |
| 148 | +-- After generating embeddings, automatically runs (if sqlite-vector is loaded): |
| 149 | +-- SELECT vector_init('listings', 'title_embedding', ...) |
| 150 | +-- SELECT vector_init('listings', 'description_embedding', ...) |
| 151 | +``` |
| 152 | + |
| 153 | +### 5. Transaction Safety |
| 154 | +All insertions wrapped in a transaction: |
| 155 | +```sql |
| 156 | +-- BEGIN TRANSACTION |
| 157 | +-- INSERT INTO listings ... |
| 158 | +-- INSERT INTO listings ... |
| 159 | +-- COMMIT |
| 160 | +-- (or ROLLBACK on error) |
| 161 | +``` |
| 162 | + |
| 163 | +## Complete Workflow Example |
| 164 | + |
| 165 | +### Airbnb RAG Workflow |
| 166 | + |
| 167 | +```sql |
| 168 | +-- 1. Load extensions |
| 169 | +.load ../sqlite-mcp/dist/mcp |
| 170 | +.load ./dist/agent |
| 171 | +.load ../sqlite-ai/dist/ai |
| 172 | +.load ../sqlite-vector/dist/vector |
| 173 | + |
| 174 | +-- 2. Initialize (one-time) |
| 175 | +SELECT llm_model_load('/path/to/model.gguf', 'gpu_layers=99'); |
| 176 | +SELECT mcp_connect('http://localhost:8000/mcp'); |
| 177 | + |
| 178 | +-- 3. Create table |
| 179 | +CREATE TABLE listings ( |
| 180 | + id INTEGER PRIMARY KEY, |
| 181 | + title TEXT, |
| 182 | + description TEXT, |
| 183 | + location TEXT, |
| 184 | + property_type TEXT, |
| 185 | + amenities TEXT, |
| 186 | + price REAL, |
| 187 | + rating REAL, |
| 188 | + embedding BLOB |
| 189 | +); |
| 190 | + |
| 191 | +-- 4. Run agent to populate table |
| 192 | +-- Auto-generates embeddings and vector index (requires sqlite-vector)! |
| 193 | +SELECT agent_run( |
| 194 | + 'Find affordable apartments in Rome under 100 EUR per night', |
| 195 | + 'listings', |
| 196 | + 8 |
| 197 | +); |
| 198 | +-- Returns: "Inserted 5 rows into listings" |
| 199 | + |
| 200 | +-- 5. Semantic search (works immediately!) |
| 201 | +SELECT title, location, price, v.distance |
| 202 | +FROM vector_full_scan('listings', 'embedding', |
| 203 | + llm_embed_generate('cozy modern apartment', ''), 5) v |
| 204 | +JOIN listings l ON l.rowid = v.rowid |
| 205 | +ORDER BY v.distance ASC; |
| 206 | + |
| 207 | +-- 6. RAG: Answer questions |
| 208 | +SELECT llm_context_create_chat(); |
| 209 | +SELECT llm_chat_respond( |
| 210 | + 'Based on these listings: ' || ( |
| 211 | + SELECT group_concat(title || ' - €' || price, '; ') |
| 212 | + FROM listings |
| 213 | + ) || '. Which is best for families?' |
| 214 | +); |
| 215 | +``` |
| 216 | + |
| 217 | +## Custom System Prompts |
| 218 | + |
| 219 | +Override the default agent behavior: |
| 220 | + |
| 221 | +```sql |
| 222 | +SELECT agent_run( |
| 223 | + 'Find vegan restaurants', |
| 224 | + 'restaurants', |
| 225 | + 10, |
| 226 | + 'You are a helpful restaurant finder. |
| 227 | + Focus on highly-rated establishments with good reviews. |
| 228 | + Extract: name, cuisine, rating, price_range, address' |
| 229 | +); |
| 230 | +``` |
| 231 | + |
| 232 | +## Error Handling |
| 233 | + |
| 234 | +### Common Errors |
| 235 | + |
| 236 | +**1. Not Connected to MCP** |
| 237 | +```sql |
| 238 | +SELECT agent_run('Find apartments', 5); |
| 239 | +-- Error: Not connected. Call mcp_connect() first |
| 240 | +``` |
| 241 | + |
| 242 | +**Solution:** |
| 243 | +```sql |
| 244 | +SELECT mcp_connect('http://localhost:8000/mcp'); |
| 245 | +``` |
| 246 | + |
| 247 | +**2. LLM Not Loaded** |
| 248 | +```sql |
| 249 | +SELECT agent_run('Find apartments', 'listings', 5); |
| 250 | +-- Error: Failed to create LLM chat context |
| 251 | +``` |
| 252 | + |
| 253 | +**Solution:** |
| 254 | +```sql |
| 255 | +SELECT llm_model_load('/path/to/model.gguf', 'gpu_layers=99'); |
| 256 | +``` |
| 257 | + |
| 258 | +**3. Table Does Not Exist** |
| 259 | +```sql |
| 260 | +SELECT agent_run('Find apartments', 'nonexistent', 5); |
| 261 | +-- Error: Table does not exist or has no columns |
| 262 | +``` |
| 263 | + |
| 264 | +**Solution:** |
| 265 | +```sql |
| 266 | +CREATE TABLE nonexistent (id INTEGER, title TEXT); |
| 267 | +``` |
| 268 | + |
| 269 | +### Checking for Errors |
| 270 | + |
| 271 | +```sql |
| 272 | +SELECT |
| 273 | + CASE |
| 274 | + WHEN result LIKE '%error%' OR result LIKE '%ERROR%' |
| 275 | + THEN 'Error: ' || result |
| 276 | + ELSE 'Success' |
| 277 | + END |
| 278 | +FROM (SELECT agent_run('Find apartments', 5) as result); |
| 279 | +``` |
| 280 | + |
| 281 | +## Performance Tips |
| 282 | + |
| 283 | +### 1. Use Appropriate Iterations |
| 284 | + |
| 285 | +```sql |
| 286 | +-- Simple tasks: 3-5 iterations |
| 287 | +SELECT agent_run('Find apartments in Rome', 3); |
| 288 | + |
| 289 | +-- Complex tasks: 8-10 iterations |
| 290 | +SELECT agent_run('Find apartments, get details, filter by amenities', 10); |
| 291 | +``` |
| 292 | + |
| 293 | +### 2. Reuse Connections |
| 294 | + |
| 295 | +```sql |
| 296 | +-- Connect once |
| 297 | +SELECT mcp_connect('http://localhost:8000/mcp'); |
| 298 | + |
| 299 | +-- Run multiple agents (connection persists) |
| 300 | +SELECT agent_run('Find apartments in Rome', 'rome_listings', 5); |
| 301 | +SELECT agent_run('Find apartments in Paris', 'paris_listings', 5); |
| 302 | +``` |
| 303 | + |
| 304 | +### 3. Cache Embeddings (Requires sqlite-vector) |
| 305 | + |
| 306 | +```sql |
| 307 | +-- For static data, embeddings are cached in the BLOB column |
| 308 | +-- No need to regenerate unless data changes |
| 309 | +UPDATE listings SET title = 'New Title' WHERE id = 1; |
| 310 | +-- Run agent again to refresh embeddings (if sqlite-vector is loaded) |
| 311 | +``` |
| 312 | + |
| 313 | +## Debugging |
| 314 | + |
| 315 | +Enable debug output by setting `AGENT_DEBUG=1` in the source: |
| 316 | + |
| 317 | +```c |
| 318 | +// In src/sqlite-agent.c |
| 319 | +#define AGENT_DEBUG 1 |
| 320 | +``` |
| 321 | +
|
| 322 | +Then rebuild: |
| 323 | +```bash |
| 324 | +make clean && make |
| 325 | +``` |
| 326 | + |
| 327 | +Debug output will show: |
| 328 | +- Agent iterations |
| 329 | +- Tool calls and arguments |
| 330 | +- LLM responses |
| 331 | +- Embedding generation |
| 332 | +- Vector index initialization |
| 333 | + |
| 334 | +## Related Documentation |
| 335 | + |
| 336 | +- [API.md](API.md) – Complete API reference |
| 337 | +- [README.md](README.md) – Project overview |
| 338 | +- [sqlite-mcp API](https://github.com/sqliteai/sqlite-mcp/blob/main/API.md) – MCP extension API |
| 339 | +- [sqlite-ai API](https://github.com/sqliteai/sqlite-ai/blob/main/API.md) – AI extension API |
| 340 | +- [sqlite-vector API](https://github.com/sqliteai/sqlite-vector/blob/main/API.md) – Vector extension API |
| 341 | + |
| 342 | +## Support |
| 343 | + |
| 344 | +- [GitHub Issues](https://github.com/sqliteai/sqlite-agent/issues) |
| 345 | +- [SQLite Extension Load Guide](https://github.com/sqliteai/sqlite-extensions-guide) |
0 commit comments