通义千问部署搭建

文章目录

  • 一、部署1
    • [1.1 打开通义千问-7B-预训练-模型库-选择资源](#1.1 打开通义千问-7B-预训练-模型库-选择资源)
    • [1.2 使用Netbook](#1.2 使用Netbook)
    • [2.1 运行](#2.1 运行)
    • [2.2 复制脚本](#2.2 复制脚本)
      • [2.2.1 问题1 :ImportError: This modeling file requires the following packages that were not found in your environment: transformers_stream_generator. Run `pip install transformers_stream_generator`](#2.2.1 问题1 :ImportError: This modeling file requires the following packages that were not found in your environment: transformers_stream_generator. Run pip install transformers_stream_generator)
    • [2.3 查看结果](#2.3 查看结果)
    • 3.其他作者的demo
    • [3.1 克隆代码](#3.1 克隆代码)
    • [3.2 加载依赖](#3.2 加载依赖)
    • [3.3 运行代码](#3.3 运行代码)
  • 二、部署2
    • [1.1 启动服务器](#1.1 启动服务器)
    • [1.2 使用官方提供的demo](#1.2 使用官方提供的demo)
    • [1.2 为了拉去大模型,更新LFS](#1.2 为了拉去大模型,更新LFS)
    • [2.1 拉千问模型文件](#2.1 拉千问模型文件)
    • [3.1 修改web.demo.py地址](#3.1 修改web.demo.py地址)
    • [4 运行](#4 运行)
    • [5 成功](#5 成功)
    • 5开启量化

部署参考视频

通义千问-7B-预训练-模型库

一、部署1

1.1 打开通义千问-7B-预训练-模型库-选择资源



1.2 使用Netbook

弹出新页面

2.1 运行

2.2 复制脚本

python 复制代码
from modelscope import AutoModelForCausalLM, AutoTokenizer
from modelscope import GenerationConfig
import datetime
print("启动时间:" + str(datetime.datetime.now()))
tokenizer = AutoTokenizer.from_pretrained("qwen/Qwen-7B-Chat", revision = 'v1.0.5',trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-7B-Chat", revision = 'v1.0.5',device_map="auto",offload_folder="offload_folder", trust_remote_code=True,fp16 = True).eval()
model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat",revision = 'v1.0.5', trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参
model.float()

print("开始执行:" + str(datetime.datetime.now()))
response, history = model.chat(tokenizer, "你好", history=None)
print(response)
print("第一个问题处理完毕:" + str(datetime.datetime.now()))
response, history = model.chat(tokenizer, "浙江的省会在哪里?", history=history) 
print(response)
print("第二个问题处理完毕:" + str(datetime.datetime.now()))
response, history = model.chat(tokenizer, "它有什么好玩的景点", history=history)
print(response)
print("第三个问题处理完毕:" + str(datetime.datetime.now()))


2.2.1 问题1 :ImportError: This modeling file requires the following packages that were not found in your environment: transformers_stream_generator. Run pip install transformers_stream_generator

解决方法
pip install transformers_stream_generator



这就好了,重新运行下

ValueError: The current device_map had weights offloaded to the disk. Please provide an offload_folder for them. Alternatively, make sure you have safetensors installed if the model you are using offers the weights in this format.

参照这哥们的

https://zhuanlan.zhihu.com/p/649272911

2.3 查看结果

3.其他作者的demo

3.1 克隆代码

git clone https://gitee.com/JokerBao/Qwen-7B-FastWeb.git

3.2 加载依赖

bash 复制代码
pip install -r requirements.txt

3.3 运行代码

bash 复制代码
python Qwen_demo.py




二、部署2

1.1 启动服务器

1.2 使用官方提供的demo

拉代码

bash 复制代码
git clone https://github.com/QwenLM/Qwen-7B.git

1.2 为了拉去大模型,更新LFS

bash 复制代码
apt-get update


bash 复制代码
apt-get install git-lfs

2.1 拉千问模型文件

bash 复制代码
git clone https://www.modelscope.cn/qwen/Qwen-7B-Chat.git

初始化一下

2.1.1模型下载成功

2.1.2 安装依赖

为了方便我把模型移动到一开始的文件夹里面

bash 复制代码
pip install -r requirements.txt

也可以使用web依赖

bash 复制代码
pip install -r requirements_web_demo.txt

3.1 修改web.demo.py地址

然后ctrl+s 保存

4 运行

bash 复制代码
 python web_demo.py 

5 成功

5开启量化

bash 复制代码
pip install bitsandbytes

添加依赖

bash 复制代码
from transformers import BitsAndBytesConfig
import torch

添加

bash 复制代码
quantization_config = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_quant_type='nf4',
            bnb_4bit_compute_dtype=torch.bfloat16)


相关推荐
IT古董31 分钟前
第四章:大模型(LLM)】06.langchain原理-(3)LangChain Prompt 用法
java·人工智能·python
fantasy_arch5 小时前
pytorch例子计算两张图相似度
人工智能·pytorch·python
WBluuue7 小时前
数学建模:智能优化算法
python·机器学习·数学建模·爬山算法·启发式算法·聚类·模拟退火算法
赴3357 小时前
矿物分类案列 (一)六种方法对数据的填充
人工智能·python·机器学习·分类·数据挖掘·sklearn·矿物分类
大模型真好玩7 小时前
一文深度解析OpenAI近期发布系列大模型:意欲一统大模型江湖?
人工智能·python·mcp
RPA+AI十二工作室7 小时前
亚马逊店铺绩效巡检_影刀RPA源码解读
chrome·python·rpa·影刀
小艳加油8 小时前
Python机器学习与深度学习;Transformer模型/注意力机制/目标检测/语义分割/图神经网络/强化学习/生成式模型/自监督学习/物理信息神经网络等
python·深度学习·机器学习·transformer
学行库小秘10 小时前
ANN神经网络回归预测模型
人工智能·python·深度学习·神经网络·算法·机器学习·回归
Yn31210 小时前
在 Python 中使用 json 模块的完整指南
开发语言·python·json
秋难降10 小时前
线段树的深度解析(最长递增子序列类解题步骤)
数据结构·python·算法