|
| 1 | +# Using the \llm Command (AI-assisted SQL) |
| 2 | + |
| 3 | +The `\llm` special command lets you ask natural-language questions and get SQL proposed for you. It uses the open‑source `llm` CLI under the hood and enriches your prompt with database context (schema and one sample row per table) so answers can include runnable SQL. |
| 4 | + |
| 5 | +Alias: `\ai` works the same as `\llm`. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Quick Start |
| 10 | + |
| 11 | +1) Configure your API key (only needed for remote providers like OpenAI): |
| 12 | + |
| 13 | +```text |
| 14 | +\llm keys set openai |
| 15 | +``` |
| 16 | + |
| 17 | +2) Ask a question. The response’s SQL (inside a ```sql fenced block) is extracted and pre-filled at the prompt: |
| 18 | + |
| 19 | +```text |
| 20 | +World> \llm "Capital of India?" |
| 21 | +-- Answer text from the model... |
| 22 | +-- ```sql |
| 23 | +-- SELECT ...; |
| 24 | +-- ``` |
| 25 | +-- Your prompt is prefilled with the SQL above. |
| 26 | +``` |
| 27 | + |
| 28 | +You can now hit Enter to run, or edit the query first. |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## What Context Is Sent |
| 33 | + |
| 34 | +When you ask a plain question via `\llm "..."`, mycli: |
| 35 | +- Sends your question. |
| 36 | +- Adds your current database schema: table names with column types. |
| 37 | +- Adds one sample row (if available) from each table. |
| 38 | + |
| 39 | +This helps the model propose SQL that fits your schema. Follow‑ups using `-c` continue the same conversation and do not re-send the DB context (see “Continue Conversation (-c)”). |
| 40 | + |
| 41 | +Note: Context is gathered from the current connection. If you are not connected, using contextual mode will fail — connect first. |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## Using `llm` Subcommands from mycli |
| 46 | + |
| 47 | +You can run any `llm` CLI subcommand by prefixing it with `\llm` inside mycli. Examples: |
| 48 | + |
| 49 | +- List models: |
| 50 | + ```text |
| 51 | + \llm models |
| 52 | + ``` |
| 53 | +- Set the default model: |
| 54 | + ```text |
| 55 | + \llm models default gpt-5 |
| 56 | + ``` |
| 57 | +- Set provider API key: |
| 58 | + ```text |
| 59 | + \llm keys set openai |
| 60 | + ``` |
| 61 | +- Install a plugin (e.g., local models via Ollama): |
| 62 | + ```text |
| 63 | + \llm install llm-ollama |
| 64 | + ``` |
| 65 | + After installing or uninstalling plugins, mycli will restart to pick up new commands. |
| 66 | + |
| 67 | +Tab completion works for `\llm` subcommands, and even for model IDs under `models default`. |
| 68 | + |
| 69 | +Aside: <https://ollama.com/> for using local models. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## Ask Questions With DB Context (default) |
| 74 | + |
| 75 | +Ask your question in quotes. mycli sends database context and extracts a SQL block if present. |
| 76 | + |
| 77 | +```text |
| 78 | +World> \llm "Most visited urls?" |
| 79 | +``` |
| 80 | + |
| 81 | +Behavior: |
| 82 | +- Response is printed in the output pane. |
| 83 | +- If the response contains a ```sql fenced block, mycli extracts the SQL and pre-fills it at your prompt. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## Continue Conversation (-c) |
| 88 | + |
| 89 | +Use `-c` to ask a follow‑up that continues the previous conversation with the model. This does not re-send the DB context; it relies on the ongoing thread. |
| 90 | + |
| 91 | +```text |
| 92 | +World> \llm "Top 10 customers by spend" |
| 93 | +-- model returns analysis and a ```sql block; SQL is prefilled |
| 94 | +World> \llm -c "Now include each customer's email and order count" |
| 95 | +``` |
| 96 | + |
| 97 | +Behavior: |
| 98 | +- Continues the last conversation in the `llm` history. |
| 99 | +- Database context is not re-sent on follow‑ups. |
| 100 | +- If the response includes a ```sql block, the SQL is pre-filled at your prompt. |
| 101 | + |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## Examples |
| 106 | + |
| 107 | +- List available models: |
| 108 | + ```text |
| 109 | + World> \llm models |
| 110 | + ``` |
| 111 | + |
| 112 | +- Change default model: |
| 113 | + ```text |
| 114 | + World> \llm models default llama3 |
| 115 | + ``` |
| 116 | + |
| 117 | +- Set API key (for providers that require it): |
| 118 | + ```text |
| 119 | + World> \llm keys set openai |
| 120 | + ``` |
| 121 | + |
| 122 | +- Ask a question with context: |
| 123 | + ```text |
| 124 | + World> \llm "Capital of India?" |
| 125 | + ``` |
| 126 | + |
| 127 | +- Use a local model (after installing a plugin such as `llm-ollama`): |
| 128 | + ```text |
| 129 | + World> \llm install llm-ollama |
| 130 | + World> \llm models default llama3 |
| 131 | + World> \llm "Top 10 customers by spend" |
| 132 | + ``` |
| 133 | + |
| 134 | +See: <https://ollama.com/> for details. |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## Customize the Prompt Template |
| 139 | + |
| 140 | +mycli uses a saved `llm` template named `mycli-llm-template` for contextual questions. You can view or edit it: |
| 141 | + |
| 142 | +```text |
| 143 | +World> \llm templates edit mycli-llm-template |
| 144 | +``` |
| 145 | + |
| 146 | +Tip: After first use, mycli ensures this template exists. To just view it without editing, use: |
| 147 | + |
| 148 | +```text |
| 149 | +World> \llm templates show mycli-llm-template |
| 150 | +``` |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## Troubleshooting |
| 155 | + |
| 156 | +- No SQL pre-fill: Ensure the model’s response includes a ```sql fenced block. The built‑in prompt encourages this, but some models may omit it; try asking the model to include SQL in a ```sql block. |
| 157 | +- Not connected to a database: Contextual questions require a live connection. Connect first. Follow‑ups with `-c` only help after a successful contextual call. |
| 158 | +- Plugin changes not recognized: After `\llm install` or `\llm uninstall`, mycli restarts automatically to load new commands. |
| 159 | +- Provider/API issues: Use `\llm keys list` and `\llm keys set <provider>` to check credentials. Use `\llm models` to confirm available models. |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +## Notes and Safety |
| 164 | + |
| 165 | +- Data sent: Contextual questions send schema (table/column names and types) and a single sample row per table. Review your data sensitivity policies before using remote models; prefer local models (such as ollama) if needed. |
| 166 | +- Help: Running `\llm` with no arguments shows a short usage message. |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## Learn More |
| 171 | + |
| 172 | +- `llm` project docs: https://llm.datasette.io/ |
| 173 | +- `llm` plugin directory: https://llm.datasette.io/en/stable/plugins/directory.html |
0 commit comments