ChatGPT Prompt for SQL Writing
Generate a prompt for writing SQL queries with LLMs like ChatGPT. Drop your database URL and table name into the script and it will generate a prompt for you to copy and paste into your favorite LLM.
What this does
- Snapshot of Table Structure: Understand the columns, types, and organization of your table at a glance.
- Sample Rows: Includes INSERT statements to describe the data in your table.
- Extracts Table and Field Comments: If you have comments on your tables or columns, they will be included in the prompt.
Usage
Install the package:
pip install llm-sql-prompt
Here's how to use it:
Usage: llm-sql-prompt [OPTIONS] DATABASE_URL [TABLE_NAME]
Options:
--help Show this message and exit.
Generate a prompt from a postgres database:
llm-sql-prompt postgresql://postgres:postgres@localhost:5555/database_name table_name | pbcopy
llm-sql-prompt $DATABASE_URL
From a local sqlite database:
llm-sql-prompt "$HOME/Library/Application Support/BeeperTexts/index.db" --all
Local CLI development
Run just install_editable to install this checkout globally in editable mode. Source changes are reflected without reinstalling.
This requires uv, just, zsh, and jq. If mise is available, the recipe also updates every installed pipx:llm-sql-prompt environment so mise can use the local checkout. Re-run after mise upgrades, which replace those editable installations.
Tunneling to a remote port
If you find yourself wanting to tunnel into a remote box and work with a production database, here's some helpful commands so you don't need to remember the weird SSH tunneling syntax:
function find_random_open_port() {
local start_port=${1:-1024}
local max_attempts=100
local attempt=0
local port=$start_port
while (( attempt < max_attempts )); do
if ! nc -z localhost $port 2>/dev/null; then
echo $port
return
fi
port=$((port + 1))
attempt=$((attempt + 1))
done
echo "No open port found after $max_attempts attempts, starting from $start_port." > /dev/stderr
return 1
}
function ssh-tunnel() {
if [ $# -lt 2 ]; then
echo "Usage: ssh-tunnel remote_host remote_port [local_port]"
echo "This function sets up SSH port forwarding."
return 1
fi
local remote_host=$1
local remote_port=$2
local local_port=${3:-$(find-random-open-port $remote_port)}
if [[ -z $local_port ]]; then
echo "Failed to find an open local port."
return 1
fi
echo "Forwarding local port $local_port to remote port $remote_port on $remote_host..."
set -x
ssh $remote_host -L ${local_port}:localhost:${remote_port}
}
TODO
Super basic script, needs a lot of work
- pg support
- one entrypoint
- use DB comments on columns + tables
- multiple tables
- prompt tweaking
- understand prompt size limits and sample records until one fits
Metadata
Release files for llm-sql-prompt 0.11.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| llm_sql_prompt-0.11.1.tar.gz | 8.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| llm_sql_prompt-0.11.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.5 kB
Release files / llm_sql_prompt-0.11.1.tar.gz
| Download URL | llm_sql_prompt-0.11.1.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
617b26c230c132dae8aa904439fdcd45de7f7d42955219cd77f570edb367fd70
|
|
BLAKE2b-256 checksum How to use checksums |
1bc8068560b5cffb3d34c2188cee08150b5acc6b0b68954bf65d13c01e1e52f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / llm_sql_prompt-0.11.1-py3-none-any.whl
| Download URL | llm_sql_prompt-0.11.1-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e5fb6a8464a59d09d3f5cc426208704dcaa1e41b888bb97e62c2d2614825c546
|
|
BLAKE2b-256 checksum How to use checksums |
905780009a67900ad97da444936055fbc73d5258789ef0bb75f2f936ef896e4f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|