Home

Awesome

Defog SQLCoder

Defog's SQLCoder is a family of state-of-the-art LLMs for converting natural language questions to SQL queries.

Interactive Demo | 🤗 HF Repo | ♾️ Colab | 🐦 Twitter

TL;DR

SQLCoder is a family of large language models that outperforms gpt-4 and gpt-4-turbo for natural language to SQL generation tasks on our sql-eval framework, and significantly outperform all popular open-source models.

Percentage of correctly generated SQL queries on novel schemas not seen in training (n = 200), with 4 beams (2)

Installing SQLCoder

If running on a device with an NVIDIA GPU with more than 16GB VRAM (best performance) pip install "sqlcoder[transformers]"

If running on Apple Silicon (less good performance, because of quantization and lack of beam search) CMAKE_ARGS="-DLLAMA_METAL=on" pip install "sqlcoder[llama-cpp]"

If running on a non-apple silicon computer without GPU access, please run this on Linux/Intel Mac CMAKE_ARGS="-DLLAMA_BLAS=ON -DLLAMA_BLAS_VENDOR=OpenBLAS" pip install "sqlcoder[llama-cpp]"

And run this on Windows

$env:CMAKE_ARGS = "-DLLAMA_BLAS=ON -DLLAMA_BLAS_VENDOR=OpenBLAS"
pip install "sqlcoder[llama-cpp]"

SQLCoder has not been tested on other platforms yet. Contributions for testing on other platforms are very welcome!

Running SQLCoder

In your terminal, run sqlcoder launch

With this, you will be able to connect straight to your database, so you can add your metadata and query it visually.

License

The code in this repo (what little there is of it) is Apache-2 licensed. The model weights have a CC BY-SA 4.0 license. The TL;DR is that you can use and modify the model for any purpose – including commercial use. However, if you modify the weights (for example, by fine-tuning), you must open-source your modified weights under the same license terms.

Training

Defog was trained on more than 20,000 human-curated questions. These questions were based on 10 different schemas. None of the schemas in the training data were included in our evaluation framework.

You can read more about our training approach and evaluation framework.

Results by question category

We classified each generated question into one of 6 categories. The table displays the percentage of questions answered correctly by each model, broken down by category.

dategroup_byorder_byratiojoinwhere
sqlcoder-70b9691.497.185.797.191.4
sqlcoder-7b-29691.494.391.494.377.1
sqlcoder-34b8094.385.777.185.780
gpt-47294.397.18091.480
gpt-4-turbo7691.491.462.888.677.1
natural-sql-7b5688.685.76088.680
sqlcoder-7b6482.974.354.374.374.3
gpt-3.57277.182.834.365.771.4
claude-25271.474.357.165.762.9

Using SQLCoder

You can use SQLCoder via the transformers library by downloading our model weights from the Hugging Face repo. We have added sample code for inference on a sample database schema.

python inference.py -q "Question about the sample database goes here"

# Sample question:
# Do we get more revenue from customers in New York compared to customers in San Francisco? Give me the total revenue for each city, and the difference between the two.

You can also use a demo on our website here

Hardware Requirements

SQLCoder-34B has been tested on a 4xA10 GPU with float16 weights. You can also load an 8-bit and 4-bit quantized version of the model on consumer GPUs with 20GB or more of memory – like RTX 4090, RTX 3090, and Apple M2 Pro, M2 Max, or M2 Ultra Chips with 20GB or more of memory.

Todo

Star History

Star History Chart