Text Generation
Transformers
Safetensors
Chinese
English
qwen2
sql
text2sql
database
gaussdb
lora
fine-tuned
conversational
Eval Results (legacy)
text-generation-inference
Instructions to use lanfers/gaussdb-sql-expert-7b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use lanfers/gaussdb-sql-expert-7b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="lanfers/gaussdb-sql-expert-7b") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("lanfers/gaussdb-sql-expert-7b") model = AutoModelForCausalLM.from_pretrained("lanfers/gaussdb-sql-expert-7b") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use lanfers/gaussdb-sql-expert-7b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "lanfers/gaussdb-sql-expert-7b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "lanfers/gaussdb-sql-expert-7b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/lanfers/gaussdb-sql-expert-7b
- SGLang
How to use lanfers/gaussdb-sql-expert-7b with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "lanfers/gaussdb-sql-expert-7b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "lanfers/gaussdb-sql-expert-7b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "lanfers/gaussdb-sql-expert-7b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "lanfers/gaussdb-sql-expert-7b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use lanfers/gaussdb-sql-expert-7b with Docker Model Runner:
docker model run hf.co/lanfers/gaussdb-sql-expert-7b
Upload README_zh.md with huggingface_hub
Browse files- README_zh.md +237 -0
README_zh.md
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# GaussDB SQL Expert 7B
|
| 2 |
+
|
| 3 |
+
**[English README](README.md)**
|
| 4 |
+
|
| 5 |
+
基于 Qwen2.5-Coder-7B-Instruct 微调的企业级数据库智能助手,专精 SQL 生成、调优、迁移、诊断等数据库领域任务。
|
| 6 |
+
|
| 7 |
+
## 模型概述
|
| 8 |
+
|
| 9 |
+
| 项目 | 详情 |
|
| 10 |
+
|------|------|
|
| 11 |
+
| 基座模型 | [Qwen/Qwen2.5-Coder-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-7B-Instruct) |
|
| 12 |
+
| 参数量 | 7.6B (Dense) |
|
| 13 |
+
| 微调方法 | LoRA (rank=64, alpha=128, target=all linear layers) |
|
| 14 |
+
| 可训参数 | 161M (2.08%) |
|
| 15 |
+
| 训练数据 | 29,863 条 ShareGPT 多轮对话 + 1,571 条验证 |
|
| 16 |
+
| 训练硬件 | 1x NVIDIA H100 80GB |
|
| 17 |
+
| 训练耗时 | 3.5 小时 |
|
| 18 |
+
| 训练框架 | [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory) v0.9.4 |
|
| 19 |
+
| 精度 | BF16 |
|
| 20 |
+
|
| 21 |
+
## 核心能力
|
| 22 |
+
|
| 23 |
+
- **Text2SQL**: 自然语言转 SQL,支持窗口函数、递归 CTE、MERGE、子查询等复杂语法
|
| 24 |
+
- **SQL 调优**: 索引失效分析、执行计划解读、参数配置优化建议
|
| 25 |
+
- **SQL 迁移**: Oracle / MySQL / SQL Server → GaussDB 语法自动转换 (50+ 差异点)
|
| 26 |
+
- **错误诊断**: 死锁、WAL 膨胀、连接耗尽、OOM 等 20+ 常见故障场景
|
| 27 |
+
- **SQL 解释**: 复杂查询的逻辑拆解与可读性分析
|
| 28 |
+
- **边界安全**: 危险操作拦截、信息不足追问、超范围拒绝
|
| 29 |
+
|
| 30 |
+
**支持 9 种主流数据库**: GaussDB, Oracle, MySQL, PostgreSQL, SQL Server, PolarDB, 达梦(DM), 金仓(KingBase), Sybase
|
| 31 |
+
|
| 32 |
+
## 评测结果
|
| 33 |
+
|
| 34 |
+
使用 100 道自动化评测题(每类 20 道),关键词匹配评分:
|
| 35 |
+
|
| 36 |
+
| 维度 | 得分 | 说明 |
|
| 37 |
+
|------|------|------|
|
| 38 |
+
| Text2SQL | 20/20 (100%) | 窗口函数、CTE、MERGE、分页等全部正确 |
|
| 39 |
+
| SQL 调优 | 18/20 (90%) | 索引失效、隐式转换、参数调优等 |
|
| 40 |
+
| SQL 迁移 | 20/20 (100%) | Oracle/MySQL/SQL Server → GaussDB 转换 |
|
| 41 |
+
| 错误诊断 | 20/20 (100%) | 死锁、WAL、OOM、连接耗尽等 |
|
| 42 |
+
| 边界安全 | 16/20 (80%) | 危险操作告警、超范围拒绝 |
|
| 43 |
+
| **综合** | **94/100 (94%)** | |
|
| 44 |
+
|
| 45 |
+
## 快速开始
|
| 46 |
+
|
| 47 |
+
### 环境要求
|
| 48 |
+
|
| 49 |
+
- Python >= 3.9
|
| 50 |
+
- PyTorch >= 2.0
|
| 51 |
+
- GPU 显存 >= 16GB(推荐)或 CPU(较慢)
|
| 52 |
+
- 磁盘空间 ~15GB(存放模型权重)
|
| 53 |
+
|
| 54 |
+
### 安装依赖
|
| 55 |
+
|
| 56 |
+
```bash
|
| 57 |
+
# 1. 安装基础依赖
|
| 58 |
+
pip install torch transformers accelerate
|
| 59 |
+
|
| 60 |
+
# 2.(可选)安装 Flash Attention 2,在 NVIDIA GPU 上加速推理
|
| 61 |
+
pip install flash-attn --no-build-isolation
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
### 下载模型
|
| 65 |
+
|
| 66 |
+
首次使用 `from_pretrained()` 时会自动下载模型,也可以手动提前下载:
|
| 67 |
+
|
| 68 |
+
```bash
|
| 69 |
+
# 方式一:huggingface-cli(推荐)
|
| 70 |
+
pip install huggingface_hub
|
| 71 |
+
huggingface-cli download lanfers/gaussdb-sql-expert-7b --local-dir ./gaussdb-sql-expert-7b
|
| 72 |
+
|
| 73 |
+
# 方式二:git-lfs
|
| 74 |
+
git lfs install
|
| 75 |
+
git clone https://huggingface.co/lanfers/gaussdb-sql-expert-7b
|
| 76 |
+
|
| 77 |
+
# 方式三:Python 脚本
|
| 78 |
+
python -c "
|
| 79 |
+
from huggingface_hub import snapshot_download
|
| 80 |
+
snapshot_download('lanfers/gaussdb-sql-expert-7b', local_dir='./gaussdb-sql-expert-7b')
|
| 81 |
+
"
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
> **国内用户加速下载**:如果 HuggingFace 下载较慢,可使用镜像站:
|
| 85 |
+
> ```bash
|
| 86 |
+
> HF_ENDPOINT=https://hf-mirror.com huggingface-cli download lanfers/gaussdb-sql-expert-7b --local-dir ./gaussdb-sql-expert-7b
|
| 87 |
+
> ```
|
| 88 |
+
|
| 89 |
+
### 使用本地模型推理
|
| 90 |
+
|
| 91 |
+
如果已手动下载到本地,将代码中的 `model_path` 改为本地路径即可:
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
model_path = "./gaussdb-sql-expert-7b" # 本地路径
|
| 95 |
+
# model_path = "lanfers/gaussdb-sql-expert-7b" # 或直接从 HuggingFace 加载
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
### Python 推理
|
| 99 |
+
|
| 100 |
+
```python
|
| 101 |
+
import torch
|
| 102 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 103 |
+
|
| 104 |
+
model_path = "lanfers/gaussdb-sql-expert-7b"
|
| 105 |
+
device = "cuda" # 或 "mps" (Mac) / "cpu"
|
| 106 |
+
|
| 107 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 108 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 109 |
+
model_path,
|
| 110 |
+
torch_dtype=torch.float16,
|
| 111 |
+
device_map=device,
|
| 112 |
+
trust_remote_code=True,
|
| 113 |
+
)
|
| 114 |
+
model.eval()
|
| 115 |
+
|
| 116 |
+
messages = [
|
| 117 |
+
{"role": "system", "content": "你是一个专业的数据库专家助手,精通 Oracle, MySQL, PostgreSQL, GaussDB, PolarDB, 达梦(DM), 金仓(KingBase), Sybase, SQL Server。"},
|
| 118 |
+
{"role": "user", "content": "数据库类型: GaussDB 505 集中式\n\n查询每个部门工资最高的前3名员工,显示部门名、员工名、工资和排名。"},
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 122 |
+
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 123 |
+
|
| 124 |
+
with torch.no_grad():
|
| 125 |
+
outputs = model.generate(
|
| 126 |
+
**inputs,
|
| 127 |
+
max_new_tokens=1024,
|
| 128 |
+
temperature=0.7,
|
| 129 |
+
do_sample=True,
|
| 130 |
+
top_p=0.8,
|
| 131 |
+
repetition_penalty=1.05,
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 135 |
+
print(response)
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
### 示例输出
|
| 139 |
+
|
| 140 |
+
**输入**: 查询每个部门工资最高的前3名员工
|
| 141 |
+
|
| 142 |
+
**输出**:
|
| 143 |
+
```sql
|
| 144 |
+
SELECT dept_name, emp_name, salary, rk
|
| 145 |
+
FROM (
|
| 146 |
+
SELECT d.dept_name, e.emp_name, e.salary,
|
| 147 |
+
DENSE_RANK() OVER(PARTITION BY e.dept_id ORDER BY e.salary DESC) AS rk
|
| 148 |
+
FROM employees e
|
| 149 |
+
JOIN departments d ON e.dept_id = d.dept_id
|
| 150 |
+
) t
|
| 151 |
+
WHERE rk <= 3;
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
**输入**: Oracle → GaussDB 迁移: `SELECT NVL(name, '未知') FROM users WHERE ROWNUM <= 10`
|
| 155 |
+
|
| 156 |
+
**输出**:
|
| 157 |
+
```sql
|
| 158 |
+
SELECT COALESCE(name, '未知') FROM users LIMIT 10;
|
| 159 |
+
-- NVL → COALESCE, ROWNUM → LIMIT
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
+
## 训练详情
|
| 163 |
+
|
| 164 |
+
### 训练超参数
|
| 165 |
+
|
| 166 |
+
```yaml
|
| 167 |
+
# LoRA 配置
|
| 168 |
+
lora_rank: 64
|
| 169 |
+
lora_alpha: 128
|
| 170 |
+
lora_dropout: 0.05
|
| 171 |
+
lora_target: all # q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
|
| 172 |
+
|
| 173 |
+
# 训练参数
|
| 174 |
+
learning_rate: 2.0e-5
|
| 175 |
+
lr_scheduler_type: cosine
|
| 176 |
+
warmup_ratio: 0.1
|
| 177 |
+
num_train_epochs: 3
|
| 178 |
+
per_device_train_batch_size: 8
|
| 179 |
+
gradient_accumulation_steps: 4 # 等效 batch_size = 32
|
| 180 |
+
cutoff_len: 2048
|
| 181 |
+
optim: adamw_torch
|
| 182 |
+
bf16: true
|
| 183 |
+
gradient_checkpointing: true
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
### 训练 Loss 曲线
|
| 187 |
+
|
| 188 |
+
```
|
| 189 |
+
训练过程:2,799 步,3 小时 29 分钟
|
| 190 |
+
|
| 191 |
+
Step Epoch Train Loss Eval Loss
|
| 192 |
+
200 0.21 1.217 1.216
|
| 193 |
+
600 0.64 1.038 1.104
|
| 194 |
+
1000 1.07 1.035 1.076
|
| 195 |
+
1400 1.50 1.062 1.058
|
| 196 |
+
1800 1.93 1.062 1.045
|
| 197 |
+
2200 2.36 0.966 1.044
|
| 198 |
+
2600 2.79 0.959 1.042 ← 最优检查点
|
| 199 |
+
```
|
| 200 |
+
|
| 201 |
+
最终 train_loss=1.039, eval_loss=1.042,两者接近,无过拟合。
|
| 202 |
+
|
| 203 |
+
### 训练数据分布
|
| 204 |
+
|
| 205 |
+
| 场景 | 占比 | 说明 |
|
| 206 |
+
|------|------|------|
|
| 207 |
+
| Text2SQL | ~30% | 自然语言 → SQL 生成 |
|
| 208 |
+
| SQL 调优 | ~20% | 慢查询分析、索引优化 |
|
| 209 |
+
| SQL 迁移 | ~15% | 跨数据库语法转换 |
|
| 210 |
+
| 错误诊断 | ~15% | 生产故障排查 |
|
| 211 |
+
| 运维知识 | ~10% | 参数调优、备份恢复 |
|
| 212 |
+
| 边界安全 | ~10% | 危险操作告警、超范围拒绝 |
|
| 213 |
+
|
| 214 |
+
## 局限性
|
| 215 |
+
|
| 216 |
+
- 边界安全能力还有提升空间:对 DELETE 全表、DROP DATABASE 等操作可能直接执行而不告警
|
| 217 |
+
- 对 GaussDB 505 特有的高级功能(如列存表、分布式特性)覆盖有限
|
| 218 |
+
- 仅支持文本输入,不支持图片(如执行计划截图)
|
| 219 |
+
- 建议在生产环境中增加推理侧安全规则兜底
|
| 220 |
+
|
| 221 |
+
## 引用
|
| 222 |
+
|
| 223 |
+
如果本模型对你有帮助,欢迎引用:
|
| 224 |
+
|
| 225 |
+
```bibtex
|
| 226 |
+
@misc{gaussdb-sql-expert-7b,
|
| 227 |
+
title={GaussDB SQL Expert 7B},
|
| 228 |
+
author={lanfers},
|
| 229 |
+
year={2026},
|
| 230 |
+
publisher={HuggingFace},
|
| 231 |
+
url={https://huggingface.co/lanfers/gaussdb-sql-expert-7b}
|
| 232 |
+
}
|
| 233 |
+
```
|
| 234 |
+
|
| 235 |
+
## 许可证
|
| 236 |
+
|
| 237 |
+
本模型基于 [Qwen2.5-Coder-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-7B-Instruct) 微调,遵循 Apache 2.0 许可证。
|