当前位置:   article > 正文

sqlcoder实践_llama-3-sqlcoder-8b

llama-3-sqlcoder-8b

背景

  • 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版本

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

得到CUDA版本为12.0

  • 下载

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

安装后的信息

  1. Installed:
  2. - Nsight for Visual Studio 2022
  3. - Nsight Monitor
  4. Not Installed:
  5. - Nsight for Visual Studio 2019
  6. Reason: VS2019 was not found
  7. - Nsight for Visual Studio 2017
  8. Reason: VS2017 was not found
  9. - Integrated Graphics Frame Debugger and Profiler
  10. Reason: see https://developer.nvidia.com/nsight-vstools
  11. - Integrated CUDA Profilers
  12. Reason: see https://developer.nvidia.com/nsight-vstools

  • 查看版本

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

torch

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

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

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

  1. C:\Users\Administrator>pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
  2. Looking in indexes: https://download.pytorch.org/whl/cu121
  3. Requirement already satisfied: torch in c:\python312\lib\site-packages (2.3.0)
  4. Collecting torchvision
  5. Downloading https://download.pytorch.org/whl/cu121/torchvision-0.18.1%2Bcu121-cp312-cp312-win_amd64.whl (5.7 MB)
  6. ---------------------------------------- 5.7/5.7 MB 5.9 MB/s eta 0:00:00
  7. Collecting torchaudio
  8. Downloading https://download.pytorch.org/whl/cu121/torchaudio-2.3.1%2Bcu121-cp312-cp312-win_amd64.whl (4.1 MB)
  9. ---------------------------------------- 4.1/4.1 MB 7.2 MB/s eta 0:00:00
  10. Requirement already satisfied: filelock in c:\python312\lib\site-packages (from torch) (3.14.0)
  11. Requirement already satisfied: typing-extensions>=4.8.0 in c:\python312\lib\site-packages (from torch) (4.12.1)
  12. Requirement already satisfied: sympy in c:\python312\lib\site-packages (from torch) (1.12.1)
  13. Requirement already satisfied: networkx in c:\python312\lib\site-packages (from torch) (3.3)
  14. Requirement already satisfied: jinja2 in c:\python312\lib\site-packages (from torch) (3.1.4)
  15. Requirement already satisfied: fsspec in c:\python312\lib\site-packages (from torch) (2024.5.0)
  16. Requirement already satisfied: mkl<=2021.4.0,>=2021.1.1 in c:\python312\lib\site-packages (from torch) (2021.4.0)
  17. Requirement already satisfied: numpy in c:\python312\lib\site-packages (from torchvision) (1.26.4)
  18. Collecting torch
  19. Downloading https://download.pytorch.org/whl/cu121/torch-2.3.1%2Bcu121-cp312-cp312-win_amd64.whl (2423.5 MB)
  20. ---------------------------------------- 2.4/2.4 GB 501.6 kB/s eta 0:00:00
  21. Collecting pillow!=8.3.*,>=5.3.0 (from torchvision)
  22. Downloading https://download.pytorch.org/whl/pillow-10.2.0-cp312-cp312-win_amd64.whl (2.6 MB)
  23. ---------------------------------------- 2.6/2.6 MB 2.5 MB/s eta 0:00:00
  24. Requirement already satisfied: intel-openmp==2021.* in c:\python312\lib\site-packages (from mkl<=2021.4.0,>=2021.1.1->torch) (2021.4.0)
  25. Requirement already satisfied: tbb==2021.* in c:\python312\lib\site-packages (from mkl<=2021.4.0,>=2021.1.1->torch) (2021.12.0)
  26. Requirement already satisfied: MarkupSafe>=2.0 in c:\python312\lib\site-packages (from jinja2->torch) (2.1.5)
  27. Requirement already satisfied: mpmath<1.4.0,>=1.1.0 in c:\python312\lib\site-packages (from sympy->torch) (1.3.0)
  28. Installing collected packages: pillow, torch, torchvision, torchaudio
  29. Attempting uninstall: torch
  30. Found existing installation: torch 2.3.0
  31. Uninstalling torch-2.3.0:
  32. Successfully uninstalled torch-2.3.0
  33. 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

编写脚本

  1. import torch
  2. from transformers import AutoTokenizer, AutoModelForCausalLM
  3. import sqlparse
  4. print("是否可用:", torch.cuda.is_available()) # 查看GPU是否可用
  5. print("GPU数量:", torch.cuda.device_count()) # 查看GPU数量
  6. print("torch方法查看CUDA版本:", torch.version.cuda) # torch方法查看CUDA版本
  7. print("GPU索引号:", torch.cuda.current_device()) # 查看GPU索引号
  8. print("GPU名称:", torch.cuda.get_device_name(0)) # 根据索引号得到GPU名称
  9. available_memory = torch.cuda.get_device_properties(0).total_memory
  10. print("GPU内存大小 :",available_memory)
  11. model_name = "llama-3-sqlcoder-8b"
  12. tokenizer = AutoTokenizer.from_pretrained(model_name)
  13. if available_memory > 20e9:
  14. # if you have atleast 20GB of GPU memory, run load the model in float16
  15. model = AutoModelForCausalLM.from_pretrained(
  16. model_name,
  17. trust_remote_code=True,
  18. torch_dtype=torch.float16,
  19. device_map="auto",
  20. use_cache=True,
  21. )
  22. else:
  23. # else, load in 4 bits – this is slower and less accurate
  24. model = AutoModelForCausalLM.from_pretrained(
  25. model_name,
  26. trust_remote_code=True,
  27. # torch_dtype=torch.float16,
  28. load_in_4bit=True,
  29. device_map="auto",
  30. use_cache=True,
  31. )
  32. prompt = """<|begin_of_text|><|start_header_id|>user<|end_header_id|>
  33. Generate a SQL query to answer this question: `{question}`
  34. DDL statements:
  35. CREATE TABLE products (
  36. product_id INTEGER PRIMARY KEY, -- Unique ID for each product
  37. name VARCHAR(50), -- Name of the product
  38. price DECIMAL(10,2), -- Price of each unit of the product
  39. quantity INTEGER -- Current quantity in stock
  40. );
  41. CREATE TABLE customers (
  42. customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer
  43. name VARCHAR(50), -- Name of the customer
  44. address VARCHAR(100) -- Mailing address of the customer
  45. );
  46. CREATE TABLE salespeople (
  47. salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson
  48. name VARCHAR(50), -- Name of the salesperson
  49. region VARCHAR(50) -- Geographic sales region
  50. );
  51. CREATE TABLE sales (
  52. sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale
  53. product_id INTEGER, -- ID of product sold
  54. customer_id INTEGER, -- ID of customer who made purchase
  55. salesperson_id INTEGER, -- ID of salesperson who made the sale
  56. sale_date DATE, -- Date the sale occurred
  57. quantity INTEGER -- Quantity of product sold
  58. );
  59. CREATE TABLE product_suppliers (
  60. supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier
  61. product_id INTEGER, -- Product ID supplied
  62. supply_price DECIMAL(10,2) -- Unit price charged by supplier
  63. );
  64. -- sales.product_id can be joined with products.product_id
  65. -- sales.customer_id can be joined with customers.customer_id
  66. -- sales.salesperson_id can be joined with salespeople.salesperson_id
  67. -- product_suppliers.product_id can be joined with products.product_id<|eot_id|><|start_header_id|>assistant<|end_header_id|>
  68. The following SQL query best answers the question `{question}`:
  69. ```sql
  70. """
  71. def generate_query(question):
  72. updated_prompt = prompt.format(question=question)
  73. inputs = tokenizer(updated_prompt, return_tensors="pt").to("cuda")
  74. generated_ids = model.generate(
  75. **inputs,
  76. num_return_sequences=1,
  77. eos_token_id=tokenizer.eos_token_id,
  78. pad_token_id=tokenizer.eos_token_id,
  79. max_new_tokens=400,
  80. do_sample=False,
  81. num_beams=1,
  82. temperature=0.0,
  83. top_p=1,
  84. )
  85. outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
  86. torch.cuda.empty_cache()
  87. torch.cuda.synchronize()
  88. # empty cache so that you do generate more results w/o memory crashing
  89. # particularly important on Colab – memory management is much more straightforward
  90. # when running on an inference service
  91. # return sqlparse.format(outputs[0].split("[SQL]")[-1], reindent=True)
  92. return outputs[0].split("```sql")[1].split(";")[0]
  93. question = "What was our revenue by product in the new york region last month?"
  94. generated_sql = generate_query(question)
  95. print(sqlparse.format(generated_sql, reindent=True))

运行过程

  1. D:\working\code> & C:/Python312/python.exe d:/working/code/sqlcode_v3.py
  2. 是否可用: True
  3. GPU数量: 1
  4. torch方法查看CUDA版本: 12.1
  5. GPU索引号: 0
  6. GPU名称: NVIDIA GeForce RTX 3060 Laptop GPU
  7. GPU内存大小 : 12884377600
  8. Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
  9. Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
  10. 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.
  11. Loading checkpoint shards: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:26<00:00, 6.59s/it]
  12. 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`.
  13. warnings.warn(
  14. 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
  15. will lead to slow inference or training speed.
  16. warnings.warn(
  17. 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.)
  18. attn_output = torch.nn.functional.scaled_dot_product_attention(
  19. SELECT p.name,
  20. SUM(s.quantity * p.price) AS total_revenue
  21. FROM products p
  22. JOIN sales s ON p.product_id = s.product_id
  23. JOIN salespeople sp ON s.salesperson_id = sp.salesperson_id
  24. WHERE sp.region = 'New York'
  25. AND s.sale_date >= CURRENT_DATE - INTERVAL '1 month'
  26. GROUP BY p.name

 

延伸场景

连接真实数据库

参照文档 Getting Started | Defog Docs

界面交互

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/1012410?site
推荐阅读
相关标签
  

闽ICP备14008679号