duckdb-nsql

7B parameter text-to-SQL model made by MotherDuck and Numbers Station.

7b
Hızlı Kurulum (Ollama kuruluysa)
ollama run duckdb-nsql

Ollama kurulu değil mi? ollama.com/download — Windows, macOS ve Linux için ücretsiz. İlk çalıştırmada model indirilir, sonrası tamamen çevrimdışıdır.

Varyantlar

Boyut büyüdükçe kalite artar, donanım ihtiyacı yükselir. Başlangıç için küçük varyantı deneyin.

EtiketBoyutBağlamGirdiKomut
latest 3.8GB 16K Text ollama run duckdb-nsql:latest
7b 3.8GB 16K Text ollama run duckdb-nsql:7b
7b-q2_K 2.5GB 16K Text ollama run duckdb-nsql:7b-q2_K
7b-q3_K_S 2.9GB 16K Text ollama run duckdb-nsql:7b-q3_K_S
7b-q3_K_M 3.3GB 16K Text ollama run duckdb-nsql:7b-q3_K_M
7b-q3_K_L 3.6GB 16K Text ollama run duckdb-nsql:7b-q3_K_L
7b-q4_0 3.8GB 16K Text ollama run duckdb-nsql:7b-q4_0
7b-q4_1 4.2GB 16K Text ollama run duckdb-nsql:7b-q4_1
7b-q4_K_S 3.9GB 16K Text ollama run duckdb-nsql:7b-q4_K_S
7b-q4_K_M 4.1GB 16K Text ollama run duckdb-nsql:7b-q4_K_M
7b-q5_0 4.7GB 16K Text ollama run duckdb-nsql:7b-q5_0
7b-q5_1 5.1GB 16K Text ollama run duckdb-nsql:7b-q5_1
7b-q5_K_S 4.7GB 16K Text ollama run duckdb-nsql:7b-q5_K_S
7b-q5_K_M 4.8GB 16K Text ollama run duckdb-nsql:7b-q5_K_M
7b-q6_K 5.5GB 16K Text ollama run duckdb-nsql:7b-q6_K
7b-q8_0 7.2GB 16K Text ollama run duckdb-nsql:7b-q8_0
7b-fp16 13GB 16K Text ollama run duckdb-nsql:7b-fp16

Model Detayları ve Benchmarklar (kaynak: ollama.com)

duckdb-nsql model

DuckDB-NSQL is a 7 billion parameter text-to-SQL model designed specifically for SQL generation tasks.

This model is based on Meta’s original Llama-2 7B model and further pre-trained on a dataset of general SQL queries and then fine-tuned on a dataset composed of DuckDB text-to-SQL pairs.

Usage

Example Prompt

Provided this schema:

CREATE TABLE taxi (
    VendorID bigint,
    tpep_pickup_datetime timestamp,
    tpep_dropoff_datetime timestamp,
    passenger_count double,
    trip_distance double,
    fare_amount double,
    extra double,
    tip_amount double,
    tolls_amount double,
    improvement_surcharge double,
    total_amount double,
);

Give me taxis with more than 2 passengers

Example output

SELECT * FROM taxi WHERE passenger_count > 2

Setting the system prompt

This model expects the schema in the system prompt as input:

/set system """Here is the database schema that the SQL query will run on:
CREATE TABLE taxi (
    VendorID bigint,
    tpep_pickup_datetime timestamp,
    tpep_dropoff_datetime timestamp,
    passenger_count double,
    trip_distance double,
    fare_amount double,
    extra double,
    tip_amount double,
    tolls_amount double,
    improvement_surcharge double,
    total_amount double,
);"""

Once the schema is provided in the system prompt, the model will use it in subsequent responses.

For the following prompt:

get all columns ending with _amount from taxi table

The model will output something like this:

SELECT COLUMNS('.*_amount') FROM taxi;

API example

$ curl http://localhost:11434/api/generate -d '{
    "model": "duckdb-nsql:7b-q4_0",
    "system": "Here is the database schema that the SQL query will run on: CREATE TABLE taxi (VendorID bigint, tpep_pickup_datetime timestamp, tpep_dropoff_datetime timestamp, passenger_count double, trip_distance double, fare_amount double, extra double, tip_amount double, tolls_amount double, improvement_surcharge double, total_amount double,);",
    "prompt": "get all columns ending with _amount from taxi table"
}'

Python library example

pip install ollama
import ollama

r = ollama.generate(
    model='duckdb-nsql:7b-q4_0',
    system='''Here is the database schema that the SQL query will run on:
CREATE TABLE taxi (
    VendorID bigint,
    tpep_pickup_datetime timestamp,
    tpep_dropoff_datetime timestamp,
    passenger_count double,
    trip_distance double,
    fare_amount double,
    extra double,
    tip_amount double,
    tolls_amount double,
    improvement_surcharge double,
    total_amount double,
);''',
    prompt='get all columns ending with _amount from taxi table',
)

print(r['response'])

Training Data

200k DuckDB text-to-SQL pairs, synthetically generated using Mixtral-8x7B-Instruct-v0.1, guided by the DuckDB v0.9.2 documentation. And text-to-SQL pairs from NSText2SQL that were transpiled to DuckDB SQL using sqlglot.

Training Procedure

DuckDB-NSQL was trained using cross-entropy loss to maximize the likelihood of sequential inputs. For finetuning on text-to-SQL pairs, we only compute the loss over the SQL portion of the pair. The model is trained using 80GB A100s, leveraging data and model parallelism. We fine-tuned for 10 epochs.

Intended Use and Limitations

The model was designed for text-to-SQL generation tasks from given table schema and natural language prompts. The model works best with the prompt format defined below and outputs. In contrast to existing text-to-SQL models, the SQL generation is not contrained to SELECT statements, but can generate any valid DuckDB SQL statement, including statements for official DuckDB extensions.

References

Hugging Face