前言¶
就在今天,文心4.5模型開源了,不是一個,而是整個系列模型正式開源。很突然,我都震驚了。文心4.5系列開源模型共10款,涵蓋了激活參數規模分別爲47B 和3B 的混合專家(MoE)模型(最大的模型總參數量爲424B),以及0.3B 的稠密參數模型。下面我們就介紹如何快速使用文心4.5模型推理,以及部署接口給Android、微信小程序等客戶端調用,注意這裏只接受文本類型的模型,實際文心4.5也有多模態的模型。
環境:
- PaddlePaddle 3.1.0
- Python 3.11
- CUDA 12.6
- 顯卡 4090 24G
- Ubuntu 22.04
搭建環境¶
- 首先安裝PaddlePaddle,如果安裝了,可以跳過。
python -m pip install paddlepaddle-gpu==3.1.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu126/
- 然後安裝fastdeploy工具。
python -m pip install fastdeploy-gpu -i https://www.paddlepaddle.org.cn/packages/stable/fastdeploy-gpu-86_89/ --extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
- 安裝aistudio-sdk,用於下載模型。
pip install --upgrade aistudio-sdk
快速使用¶
通過使用下面Python代碼,可以快速實現對話。我使用了最小的一個模型作爲開始使用,實際還有更多更大的模型,如下:
- ERNIE-4.5-0.3B-Paddle
- ERNIE-4.5-21B-A3B-Paddle
- ERNIE-4.5-300B-A47B-Paddle
執行下面代碼,會自動下載模型,然後開始在終端對話。quantization參數設置量化類型,支持wint4和wint8。
from aistudio_sdk.snapshot_download import snapshot_download
from fastdeploy import LLM, SamplingParams
# 模型名稱
model_name = "PaddlePaddle/ERNIE-4.5-0.3B-Paddle"
save_path = "./models/ERNIE-4.5-0.3B-Paddle/"
# 下載模型
res = snapshot_download(repo_id=model_name, revision='master', local_dir=save_path)
# 對話參數
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
# 加載模型
llm = LLM(model=save_path, max_model_len=32768, quantization=None)
messages = []
while True:
prompt = input("請輸入問題:")
if prompt == 'exit':
break
messages.append({"role": "user", "content": prompt})
output = llm.chat(messages, sampling_params)[0]
text = output.outputs.text
messages.append({"role": "assistant", "content": text})
print(text)
輸出日誌如下:
INFO 2025-07-01 14:20:26,232 4785 engine.py[line:206] Waitting worker processes ready...
Loading Weights: 100%|█████████████████████████████████| 100/100 [00:03<00:00, 33.26it/s]
Loading Layers: 100%|██████████████████████████████████| 100/100 [00:01<00:00, 66.54it/s]
INFO 2025-07-01 14:20:36,753 4785 engine.py[line:276] Worker processes are launched with 12.627224445343018 seconds.
請輸入問題:你好,你叫什麼名字?
Processed prompts: 100%|███████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 2.12it/s, est. speed input: 0.00 toks/s, output: 0.00 toks/s]
你好呀!我是**小天**,很高興認識你!有什麼我可以幫助你的嗎?
請輸入問題:你會什麼?
Processed prompts: 100%|███████████████████████████████████████████████████████████████| 1/1 [00:01<00:00, 1.44s/it, est. speed input: 0.00 toks/s, output: 0.00 toks/s]
我的本領可多啦!我擅長**整理知識樹**、分析歷史事件、講解科學原理,還能幫你快速完成**腦筋急轉彎**或**創意小發明**,或者用聲音給你講有趣的笑話呢。你要不要試試?
請輸入問題:我剛纔問你什麼問題?
Processed prompts: 100%|███████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 3.49it/s, est. speed input: 0.00 toks/s, output: 0.00 toks/s]
好呀!你想問什麼呢?是關於我的名字、我的愛好、或者其他有趣的話題呀?
請輸入問題:
部署接口¶
首先下載模型,這裏可以隨時替換你需要的模型。
aistudio download --model PaddlePaddle/ERNIE-4.5-0.3B-Paddle --local_dir ./models/ERNIE-4.5-0.3B-Paddle/
下載模型之後,執行下面命令開始啓動服務,端口號是8180,max-model-len是指定推理支持的最大上下文長度,max-num-seqs是解碼階段的最大併發數,如果指定了quantization,就開啓量化。更多的參數文檔可以查看:https://paddlepaddle.github.io/FastDeploy/parameters/
python -m fastdeploy.entrypoints.openai.api_server \
--model ./models/ERNIE-4.5-0.3B-Paddle/ \
--port 8180 \
--quantization wint8 \
--max-model-len 32768 \
--max-num-seqs 32
輸出日誌如下:
INFO 2025-07-01 14:25:22,033 5239 engine.py[line:206] Waitting worker processes ready...
Loading Weights: 100%|█████████████████████████████████| 100/100 [00:03<00:00, 33.26it/s]
Loading Layers: 100%|██████████████████████████████████| 100/100 [00:02<00:00, 49.91it/s]
INFO 2025-07-01 14:25:33,060 5239 engine.py[line:276] Worker processes are launched with 16.20948576927185 seconds.
INFO 2025-07-01 14:25:33,061 5239 api_server.py[line:91] Launching metrics service at http://0.0.0.0:8001/metrics
INFO 2025-07-01 14:25:33,061 5239 api_server.py[line:94] Launching chat completion service at http://0.0.0.0:8180/v1/chat/completions
INFO 2025-07-01 14:25:33,061 5239 api_server.py[line:97] Launching completion service at http://0.0.0.0:8180/v1/completions
INFO: Started server process [5239]
INFO: Waiting for application startup.
[2025-07-01 14:25:34,089] [ INFO] - Loading configuration file ./models/ERNIE-4.5-0.3B-Paddle/generation_config.json
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8180 (Press CTRL+C to quit)
INFO: 127.0.0.1:53716 - "POST /v1/chat/completions HTTP/1.1" 200 OK
調用接口¶
它是兼容OpenAI的API,所以如果使用Python調用的話,可以使用openai庫來調用,不需要指定模型名稱和api_key。
import openai
host = "192.168.0.100"
port = "8180"
client = openai.Client(base_url=f"http://{host}:{port}/v1", api_key="null")
messages = []
while True:
prompt = input("請輸入問題:")
if prompt == 'exit':
break
messages.append({"role": "user", "content": prompt})
response = client.chat.completions.create(
model="null",
messages=messages,
stream=True,
)
output = ""
for chunk in response:
if chunk.choices[0].delta:
print(chunk.choices[0].delta.content, end='')
output += chunk.choices[0].delta.content
print()
messages.append({"role": "assistant", "content": output})
輸出如下:
請輸入問題:你好
你好呀!😊 很高興能爲你提供幫助~有什麼我可以幫你解決的嗎?無論是學習上的問題,還是生活裏的小煩惱,我都在這兒哦!🧐
請輸入問題: