🚀 NSQL-Llama-2-7B
NSQL 是專門為 SQL 生成任務設計的自迴歸開源大型基礎模型(FMs)家族。本倉庫引入了 NSQL 家族的新成員 NSQL-Llama-2-7B,它基於 Meta 原始的 Llama-2 7B 模型,先在通用 SQL 查詢數據集上進行預訓練,然後在由文本到 SQL 對組成的數據集上進行微調。
🚀 快速開始
模型推理參數
推理時的參數設置如下:
inference:
parameters:
do_sample: false
max_length: 200
使用示例
以下是一些使用該模型進行文本到 SQL 生成的示例:
基礎用法
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("NumbersStation/nsql-llama-2-7B")
model = AutoModelForCausalLM.from_pretrained("NumbersStation/nsql-llama-2-7B", torch_dtype=torch.bfloat16)
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))
其他示例
以下是另外兩個不同場景的使用示例:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("NumbersStation/nsql-llama-2-7B")
model = AutoModelForCausalLM.from_pretrained("NumbersStation/nsql-llama-2-7B", torch_dtype=torch.bfloat16)
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))
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("NumbersStation/nsql-llama-2-7B")
model = AutoModelForCausalLM.from_pretrained("NumbersStation/nsql-llama-2-7B", torch_dtype=torch.bfloat16)
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 生成任務設計,能根據給定的表結構和自然語言提示生成 SQL 查詢。
- 基於強大的 Llama-2 7B 模型,經過預訓練和微調,在 SQL 生成任務上有較好的表現。
📦 訓練相關信息
訓練數據
屬性 |
詳情 |
通用 SQL 查詢數據 |
來自 The Stack 的 SQL 子集,包含 100 萬個訓練樣本。 |
標註的文本到 SQL 對 |
來自網絡上 20 多個公共來源的標準數據集。我們預留了 Spider 和 GeoQuery 數據集用於評估。 |
評估數據
我們在兩個文本到 SQL 基準測試上評估模型:Spider 和 GeoQuery。
訓練過程
NSQL 使用交叉熵損失進行訓練,以最大化序列輸入的似然性。在對文本到 SQL 對進行微調時,我們僅計算對中 SQL 部分的損失。模型使用 80GB A100 GPU 進行訓練,利用數據並行和模型並行技術。預訓練進行了 3 個 epoch,微調進行了 10 個 epoch。
📄 許可證
本模型使用 Llama 2 許可證。
⚠️ 重要提示
本模型專為文本到 SQL 生成任務設計,在給定表結構和自然語言提示的情況下工作效果最佳,尤其適合輸出 SELECT
查詢。