sqlcoder实践

2024-06-20 21:12
文章标签 实践 sqlcoder

本文主要是介绍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

这篇关于sqlcoder实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1079271

相关文章

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

Prometheus与Grafana在DevOps中的应用与最佳实践

Prometheus 与 Grafana 在 DevOps 中的应用与最佳实践 随着 DevOps 文化和实践的普及,监控和可视化工具已成为 DevOps 工具链中不可或缺的部分。Prometheus 和 Grafana 是其中最受欢迎的开源监控解决方案之一,它们的结合能够为系统和应用程序提供全面的监控、告警和可视化展示。本篇文章将详细探讨 Prometheus 和 Grafana 在 DevO

springboot整合swagger2之最佳实践

来源:https://blog.lqdev.cn/2018/07/21/springboot/chapter-ten/ Swagger是一款RESTful接口的文档在线自动生成、功能测试功能框架。 一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务,加上swagger-ui,可以有很好的呈现。 SpringBoot集成 pom <!--swagge

vue2实践:el-table实现由用户自己控制行数的动态表格

需求 项目中需要提供一个动态表单,如图: 当我点击添加时,便添加一行;点击右边的删除时,便删除这一行。 至少要有一行数据,但是没有上限。 思路 这种每一行的数据固定,但是不定行数的,很容易想到使用el-table来实现,它可以循环读取:data所绑定的数组,来生成行数据,不同的是: 1、table里面的每一个cell,需要放置一个input来支持用户编辑。 2、最后一列放置两个b

【HarmonyOS】-TaskPool和Worker的对比实践

ArkTS提供了TaskPool与Worker两种多线程并发方案,下面我们将从其工作原理、使用效果对比两种方案的差异,进而选择适用于ArkTS图片编辑场景的并发方案。 TaskPool与Worker工作原理 TaskPool与Worker两种多线程并发能力均是基于 Actor并发模型实现的。Worker主、子线程通过收发消息进行通信;TaskPool基于Worker做了更多场景化的功能封装,例

vue2实践:第一个非正规的自定义组件-动态表单对话框

前言 vue一个很重要的概念就是组件,作为一个没有经历过前几代前端开发的我来说,不太能理解它所带来的“进步”,但是,将它与后端c++、java类比,我感觉,组件就像是这些语言中的类和对象的概念,通过封装好的组件(类),可以通过挂载的方式,非常方便的调用其提供的功能,而不必重新写一遍实现逻辑。 我们常用的element UI就是由饿了么所提供的组件库,但是在项目开发中,我们可能还需要额外地定义一

《C++中的移动构造函数与移动赋值运算符:解锁高效编程的最佳实践》

在 C++的编程世界中,移动构造函数和移动赋值运算符是提升程序性能和效率的重要工具。理解并正确运用它们,可以让我们的代码更加高效、简洁和优雅。 一、引言 随着现代软件系统的日益复杂和对性能要求的不断提高,C++程序员需要不断探索新的技术和方法来优化代码。移动构造函数和移动赋值运算符的出现,为解决资源管理和性能优化问题提供了有力的手段。它们允许我们在不进行不必要的复制操作的情况下,高效地转移资源

Vue3+elementplus实现图片上传下载(最强实践)

图片上传子组件: 实现照片的上传,预览,以及转成以逗号隔开的图片地址,即时监听,并发送消息到父组件。 <!-- ImageUploader.vue --> <template><div><el-upload class="avatar-uploader" :http-request="customUpload" :before-upload="beforeUpload":show-fil

Banana Pi BPI-F3 进迭时空RISC-V架构下,AI融合算力及其软件栈实践

RISC-V架构下,AI融合算力及其软件栈实践 面对未来大模型(LLM)、AIGC等智能化浪潮的挑战,进迭时空在RISC-V方向全面布局,通过精心设计的RISC-V DSA架构以及软硬一体的优化策略,将全力为未来打造高效且易用的AI算力解决方案。目前,进迭时空已经取得了显著的进展,成功推出了第一个版本的智算核(带AI融合算力的智算CPU)以及配套的AI软件栈。 软件栈简介 AI算法部署旨