🚀 NSQL-Llama-2-7B
NSQLは、SQL生成タスクに特化して設計された自己回帰型のオープンソース大規模基礎モデル(FM)のファミリーです。このリポジトリでは、NSQLの新しいメンバーであるNSQL-Llama-2-7Bを紹介します。このモデルはMetaのオリジナルのLlama-2 7Bモデルに基づいており、一般的なSQLクエリのデータセットで事前学習され、その後、テキストからSQLへのペアで構成されるデータセットで微調整されています。
🚀 クイックスタート
このモデルは、与えられたテーブルスキーマと自然言語のプロンプトからテキストをSQLに変換するタスクに最適化されています。以下の使用例を参考に、モデルを使い始めてください。
✨ 主な機能
- 与えられたテーブルスキーマと自然言語のプロンプトからSQLを生成します。
- 一般的なSQLクエリのデータセットで事前学習され、テキストからSQLへのペアで微調整されています。
📦 インストール
このモデルを使用するには、transformers
ライブラリが必要です。以下のコマンドでインストールできます。
pip install transformers torch
💻 使用例
基本的な使用法
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 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クエリは、The StackからのSQLサブセットで、100万件の訓練サンプルを含んでいます。ラベル付きのテキストからSQLへのペアは、ウェブ上の20以上の公開ソースからの標準データセットに由来します。評価には、SpiderとGeoQueryデータセットを使用します。
評価データ
モデルは、SpiderとGeoQueryの2つのテキストからSQLへのベンチマークで評価されます。
訓練手順
NSQLは、クロスエントロピー損失を使用して、逐次入力の尤度を最大化するように訓練されます。テキストからSQLへのペアでの微調整では、ペアのSQL部分に対してのみ損失を計算します。モデルは80GBのA100を使用して、データ並列とモデル並列を活用して訓練されます。事前学習は3エポック、微調整は10エポック行われます。
意図された使用法と制限
このモデルは、与えられたテーブルスキーマと自然言語のプロンプトからテキストをSQLに変換するタスクに設計されています。以下に定義されたプロンプト形式で使用すると最適で、SELECT
クエリを出力します。
その他の情報
詳細情報(例えば、ローカルデータベースでの実行)については、このリポジトリの例を参照してください。
📄 ライセンス
このモデルは、Llama 2のライセンスの下で提供されています。
🔧 技術詳細
推論パラメータ
プロパティ |
詳細 |
do_sample |
false |
max_length |
200 |
ウィジェットの使用例
例のタイトル |
テキスト |
Number stadiums |
"CREATE TABLE stadium (\n stadium_id number,\n location text,\n name text,\n capacity number,\n)\n\n-- Using valid SQLite, answer the following questions for the tables provided above.\n\n-- how many stadiums in total?\n\nSELECT" |
Open work orders |
"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, )\n\n-- Using valid SQLite, answer the following questions for the tables provided above.\n\n-- how many work orders are open?\n\nSELECT" |
Stadium capacity |
"CREATE TABLE stadium ( stadium_id number, location text, name text, capacity number, highest number, lowest number, average number )\n\nCREATE TABLE singer ( singer_id number, name text, country text, song_name text, song_release_year text, age number, is_male others )\n\nCREATE TABLE concert ( concert_id number, concert_name text, theme text, stadium_id text, year text )\n\nCREATE TABLE singer_in_concert ( concert_id number, singer_id text )\n\n-- Using valid SQLite, answer the following questions for the tables provided above.\n\n-- What is the maximum, the average, and the minimum capacity of stadiums ?\n\nSELECT" |