🚀 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 許可證。