从 百川智能 官方 GitHub README 翻译整理的快速上手指南。
Baichuan 2 是百川智能推出的新一代开源大语言模型,使用 2.6 万亿 Tokens 的高质量语料训练而成。本次开源包含 7B 和 13B 两个尺寸的 Base(基座)和 Chat(对话)版本,Chat 版本还提供了 4bits 量化版本,方便在资源有限的设备上运行。
所有版本对学术研究完全开放。开发者通过邮件申请并获得官方商用许可后,可免费商用。
Baichuan 2 在多个权威的中文、英文和多语言 benchmark 上取得了同尺寸最佳效果,覆盖以下领域:
以 7B Base 模型为例,在 C-Eval(5-shot)上得分 54.00,MMLU(5-shot)得分 54.16,均超越同尺寸的 LLaMA2-7B、ChatGLM2-6B 等模型。
pip install torch transformers sentencepiece accelerate
模型托管在 Hugging Face 和 ModelScope 平台,国内用户推荐使用 ModelScope 镜像加速下载:
# 以 Baichuan2-7B-Chat 为例
from modelscope import snapshot_download
model_dir = snapshot_download('baichuan-inc/Baichuan2-7B-Chat')
以下是一个完整的对话推理示例,使用 Baichuan2-7B-Chat 模型:
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation.utils import GenerationConfig
# 加载模型和分词器
tokenizer = AutoTokenizer.from_pretrained(
"baichuan-inc/Baichuan2-7B-Chat",
trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
"baichuan-inc/Baichuan2-7B-Chat",
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True
)
model.generation_config = GenerationConfig.from_pretrained(
"baichuan-inc/Baichuan2-7B-Chat"
)
# 开始对话
messages = []
messages.append({"role": "user", "content": "解释一下什么是大语言模型"})
response = model.chat(tokenizer, messages)
print(response)
如果显存有限,可以使用 4bits 量化版本:
model = AutoModelForCausalLM.from_pretrained(
"baichuan-inc/Baichuan2-7B-Chat-4bits",
device_map="auto",
trust_remote_code=True
)
翻译整理自 GitHub README,原文:https://github.com/baichuan-inc/Baichuan2