LLMs之Code:SQLCoder的简介、安装、使用方法之详细攻略

LLMs之Code:SQLCoder的简介、安装、使用方法之详细攻略

目录

SQLCoder的简介

1、结果

2、按问题类别的结果

SQLCoder的安装

1、硬件要求

2、下载模型权重

3、使用SQLCoder

4、Colab中运行SQLCoder

第一步,配置环境

第二步,测试

第三步,下载模型

第四步,设置问题和提示并进行标记化

第五步,生成SQL

SQLCoder的使用方法


SQLCoder的简介

2023年8月,发布了SQLCoder,这是一个先进的LLM,用于将自然语言问题转换为SQL查询。SQLCoder在基础的StarCoder模型上进行了微调。SQLCoder是一个拥有150亿参数的模型,在我们的sql-eval框架上,它在自然语言到SQL生成任务上胜过了gpt-3.5-turbo,并且在所有流行的开源模型中表现显著。它还明显优于大小超过10倍的text-davinci-003模型。

Defog在2个时期内对10537个经过人工筛选的问题进行了训练。这些问题基于10个不同的模式。在训练数据中,没有包括评估框架中的任何模式。

训练分为2个阶段。第一阶段是关于被分类为“容易”或“中等”难度的问题,第二阶段是关于被分类为“困难”或“超级困难”难度的问题。

在easy+medium数据上的训练结果存储在一个名为defog-easy的模型中。我们发现在hard+extra-hard数据上的额外训练导致性能增加了7个百分点。

官网在线测试:https://defog.ai/sqlcoder-demo/

GitHub官网:GitHub - defog-ai/sqlcoder: SoTA LLM for converting natural language questions to SQL queries

1、结果

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、按问题类别的结果

我们将每个生成的问题分类为5个类别之一。该表显示了每个模型按类别细分的正确回答问题的百分比。

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上进行了测试,使用bfloat16权重。您还可以在具有20GB或更多内存的消费者GPU上加载8位和4位量化版本的模型。例如RTX 4090RTX 3090以及具有20GB或更多内存的Apple M2 Pro、M2 Max或M2 Ultra芯片。

2、下载模型权重

地址:defog/sqlcoder · Hugging Face

3、使用SQLCoder

您可以通过transformers库使用SQLCoder,方法是从Hugging Face存储库中下载我们的模型权重。我们已添加了有关在示例数据库架构上进行推断的示例代码。

python inference.py -q "Question about the sample database goes here"

示例问题:我们与纽约的客户相比,从旧金山的客户那里获得更多收入吗?为我提供每个城市的总收入以及两者之间的差异。您还可以在我们的网站上使用演示,或在Colab中运行SQLCoder。

4、Colab中运行SQLCoder

地址:https://colab.research.google.com/drive/1z4rmOEiFkxkMiecAWeTUlPl0OmKgfEu7?usp=sharing#scrollTo=MKuocI44V-Bo

第一步,配置环境

!pip install torch transformers bitsandbytes accelerate

第二步,测试

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipelinetorch.cuda.is_available()

第三步,下载模型

使用Colab Pro上的A100(或具有> 30GB VRAM的任何系统)在bf16中加载它。如果不可用,请使用至少具有20GB VRAM的GPU在8位中加载它,或者至少具有12GB VRAM在4位中加载它。在Colab上,它适用于V100,但在T4上崩溃。

首次下载模型然后将其加载到内存中的步骤大约需要10分钟。所以请耐心等待 :)

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,
)

第四步,设置问题和提示并进行标记化

随意更改以下问题。如果您想要尝试自己的数据库架构,请在提示中编辑模式。

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 productname VARCHAR(50), -- Name of the productprice DECIMAL(10,2), -- Price of each unit of the productquantity INTEGER  -- Current quantity in stock
);CREATE TABLE customers (customer_id INTEGER PRIMARY KEY, -- Unique ID for each customername VARCHAR(50), -- Name of the customeraddress VARCHAR(100) -- Mailing address of the customer
);CREATE TABLE salespeople (salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salespersonname VARCHAR(50), -- Name of the salespersonregion VARCHAR(50) -- Geographic sales region
);CREATE TABLE sales (sale_id INTEGER PRIMARY KEY, -- Unique ID for each saleproduct_id INTEGER, -- ID of product soldcustomer_id INTEGER,  -- ID of customer who made purchasesalesperson_id INTEGER, -- ID of salesperson who made the salesale_date DATE, -- Date the sale occurredquantity INTEGER -- Quantity of product sold
);CREATE TABLE product_suppliers (supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplierproduct_id INTEGER, -- Product ID suppliedsupply_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上可能非常缓慢。每个查询可能需要大约1-2分钟。在单个A100 40GB上,需要大约10-20秒。


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()
# 清空缓存,以便在内存崩溃时可以生成更多结果
# 在Colab上特别重要 - 内存管理要简单得多
# 在运行推断服务时
# 嗯!生成的SQL在这里:
print(outputs[0].split("```sql")[-1].split("```")[0].split(";")[0].strip() + ";")

SQLCoder的使用方法

更新中……

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/58432.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Mac Flutter web环境搭建

获取 Flutter SDK 下载以下安装包来获取最新的 stable Flutter SDK将文件解压到目标路径, 比如: cd ~/development $ unzip ~/Downloads/flutter_macos_3.13.0-stable.zip 配置 flutter 的 PATH 环境变量: export PATH"$PATH:pwd/flutter/bin" // 这个命…

【1++的数据结构】之AVL树

👍作者主页:进击的1 🤩 专栏链接:【1的数据结构】 文章目录 一,什么是AVL树二,AVL树的插入三,AVL树的旋转3.1 向左旋转3.2 向右旋转3.3 左右双旋3.4 右左双旋 四,验证AVL树是否平衡 …

LinkedList的顶级理解

目录 1.LinkedList的介绍 LinkedList的结构 2.LinkedList的模拟实现 2.1创建双链表 2.2头插法 2.3尾插法 2.4任意位置插入 2.5查找关键字 2.6链表长度 2.7遍历链表 2.8删除第一次出现关键字为key的节点 2.9删除所有值为key的节点 2.10清空链表 2.11完整代码 3.…

①matlab的命令掌握

目录 输入命令 命名变量 保存和加载变量 使用内置的函数和常量 输入命令 1.您可以通过在命令行窗口中 MATLAB 提示符 (>>) 后输入命令 任务 使用命令 3*5 将数值 3 和 5 相乘。 答案 3*5 2.除非另有指定,否则 MATLAB 会将计算结果存储在一个名为 ans…

php 文字生成图片保存到本地

你可以使用PHP的GD库来生成图片并保存到本地。首先,你需要确保你的PHP环境已经安装了GD库。然后,你可以使用GD库的函数来创建一个画布,并在上面绘制文字。最后,使用imagepng或imagejpeg函数将画布保存为PNG或JPEG格式的图片文件。…

POI groupRow 折叠分组,折叠部分不显示问题

折叠组是什么?如图就是用POI 实现的,代码很简单:sheet.groupRow(开始行,结束行)即可 但是万万没想到,最终实现出的结果,合并的组,有一部分并没有渲染出来,如下图: 因为我…

yum install libreoffice‘ returned a non-zero

The command ‘/bin/sh -c yum install libreoffice’ returned a non-zero code: 1 1. 异常信息 Is this ok [y/d/N]: Exiting on user command Your transaction was saved, rerun it with:yum load-transaction /tmp/yum_save_tx.2023-08-28.13-42.EftXfl.yumtx The comman…

基于蜜獾算法优化的BP神经网络(预测应用) - 附代码

基于蜜獾算法优化的BP神经网络(预测应用) - 附代码 文章目录 基于蜜獾算法优化的BP神经网络(预测应用) - 附代码1.数据介绍2.蜜獾优化BP神经网络2.1 BP神经网络参数设置2.2 蜜獾算法应用 4.测试结果:5.Matlab代码 摘要…

软件国产化之殇

今天又看到这么一个帖子讨论一款国产化软件,属实给我震撼到了。 对于国产化产品,一直主打的都是”自研“,难道是我对”自研“这个词的理解有误? 做一个产品,别人开源了,你拿过来使用,你可以说…

Android——基本控件(下)(十九)

1. 菜单:Menu 1.1 知识点 (1)掌握Android中菜单的使用; (2)掌握选项菜单(OptionsMenu)的使用; (3)掌握上下文菜单(ContextMenu&am…

Java doc等文件生成PDF、多个PDF合并

之前写过一遍文章是 图片生成PDF。 今天继续来对 doc等文件进行pdf合并以及多个pdf合并为一个pdf。 兄弟们&#xff0c;还是开箱即用。 1、doc生成pdf 依赖 <!-- doc生成pdf --><dependency><groupId>com.aspose</groupId><artifactId>aspose…

【会议征稿】2023智能通信与网络国际学术会议(ICN 2023)

2023智能通信与网络国际学术会议&#xff08;ICN 2023&#xff09; 2023 International Conference on Intelligent Communication and Networking (ICN2023) 2023智能通信与网络国际学术会议&#xff08;ICN 2023&#xff09;将于2023年11月10-12日在中国常州召开。ICN 2023…

Spring Boot 排除配置类的引用的方法

Spring Boot 提供的自动配置非常强大&#xff0c;某些情况下&#xff0c;自动配置的功能可能不符合我们的需求&#xff0c;需要我们自定义配置&#xff0c;这个时候就需要排除/禁用 Spring Boot 某些类的自动化配置了。 比如&#xff1a;数据源、邮件&#xff0c;这些都是提供…

设计模式——依赖倒转原则

文章目录 基本介绍应用实例依赖关系传递的三种方式和应用案例1, 接口传递&#xff0c;应用案例代码2, 构造方法传递&#xff0c;应用案例代码3, setter 方式传递&#xff0c;应用案例代码 依赖倒转原则的注意事项和细节 基本介绍 依赖倒转原则(Dependence Inversion Principle…

Vue3+TS+Vite中 vConsole 插件的使用

平时在web应用开发过程中&#xff0c;我们可以console.log去输出一些信息&#xff0c;但是在移动端&#xff0c;也就是在手机上&#xff0c;console.log的信息我们是看不到的&#xff0c;这时候就需要移动端调试工具vConsole 1. 依赖安装 npm install vconsole 或者 yarn ad…

扫雷小游戏

目录 一.扫雷小游戏 二.游戏主体一览 ​编辑 三.模块化设计扫雷游戏 3.1打印欢迎菜单 3.2创建两个二维数组 3.3棋盘稍加修改 3.4布置雷 3.5排查雷 四.游戏总体代码 4.1game.h头文件 4.2game.c函数实现源文件 4.3游戏main函数主体 五.游戏效果图 一.扫雷小游戏 这是…

Jmeter+ServerAgent

一、Jmeter 下载 https://jmeter.apache.org/download_jmeter.cgi选择Binaries二进制下载 apache-jmeter-5.6.2.tgz 修改配置文件 jmeter下的bin目录&#xff0c;打开jmeter.properties 文件 languagezh_CN启动命令 cd apache-jmeter-5.6/bin sh jmeter二、ServerAgent 监…

nginx 托管vue项目配置

server {listen 80;server_name your_domain.com;location / {root /path/to/your/vue/project;index index.html;try_files $uri $uri/ /index.html;} }奇怪的现象,在vue路由中/会跳转到/abc/def&#xff0c;但如果直接输入/abc/def会显示404&#xff0c;添加 try_files $uri…

实战 图书馆系统管理案例

config &#xff1a;敏感的配置一般都是在配置中心配置&#xff0c;比如consul或者阿波罗上面controller &#xff1a;写一些handler的&#xff0c;拿到参数要去调用service层的逻辑。&#xff08;只负责接受参数&#xff0c;怎么绑定参数&#xff0c;要去调用哪个service的&am…

Viobot输出数据说明

一.原始数据 1.ROS话题 1)相机原始图像数据 Type: sensor_msgs::Image Topic: 左目&#xff1a;/image_left 右目&#xff1a;/image_right 2&#xff09;imu数据 Type: sensor_msgs::Imu Topic: /imu 3&#xff09;TOF数据 点云数据&#xff1a; Type: sensor_msgs::P…