🚀 Think2SQL-7B 推理模型
Think2SQL-7B 是一个用于 Text2SQL 任务的推理模型,能基于给定问题、证据和数据库模式生成 SQL 脚本。
🚀 快速开始
该模型在搭配特定的系统提示和用户提示时能达到最佳性能。此模型设计用于接收三个输入:问题、证据和数据库模式。
从 transformers >= 4.43.0
版本开始,你可以使用 Transformers 的 pipeline
抽象方法,或者借助 generate()
函数的 Auto
类来进行对话式推理。
请确保通过 pip install --upgrade transformers
命令更新你的 transformers 库。
import transformers
import torch
model_id = "simone-papicchio/Think2SQL-7B"
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
model_kwargs={"torch_dtype": torch.bfloat16},
device_map="auto",
)
system_message = (
"You are a helpful AI Assistant that provides well-reasoned and detailed responses. "
"You first think about the reasoning process as an internal monologue and then provide the user with the answer. "
"Respond in the following format: <think>\n...\n</think>\n<answer>\n...\n</answer>"
).strip()
user_message = (
"Answer the following question with the SQL code. Use the piece of evidence and base your answer on the database schema. "
"Given the question, the evidence and the database schema, return in the <answer> tags only the SQL script that addresses the question.\n"
"Question:\n{question}\n\n"
"Evidence:\n{evidence}\n\n"
"Database Schema:\n{schema}\n\n"
"Return only the SQL script enclosed in <answer> tags."
).strip()
messages = [
{"role": "system", "content": system_message},
{"role": "user", "content": user_message},
]
outputs = pipeline(
messages,
max_new_tokens=30_000,
temperature=0.7,
top_p=0.95
)
print(outputs[0]["generated_text"][-1])
✨ 主要特性
此模型是在论文 Think2SQL: Reinforce LLM Reasoning Capabilities for Text2SQL 中介绍的用于 Text2SQL 任务的推理模型。
该模型是 Qwen/Qwen2.5-Coder-7B-Instruct 在 simone-papicchio/bird 数据集上的微调版本,使用 TRL 进行训练。
🔧 技术细节
训练过程

该模型使用 GRPO 方法进行训练,此方法在论文 DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models 中被提出。
框架版本
- TRL: 0.17.0.dev0
- Transformers: 4.51.0
- Pytorch: 2.5.1
- Datasets: 3.5.0
- Tokenizers: 0.21.1
📚 详细文档
模型信息
属性 |
详情 |
基础模型 |
Qwen/Qwen2.5-Coder-7B-Instruct |
训练数据集 |
simone-papicchio/bird |
库名称 |
transformers |
标签 |
generated_from_trainer、open-r1、Text2SQL、Reasoning |
许可证 |
apache-2.0 |
引用信息
@misc{papicchio2025think2sqlreinforcellmreasoning,
title={Think2SQL: Reinforce LLM Reasoning Capabilities for Text2SQL},
author={Simone Papicchio and Simone Rossi and Luca Cagliero and Paolo Papotti},
year={2025},
eprint={2504.15077},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2504.15077},
}
@inproceedings{papicchio2023qatch,
title={QATCH: benchmarking SQL-centric tasks with table representation learning models on your data},
author={Papicchio, Simone and Papotti, Paolo and Cagliero, Luca},
booktitle={Proceedings of the 37th International Conference on Neural Information Processing Systems},
pages={30898--30917},
year={2023}
}
📄 许可证
本项目采用 Apache-2.0 许可证。