Linux环境下部署百度百川AI大模型-基于厚德云

 服务器环境

显存:24 GB
CPU:16 核   
内存:100 GB
总存储: 160 GB

软件环境

git
python 3.10.13
pytorch 2.4.0

 算力平台

厚德云

 步骤一:申请算力资源

 步骤二:查看python版本

python
#退出python编译器
exit()

步骤三:下载Baichuan2代码

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

下载依赖

cd /workspace/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 lfs clone https://www.modelscope.cn/baichuan-inc/Baichuan2-7B-Chat-4bits.git

 如果git lfs install安装失败,请查看如下解决方案,会影响模型下载内容

git lfs install遇到的问题_git: 'lfs' is not a git command. see 'git --help'.-CSDN博客

sudo apt update
sudo apt install git-lfs

步骤五:修改/workspace/Baichuan2/cli_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/
python cli_demo.py

步骤七:使用模型

步骤八:网页版启动

修改web_demo.py内容

启动

cd /workspace/Baichuan2/
streamlit run web_demo.py

Streamliticon-default.png?t=N7T8http://211.93.18.69:59637/

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

参考文章:

Linux环境下部署百度百川AI大模型_linux 大模型-CSDN博客

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

数智侠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值