本项目提供基于华为昇腾 910B NPU 的 MiniCPM-V 4.5 模型推理服务,兼容 OpenAI API 格式。
.
├── deploy_minicpm_npu.sh # 一键部署脚本
├── MiniCPM-V-4_5/
│ ├── Dockerfile # NPU 适配镜像构建文件
│ ├── api/
│ │ └── main.py # FastAPI 服务入口
│ └── requirements.txt # Python 依赖确保服务器已安装:
npu-smi info 能正常输出运行以下命令:
chmod +x deploy_minicpm_npu.sh
./deploy_minicpm_npu.sh该脚本会自动:
POST http://localhost:8000/v1/chat/completions| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型名称,固定为 MiniCPM-V-4_5 |
| messages | array | 是 | 消息列表 |
| stream | boolean | 否 | 是否流式输出,默认 false |
| max_tokens | integer | 否 | 最大生成 token 数,默认 1024 |
| temperature | float | 否 | 温度参数,默认 0.7 |
| top_p | float | 否 | Top-p 采样,默认 0.8 |
消息内容 content 支持以下类型:
| 类型 | 说明 |
|---|---|
text | 文本内容 |
image_url | 图片(支持 URL 或 Base64) |
video_url | 视频(支持 URL 或 Base64) |
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MiniCPM-V-4_5",
"messages": [
{"role": "user", "content": "你好,介绍一下你自己"}
],
"max_tokens": 512
}'curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MiniCPM-V-4_5",
"messages": [
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}},
{"type": "text", "text": "描述这张图片的内容"}
]
}
]
}'# 将图片转为 Base64
IMAGE_BASE64=$(base64 -i image.jpg | tr -d '\n')
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d "{
\"model\": \"MiniCPM-V-4_5\",
\"messages\": [
{
\"role\": \"user\",
\"content\": [
{\"type\": \"image_url\", \"image_url\": {\"url\": \"data:image/jpeg;base64,${IMAGE_BASE64}\"}},
{\"type\": \"text\", \"text\": \"图片里有什么?\"}
]
}
]
}"curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MiniCPM-V-4_5",
"messages": [
{
"role": "user",
"content": [
{"type": "video_url", "video_url": {"url": "https://example.com/video.mp4"}},
{"type": "text", "text": "描述这个视频的内容"}
]
}
]
}'# 将视频转为 Base64
VIDEO_BASE64=$(base64 -i video.mp4 | tr -d '\n')
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d "{
\"model\": \"MiniCPM-V-4_5\",
\"messages\": [
{
\"role\": \"user\",
\"content\": [
{\"type\": \"video_url\", \"video_url\": {\"url\": \"data:video/mp4;base64,${VIDEO_BASE64}\"}},
{\"type\": \"text\", \"text\": \"视频里发生了什么?\"}
]
}
]
}"curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MiniCPM-V-4_5",
"messages": [
{"role": "user", "content": "讲一个故事"}
],
"stream": true
}'curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MiniCPM-V-4_5",
"messages": [
{"role": "user", "content": "1+1等于多少?"},
{"role": "assistant", "content": "1+1等于2。"},
{"role": "user", "content": "那再加上3呢?"}
]
}'import requests
import base64
API_URL = "http://localhost:8000/v1/chat/completions"
# 文本对话
def chat(message: str):
response = requests.post(API_URL, json={
"model": "MiniCPM-V-4_5",
"messages": [{"role": "user", "content": message}]
})
return response.json()["choices"][0]["message"]["content"]
# 图片理解
def analyze_image(image_path: str, question: str):
with open(image_path, "rb") as f:
image_base64 = base64.b64encode(f.read()).decode()
response = requests.post(API_URL, json={
"model": "MiniCPM-V-4_5",
"messages": [{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}},
{"type": "text", "text": question}
]
}]
})
return response.json()["choices"][0]["message"]["content"]
# 视频理解
def analyze_video(video_path: str, question: str):
with open(video_path, "rb") as f:
video_base64 = base64.b64encode(f.read()).decode()
response = requests.post(API_URL, json={
"model": "MiniCPM-V-4_5",
"messages": [{
"role": "user",
"content": [
{"type": "video_url", "video_url": {"url": f"data:video/mp4;base64,{video_base64}"}},
{"type": "text", "text": question}
]
}]
})
return response.json()["choices"][0]["message"]["content"]
# 使用示例
print(chat("你好"))
print(analyze_image("test.jpg", "描述这张图片"))
print(analyze_video("test.mp4", "视频里发生了什么?"))curl http://localhost:8000/health返回:
{"status": "healthy", "model_loaded": true}{
"id": "chatcmpl-1234567890",
"object": "chat.completion",
"created": 1234567890,
"model": "MiniCPM-V-4_5",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "模型的回复内容..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": -1,
"completion_tokens": -1,
"total_tokens": -1
}
}quay.io/ascend/cann:8.2.rc1.alpha002-910b-openeuler22.03-py3.10torch==2.1.0, torch_npu==2.1.0deploy_minicpm_npu.sh 中修改 NPU_ID 变量检查设备是否正确挂载:
docker exec -it minicpm-v-api npu-smi info确保模型路径正确,或设置 MODEL_PATH 环境变量指向模型目录。