llm-sql-prompt


Namellm-sql-prompt JSON
Version 0.10.0 PyPI version JSON
download
home_pageNone
SummaryUtility to generate ChatGPT prompts for SQL writing, offering table structure snapshots and sample row data from Postgres and sqlite databases.
upload_time2025-08-29 23:27:43
maintainerNone
docs_urlNone
authorNone
requires_python~=3.11
licenseNone
keywords chatgpt database llms postgres prompt sql sqlite
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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:

```shell
pip install llm-sql-prompt
```

Here's how to use it:

```shell
Usage: llm-sql-prompt [OPTIONS] DATABASE_URL [TABLE_NAME]

Options:
  --help  Show this message and exit.
```

Generate a prompt from a postgres database:

```shell
llm-sql-prompt postgresql://postgres:postgres@localhost:5555/database_name table_name | pbcopy
llm-sql-prompt $DATABASE_URL
```

From a local sqlite database:

```shell
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:

```shell
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

- [x] pg support
- [x] one entrypoint
- [x] use DB comments on columns + tables
- [x] multiple tables
- [ ] prompt tweaking
- [ ] understand prompt size limits and sample records until one fits
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "llm-sql-prompt",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "~=3.11",
    "maintainer_email": null,
    "keywords": "chatgpt, database, llms, postgres, prompt, sql, sqlite",
    "author": null,
    "author_email": "Michael Bianco <mike@mikebian.co>",
    "download_url": "https://files.pythonhosted.org/packages/ba/f7/e8928f5171b4caae2b3b9ac58c5b44da80ebb62db069ed0c6f80f44cf3d2/llm_sql_prompt-0.10.0.tar.gz",
    "platform": null,
    "description": "# ChatGPT Prompt for SQL Writing\n\nGenerate 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.\n\n## What this does\n\n- Snapshot of Table Structure: Understand the columns, types, and organization of your table at a glance.\n- Sample Rows: Includes INSERT statements to describe the data in your table.\n- Extracts Table and Field Comments: If you have comments on your tables or columns, they will be included in the prompt.\n\n## Usage\n\nInstall the package:\n\n```shell\npip install llm-sql-prompt\n```\n\nHere's how to use it:\n\n```shell\nUsage: llm-sql-prompt [OPTIONS] DATABASE_URL [TABLE_NAME]\n\nOptions:\n  --help  Show this message and exit.\n```\n\nGenerate a prompt from a postgres database:\n\n```shell\nllm-sql-prompt postgresql://postgres:postgres@localhost:5555/database_name table_name | pbcopy\nllm-sql-prompt $DATABASE_URL\n```\n\nFrom a local sqlite database:\n\n```shell\nllm-sql-prompt \"$HOME/Library/Application Support/BeeperTexts/index.db\" --all\n```\n\n### Tunneling to a remote port\n\nIf 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:\n\n```shell\nfunction find_random_open_port() {\n  local start_port=${1:-1024}\n  local max_attempts=100\n  local attempt=0\n  local port=$start_port\n\n  while (( attempt < max_attempts )); do\n    if ! nc -z localhost $port 2>/dev/null; then\n      echo $port\n      return\n    fi\n    port=$((port + 1))\n    attempt=$((attempt + 1))\n  done\n\n  echo \"No open port found after $max_attempts attempts, starting from $start_port.\" > /dev/stderr\n  return 1\n}\n\n\nfunction ssh-tunnel() {\n  if [ $# -lt 2 ]; then\n    echo \"Usage: ssh-tunnel remote_host remote_port [local_port]\"\n    echo \"This function sets up SSH port forwarding.\"\n    return 1\n  fi\n\n  local remote_host=$1\n  local remote_port=$2\n  local local_port=${3:-$(find-random-open-port $remote_port)}\n\n  if [[ -z $local_port ]]; then\n    echo \"Failed to find an open local port.\"\n    return 1\n  fi\n\n  echo \"Forwarding local port $local_port to remote port $remote_port on $remote_host...\"\n  set -x\n  ssh $remote_host -L ${local_port}:localhost:${remote_port}\n}\n```\n\n## TODO\n\nSuper basic script, needs a lot of work\n\n- [x] pg support\n- [x] one entrypoint\n- [x] use DB comments on columns + tables\n- [x] multiple tables\n- [ ] prompt tweaking\n- [ ] understand prompt size limits and sample records until one fits",
    "bugtrack_url": null,
    "license": null,
    "summary": "Utility to generate ChatGPT prompts for SQL writing, offering table structure snapshots and sample row data from Postgres and sqlite databases.",
    "version": "0.10.0",
    "project_urls": {
        "Homepage": "https://pypi.org/project/llm-sql-prompt/"
    },
    "split_keywords": [
        "chatgpt",
        " database",
        " llms",
        " postgres",
        " prompt",
        " sql",
        " sqlite"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "34e7b694733a5144e8d9e3461f2457a045ffe23eb93180d5d1fa30168bff0254",
                "md5": "0cf8ae8616ebab482795da652aa55868",
                "sha256": "a93ee207f5aa3fe32574a53e9cfbbcca21d1cbba5596c45de1e30e3209597855"
            },
            "downloads": -1,
            "filename": "llm_sql_prompt-0.10.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0cf8ae8616ebab482795da652aa55868",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "~=3.11",
            "size": 10820,
            "upload_time": "2025-08-29T23:27:42",
            "upload_time_iso_8601": "2025-08-29T23:27:42.887102Z",
            "url": "https://files.pythonhosted.org/packages/34/e7/b694733a5144e8d9e3461f2457a045ffe23eb93180d5d1fa30168bff0254/llm_sql_prompt-0.10.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "baf7e8928f5171b4caae2b3b9ac58c5b44da80ebb62db069ed0c6f80f44cf3d2",
                "md5": "2df4149f3a51263b1a387d1fceb33c8c",
                "sha256": "4360da3bea59eaee751dc3b09bf6933def7ce4e270c62fd95307f50637caea8c"
            },
            "downloads": -1,
            "filename": "llm_sql_prompt-0.10.0.tar.gz",
            "has_sig": false,
            "md5_digest": "2df4149f3a51263b1a387d1fceb33c8c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "~=3.11",
            "size": 19226,
            "upload_time": "2025-08-29T23:27:43",
            "upload_time_iso_8601": "2025-08-29T23:27:43.924249Z",
            "url": "https://files.pythonhosted.org/packages/ba/f7/e8928f5171b4caae2b3b9ac58c5b44da80ebb62db069ed0c6f80f44cf3d2/llm_sql_prompt-0.10.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-29 23:27:43",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "llm-sql-prompt"
}
        
Elapsed time: 1.74207s