contextsql-garvit 0.1.0__tar.gz → 0.1.2__tar.gz

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