13 篇文章带有标签 “text2sql”

LLaMA-Factory 微调 Text2SQL

安装 LLaMA-Factory

git clone https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory

python -m venv env
source env/bin/activate

pip install -e .[metrics]

下载模型

git clone https://www.modelscope.cn/qwen/Qwen1.5-4B-Chat.git

自定义数据集

data/text2sql.json [ { "instruction": "You are an SQLite database expert. Help users write SQL statements based on the following table schema.

Gradio DataFrame

Gradio DataFrame

import pandas as pd
import gradio as gr

def read_csv_from_text2sql(file_path="data/text2sql.csv"):
    try:
        df = pd.read_csv(file_path)
        return df
    except Exception as e:
        return pd.DataFrame([{"error": f"❌ {e}"}])

def selected_text2sql_dataframe(selected_index: gr.SelectData, df: gr.DataFrame):
    selected_row = df.iloc[selected_index.index[0]]
    text = selected_row.get('Text', '')
    sql = selected_row.get('SQL', '')
    return text, sql

with gr.Blocks() as demo:
    # UI
    upload_button = gr.UploadButton(label="上传 Text2SQL CSV 文件", 
                                    file_types = ['.csv'], 
                                    file_count = "single")
    df_text2sql = gr.Dataframe(headers=["Text", "SQL"], 
                                type="pandas", 
                                col_count=2, 
                                value=read_csv_from_text2sql,
                                interactive=False)
    with gr.Row():
        textbox_text = gr.Textbox(label="Text", lines=4)
        textbox_sql = gr.Textbox(label="SQL", lines=4)

    # Event
    upload_button.upload(fn=read_csv_from_text2sql, 
                            inputs=upload_button, 
                            outputs=df_text2sql)
    df_text2sql.select(fn=selected_text2sql_dataframe,
                        inputs=df_text2sql,
                        outputs=[textbox_text, textbox_sql])

demo.queue(api_open=False)
demo.launch(max_threads=30)

LangChain : SQL Chain & SQL Agent

SQL Chain

from datetime import datetime
from operator import itemgetter

from langchain.chains import create_sql_query_chain

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.runnables import RunnableLambda

from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_community.utilities import SQLDatabase
from langchain_community.tools.sql_database.tool import QuerySQLDataBaseTool


// ...

LangChain Chat Models Function & Tool Calling

Chat Models Functions & Tools

Model Function Calling Tool Calling Python Package
ChatOpenAI langchain-openai
ChatTongyi langchain-community
ChatOllama langchain-community
OllamaFunctions langchain-experimental

自定义工具

在构建自己的代理时,您需要为其提供一个工具列表,供其使用。除了实际调用的函数之外,工具还包括几个组件:

  • name (str):是必需的,并且在提供给代理的一组工具中必须是唯一的。
  • description (str):可选,但建议提供,因为代理使用它来确定工具的使用。
  • args_schema (Pydantic BaseModel):可选,但建议提供,可用于提供更多信息(例如,少量示例)或对预期参数进行验证。

定义 Function

LangChain Text2SQL Agent

OpenAI Function Call (Extraction)

这个方法只有 OpenAI 的模型支持。

from langchain.agents import tool
from langchain.chat_models import ChatOpenAI
from langchain.tools.render import format_tool_to_openai_function

from langchain.pydantic_v1 import BaseModel, Field
from enum import Enum


# 省份、直辖市
class ProvinceEnum(str, Enum):
    山东省 = "山东省"
    # 其它省份


# 山东省地级市
// ...

OpenAI gpt-3.

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(七):MLX 微调的模型转换为 GGUF 模型

将 MLX 微调的模型转换为 GGUF 模型最大的意义是可以融入 GGUF 的生态系统,可以在更多的平台上使用。

LoRA 微调

大模型 Mistral-7B-v0.1

数据集 WikiSQL

修改脚本 mlx-examples/lora/data/wikisql.py

if __name__ == "__main__":
    # ......
    for dataset, name, size in datasets:
        with open(f"data/{name}.jsonl", "w") as fid:
            for e, t in zip(range(size), dataset):
                t = t[3:]
                json.dump({"text": t}, fid)
                fid.write("\n")

执行脚本 data/wikisql.py 生成数据集。

data/wikisql.py

安装 mlx-lm

pip install mlx-lm

微调

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(六):使用 LoRA 基于 Deepseek-Coder-7B 微调

大模型 Deepseek-Coder-7B

数据集 WikiSQL

修改脚本 mlx-examples/lora/data/wikisql.py if name == "main": # ...... for dataset, name, size in datasets: with open(f"data/{name}.jsonl", "w") as fid: for e, t in zip(range(size), dataset): # deepseek-ai/deepseek-coder-7b-instruct-v1.5 # 去掉开头的 <|begin▁of▁sentence|>,因为 tokenizer 会自动添加 <|begin▁of▁sentence|> t = t[3:-4] + "<|end▁of▁sentence|>" json.dump({"text": t}, fid) fid.

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(五):对比使用 LoRA 和 QLoRA 基于 Mistral-7B 微调的效果

使用 LoRA 和 QLoRA 基于 Mistral-7B 微调的实验

LoRA 和 QLoRA 对比

微调

Iteration LoRA Train Loss LoRA Val Loss LoRA Tokens/sec QLoRA Train Loss QLoRA Val Loss QLoRA Tokens/sec
1 2.343 2.420
100 1.204 221.348 1.216 166.377
200 1.091 1.111 207.353 1.095 1.130 187.795
300 0.818 234.182 1.065 194.826
400 0.837 1.076 207.763 0.998 1.006 170.072
500 0.774 223.036 0.726 189.288
600 0.609 1.001 218.118 0.607 1.015 186.397

微调的参数量 LoRA 微调万分之 2.35 (1.704M / 7243.436M * 10000)的模型参数。 QLoRA 微调万分之 13.

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(四):使用 QLoRA 基于 Mistral-7B 微调

预训练模型 mistralai/Mistral-7B-v0.1

量化

QLoRA 微调需要量化,生成 4 位量化的 Mistral 7B 并默认将其存储在 mlx_model 目录中

python convert.py --hf-path mistralai/Mistral-7B-v0.1 -q

mlx_model 目录结构如下:

mlx_model
├── config.json
├── model.safetensors
├── special_tokens_map.json
├── tokenizer.json
├── tokenizer.model
├── tokenizer_config.json
└── weights.00.safetensors

量化后的模型 8.0G

微调

QLoRA 微调

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(三):分享微调后的模型到 HuggingFace Hub

mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL

安装 mlx-lm

pip install mlx-lm

生成 SQL

python -m mlx_lm.generate --model mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL \
                          --max-tokens 50 \
                          --prompt "table: students
columns: Name, Age, School, Grade, Height, Weight
Q: Which school did Wang Junjian come from?
A: "
SELECT School FROM Students WHERE Name = 'Wang Junjian'

上传模型到 HuggingFace Hub

  1. 加入 MLX Community 组织
  1. 在 MLX Community 组织中创建一个新的模型 mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL
  1. 克隆仓库 mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(二):使用 LoRA 基于 Mistral-7B 微调

mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL

本次微调的模型我已经上传到了 HuggingFace Hub 上,大家可以进行尝试。

安装 mlx-lm

pip install mlx-lm

生成 SQL

python -m mlx_lm.generate --model mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL \
                          --max-tokens 50 \
                          --prompt "table: students
columns: Name, Age, School, Grade, Height, Weight
Q: Which school did Wang Junjian come from?
A: "
SELECT School FROM Students WHERE Name = 'Wang Junjian'

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(一):使用 LoRA 基于 Mistral-7B 微调

📌 没有使用模型的标注格式生成数据集,导致不能结束,直到生成最大的 Tokens 数量。

这次我们来解决这个问题。

数据集 WikiSQL

修改脚本 mlx-examples/lora/data/w

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(一):使用 LoRA 基于 Mistral-7B 微调

安装

git clone https://github.com/ml-explore/mlx-examples.git
cd mlx-examples/lora

pip install -r requirements.txt

下载模型

pip install huggingface_hub hf_transfer

export HF_HUB_ENABLE_HF_TRANSFER=1
huggingface-cli download \
    --local-dir-use-symlinks False \
    --local-dir mistralai/Mistral-7B-v0.1 \
    mistralai/Mistral-7B-v0.1

数据集 WikiSQL

样本格式