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versational AI for SQL Generation
|
| 2 |
+
|
| 3 |
+
This repository contains sqlchat, a powerful and efficient language model designed specifically for Text-to-SQL tasks. It can understand natural language questions and database schemas to generate accurate SQL queries, including complex statements for creating and managing tables (Data Definition Language).
|
| 4 |
+
|
| 5 |
+
This model is provided as a standalone 4-bit quantized model, optimized for easy deployment and high-performance, low-resource inference. It was built using the Unsloth library to ensure maximum speed and memory efficiency.
|
| 6 |
+
|
| 7 |
+
Model Capabilities
|
| 8 |
+
|
| 9 |
+
Natural Language to SQL: Translates complex English questions into executable SQL queries.
|
| 10 |
+
|
| 11 |
+
Schema-Aware: Understands CREATE TABLE contexts provided in the prompt to generate relevant queries.
|
| 12 |
+
|
| 13 |
+
DDL Generation: Capable of generating CREATE TABLE statements, including constraints like PRIMARY KEY and FOREIGN KEY relationships.
|
| 14 |
+
|
| 15 |
+
Complex Query Logic: Successfully handles JOINs, aggregations (COUNT, MAX), and sorting (ORDER BY ... LIMIT).
|
| 16 |
+
|
| 17 |
+
How to Use
|
| 18 |
+
|
| 19 |
+
The easiest way to use sqlchat is with the Unsloth library, which will ensure you get the best performance.
|
| 20 |
+
|
| 21 |
+
Prerequisites
|
| 22 |
+
|
| 23 |
+
First, install the necessary libraries.
|
| 24 |
+
|
| 25 |
+
Generated bash
|
| 26 |
+
pip install unsloth
|
| 27 |
+
pip install "torch>=2.3.1"
|
| 28 |
+
|
| 29 |
+
Running Inference
|
| 30 |
+
|
| 31 |
+
Here is a simple, reusable Python script to run inference with the model.
|
| 32 |
+
|
| 33 |
+
Generated python
|
| 34 |
+
import torch
|
| 35 |
+
from unsloth import FastLanguageModel
|
| 36 |
+
from transformers import TextStreamer
|
| 37 |
+
|
| 38 |
+
# Load the sqlchat model from the Hugging Face Hub
|
| 39 |
+
# This is a standalone 4-bit model, so we load it as such.
|
| 40 |
+
print("Loading sqlchat model...")
|
| 41 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 42 |
+
model_name="nnul/sqlchat",
|
| 43 |
+
max_seq_length=4096,
|
| 44 |
+
dtype=None,
|
| 45 |
+
load_in_4bit=True,
|
| 46 |
+
)
|
| 47 |
+
print("Model loaded successfully.")
|
| 48 |
+
|
| 49 |
+
# This call optimizes the model for the fastest possible inference.
|
| 50 |
+
FastLanguageModel.for_inference(model)
|
| 51 |
+
|
| 52 |
+
def generate_sql(instruction: str, context: str = ""):
|
| 53 |
+
"""
|
| 54 |
+
A helper function to generate SQL from a natural language prompt.
|
| 55 |
+
"""
|
| 56 |
+
prompt = tokenizer.apply_chat_template(
|
| 57 |
+
[
|
| 58 |
+
{"role": "system", "content": "You are a helpful assistant that generates SQL queries based on natural language questions and database schemas."},
|
| 59 |
+
{"role": "user", "content": f"### Instruction:\n{instruction}\n\n### Context:\n{context}"},
|
| 60 |
+
],
|
| 61 |
+
tokenize=False,
|
| 62 |
+
add_generation_prompt=True,
|
| 63 |
+
enable_thinking=False, # Ensures direct SQL output
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
|
| 67 |
+
text_streamer = TextStreamer(tokenizer, skip_prompt=True, clean_up_tokenization_spaces=True)
|
| 68 |
+
|
| 69 |
+
print(f"User Instruction: {instruction}")
|
| 70 |
+
print("\nModel Output:")
|
| 71 |
+
print("---------------------------------")
|
| 72 |
+
_ = model.generate(
|
| 73 |
+
**inputs,
|
| 74 |
+
streamer=text_streamer,
|
| 75 |
+
max_new_tokens=256,
|
| 76 |
+
do_sample=False, # Use greedy decoding for deterministic output
|
| 77 |
+
use_cache=True,
|
| 78 |
+
)
|
| 79 |
+
print("---------------------------------\n")
|
| 80 |
+
|
| 81 |
+
# --- Example 1: Querying Data ---
|
| 82 |
+
generate_sql(
|
| 83 |
+
instruction="Which department has the most number of employees?",
|
| 84 |
+
context="CREATE TABLE department (name VARCHAR, num_employees INTEGER)"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# --- Example 2: Creating a Table (DDL) ---
|
| 88 |
+
generate_sql(
|
| 89 |
+
instruction="We need a table to manage student enrollments in courses. This table should link the 'students' table and the 'courses' table using their respective IDs.",
|
| 90 |
+
context="""
|
| 91 |
+
CREATE TABLE students (student_id INTEGER PRIMARY KEY, student_name VARCHAR(255));
|
| 92 |
+
CREATE TABLE courses (course_id INTEGER PRIMARY KEY, course_title VARCHAR(255));
|
| 93 |
+
"""
|
| 94 |
+
)
|
| 95 |
+
IGNORE_WHEN_COPYING_START
|
| 96 |
+
content_copy
|
| 97 |
+
download
|
| 98 |
+
Use code with caution.
|
| 99 |
+
Python
|
| 100 |
+
IGNORE_WHEN_COPYING_END
|
| 101 |
+
Expected Output
|
| 102 |
+
Generated code
|
| 103 |
+
User Instruction: Which department has the most number of employees?
|
| 104 |
+
|
| 105 |
+
Model Output:
|
| 106 |
+
---------------------------------
|
| 107 |
+
SELECT name FROM department ORDER BY num_employees DESC LIMIT 1;
|
| 108 |
+
---------------------------------
|
| 109 |
+
|
| 110 |
+
User Instruction: We need a table to manage student enrollments in courses. This table should link the 'students' table and the 'courses' table using their respective IDs.
|
| 111 |
+
|
| 112 |
+
Model Output:
|
| 113 |
+
---------------------------------
|
| 114 |
+
CREATE TABLE student_enrollment (student_id INTEGER, course_id INTEGER, PRIMARY KEY (student_id, course_id), FOREIGN KEY (student_id) REFERENCES students(student_id), FOREIGN KEY (course_id) REFERENCES courses(course_id));
|
| 115 |
+
---------------------------------
|
| 116 |
+
IGNORE_WHEN_COPYING_START
|
| 117 |
+
content_copy
|
| 118 |
+
download
|
| 119 |
+
Use code with caution.
|
| 120 |
+
IGNORE_WHEN_COPYING_END
|
| 121 |
+
Performance
|
| 122 |
+
|
| 123 |
+
The model was benchmarked on an NVIDIA A40 GPU. In a batch-processing scenario, it achieves a throughput of ~55-70 tokens/second. Single-prompt latency is well within real-time requirements for interactive applications.
|
| 124 |
+
|
| 125 |
+
Peak VRAM Usage (Inference): ~6.1 GB
|
| 126 |
+
|
| 127 |
+
Prompt Template
|
| 128 |
+
|
| 129 |
+
To get the best results, your prompts should follow this structure:
|
| 130 |
+
|
| 131 |
+
Generated code
|
| 132 |
+
<|im_start|>system
|
| 133 |
+
You are a helpful assistant that generates SQL queries based on natural language questions and database schemas.<|im_end|>
|
| 134 |
+
<|im_start|>user
|
| 135 |
+
### Instruction:
|
| 136 |
+
{Your natural language question}
|
| 137 |
+
|
| 138 |
+
### Context:
|
| 139 |
+
{The CREATE TABLE statements for the relevant tables}<|im_end|>
|
| 140 |
+
<|im_start|>assistant
|
| 141 |
+
IGNORE_WHEN_COPYING_START
|
| 142 |
+
content_copy
|
| 143 |
+
download
|
| 144 |
+
Use code with caution.
|
| 145 |
+
IGNORE_WHEN_COPYING_END
|