continue icon indicating copy to clipboard operation
continue copied to clipboard

PostgreSQL Context Provider

Open NinjaPerson24119 opened this issue 1 year ago • 2 comments

Adds a Postgres context provider that prints

  • Table schema
  • A few sample rows The context provider needs to be configured to a specific DB, and may be scoped to a specific schema.

The user can choose a specific table, or all tables. The all tables case will generate a set of independent schema files, which are the same as querying each table individually.

This makes it easy to use an LLM when writing SQL. E.g. migrations or queries, or even for writing database adjacent code such as ORM models.

Demo

Launch Postgres instance

version: '3.9'

services:
  postgres:
    container_name: cats-postgres
    image: postgres:16-alpine
    ports:
      - 5436:5432
    volumes:
      - ~/apps/postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=catsarecool
      - POSTGRES_USER=myuser
      - POSTGRES_DB=animals 

Connect and insert some data

psql -h localhost -p 5436 -U myuser -d animals 

-- Table schema for cats
CREATE TABLE cats (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    breed VARCHAR(100) NOT NULL
);

-- Table schema for dogs
CREATE TABLE dogs (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    breed VARCHAR(100) NOT NULL
);

-- Inserting data into the cats table
INSERT INTO cats (name, breed) VALUES
    ('Whiskers', 'Siamese'),
    ('Felix', 'Maine Coon'),
    ('Garfield', 'Persian');

-- Inserting data into the dogs table
INSERT INTO dogs (name, breed) VALUES
    ('Buddy', 'Golden Retriever'),
    ('Max', 'Labrador Retriever'),
    ('Bella', 'German Shepherd');

Add the config

    {
      "name": "postgres",
      "params": {
      "host": "localhost",
      "port": 5436,
      "user": "myuser",
      "password": "catsarecool",
      "database": "animals",
      "schema": "public"
      }
    }

See the results

The llama.cpp model in use here is deepseek33b

image image image image

Practical use-case with ORM image

NinjaPerson24119 avatar Feb 15 '24 05:02 NinjaPerson24119

@NinjaPerson24119 this is glorious

nothing to change, but do you want to add a quick description to docs/docs/customization/context-providers.md? Your demo here is longer than the others in that file, but if you put it in a tag it could just be copy/pasted

sestinj avatar Feb 15 '24 17:02 sestinj

Added some docs. Wasn't quite sure what you meant by "tag". I added a link to the PR since the entire description is basically a demo. @sestinj

NinjaPerson24119 avatar Feb 16 '24 02:02 NinjaPerson24119

looks great. thanks again!

sestinj avatar Feb 26 '24 06:02 sestinj