contextsql-garvit 0.1.0__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.
@@ -0,0 +1,416 @@
1
+ Metadata-Version: 2.4
2
+ Name: contextsql_garvit
3
+ Version: 0.1.0
4
+ Summary: Local, free, business-context-aware text-to-SQL agent
5
+ Requires-Python: >=3.9
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: sqlalchemy>=2.0
8
+ Requires-Dist: pymysql>=1.1
9
+ Requires-Dist: huggingface_hub>=0.20
10
+ Requires-Dist: llama-cpp-python
11
+ Provides-Extra: postgres
12
+ Requires-Dist: psycopg2-binary; extra == "postgres"
13
+
14
+ # contextsql
15
+
16
+ Ask your database questions in plain English. **Local, free and private**: the model runs on your own machine, and your data is never sent to any cloud service.
17
+
18
+ contextsql first *learns* your database (tables, columns, value patterns, business terms). When you ask a question, it picks the relevant tables, writes the SQL, runs it, and shows the result.
19
+
20
+ ```
21
+ $ contextsql ask "top 5 customers by total payments"
22
+
23
+ SQL (1 attempt, 4210 ms; tables: payments, customers, orders):
24
+ SELECT ... LIMIT 5;
25
+
26
+ name total
27
+ ------------ --------
28
+ Customer 482 39984.38
29
+ ...
30
+ ```
31
+
32
+ ---
33
+
34
+ ## Contents
35
+
36
+ 1. [Requirements](#requirements)
37
+ 2. [Installation](#installation)
38
+ 3. [Quick start](#quick-start)
39
+ 4. [Commands](#commands)
40
+ 5. [Teaching it your business terms (glossary)](#teaching-it-your-business-terms-glossary)
41
+ 6. [Improving answers (good / fix)](#improving-answers-good--fix)
42
+ 7. [Using other databases](#using-other-databases)
43
+ 8. [Safety](#safety)
44
+ 9. [Where files are stored](#where-files-are-stored)
45
+ 10. [Tips for better answers](#tips-for-better-answers)
46
+ 11. [Troubleshooting](#troubleshooting)
47
+ 12. [Updating and uninstalling](#updating-and-uninstalling)
48
+
49
+ ---
50
+
51
+ ## Requirements
52
+
53
+ | Item | Requirement |
54
+ |------|-------------|
55
+ | Python | 3.9 or newer (**3.10 to 3.12 recommended**, especially on Windows) |
56
+ | Database | MySQL (default), PostgreSQL or SQLite |
57
+ | Disk space | About 2 GB for the model (downloaded once) |
58
+ | RAM | 4 GB free or more |
59
+ | Internet | Needed only for installation and the first model download |
60
+
61
+ No GPU is needed. The model runs on the CPU.
62
+
63
+ ---
64
+
65
+ ## Installation
66
+
67
+ ### 1. Create a virtual environment (recommended)
68
+
69
+ **Windows**
70
+ ```
71
+ python -m venv contextsql_env
72
+ contextsql_env\Scripts\activate
73
+ ```
74
+
75
+ **Mac / Linux**
76
+ ```
77
+ python3 -m venv contextsql_env
78
+ source contextsql_env/bin/activate
79
+ ```
80
+
81
+ ### 2. Install the model runner first (Windows)
82
+
83
+ On Windows, install the prebuilt `llama-cpp-python` before installing contextsql. This avoids a C++ compiler error (`CMAKE_C_COMPILER not set` / `nmake` failed):
84
+
85
+ ```
86
+ pip install llama-cpp-python --prefer-binary --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
87
+ ```
88
+
89
+ Mac and Linux users can skip this step.
90
+
91
+ ### 3. Install contextsql
92
+
93
+ ```
94
+ pip install contextsql
95
+ ```
96
+
97
+ Check that it works:
98
+ ```
99
+ contextsql status
100
+ ```
101
+
102
+ ### Optional: PostgreSQL support
103
+ ```
104
+ pip install "contextsql[postgres]"
105
+ ```
106
+
107
+ ---
108
+
109
+ ## Quick start
110
+
111
+ Three steps: **connect**, **learn**, **ask**.
112
+
113
+ ### Step 1: Connect to your database
114
+
115
+ ```
116
+ contextsql connect
117
+ ```
118
+
119
+ You will be asked for:
120
+
121
+ ```
122
+ Host [127.0.0.1]:
123
+ Port [3306]:
124
+ User:
125
+ Password: (hidden while typing)
126
+ Database name:
127
+ ```
128
+
129
+ Or pass everything in one line:
130
+
131
+ ```
132
+ contextsql connect --host 127.0.0.1 --port 3306 --user myuser --password mypass --db mydatabase
133
+ ```
134
+
135
+ On the **first** connect, the model (about 1.8 GB) is downloaded from Hugging Face and cached. This happens only once, so it takes a few minutes. After that, everything works offline.
136
+
137
+ ### Step 2: Learn your database (one time)
138
+
139
+ ```
140
+ contextsql learn
141
+ ```
142
+
143
+ contextsql reads your table structure, scans the data and builds notes about your columns and values (for example, which status values exist). It prints a summary like:
144
+
145
+ ```
146
+ Done: 4 tables, 12,000 rows scanned, 7 data notes, 10 auto glossary terms, 0 glossary terms imported.
147
+ ```
148
+
149
+ Run `learn` again whenever your tables or data change a lot.
150
+
151
+ ### Step 3: Ask questions
152
+
153
+ One question:
154
+ ```
155
+ contextsql ask "how many orders were placed last month"
156
+ ```
157
+
158
+ Interactive session (the model loads once, so later questions are faster):
159
+ ```
160
+ contextsql ask
161
+ > top 5 customers by revenue
162
+ > average order value per month
163
+ > (press Enter on an empty line to quit)
164
+ ```
165
+
166
+ ---
167
+
168
+ ## Commands
169
+
170
+ ### `contextsql connect`
171
+
172
+ Saves your database login and downloads the model if needed.
173
+
174
+ | Option | Meaning | Default |
175
+ |--------|---------|---------|
176
+ | `--dialect` | `mysql`, `postgresql` or `sqlite` | `mysql` |
177
+ | `--host` | Server address | `127.0.0.1` |
178
+ | `--port` | Server port | 3306 (MySQL) / 5432 (PostgreSQL) |
179
+ | `--user` | Database user | asked |
180
+ | `--password` | Database password | asked (hidden) |
181
+ | `--db` | Database name (or file path for SQLite) | asked |
182
+ | `--model` | Path to your own `.gguf` model file | auto-download |
183
+
184
+ Run it again at any time to switch databases. The old settings are overwritten. After switching, run `contextsql learn`.
185
+
186
+ ### `contextsql learn`
187
+
188
+ Reads and analyzes the connected database. Run it once after `connect`, and again after big schema or data changes.
189
+
190
+ ### `contextsql ask`
191
+
192
+ | Usage | What it does |
193
+ |-------|--------------|
194
+ | `contextsql ask "question"` | Answers one question and exits |
195
+ | `contextsql ask` | Starts an interactive session (empty line quits) |
196
+ | `contextsql ask --threads 8 "question"` | Uses 8 CPU threads (default is half your cores) |
197
+
198
+ Every answer shows:
199
+ - the SQL it wrote,
200
+ - how many attempts it needed (it retries automatically if the SQL fails or returns nothing),
201
+ - the tables it used,
202
+ - a **low confidence** warning when it is unsure. Always read the SQL in that case.
203
+
204
+ Results show up to 20 rows on screen.
205
+
206
+ ### `contextsql glossary`
207
+
208
+ Manage your business terms. See [the next section](#teaching-it-your-business-terms-glossary).
209
+
210
+ ```
211
+ contextsql glossary # list all terms (same as "list")
212
+ contextsql glossary list
213
+ contextsql glossary add "term" "definition" "SQL expression" --tables t1,t2
214
+ contextsql glossary remove "term"
215
+ ```
216
+
217
+ ### `contextsql good`
218
+
219
+ Marks the **last answer** as correct. It is saved as an example and reused for similar questions.
220
+
221
+ ### `contextsql fix`
222
+
223
+ Replaces the last answer with your corrected SQL:
224
+
225
+ ```
226
+ contextsql fix "SELECT COUNT(*) FROM orders WHERE status = 'completed'"
227
+ ```
228
+
229
+ The SQL is checked and test-run first. It is saved only if it runs successfully.
230
+
231
+ ### `contextsql status`
232
+
233
+ Shows whether you are connected, which model file is used, how many tables were learned, and how many glossary terms exist.
234
+
235
+ ---
236
+
237
+ ## Teaching it your business terms (glossary)
238
+
239
+ Databases rarely say what *revenue*, *active customer* or *late delivery* mean in your business. The glossary lets you define them once, and contextsql uses the definition every time you ask.
240
+
241
+ ```
242
+ contextsql glossary add "revenue" "Sum of quantity times price for completed orders" "SUM(quantity*unit_price)" --tables orders,order_items
243
+ ```
244
+
245
+ Arguments, in order:
246
+
247
+ 1. **term**: the word you will use in questions
248
+ 2. **definition**: a plain description
249
+ 3. **SQL expression**: how it is calculated
250
+ 4. `--tables`: tables the term needs (comma-separated, no spaces)
251
+
252
+ Then:
253
+ ```
254
+ contextsql ask "revenue by month"
255
+ ```
256
+
257
+ Other glossary commands:
258
+ ```
259
+ contextsql glossary list
260
+ contextsql glossary remove "revenue"
261
+ ```
262
+
263
+ Terms marked `(auto)` in the list were generated automatically during `learn`. Your own terms override them. You do not need to run `learn` again after adding a term.
264
+
265
+ ---
266
+
267
+ ## Improving answers (good / fix)
268
+
269
+ contextsql gets better the more you correct it.
270
+
271
+ 1. Ask a question.
272
+ 2. If the SQL and result are right, run `contextsql good`.
273
+ 3. If they are wrong, run `contextsql fix "your corrected SQL"`.
274
+
275
+ Both commands apply to the **last question you asked**. Saved examples are used for similar questions in the future.
276
+
277
+ ---
278
+
279
+ ## Using other databases
280
+
281
+ ### MySQL (default)
282
+ ```
283
+ contextsql connect --user myuser --password mypass --db mydatabase
284
+ ```
285
+
286
+ ### PostgreSQL
287
+ ```
288
+ pip install "contextsql[postgres]"
289
+ contextsql connect --dialect postgresql --host localhost --port 5432 --user myuser --password mypass --db mydatabase
290
+ ```
291
+
292
+ ### SQLite
293
+ ```
294
+ contextsql connect --dialect sqlite --db C:\path\to\file.db
295
+ ```
296
+
297
+ ### Remote server
298
+ Use `--host` with the server's IP address or name. Make sure the server allows connections from your machine.
299
+
300
+ ---
301
+
302
+ ## Safety
303
+
304
+ - **Use a read-only database user.** Never connect with an admin account such as `root` on a production database. Example for MySQL:
305
+
306
+ ```sql
307
+ CREATE USER 'ctx'@'localhost' IDENTIFIED BY 'a_strong_password';
308
+ GRANT SELECT ON mydatabase.* TO 'ctx'@'localhost';
309
+ FLUSH PRIVILEGES;
310
+ ```
311
+
312
+ Then connect with `--user ctx`.
313
+
314
+ - **Always read the SQL** before trusting a result, especially when you see the low-confidence warning.
315
+ - **Your login is stored in plain text** in `~/.contextsql/` (see below). This is another reason to use a read-only user.
316
+ - **Your data stays on your machine.** The model runs locally. The only network use is the one-time model download.
317
+
318
+ ---
319
+
320
+ ## Where files are stored
321
+
322
+ | What | Location |
323
+ |------|----------|
324
+ | Your settings, learned data, glossary and saved examples | `.contextsql` folder in your user home (Windows: `C:\Users\<you>\.contextsql`) |
325
+ | Downloaded model | Hugging Face cache (Windows: `C:\Users\<you>\.cache\huggingface\hub`) |
326
+
327
+ **Reset everything:** delete the `.contextsql` folder, then run `connect` and `learn` again.
328
+ **Free disk space:** delete the model folder `models--Garvitarunav--contextsql-v4` inside the Hugging Face cache. It will be re-downloaded when needed.
329
+
330
+ ---
331
+
332
+ ## Tips for better answers
333
+
334
+ - **Run `learn`** after connecting and after major data changes.
335
+ - **Name things the way your tables do.** "orders per customer" works better when the tables are called `orders` and `customers`.
336
+ - **Add glossary terms** for your key metrics (revenue, margin, active user, churn, and so on).
337
+ - **Be specific.** "Top 10 customers by total payments in 2026" is better than "best customers".
338
+ - **Check date questions.** Phrases like "last month" are supported; others ("last 7 days", "year to date") may need a check. Read the SQL.
339
+ - **Use `good` and `fix`** so it learns your preferred answers.
340
+ - **Speed:** use the interactive `contextsql ask` session so the model loads once. Try `--threads` with your CPU core count.
341
+
342
+ ---
343
+
344
+ ## Troubleshooting
345
+
346
+ ### `Failed building wheel for llama-cpp-python` / `CMAKE_C_COMPILER not set`
347
+ No prebuilt wheel was found, and Windows has no C++ compiler. Run this first, then install contextsql:
348
+ ```
349
+ pip install llama-cpp-python --prefer-binary --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
350
+ pip install contextsql
351
+ ```
352
+ If it still tries to build, check `python --version`. Prebuilt wheels exist for Python 3.10 to 3.12.
353
+
354
+ ### `Could not connect: (1045, "Access denied for user ...")`
355
+ The username or password is wrong, or you pressed Enter at the prompts and left them empty. Test your login outside contextsql:
356
+ ```
357
+ mysql -u myuser -p
358
+ ```
359
+ Then run `contextsql connect` with the same user and password. Also check that the user has access to the database (`GRANT SELECT ON mydatabase.* TO 'myuser'@'localhost';`).
360
+
361
+ ### `Could not connect: (2003, "Can't connect to MySQL server")`
362
+ The host or port is wrong, or the server is not running or does not allow your machine to connect.
363
+
364
+ ### `Not ready yet - run: contextsql connect` or `contextsql learn`
365
+ You skipped a step. Run `connect`, then `learn`, then `ask`.
366
+
367
+ ### Model download fails or is interrupted
368
+ Check your internet connection and free disk space (about 2 GB), then run `contextsql connect` again. A partial download resumes automatically.
369
+
370
+ ### Answers are wrong or "low confidence"
371
+ 1. Read the SQL shown.
372
+ 2. Rephrase with exact table or column names.
373
+ 3. Add a glossary term for the concept.
374
+ 4. Use `contextsql fix "correct SQL"` to teach it.
375
+ 5. Run `contextsql learn` again if your data has changed.
376
+
377
+ ### It is slow
378
+ - Use the interactive `contextsql ask` session, so the model loads only once.
379
+ - Close other heavy programs and use `--threads <number of CPU cores>`.
380
+ - The first question after loading the model is always slower than later ones.
381
+
382
+ ### Everything seems stuck on an old database
383
+ Settings are shared per user. Run `contextsql status` to see what you are connected to. To start fresh, delete the `.contextsql` folder.
384
+
385
+ ---
386
+
387
+ ## Updating and uninstalling
388
+
389
+ **Update**
390
+ ```
391
+ pip install --upgrade contextsql
392
+ ```
393
+
394
+ **Uninstall**
395
+ ```
396
+ pip uninstall contextsql
397
+ ```
398
+ Then optionally delete the `.contextsql` folder and the Hugging Face model cache (see [Where files are stored](#where-files-are-stored)).
399
+
400
+ ---
401
+
402
+ ## Command cheat sheet
403
+
404
+ ```
405
+ pip install contextsql
406
+ contextsql connect --user USER --password PASS --db DBNAME
407
+ contextsql learn
408
+ contextsql ask "your question"
409
+ contextsql ask # interactive session
410
+ contextsql glossary add "term" "definition" "SQL" --tables t1,t2
411
+ contextsql glossary list
412
+ contextsql glossary remove "term"
413
+ contextsql good # last answer was right
414
+ contextsql fix "SELECT ..." # last answer was wrong
415
+ contextsql status
416
+ ```
@@ -0,0 +1,20 @@
1
+ contextsql/__init__.py,sha256=kUR5RAFc7HCeiqdlX36dZOHkUI5wI6V_43RpEcD8b-0,22
2
+ contextsql/agent.py,sha256=bNyqpflDAYwdh5TH8fkk_WsHo1AeosySxtiInB-rGMo,9470
3
+ contextsql/autoglossary.py,sha256=nFlj2FbdlLmKbQp-Dyygp6GYQZ1A-Yk6FCrjXsfVXeo,18230
4
+ contextsql/cli.py,sha256=DEf-gRmORvBA0AH_Vzb-Z7IclhQKxl9aBr6vvW8alcY,7563
5
+ contextsql/config.py,sha256=WRWZy669LTPKS8XsenKw71nkr4HLG85uuFF3Qj-aqyM,680
6
+ contextsql/db.py,sha256=9DCkFt0r3kQirmNzE93jWLS9fT4BtbXWOCp-EMf_Xsg,547
7
+ contextsql/feedback.py,sha256=hpR43jp7q9Vk_mPBQmhQ9aVZt2p5Ac1olYHR625SdT0,1166
8
+ contextsql/glossary.py,sha256=V5nM7ZcTIm638uRZEuRaep54VAsEy25g0xwSL5cYDMA,933
9
+ contextsql/hints.py,sha256=j1WFF5wpC0BI42c0SzV-RvfVmzzF5Atn9pCRYFCkavM,467
10
+ contextsql/introspect.py,sha256=e0ZqeBT1KZDOp-GhACKJdL5ZLZBrqvKqYFj8HluOIRY,5900
11
+ contextsql/learn.py,sha256=a-DjsiWXsmKVop8QQkkdZ8P3SQ63H8RyUXphntKQFM8,2110
12
+ contextsql/names.py,sha256=0gHnf7zFLqBAmMeLNxnT5v-p1-mK-x6Qe-OXPz0wpZU,2383
13
+ contextsql/phrasing.py,sha256=KDmjd5t8NkW6SDa0rirRcbM4rQEXBMUUOCjV09JHd60,2712
14
+ contextsql/prompt.py,sha256=WAI-T8YK15HlLeZ9AuAi2JXPbcYBgTYJC60NNQptBhY,391
15
+ contextsql/retrieve.py,sha256=2oT9fesF41iXULGLqronIZQ9JcUuQDgsbot5VhISuEw,4909
16
+ contextsql_garvit-0.1.0.dist-info/METADATA,sha256=TukWDINpHNBYvtPKGBpWFw1qQnftA9aXiZod-xI0P70,13297
17
+ contextsql_garvit-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
18
+ contextsql_garvit-0.1.0.dist-info/entry_points.txt,sha256=qymGMaJxLClphugsUU_K4Jh_2VcoRiRcpHCaETgi3u4,51
19
+ contextsql_garvit-0.1.0.dist-info/top_level.txt,sha256=a6B-S8T68AT0NOiC6eMdG8E2JKoQxVenx85r3GJf07Y,11
20
+ contextsql_garvit-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ contextsql = contextsql.cli:main
@@ -0,0 +1 @@
1
+ contextsql