sqlcoder/README.md

68 lines
4.2 KiB
Markdown
Raw Normal View History

2023-08-18 06:10:53 +08:00
# Defog SQLCoder
2024-02-06 06:59:52 +08:00
Defog's SQLCoder is a family of state-of-the-art LLMs for converting natural language questions to SQL queries.
2023-08-18 06:10:53 +08:00
2024-02-07 15:13:45 +08:00
[Interactive Demo](https://defog.ai/sqlcoder-demo/) | [🤗 HF Repo](https://huggingface.co/defog/sqlcoder-7b-2) | [♾️ Colab](https://colab.research.google.com/drive/1z4rmOEiFkxkMiecAWeTUlPl0OmKgfEu7?usp=sharing) | [🐦 Twitter](https://twitter.com/defogdata)
2023-08-18 06:10:53 +08:00
## TL;DR
2024-01-30 21:44:16 +08:00
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](https://github.com/defog-ai/sql-eval) framework, and significantly outperform all popular open-source models.
2023-08-18 14:42:58 +08:00
2024-02-08 03:58:58 +08:00
![Percentage of correctly generated SQL queries on novel schemas not seen in training (n = 200) in SQL-Eval](https://github.com/defog-ai/sqlcoder/assets/5008293/839aa98f-101d-4baa-8e80-bb4fd5110012)]
2024-02-08 03:41:57 +08:00
## Installing SQLCoder
If running on a device with an NVIDIA GPU with more than 16GB VRAM (best performance)
2024-02-08 03:58:58 +08:00
`pip install "sqlcoder[transformers]"`
2024-02-08 03:41:57 +08:00
If running on Apple Silicon (medium performance)
2024-02-08 03:58:58 +08:00
`CMAKE_ARGS="-DLLAMA_METAL=on" pip install "sqlcoder[llama-cpp]"`
2024-02-08 03:41:57 +08:00
2024-02-08 04:06:55 +08:00
SQLCoder has not been tested on other platforms yet. Contributions for testing on other platforms are very welcome!
2024-02-08 03:41:57 +08:00
2024-02-08 03:58:58 +08:00
## Running SQLCoder
In your terminal, run
`sqlcoder launch`
2023-08-18 06:10:53 +08:00
2024-02-08 04:17:03 +08:00
With this, you will be able to connect straight to your database, so you can add your metadata and query it visually.
2023-11-15 01:45:13 +08:00
2023-08-21 16:31:33 +08:00
## License
2023-08-21 16:32:13 +08:00
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.
2023-08-21 16:31:33 +08:00
2023-08-18 06:10:53 +08:00
## Training
2023-10-04 19:45:05 +08:00
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.
2023-08-18 06:10:53 +08:00
2023-10-04 19:45:05 +08:00
You can read more about our [training approach](https://defog.ai/blog/open-sourcing-sqlcoder2-7b/) and [evaluation framework](https://defog.ai/blog/open-sourcing-sqleval/).
2023-08-18 06:10:53 +08:00
## Results by question category
2024-01-30 21:44:16 +08:00
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.
2024-02-07 15:13:30 +08:00
| | date | group_by | order_by | ratio | join | where |
| -------------- | ---- | -------- | -------- | ----- | ---- | ----- |
| sqlcoder-70b | 96 | 91.4 | 97.1 | 85.7 | 97.1 | 91.4 |
2024-02-07 21:05:49 +08:00
| sqlcoder-7b-2 | 96 | 91.4 | 94.3 | 91.4 | 94.3 | 77.1 |
2024-02-07 15:13:30 +08:00
| sqlcoder-34b | 80 | 94.3 | 85.7 | 77.1 | 85.7 | 80 |
| gpt-4 | 72 | 94.3 | 97.1 | 80 | 91.4 | 80 |
| gpt-4-turbo | 76 | 91.4 | 91.4 | 62.8 | 88.6 | 77.1 |
| natural-sql-7b | 56 | 88.6 | 85.7 | 60 | 88.6 | 80 |
| sqlcoder-7b | 64 | 82.9 | 74.3 | 54.3 | 74.3 | 74.3 |
| gpt-3.5 | 72 | 77.1 | 82.8 | 34.3 | 65.7 | 71.4 |
| claude-2 | 52 | 71.4 | 74.3 | 57.1 | 65.7 | 62.9 |
2023-11-15 01:45:13 +08:00
2023-08-18 06:10:53 +08:00
## 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](./inference.py) on a [sample database schema](./metadata.sql).
```bash
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.
```
2023-11-15 01:37:57 +08:00
You can also use a demo on our website [here](https://defog.ai/sqlcoder-demo)
2023-08-18 06:10:53 +08:00
2023-08-18 19:03:43 +08:00
## Hardware Requirements
2023-11-15 01:37:57 +08:00
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.
2023-08-18 19:03:43 +08:00
2023-08-18 06:10:53 +08:00
## Todo
2023-08-18 06:20:36 +08:00
- [x] Open-source the v1 model weights
2023-10-04 19:45:05 +08:00
- [x] Train the model on more data, with higher data variance
2023-08-18 06:20:36 +08:00
- [ ] Tune the model further with Reward Modelling and RLHF
- [ ] Pretrain a model from scratch that specializes in SQL analysis