如何通过curl请求向llama.cpp server传递图像调用多模态模型?
解决llama-server多模态请求"file not found"问题
1. 检查并更新启动参数
llama-server启用多模态时,建议显式添加--multimodal参数,更新你的systemd启动脚本:
ExecStart=/root/llama.cpp/build/bin/llama-server -m /root/llama.cpp/models/ggml-model-Q4_K_M.gguf --mmproj /root/llama.cpp/models/mmproj-model-f16.gguf --port 8082 --multimodal
重启服务生效:
sudo systemctl daemon-reload sudo systemctl restart llama-server.service
通过日志确认服务正常加载多模态模型:
journalctl -u llama-server.service -f
需看到类似Multimodal projection model loaded的日志信息。
2. 修正curl请求的端点与格式
llama-server的多模态对话请求应使用/v1/chat/completions端点(而非/v1/generate),以下提供两种可靠的请求方式:
方式一:Base64编码传递(需确保编码无换行)
先将图片转为单行Base64编码:
base64 -w 0 /path/to/your/image.jpg > image.base64
再执行curl请求:
curl -X POST http://MY_IP:8082/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "ggml-model-Q4_K_M", "messages": [ { "role": "user", "content": [ {"type": "text", "text": "Describe this image"}, {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,'$(cat image.base64)'"}} ] } ] }'
方式二:Multipart表单上传图片(更稳定)
直接上传本地图片文件,避免Base64编码问题:
curl -X POST http://MY_IP:8082/v1/chat/completions \ -F "model=ggml-model-Q4_K_M" \ -F "messages=[{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":\"Describe this image\"},{\"type\":\"image_url\",\"image_url\":{\"url\":\"attachment://image.jpg\"}}]}]" \ -F "image.jpg=@/path/to/your/image.jpg"
3. 额外排查步骤
- 若仍报错,检查llama.cpp版本,旧版本可能不支持Data URI格式,建议更新:
cd /root/llama.cpp git pull make clean && make
- 确认Base64字符串无截断、换行或多余字符,可通过
cat image.base64检查是否为单行。
内容的提问来源于stack exchange,提问作者user3102556




