当前位置:   article > 正文

Qwen部署、微调_qwen 流式

qwen 流式

部署

Web UI的部署如下:

pip install -r requirements.txt
pip install -r requirements_web_demo.txt
python web_demo.py
  • 1
  • 2
  • 3

命令行的部署如下:

pip install -r requirements.txt
python cli_demo.py
  • 1
  • 2

阿里通义千问完整技术报告:https://zhuanlan.zhihu.com/p/685633623

Lora微调

先用lora微调,保存lora微调后的额外参数

bash finetune/finetune_ds.sh
  • 1

运行下列代码获得原参数+微调参数的完整模型

# lora微调后的模型与原模型混合
from peft import AutoPeftModelForCausalLM

path_to_adapter = "model/T_LoRa_model_14B_10E_30"  # lora参数的保存地址
new_model_directory = "./model/T_LoRa_14B_10E_30_merged"  # 完整模型的保存地址
model = AutoPeftModelForCausalLM.from_pretrained(
    path_to_adapter, # path to the output directory
    device_map="auto",
    trust_remote_code=True
).eval()

merged_model = model.merge_and_unload()
# max_shard_size and safe serialization are not necessary.
# They respectively work for sharding checkpoint and save the model to safetensors
merged_model.save_pretrained(new_model_directory, max_shard_size="2048MB", safe_serialization=True)


from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(
    path_to_adapter, # path to the output directory
    trust_remote_code=True
)
tokenizer.save_pretrained(new_model_directory)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

微调数据量与微调效果

  • 问题
    1.你是谁
    2.你叫什么名字
    3.你的名字是通义千问对吗
    4.你的名字是小义对吗
    5.你会如何介绍自己

10Epoch 200条数据

训练时长(funtune.py->merge.py->web_demo.py):50min
在这里插入图片描述

10Epoch 100条数据

训练时长(funtune.py->merge.py->web_demo.py):45min
merge.py加载模型:14:03-14:10
web_demo.py加载模型:14:12-14:21
在这里插入图片描述

10Epoch 50条数据

训练时长(funtune.py->merge.py->web_demo.py):45min
在这里插入图片描述

10Epoch 25条数据

训练时长(funtune.py->merge.py->web_demo.py): 40min
(无法正确回答数据集)
在这里插入图片描述

10Epoch 40条数据

训练时长(funtune.py->merge.py->web_demo.py): 40min
第一次问:
(你的名字叫通义千问对吗?是的)
在这里插入图片描述
第二次问:
在这里插入图片描述

10Epoch 30条数据

训练时长(funtune.py->merge.py->web_demo.py): 40min
在这里插入图片描述

总结

数据集大小为40时,开始时而回答“小义”,时而回答“通义千问”
小于40时,回答“通义千问”

Qwen的API部署

执行下述命令

python openai_api.py
  • 1

默认的Ip地址是127.0.0.1端口是8000
使用API示例

import openai
# 使用本地部署的qwen,运行本文件前需要先运行openai_api.py
openai.api_base = "http://localhost:8000/v1"
openai.api_key = "none"

# 使用流式回复的请求
for chunk in openai.ChatCompletion.create(
    model="Qwen",
    messages=[
        {"role": "user", "content": "西红柿炒鸡蛋怎么做"}
    ],
    stream=True
    # 流式输出的自定义stopwords功能尚未支持,正在开发中
):
    if hasattr(chunk.choices[0].delta, "content"):
        print(chunk.choices[0].delta.content, end="", flush=True)

# 不使用流式回复的请求
response = openai.ChatCompletion.create(
    model="Qwen",
    messages=[
        {"role": "user", "content": "你好"}
    ],
    stream=False,
    stop=[] # 在此处添加自定义的stop words 例如ReAct prompting时需要增加: stop=["Observation:"]。
)
print(response.choices[0].message.content)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/870767
推荐阅读
相关标签
  

闽ICP备14008679号