【AI】本地大模型部署(低配CPU可实现)

写在前面

  • 部署过程的简单记录!
  • 本次部署主要基于:大模型的本机部署_本地部署大模型-CSDN博客,以下简称这篇文章为【博客】,方便应用
  • 主要部署步骤参考【博客】,此处仅记录部分需改动内容
  • 大致环境:
    • Windows
    • 无N卡,CPU部署
    • 内存大致消耗:5G±

环境准备

创建和激活conda环境

  • 步骤同博客的==2 环境配置== 不过我选的python版本是3.10
1
2
3
conda create -n llm-chat python=3.10

conda activate llm-chat

安装pytorch

  • 这里我选的是CPU对应安装方式
1
pip install torch torchvision torchaudio
  • 安装完pytorch就直接跳到博客步骤==2.4== 拉取仓库步骤
1
git clone https://github.com/OnlyAR/nano-ai-chat.git
  • 需要魔法hhh

安装依赖

1
2
3
cd nano-ai-chat

pip install -r requirements.txt
  • 此处建议:在pycharm终端进行后续流程
  • 因为pycharm有时候会弹出来提醒,哪几个依赖没有安装
  • 未解之谜:有几个依赖,例如modelscope llmtuner 安装好几次失败,然后再安装就莫名其妙成功了……

模型下载

  • 此处我选了Qwen1.5-0.5B-Chat(低配orz
  • 以后配置好了就选7B的!!!

原博客提示:

只有模型名字里带有 Chat 的才是对话模型,否则是基础模型,只能续写句子不能对话。模型的 -xB- 表示参数量,1B 表示十亿,CPU 玩家建议用 0.5B 或 1.8B 试试

  • 将模型下载到本地
1
python download.py --model qwen/Qwen1.5-0.5B-Chat

原博客提示:

对于上表里有的模型,--model 参数请填写 path,没有的模型可以去主页查。脚本默认将模型下载到当前目录下的 model 文件夹,可以通过 --output 参数修改

代码微调(本文新增)

  • web_demo.py 本地Web端启动模型对应文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# web_demo.py
from llmtuner.webui.interface import create_web_demo


def main():
create_web_demo().queue().launch(
server_name="0.0.0.0",
server_port=None,
share=False,
inbrowser=True)


if __name__ == "__main__":
main()
  • 原来from llmtuner.webui.interface import create_web_demo这一行是from llmtuner import create_web_demo
  • 反复pip好几次llmtuner还是报错,显示找不到create_web_demo,b溃的很
  • 幸好pycharm的报错提示还有建议修复的功能,遂更改,然后可以运行
  • 以下命令行方式对应py文件cli_demo.py同理
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from llmtuner.chat import ChatModel
from llmtuner.extras.misc import torch_gc


try:
import platform

if platform.system() != "Windows":
import readline # noqa: F401
except ImportError:
print("Install `readline` for a better experience.")


def main():
chat_model = ChatModel()
messages = []
print("Welcome to the CLI application, use `clear` to remove the history, use `exit` to exit the application.")

while True:
try:
query = input("\nUser: ")
except UnicodeDecodeError:
print("Detected decoding error at the inputs, please set the terminal encoding to utf-8.")
continue
except Exception:
raise

if query.strip() == "exit":
break

if query.strip() == "clear":
messages = []
torch_gc()
print("History has been removed.")
continue

messages.append({"role": "user", "content": query})
print("Assistant: ", end="", flush=True)

response = ""
for new_text in chat_model.stream_chat(messages):
print(new_text, end="", flush=True)
response += new_text
print()
messages.append({"role": "assistant", "content": response})


if __name__ == "__main__":
main()
  • 以上两个关键文件的依赖导入部分微调完毕

启动模型

  • Web 端
1
python web_demo.py --model_name_or_path model/qwen/Qwen1___5-0___5B-Chat --template qwen
  • 命令行
1
python cli_demo.py --model_name_or_path model/qwen/Qwen1___5-0___5B-Chat --template qwen

完事

  • Copyrights © 2024-2025 brocademaple
  • 访问人数: | 浏览次数:

      请我喝杯咖啡吧~

      支付宝
      微信