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.

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

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.5.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

llm_sql_prompt-0.5.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_sql_prompt-0.5.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

File hashes

Hashes for llm_sql_prompt-0.5.1.tar.gz
Algorithm Hash digest
SHA256 26c369b0a034f5feb19b099e34184bc87a380e56c3802d731a7e415c2d211db3
MD5 1497c09713f5bb6c0727d3a37f1d3e7f
BLAKE2b-256 f5043351c4a61750b03c71d0593aa37e84cd0027a8d5b6e1130eae1d1f7c4c09

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llm_sql_prompt-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

File hashes

Hashes for llm_sql_prompt-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ee6b3829951097f6a155a00a7cdb4a3b6d2303dfea9588ef0cd97cc023441e0a
MD5 608b848c834d9fa243c3f05865916cd9
BLAKE2b-256 9a40da8e2a6ba25cb969d414e16730c27985bf91e484141febbbd42f0f17d8e1

See more details on using hashes here.

Supported by

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