sqlcoder实践

背景

  • Defog

  • llama-3

意义

翻译自然语言到sql,类似脑机接口,大模型重要应用领域

  • sql是数据库查询标准;关系数据库,工具(datax,sqoop,logstash,hive),非关系数据库(MongoDB,图数据库)等都支持sql查询

  • BI,数字化运营,商业分析,大数据分析

  • 智能问数

  • 智能问答

  • 没有大模型前智能问答方案 :

  • 开源项目 QABasedOnMedicaKnowledgeGraph

  • https://gitcode.com/liuhuanyong/QASystemOnMedicalKG/overview?utm_source=csdn_github_accelerator&isLogin=1

待完善

  • 可靠性

  • 复杂,不规范的数据库表

  • 信息安全

llama-3-sqlcoder-8b

要求

  • 能翻墙

  • Nvidia 显卡

模型下载

  • https://huggingface.co/defog/llama-3-sqlcoder-8b

  • https://aifasthub.com/models/defog

环境配置

cuda

  • 检查电脑适配cuda版本

D:\working\code> nvidia-smi   
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 528.49       Driver Version: 528.49       CUDA Version: 12.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ... WDDM  | 00000000:03:00.0  On |                  N/A |
| N/A   32C    P8     9W /  80W |    616MiB / 12288MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------++-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1476    C+G   C:\Windows\System32\dwm.exe     N/A      |
|    0   N/A  N/A      2572    C+G   ...wekyb3d8bbwe\Video.UI.exe    N/A      |
|    0   N/A  N/A      2964    C+G   ...d\runtime\WeChatAppEx.exe    N/A      |
|    0   N/A  N/A      4280    C+G   ...2txyewy\TextInputHost.exe    N/A      |
|    0   N/A  N/A      4656    C+G   ...artMenuExperienceHost.exe    N/A      |
|    0   N/A  N/A      7636    C+G   C:\Windows\explorer.exe         N/A      |
|    0   N/A  N/A      7924    C+G   ...icrosoft VS Code\Code.exe    N/A      |
|    0   N/A  N/A      8796    C+G   ...5n1h2txyewy\SearchApp.exe    N/A      |
|    0   N/A  N/A      9376    C+G   ...me\Application\chrome.exe    N/A      |
|    0   N/A  N/A     10540      C   ...rograms\Ollama\ollama.exe    N/A      |
|    0   N/A  N/A     11720    C+G   ...y\ShellExperienceHost.exe    N/A      |
|    0   N/A  N/A     13676    C+G   ...ontend\Docker Desktop.exe    N/A      |
+-----------------------------------------------------------------------------+
 

得到CUDA版本为12.0

  • 下载

https://developer.nvidia.com/cuda-toolkit-archive

安装后的信息

Installed:- Nsight for Visual Studio 2022- Nsight Monitor
Not Installed:- Nsight for Visual Studio 2019Reason: VS2019 was not found- Nsight for Visual Studio 2017Reason: VS2017 was not found- Integrated Graphics Frame Debugger and ProfilerReason: see https://developer.nvidia.com/nsight-vstools- Integrated CUDA ProfilersReason: see https://developer.nvidia.com/nsight-vstools

  • 查看版本

C:\Users\Administrator>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Mon_Oct_24_19:40:05_Pacific_Daylight_Time_2022
Cuda compilation tools, release 12.0, V12.0.76
Build cuda_12.0.r12.0/compiler.31968024_0
 

torch

  • torch是一个Python库,用于构建和训练深度学习和张量计算模型

  • 去torch官网中查看老版本CUDA适配的torch版本:

https://pytorch.org/get-started/locally/

C:\Users\Administrator>pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
Looking in indexes: https://download.pytorch.org/whl/cu121
Requirement already satisfied: torch in c:\python312\lib\site-packages (2.3.0)
Collecting torchvisionDownloading https://download.pytorch.org/whl/cu121/torchvision-0.18.1%2Bcu121-cp312-cp312-win_amd64.whl (5.7 MB)---------------------------------------- 5.7/5.7 MB 5.9 MB/s eta 0:00:00
Collecting torchaudioDownloading https://download.pytorch.org/whl/cu121/torchaudio-2.3.1%2Bcu121-cp312-cp312-win_amd64.whl (4.1 MB)---------------------------------------- 4.1/4.1 MB 7.2 MB/s eta 0:00:00
Requirement already satisfied: filelock in c:\python312\lib\site-packages (from torch) (3.14.0)
Requirement already satisfied: typing-extensions>=4.8.0 in c:\python312\lib\site-packages (from torch) (4.12.1)
Requirement already satisfied: sympy in c:\python312\lib\site-packages (from torch) (1.12.1)
Requirement already satisfied: networkx in c:\python312\lib\site-packages (from torch) (3.3)
Requirement already satisfied: jinja2 in c:\python312\lib\site-packages (from torch) (3.1.4)
Requirement already satisfied: fsspec in c:\python312\lib\site-packages (from torch) (2024.5.0)
Requirement already satisfied: mkl<=2021.4.0,>=2021.1.1 in c:\python312\lib\site-packages (from torch) (2021.4.0)
Requirement already satisfied: numpy in c:\python312\lib\site-packages (from torchvision) (1.26.4)
Collecting torchDownloading https://download.pytorch.org/whl/cu121/torch-2.3.1%2Bcu121-cp312-cp312-win_amd64.whl (2423.5 MB)---------------------------------------- 2.4/2.4 GB 501.6 kB/s eta 0:00:00
Collecting pillow!=8.3.*,>=5.3.0 (from torchvision)Downloading https://download.pytorch.org/whl/pillow-10.2.0-cp312-cp312-win_amd64.whl (2.6 MB)---------------------------------------- 2.6/2.6 MB 2.5 MB/s eta 0:00:00
Requirement already satisfied: intel-openmp==2021.* in c:\python312\lib\site-packages (from mkl<=2021.4.0,>=2021.1.1->torch) (2021.4.0)
Requirement already satisfied: tbb==2021.* in c:\python312\lib\site-packages (from mkl<=2021.4.0,>=2021.1.1->torch) (2021.12.0)
Requirement already satisfied: MarkupSafe>=2.0 in c:\python312\lib\site-packages (from jinja2->torch) (2.1.5)
Requirement already satisfied: mpmath<1.4.0,>=1.1.0 in c:\python312\lib\site-packages (from sympy->torch) (1.3.0)
Installing collected packages: pillow, torch, torchvision, torchaudioAttempting uninstall: torchFound existing installation: torch 2.3.0Uninstalling torch-2.3.0:Successfully uninstalled torch-2.3.0
Successfully installed pillow-10.2.0 torch-2.3.1+cu121 torchaudio-2.3.1+cu121 torchvision-0.18.1+cu121

transformers

 

pip install transformers

编写脚本

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import sqlparse
print("是否可用:", torch.cuda.is_available())        # 查看GPU是否可用
print("GPU数量:", torch.cuda.device_count())        # 查看GPU数量
print("torch方法查看CUDA版本:", torch.version.cuda)  # torch方法查看CUDA版本
print("GPU索引号:", torch.cuda.current_device())    # 查看GPU索引号
print("GPU名称:", torch.cuda.get_device_name(0))    # 根据索引号得到GPU名称
available_memory = torch.cuda.get_device_properties(0).total_memory
print("GPU内存大小 :",available_memory)model_name = "llama-3-sqlcoder-8b"
tokenizer = AutoTokenizer.from_pretrained(model_name)
if available_memory > 20e9:# if you have atleast 20GB of GPU memory, run load the model in float16model = AutoModelForCausalLM.from_pretrained(model_name,trust_remote_code=True,torch_dtype=torch.float16,device_map="auto",use_cache=True,)
else:# else, load in 4 bits – this is slower and less accuratemodel = AutoModelForCausalLM.from_pretrained(model_name,trust_remote_code=True,# torch_dtype=torch.float16,load_in_4bit=True,device_map="auto",use_cache=True,)prompt = """<|begin_of_text|><|start_header_id|>user<|end_header_id|>Generate a SQL query to answer this question: `{question}`DDL statements: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<|eot_id|><|start_header_id|>assistant<|end_header_id|>The following SQL query best answers the question `{question}`:
```sql
"""def generate_query(question):updated_prompt = prompt.format(question=question)inputs = tokenizer(updated_prompt, return_tensors="pt").to("cuda")generated_ids = model.generate(**inputs,num_return_sequences=1,eos_token_id=tokenizer.eos_token_id,pad_token_id=tokenizer.eos_token_id,max_new_tokens=400,do_sample=False,num_beams=1,temperature=0.0,top_p=1,)outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)torch.cuda.empty_cache()torch.cuda.synchronize()# empty cache so that you do generate more results w/o memory crashing# particularly important on Colab – memory management is much more straightforward# when running on an inference service# return sqlparse.format(outputs[0].split("[SQL]")[-1], reindent=True)return outputs[0].split("```sql")[1].split(";")[0]question = "What was our revenue by product in the new york region last month?"
generated_sql = generate_query(question)
print(sqlparse.format(generated_sql, reindent=True))

运行过程

D:\working\code> & C:/Python312/python.exe d:/working/code/sqlcode_v3.py
是否可用: True
GPU数量: 1
torch方法查看CUDA版本: 12.1
GPU索引号: 0
GPU名称: NVIDIA GeForce RTX 3060 Laptop GPU
GPU内存大小 : 12884377600
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
The `load_in_4bit` and `load_in_8bit` arguments are deprecated and will be removed in the future versions. Please, pass a `BitsAndBytesConfig` object in `quantization_config` argument instead.
Loading checkpoint shards: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:26<00:00,  6.59s/it]
C:\Python312\Lib\site-packages\transformers\generation\configuration_utils.py:515: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.0` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.warnings.warn(
C:\Python312\Lib\site-packages\bitsandbytes\nn\modules.py:426: UserWarning: Input type into Linear4bit is torch.float16, but bnb_4bit_compute_dtype=torch.float32 (default). This 
will lead to slow inference or training speed.warnings.warn(
C:\Python312\Lib\site-packages\transformers\models\llama\modeling_llama.py:649: UserWarning: 1Torch was not compiled with flash attention. (Triggered internally at ..\aten\src\ATen\native\transformers\cuda\sdp_utils.cpp:455.)attn_output = torch.nn.functional.scaled_dot_product_attention(SELECT p.name,SUM(s.quantity * p.price) AS total_revenue
FROM products p
JOIN sales s ON p.product_id = s.product_id
JOIN salespeople sp ON s.salesperson_id = sp.salesperson_id
WHERE sp.region = 'New York'AND s.sale_date >= CURRENT_DATE - INTERVAL '1 month'
GROUP BY p.name

 

延伸场景

连接真实数据库

参照文档 Getting Started | Defog Docs

界面交互

  • 百度智能云,千帆大模型

SQLCoder-7B是由Defog研发、基于Mistral-7B微调的语言模型 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Hlo472sa2

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

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

相关文章

上新:NFTScan 正式上线 Bitcoin-brc20 浏览器!

近日&#xff0c;NFTScan 团队正式对外发布了 Bitcoin-brc20 浏览器&#xff0c;将为 Bitcoin 生态的 NFT 开发者和用户提供简洁高效的 NFT 数据搜索查询服务。作为比特币生态中最火热的标准之一&#xff0c;brc20 也吸引着广泛的关注。洞悉其巨大潜力&#xff0c;NFTScan 对 b…

协同编辑:只是在线协作这么简单吗?揭秘协同编辑的深层价值

经常很多朋友咨询&#xff0c;无忧企业文档是否支持协同编辑&#xff0c;首先肯定是支持的。但是&#xff0c;我发现很多人对于“协同编辑”的理解可能比较表面&#xff0c;仅仅停留在多人同时编辑一份文档的层面。实际上&#xff0c;协同编辑的功能远不止于此&#xff0c;它更…

两个方法,批量替换PPT中的字体

经常制作ppt的朋友可能会遇到需要批量替换字体的情况&#xff0c;如果我们想要更换ppt中的字体&#xff0c;今天分享PPT批量替换字体的两个方法。 方法一&#xff1a; 找到功能栏中的编辑选项卡&#xff0c;点击替换 – 替换字体&#xff0c;在里面选择我们想要替换的字体就可…

通过MindSpore API实现深度学习模型

快速入门 将相应的包逐一导入到项目中&#xff0c;这是制作项目的第一步。 import mindspore from mindspore import nn from mindspore.dataset import vision, transforms from mindspore.dataset import MnistDataset 处理数据集 先从网上下载对应的数据集文件,MindSpor…

《C++ Primer》导学系列:第 6 章 - 函数

6.1 函数基础 6.1.1 基本概念 函数是C程序的基本组成单元&#xff0c;用于将代码组织成可以复用的模块。函数通过函数名进行调用&#xff0c;并且可以接受参数和返回值。函数的定义包括函数头和函数体&#xff0c;其中函数头描述了函数的接口&#xff0c;函数体包含了具体的实…

最新OPPO 真我手机 一加手机 使用adb命令永久关闭系统更新教程

使用adb命令永久关闭系统更新 一、先了解手机系统二、Android 11 以下使用adb 命令永久关闭系统更新1、adb 官方下载2、小白开启 USB 调试模式教程&#xff08;熟手跳过&#xff09;三、Android 12 以上使用adb 命令永久关闭系统更新什么您还是不会弄&#xff01;赞赏我&#x…

MYSQL 四、mysql进阶 3(存储引擎)

mysql中表使用了不同的存储引擎也就决定了我们底层文件系统中文件的相关物理结构。 为了管理方便&#xff0c;人们把连接管理、语法解析、查询优化这些并不涉及真实数据存储的功能划分为 Mysql Server的功能&#xff0c;把真实存取数据的功能划分为存储引擎的功能&…

systemd的实现原理

systemd是现代Linux系统中的初始化系统和服务器管理器&#xff0c;而systemctl是用于与systemd交互的命令行工具。 systemd是一个守护进程&#xff0c;systemctl是命令行管理工具&#xff1a;systemd是用于管理Linux系统的初始化过程和后台服务的初始化系统&#xff0c;而syst…

Windows10 + fydeOS双系统!简单几步完成

前言 最近发现小伙伴对于fydeOS热情是真的不减&#xff0c;啧啧啧……今天闲来无事&#xff0c;就来讲讲双系统Windows10 fydeOS的安装方法吧&#xff01; Windows10 FydeOS双系统安装过程其实很简单&#xff0c;不过要建议先安装好Windows10系统。 虽然先安装好fydeOS之后…

SpringBootWeb 篇-入门了解 Vue 前端工程的创建与基本使用

&#x1f525;博客主页&#xff1a; 【小扳_-CSDN博客】 ❤感谢大家点赞&#x1f44d;收藏⭐评论✍ 文章目录 1.0 基于脚手架创建前端工程 1.1 基于 Vue 开发前端项目的环境要求 1.2 前端工程创建的方式 1.2.1 基于命令的方式来创建前端工程 1.2.2 使用图形化来创建前端工程 1.…

如何建立私域流量?私域流量怎么运营,一文读懂

当全网都在讨论私域流量&#xff0c;你是不是也有很多问号呢&#xff1f; 互联网高速发达&#xff0c;消费形式日新月异&#xff0c;跟不上时代就会被时代淘汰&#xff0c;接下来&#xff0c;我们就从3个层面深度讨论下私域流量究竟是什么&#xff1f;为什么要玩转私域流量&am…

【保姆级教程】Linux 基于 Docker 部署 MySQL 和 Nacos 并配置两者连接

一、Linux 部署 Docker 1.1 卸载旧版本&#xff08;如有&#xff09; sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine1.2 安装 yum-utils 包 sudo yum install -y…

微信多开器

由于微信的限制&#xff0c;我们平时只能登录一个微信&#xff0c;要登录多个微信一般需要多台手机&#xff0c;很显然这种方法很费手机&#xff01;&#xff01;一个微信多开神器可以给你省下好几台手机钱&#xff0c;抓紧拉下来放手机里落灰http://www.xbydon.online/?p132 …

NetSuite 审批工作流与事务处理类型的限制关系

在最近的实践中&#xff0c;用户提出可否对Credit Memo与Vendor Prepayment Application两种事务处理类型进行审批参与&#xff0c;当提出来的时候我们并没有直接在系统中进行测试&#xff0c;而是以常规事务处理的角度认为可以满足客户的需求&#xff1b; 但在沙盒环境中讨论…

RocketMQ快速入门:如何保证消息不丢失|保证消息可靠性(九)

0. 引言 在金融、电商等对数据完整性要求极高的行业&#xff0c;消息的丢失可能会导致数据不一致&#xff0c;严重影响业务逻辑和数据统计&#xff0c;也影响客户体验&#xff0c;所以在很多业务场景下&#xff0c;我们都要求数据不能丢失。而rocketmq中&#xff0c;如何对消息…

当游戏遭遇安全问题,我们应该怎么做?

在游戏安全领域&#xff0c;专业性最差、但最常见的案例类型是DDoS攻击&#xff08;分布式拒绝服务攻击&#xff09;。出于它的特性&#xff0c;中小厂商、独立开发者较容易遭受这类攻击。 例如&#xff0c;今年2月29日上线的手游《雷索纳斯》就遭受了名为ACCN组织发起的DDoS攻…

【内含优惠码】重磅发售!《2023年度中国量化投资白皮书》(纸质版)

这是可以公开了解量化行业图景的&#xff0c;为数不多资料。 简介 《2023年度中国量化投资白皮书》由宽邦科技、华泰证券、金融阶、华锐技术、AMD、阿里云、英迈中国等多家机构联合发起编写&#xff0c;并于2024年6月15日正式发布&#xff0c;全书公17万字6大章节勾勒最新量化…

Studying-代码随想录训练营day15| 222.完全二叉树的节点个数、110.平衡二叉树、257.二叉树的所有路径、404.左叶子之和

第十五天&#xff0c;二叉树part03&#x1f4aa;&#xff0c;编程语言&#xff1a;C 目录 257.完全二叉树的节点个数 110.平衡二叉树 257.二叉树的所有路径 404.左叶子之和 总结 257.完全二叉树的节点个数 文档讲解&#xff1a;代码随想录完全二叉树的节点个数 视频讲解…

Arduino平台软硬件原理及使用——无源蜂鸣器模块的使用

文章目录 一、蜂鸣器发声原理 二、无源蜂鸣器与有源蜂鸣器的区分 三、无源蜂鸣器模块在Arduino中的使用 一、蜂鸣器发声原理 上图为常见的不同封装及规格的蜂鸣器。 同蜜蜂、知了等昆虫发声原理一样&#xff0c;蜂鸣器同样靠振动来发出声音&#xff1b; 如上图为无源蜂鸣器的内…

【总结】ui自动化selenium知识点总结

1. 大致原理 首页安装第三方库selenium库&#xff0c; 其次要下载好浏览器驱动文件&#xff0c;比如谷歌的 chromedriver.exe&#xff0c;配置上环境变量。 使用selenium的webdriver类去创建一个浏览器驱动对象赋值叫driver&#xff0c;一个浏览器驱动对象就可以 实现 对浏…