AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


FastChat:一个大语言模型部署的利器
发布日期:2024-06-03 20:11:42 浏览次数: 2256


FastChat是一个开源的大模型训练、推理工具。我们可以用它来部署Qwen、LLama、ChatGLM等各种常用模型;而且它还支持同时部署多个不同的大语言模型来提供推理服务,当然也可以部署多个相同的大语言模型从而可以简单的实现大模型分布服务。

地址:https://github.com/lm-sys/FastChat

安装

FastChat支持两种安装方式:
  • pip安装包

  • 源码安装

pip安装包

pip3 install "fschat[model_worker,webui]"
如果需要安装所有功能,可能用下列安装命令:
pip3 install "fschat[all]"

源码安装

1. 首先下载源码,进入到根目录。
git clone https://github.com/lm-sys/FastChat.gitcd FastChat
2. 执行安装命令
pip3 install --upgrade pip# enable PEP 660 supportpip3 install -e ".[model_worker,webui]"
-e 表示 可编辑,意思是当你修改源码后,会马上生效。
推理服务

FastChat支持三种推理方式:
  • 命令行

  • API

  • Web GUI

命令行

FastChat可以通过命令行的方式部署大模型,同时还支持GPU、CPU、

XPU等设备。

单GPU部署命令:

python3 -m fastchat.serve.cli --model-path xxx

如果你需要实现多GPU部署,你可以通过--num-gpus参数指定数量:

python3 -m fastchat.serve.cli --model-path xxx --num-gpus 2

同时还可以通过参数--max-gpu-memory限制单个GPU使用的内存大小,

python3 -m fastchat.serve.cli --model-path xxx --num-gpus 2 --max-gpu-memory 8GiB
如果你的主机没有GPU,那么也可以通过CPU来部署:
python3 -m fastchat.serve.cli --model-path xxx --device cpu
对于其它设置的部署方式可以参考github的说明。
API
在日常中使用大模型一般都是通过API来访问,这种方式更加方便;Fastchat当然也支持API这种方式,部署步骤如下:
1. 启动controller服务。
python3 -m fastchat.serve.controller
2. 启动work服务。
部署work服务其实就是部署大模型,它支持 huggingface/transformers和vllm两种部署方式,transformers的方式推理速度可能会慢一点,所以我们一般采用vllm的方式来部署。
pip install vllm
python3 -m fastchat.serve.vllm_worker --model-path xxx
对于部署参数大一些的模型,单块GPU的内存可能不够,需要多个GPU,那么就需要指定--num-gpus参数,同时指定运行设备的环境变量:
CUDA_VISIBLE_DEVICES=0,1 python3 -m fastchat.serve.vllm_worker --model-path xxx --controller-address http://localhost:21001 --port 31000 --worker-address http://localhost:31000 --num-gpus 2
如果你需要在其它CUDA设备中部署从而实现分布式大模型推理服务,你可以继续部署一个大模型:
CUDA_VISIBLE_DEVICES=2,3 python3 -m fastchat.serve.vllm_worker --model-path xxx --controller-address http://localhost:21001 --port 31001 --worker-address http://localhost:31001 --num-gpus 2
甚至你还可以部署一个不同的大模型:
CUDA_VISIBLE_DEVICES=4,5 python3 -m fastchat.serve.vllm_worker --model-path yyy --controller-address http://localhost:21001 --port 31001 --worker-address http://localhost:31001 --num-gpus 2
3. 启动RESTful API服务。
python3 -m fastchat.serve.openai_api_server --host localhost --port 8000
这是一个openai风格的API,对外暴露的端口8000,这样就可以使用大模型的推理服务了,既可以使用openai官方的sdk,也可以直接通过http接口访问。
openai的方式:
import openai
openai.api_key = "EMPTY"openai.base_url = "http://localhost:8000/v1/"
model = "vicuna-7b-v1.5"prompt = "Once upon a time"
# create a completioncompletion = openai.completions.create(model=model, prompt=prompt, max_tokens=64)# print the completionprint(prompt + completion.choices[0].text)
# create a chat completioncompletion = openai.chat.completions.create(model=model,messages=[{"role": "user", "content": "Hello! What is your name?"}])# print the completionprint(completion.choices[0].message.content)
http接口:
curl http://localhost:8000/v1/chat/completions \-H "Content-Type: application/json" \-d '{"model": "vicuna-7b-v1.5","messages": [{"role": "user", "content": "Hello! What is your name?"}]}'
Web GUI
如果你需要通过web界面来访问大模型推理服务,Fastchat也是支持的,具体操作步骤如下:
1. 启动controller服务。
python3 -m fastchat.serve.controller
2. 启动work服务。

和API部署方式一样,这里采用vllm方式部署大模型。

pip install vllm
python3 -m fastchat.serve.vllm_worker --model-path xxx
3. 启动Gradio web服务。
python3 -m fastchat.serve.gradio_web_server
这样就可以通过界面访问大模型推理服务了。


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询