服务器环境
显存: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
依靠 streamlit 运行命令,会在本地启动一个 web 服务,把控制台给出的地址放入浏览器即可访问。网页 demo 工具是为 Chat 场景设计,因此不支持使用该工具调用 Base 模型。
参考文章: