Linux环境下部署百度百川AI大模型

服务器环境

显存:24 GB
CPU:8 核   
内存:62 GB
总存储: 680 GB

软件环境

git
python 3.10.14
pytorch 2.2.2 

 算力平台

派欧算力云:派欧算力云

 步骤一:申请算力资源

步骤二:查看python版本

python
#退出python编译器
exit()

步骤三:下载Baichuan2代码

cd /workspace/
git clone  https://github.com/baichuan-inc/Baichuan2

下载依赖

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install bitsandbytes==0.41.1
pip install accelerate==0.25.0

步骤四:下载模型库

mkdir /workspace/Baichuan2/baichuan-inc
cd /workspace/Baichuan2/baichuan-inc/
git lfs install
git clone https://www.modelscope.cn/baichuan-inc/Baichuan2-7B-Chat-4bits.git

步骤五:修改/workspace/Baichuan2/cli_demo.py文件内容

# coding=gbk
import os
import torch
import platform
import subprocess
from colorama import Fore, Style
from tempfile import NamedTemporaryFile
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation.utils import GenerationConfig


def init_model():
    print("init model ...")
    model = AutoModelForCausalLM.from_pretrained(
        "baichuan-inc/Baichuan2-7B-Chat-4bits",
        torch_dtype=torch.float16,
        device_map="auto",
        trust_remote_code=True
    )
    model.generation_config = GenerationConfig.from_pretrained(
        "baichuan-inc/Baichuan2-7B-Chat-4bits"
    )
    tokenizer = AutoTokenizer.from_pretrained(
        "baichuan-inc/Baichuan2-7B-Chat-4bits",
        use_fast=False,
        trust_remote_code=True
    )
    return model, tokenizer


def clear_screen():
    if platform.system() == "Windows":
        os.system("cls")
    else:
        os.system("clear")
    print(Fore.YELLOW + Style.BRIGHT + "欢迎使用百川大模型,输入进行对话,vim 多行输入,clear 清空历史,CTRL+C 中断生成,stream 开关流式生成,exit 结束")
    return []


def vim_input():
    with NamedTemporaryFile() as tempfile:
        tempfile.close()
        subprocess.call(['vim', '+star', tempfile.name])
        text = open(tempfile.name).read()
    return text


def main(stream=True):
    model, tokenizer = init_model()
    messages = clear_screen()
    while True:
        prompt = input(Fore.GREEN + Style.BRIGHT + "\n用户" + Style.NORMAL)
        if prompt.strip() == "exit":
            break
        if prompt.strip() == "clear":
            messages = clear_screen()
            continue
        if prompt.strip() == 'vim':
            prompt = vim_input()
            print(prompt)
        print(Fore.CYAN + Style.BRIGHT + "\nBaichuan 2" + Style.NORMAL, end='')
        if prompt.strip() == "stream":
            stream = not stream
            print(Fore.YELLOW + "({}流式生成)\n".format("开" if stream else "关闭"), end='')
            continue
        messages.append({"role": "user", "content": prompt})
        if stream:
            position = 0
            try:
                for response in model.chat(tokenizer, messages, stream=True):
                    print(response[position:], end='', flush=True)
                    position = len(response)
                    if torch.backends.mps.is_available():
                        torch.mps.empty_cache()
            except KeyboardInterrupt:
                pass
            print()
        else:
            response = model.chat(tokenizer, messages)
            print(response)
            if torch.backends.mps.is_available():
                torch.mps.empty_cache()
        messages.append({"role": "assistant", "content": response})
    print(Style.RESET_ALL)


if __name__ == "__main__":
    main()

步骤六:启动大模型会话

命令行启动

cd /workspace/Baichuan2/
python cli_demo.py

步骤七:使用模型

步骤八:网页版启动

修改web_demo.py内容

import json
import torch
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation.utils import GenerationConfig


st.set_page_config(page_title="Baichuan 2")
st.title("Baichuan 2")


@st.cache_resource
def init_model():
    model = AutoModelForCausalLM.from_pretrained(
        "baichuan-inc/Baichuan2-7B-Chat-4bits",
        torch_dtype=torch.float16,
        device_map="auto",
        trust_remote_code=True
    )
    model.generation_config = GenerationConfig.from_pretrained(
        "baichuan-inc/Baichuan2-7B-Chat-4bits"
    )
    tokenizer = AutoTokenizer.from_pretrained(
        "baichuan-inc/Baichuan2-7B-Chat-4bits",
        use_fast=False,
        trust_remote_code=True
    )
    return model, tokenizer


def clear_chat_history():
    del st.session_state.messages


def init_chat_history():
    with st.chat_message("assistant", avatar='🤖'):
        st.markdown("您好,我是百川大模型,很高兴为您服务🥰")

    if "messages" in st.session_state:
        for message in st.session_state.messages:
            avatar = '🧑‍�? if message["role"] == "user" else '🤖'
            with st.chat_message(message["role"], avatar=avatar):
                st.markdown(message["content"])
    else:
        st.session_state.messages = []

    return st.session_state.messages


def main():
    model, tokenizer = init_model()
    messages = init_chat_history()

    if prompt := st.chat_input("Shift + Enter 换行, Enter 发�?):
        with st.chat_message("user", avatar='🧑‍�?):
            st.markdown(prompt)
        messages.append({"role": "user", "content": prompt})
        print(f"[user] {prompt}", flush=True)
        with st.chat_message("assistant", avatar='🤖'):
            placeholder = st.empty()
            for response in model.chat(tokenizer, messages, stream=True):
                placeholder.markdown(response)
                if torch.backends.mps.is_available():
                    torch.mps.empty_cache()
        messages.append({"role": "assistant", "content": response})
        print(json.dumps(messages, ensure_ascii=False), flush=True)

        st.button("清空对话", on_click=clear_chat_history)


if __name__ == "__main__":
    main()

启动

cd /workspace/Baichuan2/
streamlit run web_demo.py

依靠 streamlit 运行命令,会在本地启动一个 web 服务,把控制台给出的地址放入浏览器即可访问。网页 demo 工具是为 Chat 场景设计,因此不支持使用该工具调用 Base 模型。

参考文章:

Baichuan Intelligent Technology · GitHub

GitHub - baichuan-inc/Baichuan2: A series of large language models developed by Baichuan Intelligent Technology

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

https://www.zhihu.com/question/620743448/answer/3214825690

百川2代大模型(Baichuan2-7B、Baichuan2-13B)可以导入千帆平台使用啦! - 百度智能云千帆社区

魔搭社区

部署百川大语言模型Baichuan2_win11 部署百川-CSDN博客

https://www.cnblogs.com/xhq1024/p/17628267.html

Python问题解决7:爬虫报错SyntaxError: Non-UTF-8 code starting with ‘\xbb‘ in file-CSDN博客

Baichuan2-13B-Chat - 千帆大模型平台 | 百度智能云文档

GPT实战系列-Baichuan2本地化部署实战方案-CSDN博客

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数智侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值