LLMs之Code:SQLCoder的簡(jiǎn)介、安裝、使用方法之詳細(xì)攻略
目錄
SQLCoder的簡(jiǎn)介
1、結(jié)果
2、按問(wèn)題類(lèi)別的結(jié)果
SQLCoder的安裝
1、硬件要求
2、下載模型權(quán)重
3、使用SQLCoder
4、Colab中運(yùn)行SQLCoder
第一步,配置環(huán)境
第二步,測(cè)試
第三步,下載模型
第四步,設(shè)置問(wèn)題和提示并進(jìn)行標(biāo)記化
第五步,生成SQL
SQLCoder的使用方法
SQLCoder的簡(jiǎn)介
2023年8月,發(fā)布了SQLCoder,這是一個(gè)先進(jìn)的LLM,用于將自然語(yǔ)言問(wèn)題轉(zhuǎn)換為SQL查詢。SQLCoder在基礎(chǔ)的StarCoder模型上進(jìn)行了微調(diào)。SQLCoder是一個(gè)擁有150億參數(shù)的模型,在我們的sql-eval框架上,它在自然語(yǔ)言到SQL生成任務(wù)上勝過(guò)了gpt-3.5-turbo,并且在所有流行的開(kāi)源模型中表現(xiàn)顯著。它還明顯優(yōu)于大小超過(guò)10倍的text-davinci-003模型。
Defog在2個(gè)時(shí)期內(nèi)對(duì)10537個(gè)經(jīng)過(guò)人工篩選的問(wèn)題進(jìn)行了訓(xùn)練。這些問(wèn)題基于10個(gè)不同的模式。在訓(xùn)練數(shù)據(jù)中,沒(méi)有包括評(píng)估框架中的任何模式。
訓(xùn)練分為2個(gè)階段。第一階段是關(guān)于被分類(lèi)為“容易”或“中等”難度的問(wèn)題,第二階段是關(guān)于被分類(lèi)為“困難”或“超級(jí)困難”難度的問(wèn)題。
在easy+medium數(shù)據(jù)上的訓(xùn)練結(jié)果存儲(chǔ)在一個(gè)名為defog-easy的模型中。我們發(fā)現(xiàn)在hard+extra-hard數(shù)據(jù)上的額外訓(xùn)練導(dǎo)致性能增加了7個(gè)百分點(diǎn)。
官網(wǎng)在線測(cè)試:https://defog.ai/sqlcoder-demo/
GitHub官網(wǎng):GitHub - defog-ai/sqlcoder: SoTA LLM for converting natural language questions to SQL queries
1、結(jié)果
model |
perc_correct |
gpt-4 |
74.3 |
defog-sqlcoder |
64.6 |
gpt-3.5-turbo |
60.6 |
defog-easysql |
57.1 |
text-davinci-003 |
54.3 |
wizardcoder |
52.0 |
starcoder |
45.1 |
2、按問(wèn)題類(lèi)別的結(jié)果
我們將每個(gè)生成的問(wèn)題分類(lèi)為5個(gè)類(lèi)別之一。該表顯示了每個(gè)模型按類(lèi)別細(xì)分的正確回答問(wèn)題的百分比。
query_category |
gpt-4 |
defog-sqlcoder |
gpt-3.5-turbo |
defog-easy |
text-davinci-003 |
wizard-coder |
star-coder |
group_by |
82.9 |
77.1 |
71.4 |
62.9 |
62.9 |
68.6 |
54.3 |
order_by |
71.4 |
65.7 |
60.0 |
68.6 |
60.0 |
54.3 |
57.1 |
ratio |
62.9 |
57.1 |
48.6 |
40.0 |
37.1 |
22.9 |
17.1 |
table_join |
74.3 |
57.1 |
60.0 |
54.3 |
51.4 |
54.3 |
51.4 |
where |
80.0 |
65.7 |
62.9 |
60.0 |
60.0 |
60.0 |
45.7 |
SQLCoder的安裝
1、硬件要求
SQLCoder已在A100 40GB GPU上進(jìn)行了測(cè)試,使用bfloat16權(quán)重。您還可以在具有20GB或更多內(nèi)存的消費(fèi)者GPU上加載8位和4位量化版本的模型。例如RTX 4090、RTX 3090以及具有20GB或更多內(nèi)存的Apple M2 Pro、M2 Max或M2 Ultra芯片。
2、下載模型權(quán)重
地址:defog/sqlcoder · Hugging Face
3、使用SQLCoder
您可以通過(guò)transformers庫(kù)使用SQLCoder,方法是從Hugging Face存儲(chǔ)庫(kù)中下載我們的模型權(quán)重。我們已添加了有關(guān)在示例數(shù)據(jù)庫(kù)架構(gòu)上進(jìn)行推斷的示例代碼。
python inference.py -q "Question about the sample database goes here"
示例問(wèn)題:我們與紐約的客戶相比,從舊金山的客戶那里獲得更多收入嗎?為我提供每個(gè)城市的總收入以及兩者之間的差異。您還可以在我們的網(wǎng)站上使用演示,或在Colab中運(yùn)行SQLCoder。
4、Colab中運(yùn)行SQLCoder
地址:https://colab.research.google.com/drive/1z4rmOEiFkxkMiecAWeTUlPl0OmKgfEu7?usp=sharing#scrollTo=MKuocI44V-Bo
第一步,配置環(huán)境
!pip install torch transformers bitsandbytes accelerate
第二步,測(cè)試
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
torch.cuda.is_available()
第三步,下載模型
使用Colab Pro上的A100(或具有> 30GB VRAM的任何系統(tǒng))在bf16中加載它。如果不可用,請(qǐng)使用至少具有20GB?VRAM的GPU在8位中加載它,或者至少具有12GB VRAM在4位中加載它。在Colab上,它適用于V100,但在T4上崩潰。
首次下載模型然后將其加載到內(nèi)存中的步驟大約需要10分鐘。所以請(qǐng)耐心等待 :)
model_name = "defog/sqlcoder"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
trust_remote_code=True,
# torch_dtype=torch.bfloat16,
# load_in_8bit=True,
load_in_4bit=True,
device_map="auto",
use_cache=True,
)
第四步,設(shè)置問(wèn)題和提示并進(jìn)行標(biāo)記化
隨意更改以下問(wèn)題。如果您想要嘗試自己的數(shù)據(jù)庫(kù)架構(gòu),請(qǐng)?jiān)谔崾局芯庉嬆J健?/p>
question = "What product has the biggest fall in sales in 2022 compared to 2021? Give me the product name, the sales amount in both years, and the difference."
prompt = """### Instructions:
Your task is to convert a question into a SQL query, given a Postgres database schema.
Adhere to these rules:
- **Deliberately go through the question and database schema word by word** to appropriately answer the question
- **Use Table Aliases** to prevent ambiguity. For example, `SELECT table1.col1, table2.col1 FROM table1 JOIN table2 ON table1.id = table2.id`.
- When creating a ratio, always cast the numerator as float
### Input:
Generate a SQL query that answers the question `{question}`.
This query will run on a database whose schema is represented in this string:
CREATE TABLE products (
product_id INTEGER PRIMARY KEY, -- Unique ID for each product
name VARCHAR(50), -- Name of the product
price DECIMAL(10,2), -- Price of each unit of the product
quantity INTEGER -- Current quantity in stock
);
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer
name VARCHAR(50), -- Name of the customer
address VARCHAR(100) -- Mailing address of the customer
);
CREATE TABLE salespeople (
salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson
name VARCHAR(50), -- Name of the salesperson
region VARCHAR(50) -- Geographic sales region
);
CREATE TABLE sales (
sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale
product_id INTEGER, -- ID of product sold
customer_id INTEGER, -- ID of customer who made purchase
salesperson_id INTEGER, -- ID of salesperson who made the sale
sale_date DATE, -- Date the sale occurred
quantity INTEGER -- Quantity of product sold
);
CREATE TABLE product_suppliers (
supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier
product_id INTEGER, -- Product ID supplied
supply_price DECIMAL(10,2) -- Unit price charged by supplier
);
-- sales.product_id can be joined with products.product_id
-- sales.customer_id can be joined with customers.customer_id
-- sales.salesperson_id can be joined with salespeople.salesperson_id
-- product_suppliers.product_id can be joined with products.product_id
### Response:
Based on your instructions, here is the SQL query I have generated to answer the question `{question}`:
```sql
""".format(question=question)
eos_token_id = tokenizer.convert_tokens_to_ids(["```"])[0]
第五步,生成SQL
在具有4位量化的V100上可能非常緩慢。每個(gè)查詢可能需要大約1-2分鐘。在單個(gè)A100 40GB上,需要大約10-20秒。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-681018.html
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
generated_ids = model.generate(
**inputs,
num_return_sequences=1,
eos_token_id=eos_token_id,
pad_token_id=eos_token_id,
max_new_tokens=400,
do_sample=False,
num_beams=5
)
outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
torch.cuda.empty_cache()
torch.cuda.synchronize()
# 清空緩存,以便在內(nèi)存崩潰時(shí)可以生成更多結(jié)果
# 在Colab上特別重要 - 內(nèi)存管理要簡(jiǎn)單得多
# 在運(yùn)行推斷服務(wù)時(shí)
# 嗯!生成的SQL在這里:
print(outputs[0].split("```sql")[-1].split("```")[0].split(";")[0].strip() + ";")
SQLCoder的使用方法
更新中……文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-681018.html
到了這里,關(guān)于LLMs之Code:SQLCoder的簡(jiǎn)介、安裝、使用方法之詳細(xì)攻略的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!