目标:利用罗胖60秒语音数据,构造问答对,进行有监督微调(SFT),实现AI罗胖的问答,发布到浦源平台
1、构造训练数据
用标题中的问题作为query,语音内容作为response,构造训练集。
2、微调大模型
使用swift微调框架对qwen-7b-chat大模型进行lora微调,参考脚本:
swift sft \
--model_type qwen-7b-chat \
--custom_train_dataset_path ./train_data.json \
--train_dataset_sample -1 \
--logging_steps 5 \
--max_length 2048 \
--warmup_ratio 0.4 \
--output_dir output \
--use_flash_attn True \
3、模型推理
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import (
get_model_tokenizer, get_template, inference, ModelType, get_default_template_type
)
from swift.tuners import Swift
from modelscope import snapshot_download
ckpt_dir = 'output/qwen-7b-chat/v0-20240410-224450/checkpoint-61'
# ckpt_dir = snapshot_download('andytl/news_assistant')
model_type = ModelType.qwen_7b_chat
template_type = get_default_template_type(model_type)
model, tokenizer = get_model_tokenizer(model_type, model_kwargs={'device_map': 'auto'})
model = Swift.from_pretrained(model, ckpt_dir, inference_mode=True)
template = get_template(template_type, tokenizer)
query = ' 什么是快乐星球?'
response, history = inference(model, template, query)
# print(f'response: {response}')
print(f'history: {history}')
[(' 什么是快乐星球?', '1.\xa0你真的需要读这本书吗?\n2.\xa0最近的社交网络上,一位85后摄影师李穆柯写了一本书,名字叫《快乐星球》,这本书告诉我们,那些我们通常认为是偶然的东西,其实都有背后的逻辑和规则。\n3.\xa0比如说,你知道冰激凌为什么要用铁棍儿夹着,或者车为什么会设两个门吗?又比如,为什么过年的时候要拜年、送礼呢?答案就藏在这本书里头。\n4.\xa0李穆柯老师还有一个观察世界的法门。他说,生活的各个领域里都有天才,你要相信一个基本道理:一切伟大的发明都是天才想出来的,但是天才却经常是我们普通人发现的。\n')]
评论