19 篇文章带有标签 “Text2SQL”

使用大型语言模型微调命名实体识别

定义了一套电力领域的命名实体类型:

  • Province: 省份。例如:山东省。
  • City: 城市。例如:济南市、济南。
  • Company: 供电公司。例如:长清区供电公司、市中供电中心。
  • Substation: 供电所。例如:崮山供电所。
  • Indicator: 指标。例如:投诉、意见。
  • Date: 日期。例如:今天、昨天、今年、去年、本周、上周、本月、上月、3月、本季度、上季度、一季度、今年第一季度、2022年、2024年5月。

对用户的输入进行命名实体识别标注,输出的结果应该包含所有的电力领域实体类型的实例。

  • 山东省菏泽巨野县供电公司麒麟供电所投诉数量

<Province>山东省</Province><City>菏泽</City><Company>巨野县供电公司</Company><Substation>麒麟供电所</Substation><Indicator>投诉</Indicator>数量

  • 菏泽巨野县供电公司麒麟供电所投诉数量

<City>菏泽</City><Company>巨野县供电公司</Company><Substation>麒麟供电所</Substation><Indicator&gt

使用大型语言模型微调命名实体识别生成

定义了一套电力领域的命名实体类型:

  • Province: 省份。例如:山东省。
  • City: 城市。例如:济南市、济南。
  • Company: 供电公司。例如:长清区供电公司、市中供电中心。
  • Substation: 供电所。例如:崮山供电所。
  • Indicator: 指标。例如:投诉、意见。
  • Date: 日期。例如:今天、昨天、今年、去年、本周、上周、本月、上月、3月、本季度、上季度、一季度、今年第一季度、2022年、2024年5月。

理想的情况下的输入可能是这样的:{DATE}山东省菏泽巨野县供电公司麒麟供电所投诉数量

但是用户的输入是多种多样的:

  • 山东省菏泽巨野县供电公司麒麟供电所投诉数量
  • 菏泽巨野县供电公司麒麟供电所投诉数量
  • 菏泽巨野投诉数量
  • 菏泽麒麟投诉数量
  • 巨野县供电公司麒麟供电所投诉数量
  • 巨野麒麟投诉数量
  • 巨野投诉数量
  • 麒麟供电所投诉数量
  • 麒麟投诉数量

通过微调后的模型可以生成如下实体标注: <Date>本月</Date><Province>山东省</Province><City>菏泽</City><Company>巨野县供电公司</Company><Substation>麒麟供电所</Substation><Indicator>投诉</Indic

使用大型语言模型进行命名实体识别

question = "山东省济南高新供电中心投诉总数"
<Province>山东省</Province><City>济南市</City><Company>高新供电中心</Company><Indicator>投诉</Indicator>总数
  • 济南增加了
question = "山东省济南市平阴县供电公司投诉总数"
<Province>山东省</Province><City>济南市</City><Company>平阴县供电公司</Company><Indicator>投诉</Indicator>总数
question = "济南市平阴县供电公司投诉总数"
<City>济南市</City><Company>平阴县供电公司</Company><Indicator>投诉</Indicator>总数

LLaMA-Factory 微调 Text2SQL

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
CUDA_VISIBLE_DEVICES=0 llamafactory-cli train text2sql/qwen1.5-4b-chat_lora_sft.yaml
CUDA_VISIBLE_DEVICES=0 llamafactory-cli chat text2sql/qwen1.5-4b-chat_lora_sft-inference.yaml
CUDA_VISIBLE_DEVICES=0 llamafactory-cli export text2sql/qwen1.5-4b-chat_lora_sft-merge.yaml 
CUDA_VISIBLE_DEVICES=0 llamafactory-cli chat text2sql/text2sql-inference.yaml

支持两种格式的数据集:alpaca 和 sharegpt ,这里使用的是 alp

ChatTongyi

from langchain_core.messages import HumanMessage
from langchain_community.chat_models.tongyi import ChatTongyi


model = ChatTongyi(model="qwen-turbo", top_p=0.01)
gen = model.stream([HumanMessage(content="你是谁")])

for response in gen:
    print("🤖", response)
🤖 content='我是' id='run-57fca077-5e62-4cd5-ba25-c71b65049604'
🤖 content='通' id='run-57fca077-5e62-4cd5-ba25-c71b65049604'
🤖 content='义' id='run-57fca077-5e62-4cd5-ba25-c71b65049604'
🤖 content='千问,由阿里' id='run-57fca077-5e62-4cd5-ba25-c71b65049604'
// ...

Gradio Chatbot

import os
import pandas as pd
import gradio as gr
from http import HTTPStatus
from dashscope import Generation
from dashscope.api_entities.dashscope_response import Role
from typing import List, Optional, Tuple, Dict, Generator
from urllib.error import HTTPError


DEFAULT_SYSTEM = '您是一个有用的助手。'

History = List[Tuple[str, str]]
Messages = List[Dict[str, str]]

// ...

LangChain : SQL Chain & SQL Agent

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 : Tagging and Extraction Using OpenAI functions

from enum import Enum
from typing import Optional, Type
from langchain.pydantic_v1 import BaseModel, Field


class ProvinceEnum(str, Enum):
    """省、直辖市、自治区"""
    山东省 = "山东省"

class CityEnum(str, Enum):
    """山东省地级市"""
    济南 = "济南"
    青岛 = "青岛"
    淄博 = "淄博"
    枣庄 = "枣庄"
// ...
from langchain_openai import ChatOpenAI

model = ChatOpenAI(temperature=0).bind(
    functions=functions,
    function_call={"name": PowerSupplyStationLocation.__name__}
)

response = model.invoke(prompt)
print(response)

LangChain Chat Models Function & Tool Calling

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):可选,但建议提供,可用于提供更多信息(例如,少量示例)或对预期参数进行验证。

LangChain Text2SQL Agent

这个方法只有 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):
    山东省 = "山东省"
    # 其它省份


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

mistral:latest content=" 由于我不能直接获得江苏省或山东省 Specifically, Shandong Province's Jining City's Longching District Power Company's (在山东省济南市长清区位置) 实际情况和最新信息,因此无法提供具体的意见合计。但是,以下是一些可能影响供电公司业务发展和经营效益的方面: 1. 能源政策:国内外能源政策变化对供电公司有重大影响,例如新能源汽车普及、煤气价格波动等。 2.

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(八):使用 LoRA 基于 TinyLlama 微调

  • 输入
<|system|>
You are a chatbot who can help code!</s>
<|user|>
Write me a function to calculate the first 10 digits of the fibonacci sequence in Python and print it out to the CLI.</s>
<|assistant|>
  • 输出
[
  {
    "generated_text": "<|system|>\nYou are a chatbot who can help code!</s>\n<|user|>\nWrite me a function to calculate the first 10 digits of the fibonacci sequence in Python and print it out to the CLI.</s>\n<|assistant|>\nHere's a Python function that calculates the first 10 digits of the Fib"
  }
]
  • 生成

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

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

修改脚本 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
pip install mlx-lm

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

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

计算测试集困惑度(PPL)和交叉熵损失(Loss)。

Iteration LoRA Test Loss LoRA Test PPL QLoRA Test Loss QLoRA Test PPL
600 1.351 3.863 1.396 4.040
Prompt: table: students
columns: Name, Age, School, Grade, Height, Weight
Q: What is Wang Junjian's name?
A: 

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

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

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

pip install mlx-lm
  1. 加入 MLX Community 组织
  1. 在 MLX Community 组织中创建一个新的模型 mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL
  1. 克隆仓库 mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL
git clone https://huggingface.co/mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL
  1. 将生成的模型文件(lora_fused_model 目录下的所有文件)复制到仓库目录下
  1. 上传模型到 HuggingFace Hub
git add .
git commit -m "Fine tuning Text2SQL based on Mistral-7B using LoRA on MLX" 
git push
  1. 不能 push

错误信息:

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

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

pip install mlx-lm

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

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

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

table: 1-10753917-1
columns: Season, Driver, Team, Engine, Poles, Wins, Podiums, Points, Margin of defeat
Q: Which podiums did the alfa romeo team have?
A: SELECT Podiums FROM 1-10753917-1 WHERE Team = 'Alfa Romeo'</s>

在 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