随着大模型技术的发展,越来越多的企业和个人开始探索私有化部署的可行路径。百度文心大模型ERNIE系列作为国产大模型的佼佼者,具备强大的语言理解与生成能力。而在文心4.5发布之后,百度也在HuggingFace平台上开源了ERNIE-4.5-21B-A3B-Paddle模型权重。
本文将基于FastDeploy
工具链,介绍如何在服务器部署该模型并启动兼容OpenAI接口的服务。包括环境配置、模型下载、依赖安装、服务启动及常见问题处理等,帮助开发者快速上手并完成模型落地实践。
部署ERNIE-4.5-21B模型建议具备较强的硬件支持,尤其是显卡资源,需要80G以上。以下是本文测试使用的服务器配置:
组件 | 配置说明 |
---|---|
GPU型号 | NVIDIA A800 SXM4 80GB |
GPU数量 | 1 块 |
显存容量 | 80GB |
CPU型号 | Intel Xeon Platinum 8358P |
vCPU数量 | 15 核 |
内存容量 | 249GB |
系统盘 | 100GB SSD |
数据盘 | 50GB |
操作系统 | Ubuntu 22.04 LTS |
若部署更大参数量模型(如多卡部署ERNIE-4.5-100B),建议至少使用A100或H100集群。
建议使用Python 3.9及以上,并先确保pip为最新版本:
pip install --upgrade pip
由于百度将模型权重托管于 HuggingFace 平台,因此需借助 huggingface_hub
工具进行模型下载:
pip install huggingface_hub
安装完成后,huggingface-cli
命令可用。若遇到找不到命令,确认环境变量或使用 python -m huggingface_hub
替代。
如无法访问 huggingface.co
,可切换至镜像站点:
export HF_ENDPOINT=https://hf-mirror.com
使用以下命令下载ERNIE-4.5-21B-A3B模型到本地目录:
huggingface-cli download baidu/ERNIE-4.5-21B-A3B-Paddle --local-dir baidu/ERNIE-4.5-21B-A3B-Paddle
若仍报如下错误:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='huggingface.co'...
可能是网络未连通 HuggingFace,建议使用国内镜像或配置代理。
确保安装匹配的 CUDA 驱动与 Paddle:
python -m pip install paddlepaddle-gpu==3.1.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu126/
请根据具体环境选择正确的 CUDA 版本(例如 cu113、cu118、cu126)。
针对 SM80/90 架构(如 A30/A100/H100),使用如下命令安装 FastDeploy:
python -m pip install fastdeploy-gpu -i https://www.paddlepaddle.org.cn/packages/stable/fastdeploy-gpu-80_90/ \
--extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
若显卡非 SM80/90 架构,请参考 FastDeploy 官网文档 选择合适安装源。
完成模型下载与依赖安装后,即可通过以下命令启动模型服务:
python -m fastdeploy.entrypoints.openai.api_server \
--model baidu/ERNIE-4.5-21B-A3B-Paddle \
--port 8180 \
--metrics-port 8181 \
--engine-worker-queue-port 8182 \
--max-model-len 32768 \
--max-num-seqs 32
参数说明:
--model
:本地模型目录(需包含model_index.json
)--port
:主服务端口,提供 /v1/chat/completions
接口--metrics-port
:Prometheus监控端口(可选)--max-model-len
:最大上下文长度--max-num-seqs
:最大同时对话序列数(并发控制)服务启动成功后控制台输出如下信息:
INFO engine.py[line:276] Worker processes are launched with 63.73 seconds.
INFO api_server.py[line:91] Launching metrics service at http://0.0.0.0:8181/metrics
INFO api_server.py[line:94] Launching chat completion service at http://0.0.0.0:8180/v1/chat/completions
INFO api_server.py[line:97] Launching completion service at http://0.0.0.0:8180/v1/completions
此时,API服务已启动完成,可使用curl、Postman或兼容OpenAI SDK进行调用测试。
可能原因为命令未加入环境变量。可通过以下方式调用:
python -m huggingface_hub download baidu/ERNIE-4.5-21B-A3B-Paddle --local-dir xxx
或手动添加到环境变量 ~/.bashrc
:
export PATH="$HOME/.local/bin:$PATH"
若出现网络错误或连接超时,可能为 HuggingFace 主站不可达:
HF_ENDPOINT=https://hf-mirror.com
在24GB显存GPU上运行21B模型属于边缘配置,如出现OOM:
支持以下两类OpenAI标准接口:
/v1/completions
/v1/chat/completions
例如使用curl进行调用:
curl http://d1kvbl7hri0c73esff80-8180.agent.damodel.com/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "ERNIE-4.5-21B",
"messages": [{"role": "user", "content": "介绍一下文心大模型"}],
"temperature": 0.8
}'
import requests
import json
def chat_with_model(prompt, model="ERNIE-4.5", max_tokens=512):
"""
调用对话模型 API 并返回回答
参数:
prompt (str): 用户输入的提示词
model (str): 模型名称(可选)
max_tokens (int): 生成的最大 token 数
返回:
str: 模型的回答
"""
url = "http://d1kvbl7hri0c73esff80-8180.agent.damodel.com/v1/chat/completions"
headers = {
"Content-Type": "application/json"
}
data = {
"model": model,
"messages": [
{"role": "user", "content": prompt}
],
"max_tokens": max_tokens,
"temperature": 0.7 # 控制生成结果的随机性(0~1)
}
try:
response = requests.post(url, headers=headers, data=json.dumps(data))
response.raise_for_status() # 检查 HTTP 错误
result = response.json()
# 提取模型的回答
reply = result["choices"][0]["message"]["content"]
return reply.strip()
except requests.exceptions.RequestException as e:
return f"请求失败: {e}"
except (KeyError, IndexError) as e:
return f"解析响应失败: {e}"
if __name__ == "__main__":
while True:
user_input = input("你: ")
if user_input.lower() in ["exit", "quit"]:
print("对话结束。")
break
reply = chat_with_model(user_input)
print("AI:", reply)
本文介绍了如何使用 FastDeploy 工具链部署百度开源的 ERNIE-4.5-21B-A3B-Paddle 模型,并提供了完整的环境配置、模型下载、依赖安装、服务启动及常见问题排查流程。通过此部署方案,用户可以低门槛接入国产大模型,开展自定义应用开发与能力评估。
一起来轻松玩转文心大模型吧,文心大模型免费下载地址:https://ai.gitcode.com/theme/1939325484087291906