explaingit

omarish/llm-sql

Analysis updated 2026-05-18

1PLpgSQLAudience · developerComplexity · 4/5Setup · moderate

TLDR

A learning project that runs GPT-2 text generation entirely inside a Postgres database, implementing the whole model as SQL functions using the pgvector extension.

Mindmap

mindmap
  root((llm-sql))
    What it does
      Runs GPT-2 in SQL
      Generates text in Postgres
      Tokenizes in SQL
    Tech stack
      PLpgSQL
      Postgres
      pgvector
      Python
    Use cases
      Learn transformer internals
      Explore pgvector matmuls
      Experiment with SQL AI
    Audience
      Developers
      Researchers
    Setup
      Docker Postgres
      Export weights
      Load and run demo

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Study how a transformer language model's forward pass works by reading it expressed as SQL.

USE CASE 2

Experiment with pgvector for storing and multiplying model weight vectors inside a database.

USE CASE 3

Run small text completions directly from a SQL query for teaching or demo purposes.

What is it built with?

PLpgSQLPostgrespgvectorPythonDocker

How does it compare?

omarish/llm-sqlsaiprajoth/timescaledb-labakshayramabhat/claude-security-kit
Stars110
LanguagePLpgSQLPLpgSQLPLpgSQL
Setup difficultymoderatemoderate
Complexity4/53/52/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · moderate Time to first run · 30min

Requires Docker, the uv Python tool, and a psql client, plus downloading and exporting GPT-2 weights before first run.

In plain English

llm-sql is a project that runs GPT-2, a small text generating AI model, entirely inside a Postgres database using plain SQL. Every part of how the model produces text, including looking up word pieces, tracking position in a sentence, its internal math layers, and even the process that breaks input text into tokens, is written as SQL functions instead of the usual Python or specialized AI code. The model's weights, which are the numbers the AI learned during training, are stored as regular database rows using a Postgres extension called pgvector that is built for storing and comparing lists of numbers efficiently. When you type a SQL command like asking it to complete the phrase "Hello, I", the database itself performs all the calculations needed to guess what text comes next, one word piece at a time. This is explicitly a toy and learning project rather than a fast or production ready tool. Generating each word piece takes about one second, since the database is doing work that would normally run on specialized AI hardware. The project does include a caching trick so that generating a new word only requires looking at the newest input rather than reprocessing the whole conversation each time, which keeps it from getting slower as the text grows. To try it, you need Docker to run the Postgres database, a Python tool called uv to manage dependencies, and the psql command line client. The setup process downloads the GPT-2 model, exports its weights into files, loads them into the database, and installs the SQL functions. A demo script then lets you type a prompt and see the generated text. The author is upfront that this should never be used anywhere exposed to the internet, since the included database setup disables normal safety checks and uses simple default passwords, only meant for a private test on your own machine. It only supports the smallest version of GPT-2, and its word splitting logic is a close but not perfect match for the original.

Copy-paste prompts

Prompt 1
Explain how this project implements GPT-2's self-attention using pgvector dot products.
Prompt 2
Help me set up Docker and load the GPT-2 weights so I can run the demo.sh script.
Prompt 3
Walk me through how the SQL tokenizer in gpt2_encode() approximates GPT-2's real BPE tokenizer.
Prompt 4
Show me how the KV cache table keeps token generation from getting slower as text grows.

Frequently asked questions

What is llm-sql?

A learning project that runs GPT-2 text generation entirely inside a Postgres database, implementing the whole model as SQL functions using the pgvector extension.

What language is llm-sql written in?

Mainly PLpgSQL. The stack also includes PLpgSQL, Postgres, pgvector.

How hard is llm-sql to set up?

Setup difficulty is rated moderate, with roughly 30min to a first successful run.

Who is llm-sql for?

Mainly developer.

Open on GitHub → Explain another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.