messages =[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"What is the tallest building in the world?"}]# Apply the chat template to the messages
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)# Generate the output using the pipeline
outputs = pipe(prompt, max_new_tokens=120, do_sample=True)# Print the generated textprint(outputs[0]["generated_text"])技术支持
我们得到了准确且详细的结果。
<|system|>
You are a helpful assistant.<|end|><|user|>
What is the tallest building in the world?<|end|><|assistant|>
As of my knowledge cutoff in2023, the tallest building in the world is the Burj Khalifa, located in Dubai, United Arab Emirates. It stands at a remarkable 828 meters (2,716.5 feet) tall once its antenna is included. Completed in January 2010, the Burj Khalifa marks a significant achievement in architecture and engineering, setting numerous records. It provides office space, luxury condominiums,and various leisure facilities. This landmark continues to技术支持
我们还可以基于说明与通话转写编写自定义提示,交给文本生成管道,以检查模型是否遵循用户指令。
prompt ="""
In a call center environment, classify customer interactions as 'Fraudulent' or 'Non-Fraudulent'.
Consider factors such as the nature of the inquiry, caller verification details, transaction history, and any red flags raised during the call.
[Lisa Adams, contacts the call center claiming unauthorized transactions on her credit card statement. She demands a full refund, asserting that she has never visited the merchant in question.] =
"""
outputs = pipe(prompt, max_new_tokens=120, do_sample=True)print(outputs[0]["generated_text"])技术支持
如您所见,模型表现不错,将该通话标记为欺诈并给出了理由。
In a call center environment, classify customer interactions as'Fraudulent'or'Non-Fraudulent'.
Consider factors such as the nature of the inquiry, caller verification details, transaction history,andany red flags raised during the call.[Lisa Adams, contacts the call center claiming unauthorized transactions on her credit card statement. She demands a full refund, asserting that she has never visited the merchant in question.]=
Call Interaction Classification: Fraudulent
Explanation:
The situation described by Lisa Adams indicates a potential case of credit card fraud. There are several red flags in this interaction that suggest the customer might be reporting unauthorized transactions:1. The caller claims unauthorized transactions - This is a common indicator of fraud, especially if the transactions were for places or services the customer did not recognize or didn't patronize according to their personal knowledge or documented transaction history (e.g., no visits to the技术支持
import wandb
from kaggle_secrets import UserSecretsClient
user_secrets = UserSecretsClient()
wb_token = user_secrets.get_secret("wandb")
wandb.login(key=wb_token)
run = wandb.init(
project='Fine-tune Phi-3.5-it on Ecommerce Text Classification',
job_type="training",
anonymous="allow"技术支持
最后,加载微调与评估过程中将用到的所有 Python 包与函数。
import numpy as np
import pandas as pd
import os
from tqdm import tqdm
import bitsandbytes as bnb
import torch
import torch.nn as nn
import transformers
from datasets import Dataset
from peft import LoraConfig, PeftConfig
from trl import SFTTrainer
from trl import setup_chat_format
from transformers import(AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
TrainingArguments,
pipeline,
logging)from sklearn.metrics import(accuracy_score,
classification_report,
confusion_matrix)from sklearn.model_selection import train_test_split技术支持
# Define the prompt generation functionsdefgenerate_prompt(data_point):returnf"""
Classify the E-commerce text into Electronics, Household, Books and Clothing.
text: {data_point["text"]}
label: {data_point["label"]}""".strip()defgenerate_test_prompt(data_point):returnf"""
Classify the E-commerce text into Electronics, Household, Books and Clothing.
text: {data_point["text"]}
label: """.strip()# Generate prompts for training and evaluation data
X_train.loc[:,'text']= X_train.apply(generate_prompt, axis=1)
X_eval.loc[:,'text']= X_eval.apply(generate_prompt, axis=1)# Generate test prompts and extract true labels
y_true = X_test.loc[:,'label']
X_test = pd.DataFrame(X_test.apply(generate_test_prompt, axis=1), columns=["text"])技术支持
output_dir="Phi-3.5-mini-instruct"
peft_config = LoraConfig(
lora_alpha=16,
lora_dropout=0,
r=64,
bias="none",
task_type="CAUSAL_LM",
target_modules=modules,)
training_arguments = TrainingArguments(
output_dir=output_dir,# directory to save and repository id
num_train_epochs=1,# number of training epochs
per_device_train_batch_size=1,# batch size per device during training
gradient_accumulation_steps=4,# number of steps before performing a backward/update pass
gradient_checkpointing=True,# use gradient checkpointing to save memory
optim="paged_adamw_8bit",
logging_steps=1,
learning_rate=2e-5,# learning rate, based on QLoRA paper
weight_decay=0.001,
fp16=False,
bf16=False,
max_grad_norm=0.3,# max gradient norm based on QLoRA paper
max_steps=-1,
warmup_ratio=0.03,# warmup ratio based on QLoRA paper
group_by_length=False,
lr_scheduler_type="cosine",# use cosine learning rate scheduler
report_to="wandb",# report metrics to w&b
eval_strategy="steps",# save checkpoint every epoch
eval_steps =0.2)
trainer = SFTTrainer(
model=model,
args=training_arguments,
train_dataset=train_data,
eval_dataset=eval_data,
peft_config=peft_config,
dataset_text_field="text",
tokenizer=tokenizer,
max_seq_length=512,
packing=False,
dataset_kwargs={"add_special_tokens":False,"append_concat_token":False,})技术支持
微软推出 Phi-3.5 系列,与 Meta AI 等共同角逐大语言模型赛道。该系列包含小型语言模型、视觉语言模型,并采用专家混合(Mixture-of-Experts)方法,带来一流表现。
在本教程中,我们将探索微软 Phi-3.5 模型族。我们将加载 Phi-3.5-mini-instruct 模型,并对其进行微调,使其能基于文本描述对电商产品进行分类。最后,我们将演示如何将 LoRA(低秩适配)与基础模型合并并推送至 Hugging Face,以便高效地在云端部署,使模型可用于各种应用场景。
选修《精通大语言模型(LLMs)概念》课程,了解 LLM 的应用、训练方法、伦理考量及最新研究。
图片作者自制
认识 Microsoft Phi-3.5
此次Microsoft Phi-3.5 发布带来了三款创新模型:Phi-3.5-mini、Phi-3.5-vision,以及最新的 Phi-3.5-MoE(专家混合模型)。
Phi-3.5-mini 针对多语言支持进行了优化,具备令人印象深刻的 128K 上下文长度。尽管体量更小,但凭借监督微调、近端策略优化和直接偏好优化等严格强化,性能可与更大模型比肩,且能严格遵循指令。
Phi-3.5-vision 是一款前沿、轻量的多模态模型,使用合成数据与筛选的公共网站数据进行训练。它擅长多帧图像理解与推理,适用于细致的图像对比、多图总结/讲述与视频摘要,应用前景广阔。
其中的亮点模型 Phi-3.5-MoE 采用包含 16 个专家、66 亿激活参数的专家混合架构,在保证强大安全性的同时,提供低时延与卓越性能,并具备完善的多语言支持。
Phi-3.5 系列为开源社区提供了高性价比且高性能的解决方案,推动小型语言模型与生成式 AI 的发展。
如需了解 Phi-3 的架构、特性与应用,请参阅《Phi-3 教程:上手微软最小 AI 模型》指南。
访问 Microsoft Phi-3.5 模型
本节我们将在 Kaggle 平台加载 Phi-3.5-mini-instruct 模型并运行推理。
pip命令安装所有必需的 Python 包。text-generation管道。我们得到了准确且详细的结果。
如您所见,模型表现不错,将该通话标记为欺诈并给出了理由。
如果您在 Kaggle 平台运行模型时遇到问题,请参考《Phi-3.5 简单模型推理》Kaggle 笔记本。它包含预构建的环境与代码及其输出。
微调 Microsoft Phi-3.5-mini-instruct
本指南将学习如何加载并处理电商文本分类数据;加载模型与分词器;在微调前于测试集上评估模型;构建训练器;在训练集上微调模型;并在微调后进行测试。
如果您对流程还不熟悉,请先阅读我们的《LLM 微调入门指南》,了解微调背后的理论。
1. 环境设置
启动新的 Kaggle 笔记本并启用 GPU 加速。然后,使用 Kaggle secrets 将 Hugging Face 与 Weights & Biases 的 API token 设置为环境变量。
安装所有必需的 Python 包。
登录 Weights & Biases 服务,签署 API key,并新建项目。
最后,加载微调与评估过程中将用到的所有 Python 包与函数。
2. 加载与处理数据集
将E-commerce Text Classification 数据集添加到您的笔记本,如下所示。该数据集包含两列:标签(电商类别)与产品的文本描述。
加载 CSV 文件、进行处理,并查看前 5 行。
数据集由产品描述与类别标签构成。
将数据集打乱并仅选择前 2000 行。本指南以示例为主,通过在有限样本上微调加快流程。
接下来,我们将数据划分为训练集、验证集与测试集。
我们将创建两个函数。
generate_prompt会将文本列转换为提示,包含指令、文本描述与标签;generate_test_prompt与之相同,但不包含标签。将 pandas 的训练与验证 DataFrame 转换为 Hugging Face 数据集。
文本包含系统指令、产品描述与标签。
3. 加载模型与分词器
使用仓库 ID 从 Hugging Face Hub 加载 4-bit 量化模型与分词器。随后完成必要设置,准备就绪。
4. 微调前的模型评估
在微调前需要评估基础模型,以判断微调是否带来提升。为此,我们将创建
predict函数,接收测试数据集,并基于产品文本描述生成电商类别。我们已得到预测类别列表,接下来将其与真实类别对比,生成模型评估报告。该 “evaluate” 函数接收预测与真实类别列表,输出详细评估,包括平均准确率、各类别单独准确率、分类报告与混淆矩阵。
我们获得了 65% 的平均准确率。看看微调是否能进一步提升。
5. 模型设置
从模型中提取线性模块名称。
使用线性模块名称来创建 LoRA。我们仅微调 LoRA,保留其余模型参数不变,以节省内存并加快训练。
接着,为 Kaggle 环境配置模型超参数。您可根据机器配置调整这些参数,以提升准确率或缩短训练时间。关于各超参数的说明,请参阅《微调 Llama 2》教程。
现在设置监督微调(SFT)训练器,提供训练与验证数据集、LoRA 配置、训练参数、分词器与模型。
6. 模型训练
使用
.train函数启动微调过程。损失在逐步下降,如果增加训练轮次,结果可能更好。
结束 Weights & Biases 运行以生成评估报告。
您可以访问 Weights & Biases 网站,选择您的项目并查看训练分析,以评估模型表现。
将模型与分词器保存到本地,便于后续合并并推送到远端。
本指南代码量较多。如果您想要更简便的方案,可按照《LlaMA-Factory WebUI 新手指南:微调 LLM》微调一款开源模型。若希望进一步简化且无需操心硬件,我们强烈推荐《微调 GPT-4o Mini:分步指南》,可在云端以最少代码完成微调。
7. 微调后的模型测试
测试微调后模型的性能是否提升。我们先生成预测标签列表,并与真实标签一起传入
evaluate函数。模型准确率提升约 32.31%,其余性能指标同样出色。除 Books 类别外,其余类别的识别均相当准确。
请务必通过右上角的 “Save Version” 按钮保存 Kaggle 笔记本。选择快速保存选项,并将保存输出设置为包含模型文件与全部代码。
如果您在微调过程中遇到问题,请参考《Fine-tune Phi-3.5 for Text Classification》Kaggle 笔记本。
合并并导出微调后的模型
要将模型合并并导出至 Hugging Face,我们将先创建一个新的 Kaggle 笔记本,并添加已保存的笔记本,以访问微调后的模型与分词器。
添加另一个 Kaggle 笔记本的方式与添加数据集或模型类似。点击 “+ Add Input” 按钮,粘贴笔记本链接,然后点击添加。
使用 Kaggle secrets 将 Hugging Face API 设置为环境变量,并安装加载与合并模型所需的全部 Python 包。
设置基础模型与微调后模型变量,分别为模型 ID 与适配器的位置。
从 Hugging Face hub 加载完整基础模型与分词器。
仅用两行代码即可将基础模型与适配器合并。
为测试是否合并成功,我们将使用合并后的模型与分词器创建文本生成管道,并传入示例提示以生成响应。
模型准确预测了该产品的类别。
我们将提供目录名,将完整模型保存到本地。
将已保存的模型推送到 Hugging Face hub。首先使用从 Kaggle secrets 中获取的 API key 登录 Hugging Face CLI,然后使用
push_to_hub将模型与分词器一并推送。已创建新的模型仓库,并将所有文件推送至 Hugging Face 模型库。
来源:kingabzpro/Phi-3.5-mini-instruct-Ecommerce-Text-Classification
若您在合并与导出模型时遇到问题,请查看《将 Phi-3.5 微调适配器合并到完整模型》Kaggle 笔记本。
另外,欢迎查看我们的最新教程《微调 Llama 3.2 并本地使用:分步指南》。在该教程中,您将学习如何使用免费 GPU 在自定义数据集上微调模型,将模型合并并导出至 Hugging Face Hub,并转换为 GGUF 格式,以便配合 Jan 应用在本地使用。
总结
大语言模型正变得更小更高效,从而降低运营成本,并在各领域具备更强适应性。在本教程中,我们探索了 Phi-3.5 Mini、Vision 与 MoE 模型,并学习了如何通过 Kaggle 笔记本访问 Phi-3.5 Mini 模型。
随后我们在分类数据上对模型进行微调并评估表现,将准确率从 65% 大幅提升至 86%——这一成果相当可观。仅靠 RAG 或函数调用无法达到这样的效果。
最后,我们将 LoRA 与基础模型集成,并将完整模型导出至 Hugging Face Hub,供所有人使用。
下一步,按照这份《12 个适合各水平的 LLM 项目》清单动手搭建项目。内容覆盖初学者、中级学习者、毕业年级与专家级别。每个项目都配有源码、可视化指南与参考链接。