🚀 NSQL (NSQL-6B)
NSQL是專門為SQL生成任務設計的自迴歸開源大型基礎模型(FMs)系列。本模型能根據給定的表結構和自然語言提示生成SQL查詢,為SQL相關任務提供了高效的解決方案。
🚀 快速開始
模型推理參數
屬性 |
詳情 |
推理參數 |
do_sample: false, max_length: 200 |
使用示例
以下是一些使用NSQL模型進行SQL生成的示例:
示例1:計算體育場總數
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("NumbersStation/nsql-6B")
model = AutoModelForCausalLM.from_pretrained("NumbersStation/nsql-6B")
text = """CREATE TABLE stadium (
stadium_id number,
location text,
name text,
capacity number,
highest number,
lowest number,
average number
)
CREATE TABLE singer (
singer_id number,
name text,
country text,
song_name text,
song_release_year text,
age number,
is_male others
)
CREATE TABLE concert (
concert_id number,
concert_name text,
theme text,
stadium_id text,
year text
)
CREATE TABLE singer_in_concert (
concert_id number,
singer_id text
)
-- Using valid SQLite, answer the following questions for the tables provided above.
-- What is the maximum, the average, and the minimum capacity of stadiums ?
SELECT"""
input_ids = tokenizer(text, return_tensors="pt").input_ids
generated_ids = model.generate(input_ids, max_length=500)
print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
示例2:計算體育場總數(簡化表結構)
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("NumbersStation/nsql-6B")
model = AutoModelForCausalLM.from_pretrained("NumbersStation/nsql-6B")
text = """CREATE TABLE stadium (
stadium_id number,
location text,
name text,
capacity number,
)
-- Using valid SQLite, answer the following questions for the tables provided above.
-- how many stadiums in total?
SELECT"""
input_ids = tokenizer(text, return_tensors="pt").input_ids
generated_ids = model.generate(input_ids, max_length=500)
print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
示例3:計算未完成的工作訂單數量
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("NumbersStation/nsql-6B")
model = AutoModelForCausalLM.from_pretrained("NumbersStation/nsql-6B")
text = """CREATE TABLE work_orders (
ID NUMBER,
CREATED_AT TEXT,
COST FLOAT,
INVOICE_AMOUNT FLOAT,
IS_DUE BOOLEAN,
IS_OPEN BOOLEAN,
IS_OVERDUE BOOLEAN,
COUNTRY_NAME TEXT,
)
-- Using valid SQLite, answer the following questions for the tables provided above.
-- how many work orders are open?
SELECT"""
input_ids = tokenizer(text, return_tensors="pt").input_ids
generated_ids = model.generate(input_ids, max_length=500)
print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
更多信息(例如,在本地數據庫上運行),請參考此倉庫中的示例。
✨ 主要特性
- 專為SQL生成設計:NSQL是專門為SQL生成任務打造的自迴歸開源大型基礎模型,能夠根據自然語言提示準確生成SQL查詢。
- 基於預訓練和微調:該模型基於Salesforce的CodeGen - Multi 6B進行預訓練,並在通用SQL查詢數據集上進一步預訓練,然後在文本到SQL對的數據集上進行微調。
📦 安裝指南
文檔未提及安裝步驟,故跳過此章節。
📚 詳細文檔
模型描述
NSQL是專門為SQL生成任務設計的自迴歸開源大型基礎模型(FMs)系列。本倉庫中的檢查點基於Salesforce的CodeGen - Multi 6B,並在通用SQL查詢數據集上進一步預訓練,然後在由文本到SQL對組成的數據集上進行微調。
訓練數據
- 通用SQL查詢:來自The Stack的SQL子集,包含100萬個訓練樣本。
- 文本到SQL對:來自網絡上20多個公共來源的標準數據集。我們預留了Spider和GeoQuery數據集用於評估。
評估數據
我們在兩個文本到SQL基準測試上評估模型:Spider和GeoQuery。
訓練過程
NSQL使用交叉熵損失進行訓練,以最大化序列輸入的似然性。對於文本到SQL對的微調,我們僅計算對中SQL部分的損失。該系列模型使用80GB A100 GPU進行訓練,利用數據和模型並行性。我們進行了3個週期的預訓練和10個週期的微調。
預期用途和限制
該模型專為根據給定的表結構和自然語言提示進行文本到SQL生成任務而設計。該模型在以下定義的提示格式下效果最佳,並輸出SELECT
查詢。
🔧 技術細節
NSQL模型的訓練基於交叉熵損失,通過在通用SQL查詢數據集上預訓練和在文本到SQL對數據集上微調,不斷優化模型的性能。在訓練過程中,利用了數據和模型並行性,使用80GB A100 GPU進行高效訓練。預訓練和微調的週期設置分別為3個週期和10個週期,以確保模型能夠學習到足夠的知識來完成SQL生成任務。
📄 許可證
本項目採用BSD 3 - 條款許可證(BSD-3-Clause)。