File size: 8,359 Bytes
c447aa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22df337
c447aa0
22df337
c447aa0
22df337
c447aa0
22df337
 
 
 
 
 
 
 
 
 
 
c447aa0
22df337
c447aa0
22df337
 
 
 
 
 
c447aa0
22df337
c447aa0
22df337
c447aa0
22df337
c447aa0
22df337
 
 
 
 
 
 
 
c447aa0
22df337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c447aa0
 
22df337
 
 
 
 
 
 
 
 
 
 
 
 
c447aa0
 
22df337
 
 
 
 
 
c447aa0
 
 
 
 
22df337
 
c447aa0
 
 
 
 
 
 
 
 
 
 
22df337
 
c447aa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22df337
c447aa0
22df337
c447aa0
22df337
c447aa0
 
 
 
 
 
 
 
 
 
 
22df337
c447aa0
22df337
c447aa0
22df337
c447aa0
 
 
22df337
c447aa0
22df337
c447aa0
 
22df337
c447aa0
 
 
 
 
22df337
c447aa0
 
 
 
 
22df337
c447aa0
 
 
 
 
 
22df337
c447aa0
 
22df337
c447aa0
 
 
 
 
 
 
 
22df337
c447aa0
 
22df337
c447aa0
22df337
c447aa0
22df337
 
 
 
 
 
 
 
c447aa0
22df337
c447aa0
22df337
 
 
 
c447aa0
22df337
c447aa0
22df337
c447aa0
 
 
 
22df337
c447aa0
 
22df337
c447aa0
 
 
22df337
c447aa0
22df337
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
---
license: apache-2.0
language:
  - zh
  - en
base_model: Qwen/Qwen2.5-Coder-7B-Instruct
tags:
  - sql
  - text2sql
  - database
  - gaussdb
  - lora
  - fine-tuned
pipeline_tag: text-generation
library_name: transformers
datasets:
  - custom
model-index:
  - name: GaussDB-SQL-Expert-7B
    results:
      - task:
          type: text-generation
          name: Database SQL Expert
        metrics:
          - name: Text2SQL Accuracy
            type: accuracy
            value: 100
          - name: SQL Migration Accuracy
            type: accuracy
            value: 100
          - name: Error Diagnosis Accuracy
            type: accuracy
            value: 100
          - name: SQL Tuning Accuracy
            type: accuracy
            value: 90
          - name: Boundary Safety Accuracy
            type: accuracy
            value: 80
          - name: Overall Accuracy
            type: accuracy
            value: 94
---

# GaussDB SQL Expert 7B

**[中文版 README](README_zh.md)**

A domain-specific database assistant fine-tuned on Qwen2.5-Coder-7B-Instruct, specialized in SQL generation, optimization, cross-database migration, error diagnosis, and more.

## Model Overview

| Item | Details |
|------|---------|
| Base Model | [Qwen/Qwen2.5-Coder-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-7B-Instruct) |
| Parameters | 7.6B (Dense) |
| Fine-tuning | LoRA (rank=64, alpha=128, target=all linear layers) |
| Trainable Params | 161M (2.08% of total) |
| Training Data | 29,863 ShareGPT conversations + 1,571 validation |
| Hardware | 1x NVIDIA H100 80GB |
| Training Time | 3.5 hours |
| Framework | [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory) v0.9.4 |
| Precision | BF16 |

## Core Capabilities

- **Text2SQL**: Natural language to SQL with support for window functions, recursive CTEs, MERGE, subqueries, and more
- **SQL Tuning**: Index invalidation analysis, execution plan interpretation, parameter optimization advice
- **SQL Migration**: Oracle / MySQL / SQL Server → GaussDB syntax conversion (50+ difference points)
- **Error Diagnosis**: Deadlock, WAL bloat, connection exhaustion, OOM, and 20+ common production issues
- **SQL Explanation**: Logic breakdown and readability analysis of complex queries
- **Boundary Safety**: Dangerous operation interception, clarification requests, out-of-scope rejection

**Supports 9 major databases**: GaussDB, Oracle, MySQL, PostgreSQL, SQL Server, PolarDB, DM (Dameng), KingBase, Sybase

## Benchmark Results

Evaluated on 100 automated test cases (20 per category) using keyword matching:

| Category | Score | Notes |
|----------|-------|-------|
| Text2SQL | 20/20 (100%) | Window functions, CTE, MERGE, pagination all correct |
| SQL Tuning | 18/20 (90%) | Index invalidation, implicit conversion, parameter tuning |
| SQL Migration | 20/20 (100%) | Oracle/MySQL/SQL Server → GaussDB conversion |
| Error Diagnosis | 20/20 (100%) | Deadlock, WAL, OOM, connection exhaustion |
| Boundary Safety | 16/20 (80%) | Dangerous operation alerts, out-of-scope rejection |
| **Overall** | **94/100 (94%)** | |

## Quick Start

### Requirements

- Python >= 3.9
- PyTorch >= 2.0
- GPU with >= 16GB VRAM (recommended) or CPU (slower)
- ~15GB disk space for model weights

### Installation

```bash
# 1. Install dependencies
pip install torch transformers accelerate

# 2. (Optional) Install Flash Attention 2 for faster inference on NVIDIA GPUs
pip install flash-attn --no-build-isolation
```

### Download Model

The model will be downloaded automatically on first use via `from_pretrained()`. You can also download it manually:

```bash
# Option A: Using huggingface-cli
pip install huggingface_hub
huggingface-cli download lanfers/gaussdb-sql-expert-7b --local-dir ./gaussdb-sql-expert-7b

# Option B: Using git-lfs
git lfs install
git clone https://huggingface.co/lanfers/gaussdb-sql-expert-7b

# Option C: Using Python
python -c "
from huggingface_hub import snapshot_download
snapshot_download('lanfers/gaussdb-sql-expert-7b', local_dir='./gaussdb-sql-expert-7b')
"
```

> **China Mainland Users**: If download is slow, use a mirror:
> ```bash
> HF_ENDPOINT=https://hf-mirror.com huggingface-cli download lanfers/gaussdb-sql-expert-7b --local-dir ./gaussdb-sql-expert-7b
> ```

### Inference

```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_path = "lanfers/gaussdb-sql-expert-7b"
device = "cuda"  # or "mps" (Mac) / "cpu"

tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_path,
    torch_dtype=torch.float16,
    device_map=device,
    trust_remote_code=True,
)
model.eval()

messages = [
    {"role": "system", "content": "You are a professional database expert assistant, proficient in Oracle, MySQL, PostgreSQL, GaussDB, PolarDB, DM, KingBase, Sybase, SQL Server."},
    {"role": "user", "content": "Database: GaussDB 505\n\nFind the top 3 highest-paid employees in each department, showing department name, employee name, salary, and rank."},
]

text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(device)

with torch.no_grad():
    outputs = model.generate(
        **inputs,
        max_new_tokens=1024,
        temperature=0.7,
        do_sample=True,
        top_p=0.8,
        repetition_penalty=1.05,
    )

response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
print(response)
```

### Example Outputs

**Input**: Find the top 3 highest-paid employees in each department

**Output**:
```sql
SELECT dept_name, emp_name, salary, rk
FROM (
    SELECT d.dept_name, e.emp_name, e.salary,
        DENSE_RANK() OVER(PARTITION BY e.dept_id ORDER BY e.salary DESC) AS rk
    FROM employees e
    JOIN departments d ON e.dept_id = d.dept_id
) t
WHERE rk <= 3;
```

**Input**: Migrate Oracle to GaussDB: `SELECT NVL(name, 'unknown') FROM users WHERE ROWNUM <= 10`

**Output**:
```sql
SELECT COALESCE(name, 'unknown') FROM users LIMIT 10;
-- NVL → COALESCE, ROWNUM → LIMIT
```

## Training Details

### Hyperparameters

```yaml
# LoRA config
lora_rank: 64
lora_alpha: 128
lora_dropout: 0.05
lora_target: all  # q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj

# Training config
learning_rate: 2.0e-5
lr_scheduler_type: cosine
warmup_ratio: 0.1
num_train_epochs: 3
per_device_train_batch_size: 8
gradient_accumulation_steps: 4  # effective batch_size = 32
cutoff_len: 2048
optim: adamw_torch
bf16: true
gradient_checkpointing: true
```

### Training Loss

```
Total steps: 2,799 | Duration: 3h 29m

Step    Epoch   Train Loss   Eval Loss
  200   0.21      1.217       1.216
  600   0.64      1.038       1.104
 1000   1.07      1.035       1.076
 1400   1.50      1.062       1.058
 1800   1.93      1.062       1.045
 2200   2.36      0.966       1.044
 2600   2.79      0.959       1.042  ← best checkpoint
```

Final train_loss=1.039, eval_loss=1.042. Near-identical values indicate no overfitting.

### Training Data Distribution

| Category | Proportion | Description |
|----------|-----------|-------------|
| Text2SQL | ~30% | Natural language → SQL generation |
| SQL Tuning | ~20% | Slow query analysis, index optimization |
| SQL Migration | ~15% | Cross-database syntax conversion |
| Error Diagnosis | ~15% | Production incident troubleshooting |
| Operations | ~10% | Parameter tuning, backup & recovery |
| Boundary Safety | ~10% | Dangerous operation alerts, scope rejection |

## Limitations

- Boundary safety has room for improvement: may execute `DELETE` without `WHERE` or `DROP DATABASE` without warning
- Limited coverage of GaussDB 505 advanced features (e.g., column-store tables, distributed features)
- Text-only input; does not support images (e.g., execution plan screenshots)
- Recommended to add inference-side safety rules for production environments

## Citation

If this model is helpful, please cite:

```bibtex
@misc{gaussdb-sql-expert-7b,
  title={GaussDB SQL Expert 7B},
  author={lanfers},
  year={2026},
  publisher={HuggingFace},
  url={https://huggingface.co/lanfers/gaussdb-sql-expert-7b}
}
```

## License

Fine-tuned from [Qwen2.5-Coder-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-7B-Instruct) under the Apache 2.0 License.