Skip to main content

Utility to generate ChatGPT prompts for SQL writing, offering table structure snapshots and sample row data from Postgres and sqlite databases.

Project description

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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_sql_prompt-0.10.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_sql_prompt-0.10.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file llm_sql_prompt-0.10.0.tar.gz.

File metadata

  • Download URL: llm_sql_prompt-0.10.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.27

File hashes

Hashes for llm_sql_prompt-0.10.0.tar.gz
Algorithm Hash digest
SHA256 4360da3bea59eaee751dc3b09bf6933def7ce4e270c62fd95307f50637caea8c
MD5 2df4149f3a51263b1a387d1fceb33c8c
BLAKE2b-256 baf7e8928f5171b4caae2b3b9ac58c5b44da80ebb62db069ed0c6f80f44cf3d2

See more details on using hashes here.

File details

Details for the file llm_sql_prompt-0.10.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_sql_prompt-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a93ee207f5aa3fe32574a53e9cfbbcca21d1cbba5596c45de1e30e3209597855
MD5 0cf8ae8616ebab482795da652aa55868
BLAKE2b-256 34e7b694733a5144e8d9e3461f2457a045ffe23eb93180d5d1fa30168bff0254

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page