loftytechlabsdev commited on
Commit
8dbcbee
ยท
verified ยท
1 Parent(s): 1264537

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +161 -0
README.md CHANGED
@@ -1,3 +1,164 @@
1
  ---
2
  license: apache-2.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ language:
4
+ - en
5
+ - sql
6
+ tags:
7
+ - text-to-sql
8
+ - qwen
9
+ - gguf
10
+ - local-ai
11
+ - unsloth
12
+ - sql-generation
13
+ datasets:
14
+ - xlangai/spider
15
+ pipeline_tag: text-generation
16
+ base_model:
17
+ - Qwen/Qwen2.5-Coder-1.5B-Instruct
18
  ---
19
+
20
+ # ๐Ÿš€ Caden SQL (1.5B) - Text-to-SQL AI
21
+
22
+ <img src="https://img.shields.io/badge/Status-Active-brightgreen" alt="Model Status" /> <img src="https://img.shields.io/badge/Size-1.5B-blue" alt="Model Size" /> <img src="https://img.shields.io/badge/Quantization-Q4__K__M-orange" alt="Quantization" /> <img src="https://img.shields.io/badge/Framework-Unsloth-purple" alt="Framework" />
23
+
24
+ **Caden** is a highly specialized, localized Artificial Intelligence designed to convert natural language questions into complex, production-ready SQL queries. It was built by fine-tuning the powerful `Qwen2.5-Coder-1.5B-Instruct` model on the extensive **Spider** dataset using Unsloth.
25
+
26
+ By utilizing 4-bit GGUF quantization, Caden is designed to be completely offline and privacy-first. You can query your private company databases locally on a standard laptop without ever sending your sensitive database schema to cloud APIs like OpenAI or Anthropic.
27
+
28
+ ---
29
+
30
+ ## ๐Ÿ“Š Model Architecture & Details
31
+ * **Base Model:** `Qwen/Qwen2.5-Coder-1.5B-Instruct`
32
+ * **Parameters:** 1.5 Billion
33
+ * **Format:** GGUF (`Q4_K_M` Quantized)
34
+ * **Training Framework:** [Unsloth](https://github.com/unslothai/unsloth) (Fast LoRA adapters, successfully merged)
35
+ * **Context Length:** 32,768 tokens (Capable of ingesting massive database schemas!)
36
+
37
+ ---
38
+
39
+ ## ๐Ÿง  Capabilities
40
+ Unlike basic SQL generators that only output `SELECT * FROM table`, Caden has been aggressively fine-tuned to master complex relationships:
41
+ * **Advanced Joins:** Seamlessly traverses foreign keys and uses `LEFT JOIN` and `INNER JOIN` appropriately.
42
+ * **Correlated Subqueries:** Can nest queries dynamically (`WHERE salary > (SELECT AVG(salary) FROM...)`).
43
+ * **Analytical Window Functions:** Excels at `RANK()`, `ROW_NUMBER()`, and `PARTITION BY`.
44
+ * **Set Operations:** Fluidly handles `INTERSECT`, `EXCEPT`, and `UNION`.
45
+ * **Conversational Awareness:** Caden is trained to converse naturally when greeted, but strictly outputs SQL blocks when requested.
46
+
47
+ ---
48
+
49
+ ## ๐Ÿ› ๏ธ Prompt Format (ChatML)
50
+
51
+ The model expects inputs formatted in **ChatML** with a specific structured template:
52
+
53
+ ```json
54
+ [
55
+ {
56
+ "role": "system",
57
+ "content": "You are Caden, an expert SQL engineer and helpful database assistant. Your primary task is to write single, accurate, and efficient SQL queries based on the given database schema and user questions."
58
+ },
59
+ {
60
+ "role": "user",
61
+ "content": "### Database Schema DDL:\nCREATE TABLE head (age INT, name VARCHAR(20));\n\n### User Request:\nFind names of heads whose age is older than 50.\n\nGenerate the SQL query that answers the user request."
62
+ }
63
+ ]
64
+ ```
65
+
66
+ ### Response Example:
67
+ ```sql
68
+ SELECT name FROM head WHERE age > 50;
69
+ ```
70
+
71
+ ---
72
+
73
+ ## ๐Ÿ’ป Getting Started & Usage
74
+
75
+ ### 1. Running Locally with Ollama (Recommended)
76
+ Ollama is the fastest way to run Caden on MacOS, Windows, or Linux.
77
+
78
+ 1. Download the `caden-sql-1.5b-q4_k_m.gguf` file from the Files tab.
79
+ 2. Create a file named `Modelfile` in the same folder with this configuration:
80
+ ```dockerfile
81
+ FROM ./caden-sql-1.5b-q4_k_m.gguf
82
+ SYSTEM """You are Caden, an expert SQL engineer and helpful database assistant. Your primary task is to write single, accurate, and efficient SQL queries based on the given database schema and user questions.
83
+
84
+ Follow these strict rules when the user asks for data or a query:
85
+ 1. Generate valid SQL syntax only.
86
+ 2. Use ONLY the table and column names present in the provided schema DDL.
87
+ 3. Carefully observe foreign key relationships when performing JOIN operations.
88
+ 4. Unless explicitly requested by the user, only produce read-only queries (SELECT).
89
+ 5. Provide your SQL query enclosed in a single ```sql ... ``` block.
90
+
91
+ If the user asks a general conversational question, respond conversationally and naturally without generating SQL."""
92
+ ```
93
+ 3. Build and register the model with Ollama:
94
+ ```bash
95
+ ollama create caden-sql -f Modelfile
96
+ ```
97
+ 4. Query the model in your terminal:
98
+ ```bash
99
+ ollama run caden-sql
100
+ ```
101
+
102
+ ### 2. Running via Hugging Face Transformers (Python)
103
+
104
+ If you are using the unquantized or merged weights directly via Python:
105
+
106
+ ```python
107
+ import torch
108
+ from transformers import AutoModelForCausalLM, AutoTokenizer
109
+
110
+ model_name = "loftytechlabsdev/Caden-SQL-1.5B"
111
+
112
+ model = AutoModelForCausalLM.from_pretrained(
113
+ model_name,
114
+ torch_dtype=torch.float16,
115
+ device_map="auto"
116
+ )
117
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
118
+
119
+ # Format the message using ChatML
120
+ messages = [
121
+ {
122
+ "role": "system",
123
+ "content": "You are Caden, an expert SQL engineer and helpful database assistant. Your primary task is to write single, accurate, and efficient SQL queries based on the given database schema and user questions."
124
+ },
125
+ {
126
+ "role": "user",
127
+ "content": "### Database Schema DDL:\nCREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(50), city VARCHAR(50));\n\n### User Request:\nHow many customers are from London?\n\nGenerate the SQL query that answers the user request."
128
+ }
129
+ ]
130
+
131
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
132
+ inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
133
+
134
+ outputs = model.generate(**inputs, max_new_tokens=256, use_cache=True)
135
+ response = tokenizer.decode(outputs[0][len(inputs.input_ids[0]):], skip_special_tokens=True)
136
+ print(response)
137
+ ```
138
+
139
+ ---
140
+
141
+ ## ๐Ÿ“ˆ Training Details & Hyperparameters
142
+
143
+ * **Base Model**: `unsloth/Qwen2.5-Coder-1.5B-Instruct`
144
+ * **Method**: QLoRA (4-bit quantization, rank `r = 16`, `lora_alpha = 16`)
145
+ * **Dataset**: `xlangai/spider` / `philikai/SQL_Spider_DDL` (containing 10,000+ text-to-SQL alignment examples)
146
+ * **Optimizer**: `adamw_8bit`
147
+ * **Learning Rate**: `2e-4`
148
+ * **Scheduler**: `linear`
149
+ * **Weight Decay**: `0.01`
150
+ * **Batch Size**: 4 per device, 4 gradient accumulation steps (Effective batch size = 16)
151
+ * **Training Steps**: 200 SFT steps (tuned to prevent overfitting while maintaining high zero-shot SQL validation accuracy)
152
+
153
+ ---
154
+
155
+ ## โš ๏ธ Limitations & Bias
156
+
157
+ * **Database Engine**: Primarily trained and evaluated on general SQLite/ANSI SQL. Syntaxes specific to PostgreSQL, MSSQL, or Oracle may require small manual adjustments or specialized fine-tuning.
158
+ * **Query Complexity**: Excellent for joins, subqueries, group by, and aggregate operations. Highly complex recursive CTEs or vendor-specific window functions may occasionally require manual corrections.
159
+
160
+ ---
161
+
162
+ ## ๐Ÿ“œ License
163
+
164
+ This project is released under the **Apache 2.0** License, adhering to the base model guidelines of the Qwen series.