sqlcoder实践

news2024/10/23 2:46:27

背景

  • 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 2019
       Reason: VS2019 was not found
     - Nsight for Visual Studio 2017
       Reason: VS2017 was not found
     - Integrated Graphics Frame Debugger and Profiler
       Reason: see https://developer.nvidia.com/nsight-vstools
     - Integrated CUDA Profilers
       Reason: 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 torchvision
  Downloading 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 torchaudio
  Downloading 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 torch
  Downloading 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, torchaudio
  Attempting uninstall: torch
    Found existing installation: torch 2.3.0
    Uninstalling 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 float16
    model = 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 accurate
    model = 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 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<|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.coloradmin.cn/o/1844088.html

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

相关文章

安装Xshell和Xftp后,打开程序下无法使用,要继续使用此程序,您必须应用最新的更新或使用新版本。

之前我遇到这种情况的时候&#xff0c;修改了nslicense.dll&#xff0c;结果这次重新下载结果把修改代码给忘了&#xff08;因为我也不会这个&#xff09;&#xff0c;于是我用了最直接的办法去官网找最新版本下载&#xff1a;xshell/xftp官网 后面直接点下载&#xff0c;然后开…

网络编程5----初识http

1.1 请求和响应的格式 http协议和前边学过的传输层、网络层协议不同&#xff0c;它是“一问一答”形式的&#xff0c;所以要分为请求和响应两部分看待&#xff0c;同时&#xff0c;请求和响应的格式是不同的&#xff0c;我们来具体介绍一下。 1.1.1 请求 在介绍请求之前&…

PMP报考千万不要踩这些坑

8月份的PMP考试即将到来&#xff0c;在这里预祝大家都能一次过~~~~ - 现在这个大环境不是很好&#xff0c;很多公司都是需要学历、相关证书的&#xff0c;说实在的我公司要求就是除了要有专业证书之外还要额外其他的证书&#xff0c;例如&#xff0c;pmp证书。这段时间我的同…

卷积篇 | YOLOv8改进之引入重新参数化再聚焦卷积RefConv | 即插即用

前言:Hello大家好,我是小哥谈。论文提出了重新参数化再聚焦卷积RefConv作为常规卷积层的替代品,它是一个即插即用模块,可以在没有任何推理成本的情况下提高性能。具体来说,在给定预训练模型的情况下,RefConv对从预训练模型继承的基核进行可训练的再聚焦变换,以建立参数之…

AI制作PPT如何简化大学生的答辩准备?

时间过的好快&#xff0c;马上又到了一年一度的毕业季了。大学生活是丰富多彩的&#xff0c;同时大学的学业也是非常重要的。对于大学生来说最后的毕业答辩是非常重要的&#xff0c;关乎到能不能顺利毕业&#xff0c;想到自己为了答辩PPT熬的夜&#xff0c;掉的头发&#xff0c…

番外篇 | 基于改进YOLOv5的安全帽佩戴检测 | 重参数化结构RepVGG + 空间对象注意力机制RCS-OSA模块

前言:Hello大家好,我是小哥谈。RCS-YOLO是一种目标检测算法,它是基于YOLOv3算法的改进版本。通过查看RCS-YOLO的整体架构可知,其中包括RCS-OSA模块。RCS-OSA模块在模型中用于堆叠RCS模块,以确保特征的复用并加强不同层之间的信息流动。本文针对安全帽佩戴的检测就是基于RC…

dup和dup2函数

#include <unistd.h> int dup(int oldfd); 功能&#xff1a;根据oldfd生成newfd,newfd采用最小位使用原则分配&#xff0c;新旧文件描述符都可以操作文件&#xff0c;它们共享光标和打开文件的方式。 参数&#xff1a;olfd:旧的文件描述符 返回值&#xff1a;成功返回新的…

视频监控平台功能:国外的硬盘录像机NVR通过ISUP协议(原ehome协议)接入AS-V1000视频平台

目录 一、背景说明 二、ISUP协议介绍 1、海康ISUP协议概述 2、ISUP协议支持主码流和子码流切换 &#xff08;1&#xff09;灵活配置和个性化 &#xff08;2&#xff09;适应不同网络带宽&#xff0c;提高使用体验 3、海康ehome相关文章 三、ISUP协议接入说明 1、平台侧…

【ARMv8/v9 GIC 系列 4.1 -- GIC CPU Interface 访问支持情况】

文章目录 GIC CPU Interface 访问支持Bit[27:24]: GIC CPU接口汇编代码实现访问小结 GIC CPU Interface 访问支持 在ARMv8架构中&#xff0c;ID_AA64PFR0_EL1是一个系统寄存器&#xff0c;提供了有关处理器功能的详绀信息。这个寄存器的位[27:24]专门用于描述GIC&#xff08;通…

VScode中js关闭烦人的ts检查

类似如下的代码在vscode 会报错&#xff0c;我们可以在前面添加忽略检查或者错误&#xff0c;如下&#xff1a; 但是&#xff01;&#xff01;&#xff01;这太不优雅了&#xff01;&#xff01;&#xff01;&#xff0c;js代码命名没有问题&#xff0c;错在ts上面&#xff0c;…

码蹄集 BD202401 补给

错误解法&#xff1a;简单将取半前后的综合排序后取最小值&#xff0c;这样没有考虑这样一种情况&#xff1a;取半的时机不对&#xff0c;也许取半某个大一点的P之后反而能进一步取一个补给点了呢&#xff1f;&#xff1f;对不对。这样简单排序只不过是“最省钱”的一种&#x…

Linux源码阅读笔记02-进程原理及系统调用

进程和进程的生命周期 进程&#xff1a;指计算机中已运行的程序。进程本身不是基本的运行单位&#xff0c;而是线程的容器。程序本身不是基本的运行单位&#xff0c;而是线程的容器。程序是指令、数据和组织形式的描述&#xff0c;进程才是程序的真正运行实例。Linux内核把进程…

AndroidStudio中如何运行class中的main()函数?

直接右键运行Run main()是会报错的, * What went wrong: A problem occurred configuring project :app. > Could not create task :app:RsaUtils.main(). > SourceSet with name main not found. 需要Run main with Coverage 有些文章说需要在gradle.xml里面加上&…

HNU操作系统2023期中考试试卷及参考答案

本试题参考 甘晴void 的CSDN博客【2.2】操作系统OS_甘晴void的博客-CSDN博客&#xff0c;本意为期中复习自用&#xff0c;答案在其基础上进行进一步完善&#xff0c;若有错误还请指正&#xff01; 第一题&#xff1a;基础题&#xff08;20分&#xff09; 1.1&#xff08;4分&a…

智慧物流:实现供应链的技术升级

智慧物流不仅是物流管理系统的智能化升级&#xff0c;更是以物联网、大数据分析等技术手段为基础的一种高效、智能的供应链解决方案。通过实时监控、数据分析和智能优化&#xff0c;智慧物流将传统物流的各个环节有效连接起来&#xff0c;实现信息流、资金流和物流的无缝对接。…

全网最全postman接口测试教程和项目实战~从入门到精通

Postman实现接口测试内容大纲一览&#xff1a; 一、什么是接口&#xff1f;为什么需要接口&#xff1f; 接口指的是实体或者软件提供给外界的一种服务。 因为接口能使我们的实体或者软件的内部数据能够被外部进行修改。从而使得内部和外部实现数据交互。所以需要接口。 比如&…

GPT-4o与GPT-4的价格对比

截图来源于微软Azure的OpenAI服务。 GPT-4o比GPT-4的价格降低是肉眼可见&#xff0c;可惜计价是美元单位&#xff0c;较国内的价格而言还是比较贵&#xff0c;拿来做Demo演示可以&#xff0c;商用的话&#xff0c;还要仔细考量考量。 国内的地板价已经来到了0.5元/百万Token的…

python使用pyautogui自动化模拟鼠标、键盘操作、截屏、识别图片位置

&#x1f308;所属专栏&#xff1a;【python】✨作者主页&#xff1a; Mr.Zwq✔️个人简介&#xff1a;一个正在努力学技术的Python领域创作者&#xff0c;擅长爬虫&#xff0c;逆向&#xff0c;全栈方向&#xff0c;专注基础和实战分享&#xff0c;欢迎咨询&#xff01; 您的…

进入全球市场的游戏本地化策略

开发商在开发游戏时已经很久没有针对单一国家了。对于目前正在开发的许多游戏来说&#xff0c;它们都致力于进入全球市场并吸引各种客户。然而&#xff0c;必须考虑一些游戏是否适合某些市场&#xff0c;以及某些国家是否具有足够的市场潜力。此外&#xff0c;在进入海外市场时…

实现一个简易动态线程池

项目完整代码&#xff1a;https://github.com/YYYUUU42/Yu-dynamic-thread-pool 如果该项目对你有帮助&#xff0c;可以在 github 上点个 ⭐ 喔 &#x1f970;&#x1f970; 1. 线程池概念 2. ThreadPoolExecutor 介绍 2.1. ThreadPoolExecutor是如何运行&#xff0c;如何同时…