赞
踩
本文将会介绍如何使用FastChat来部署国产大模型——百川模型。
在此之前,我们先来了解两个概念——百川模型
和FastChat
.
2023年6月15日,被称为「中国 ChatGPT 梦之队」的百川智能公司,推出了 70 亿参数量的中英文预训练大模型——baichuan-7B
。
baichuan-7B
是由百川智能开发的一个开源的大规模预训练模型。基于Transformer结构,在大约1.2万亿tokens上训练的70亿参数模型,支持中英双语,上下文窗口长度为4096。在标准的中文和英文权威benchmark(C-EVAL/MMLU)上均取得同尺寸最好的效果。
在构建预训练语料库方面,百川智能以高质量中文语料为基础,同时融合了优质的英文数据。相较于其他同参数规模的开源中文预训练模型,数据量提高了超过 50%。
不同于LLaMA完全禁止商业使用,baichuan-7B
代码使用更宽松的开源协议——Apache-2.0协议,允许用于商业目的
FastChat
是用于对话机器人模型训练、部署、评估的开放平台,其核心特性包括:
FastChat
集成了Vicuna、Koala、alpaca、LLaMA等开源模型,其中Vicuna号称能够达到GPT-4的90%的质量,是开源的chatGPT模型中对答效果比较好的。
FastChat
的访问地址是:https://chat.lmsys.org/ , FastChat
的安装方式为:pip3 install fschat
.
在Huggingface Hub上下载baichuan-7B
模型,访问网址为:https://huggingface.co/baichuan-inc/Baichuan-7B ,放在GPU机器上的本地路径。
笔者的GPU机器为4 * RTX6000,每张RTX6000的显存为80G。
FastChat
使用CLI部署百川大模型的命令为:
python3 -m fastchat.serve.cli --model-path path_of_Baichuan-7B --num-gpus 2
在CLI部署时,如遇到以下的报错:trust_remote_code=True
,参考issue网址:https://github.com/lm-sys/FastChat/issues/1789 ,则在对应的Python路径下,将FastChat的fastchat/model/model_adapter.py文件中的代码中的第57至61行:
tokenizer = AutoTokenizer.from_pretrained(
model_path,
use_fast=self.use_fast_tokenizer,
revision=revision,
)
和69至71行
model = AutoModelForCausalLM.from_pretrained(
model_path, low_cpu_mem_usage=True, **from_pretrained_kwargs
)
中添加代码:`trust_remote_code=True
` ,则可顺利部署。
部署成功后的界面如下:
FastChat
还支持WEB部署,可Web UI和OpenAI兼容的RESTful APIs.
这里主要介绍如何实现与OpenAI兼容的具有RESTful APIs的部署方式,参考网址为:https://github.com/lm-sys/FastChat/blob/main/docs/openai_api.md .
部署一共分为三步:
python3 -m fastchat.serve.controller
python3 -m fastchat.serve.model_worker --model-path path_of_Baichuan-7B
python3 -m fastchat.serve.openai_api_server --host localhost --port 8000
在部署过程中,如果遇到PydanticImportError
,原因为pydantic版本的问题,只需将pydantic版本降为1.*
版本即可。
部署成功后,该服务可提供与OpenAI风格类似的RESTful APIs,如下:
curl命令为:
curl http://localhost:8000/v1/models
输出结果为:
{ "object": "list", "data": [ { "id": "baichun_7b", "object": "model", "created": 1689004839, "owned_by": "fastchat", "root": "baichun_7b", "parent": null, "permission": [ { "id": "modelperm-UERow2kYwq5B2M8aVQkwdk", "object": "model_permission", "created": 1689004839, "allow_create_engine": false, "allow_sampling": true, "allow_logprobs": true, "allow_search_indices": true, "allow_view": true, "allow_fine_tuning": false, "organization": "*", "group": null, "is_blocking": false } ] } ] }
curl命令为:
curl http://localhost:8000/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "baichun_7b",
"prompt": "Once upon a time",
"max_tokens": 40,
"temperature": 0.5
}' | jq .
输出结果为:
{ "id": "cmpl-izbe3cRRiY4zAbJueBAyxZ", "object": "text_completion", "created": 1689004991, "model": "baichun_7b", "choices": [ { "index": 0, "text": ", you could find a variety of different types of chocolate in stores. But now, many chocolate companies are focusing on creating vegan chocolate that is not only delicious but also cruelty-free. Here are", "logprobs": null, "finish_reason": "length" } ], "usage": { "prompt_tokens": 4, "total_tokens": 43, "completion_tokens": 39 } }
curl命令为:
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "baichun_7b",
"messages": [{"role": "user", "content": "请用中文简单介绍三国演义?"}]
}' | jq .
输出结果为:
{ "id": "chatcmpl-3SiRqRgbZR8v6gLnQYo9eJ", "object": "chat.completion", "created": 1689005219, "model": "baichun_7b", "choices": [ { "index": 0, "message": { "role": "assistant", "content": " 三国演义是中国古代长篇小说,讲述了东汉末年至晋朝初年的历史故事。主要人物包括曹操、刘备、孙权和关羽等。故事情节曲折复杂,涉及政治、军事、文化等多个方面,被誉为中国古代小说的经典之作。《三国演义》不仅是一部文学作品,也是中国文化的重要组成部分,对中国历史和文化产生了深远的影响。" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 533, "total_tokens": 629, "completion_tokens": 96 } }
curl命令为:
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "baichun_7b",
"messages": [{"role": "user", "content": "请用中文简单介绍西游记?"}, {"role": "assistant", "content": "三国演义是中国古代长篇小说,讲述了东汉末年至晋朝初年的历史故事。主要人物包括曹操、刘备、孙权和关羽等。故事情节曲折复杂,涉及政治、军事、文化等多个方面,被誉为中国古代小说的经典之作。《三国演义》不仅是一部文学作品,也是中国文化的重要组成部分,对中国历史和文化产生了深远的影响。"}, {"role": "user", "content": "它的作者是谁?"}]
}' | jq .
输出结果为:
{ "id": "chatcmpl-8oE57oXC862wKYyrPLnSGM", "object": "chat.completion", "created": 1689005374, "model": "baichun_7b", "choices": [ { "index": 0, "message": { "role": "assistant", "content": " 《三国演义》的作者是明代小说家罗贯中。罗贯中是明代文学家,他的代表作品还有《水浒传》和《西游记》等。他在创作《三国演义》时,参考了大量的历史资料和传说,将这些内容融合在一起,创造了一个虚构的世界,成为了中国文学史上的经典之作。" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 640, "total_tokens": 724, "completion_tokens": 84 } }
import openai openai.api_key = "EMPTY" # Not support yet openai.api_base = "http://localhost:8000/v1" model = "baichun_7b" prompt = "Once upon a time" # create a completion completion = openai.Completion.create(model=model, prompt=prompt, max_tokens=64) # print the completion print(prompt + completion.choices[0].text) # create a chat completion completion = openai.ChatCompletion.create( model=model, messages=[{"role": "user", "content": "Hello! What is your name?"}] ) # print the completion print(completion.choices[0].message.content)
以上两种部署方式,都支持流式输出,且模型推理速度较快,笔者在上述测试例子中的推理时间一般为5-7秒,且支持分布式部署,并发量高。
本文主要介绍了如何使用FastChat来部署国产大模型——百川模型,并演示了两种部署方式——WEB部署和CLI部署,以及在部署过程中出现的问题和解决方案,希望能给读者带来启示。
python -m fastchat.serve.openai_api_server --host localhost --port 8000
: https://github.com/lm-sys/FastChat/issues/1641欢迎关注我的公众号NLP奇幻之旅,原创技术文章第一时间推送。
欢迎关注我的知识星球“自然语言处理奇幻之旅”,笔者正在努力构建自己的技术社区。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。