Downloads · 30 days
0
Moyonx/Qwen2.5-SQL-LoRA
Qwen2.5-SQL-LoRA is a machine learning model from Moyonx. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
Downloads · 30 days
0
Access
Public
Updated Dec 6, 2025
Repo size
340 MB
Likes
1
Public
Click a slice to open those files.
.safetensors329 MB · 95%
From the Hugging Face model README
Qwen2.5-SQL-LoRA 是一个基于 Qwen2.5-7B-Instruct 微调的垂直领域 Text-to-SQL(文本转SQL) 模型。它专为解决通用大模型在数据库查询场景中容易产生幻觉、语法错误等问题而设计。
该模型采用了前沿的 DoRA (Weight-Decomposed Low-Rank Adaptation) 与 4-bit QLoRA 技术进行训练,在保证极低显存占用的同时,显著提升了 SQL 语法的严谨性以及对复杂逻辑(如分组、排序、嵌套查询、日期处理)的推理能力。
| 类别 | 详情 |
|---|---|
| 基座模型 | Qwen/Qwen2.5-7B-Instruct |
| 微调方法 | DoRA (Rank=32, Alpha=64) + 4-bit QLoRA |
| 训练数据 | 2000+ 条高质量 Schema-Aware 数据 (Spider/WikiSQL 清洗版) |
| 训练环境 | LLaMA-Factory (单卡 RTX 3090 24GB) |
| Loss | 0.0503 (极佳收敛) |
pip install transformers peft torch
你可以直接复制以下代码来测试模型:
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import torch
# ================= 配置区 =================
# 1. 加载基座模型 (Qwen2.5-7B-Instruct)
base_model_id = "Qwen/Qwen2.5-7B-Instruct"
model = AutoModelForCausalLM.from_pretrained(
base_model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
# 2. 加载 LoRA 适配器 (本模型)
lora_model_id = "Moyonx/Qwen2.5-SQL-LoRA"
model = PeftModel.from_pretrained(model, lora_model_id)
# ================= 推理函数 =================
def generate_sql(schema, question):
# 构造 Prompt (与训练格式严格对齐)
prompt = (
f"将以下自然语言转换为SQL查询语句。\n"
f"上下文信息:{schema}\n"
f"用户提问:{question}\n"
f"SQL语句:"
)
messages = [
{"role": "system", "content": "You are a professional database engineer."},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
# 生成参数:温度设为 0.1 以保证代码生成的严谨性
generated_ids = model.generate(
**model_inputs,
max_new_tokens=512,
temperature=0.1,
top_p=0.9
)
return tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
# ================= 运行测试 =================
# 定义表结构
schema_example = 'Database Schema: {"sales": ["id", "amount", "date"], "products": ["id", "name", "category"]}'
# 提问
query_example = "计算2023年'电子产品'类别的总销售额。"
# 输出结果
print(generate_sql(schema_example, query_example))
我们在留出的验证集上进行了逻辑匹配和执行准确率的评估:
| 指标 | 分数 | 说明 |
|---|---|---|
| Training Loss | 0.0503 | 模型已深度掌握 SQL 结构模式 |
| Syntax Error | 0.00% | 验证集中未出现语法错误 |
| Hallucination | 0.00% | 严格遵循提供的表结构,无幻觉 |
如果您使用了本模型,请致谢 Qwen 团队与 LLaMA-Factory 框架。