DMR REST API
启用 Model Runner 后,将提供新的 API 端点。你可以使用这些端点以编程方式与模型交互。
确定基础 URL
与端点交互的基础 URL 取决于你运行 Docker 的方式:
- 从容器内:
http://model-runner.docker.internal/ - 从主机进程:
http://localhost:12434/(假设在默认端口 12434 上启用了 TCP 主机访问)
- 从容器内:
http://172.17.0.1:12434/(其中172.17.0.1表示主机网关地址) - 从主机进程:
http://localhost:12434/
Note
172.17.0.1接口默认情况下可能对 Compose 项目中的容器不可用。 在这种情况下,在你的 Compose 服务 YAML 中添加extra_hosts指令:extra_hosts: - "model-runner.docker.internal:host-gateway"然后你可以通过 http://model-runner.docker.internal:12434/ 访问 Docker Model Runner API
可用的 DMR 端点
-
创建模型:
POST /models/create -
列出模型:
GET /models -
获取模型:
GET /models/{namespace}/{name} -
删除本地模型:
DELETE /models/{namespace}/{name}
可用的 OpenAI 端点
DMR 支持以下 OpenAI 端点:
-
列出模型:
GET /engines/llama.cpp/v1/models -
检索模型:
GET /engines/llama.cpp/v1/models/{namespace}/{name} -
POST /engines/llama.cpp/v1/chat/completions -
创建完成:
POST /engines/llama.cpp/v1/completions -
创建嵌入:
POST /engines/llama.cpp/v1/embeddings
要通过 Unix 套接字(/var/run/docker.sock)调用这些端点,需在其路径前添加 /exp/vDD4.40。
Note你可以从路径中省略
llama.cpp。例如:POST /engines/v1/chat/completions。
REST API 示例
从容器内请求
要从另一个容器内使用 curl 调用 chat/completions OpenAI 端点:
#!/bin/sh
curl http://model-runner.docker.internal/engines/llama.cpp/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "ai/smollm2",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Please write 500 words about the fall of Rome."
}
]
}'使用 TCP 从主机请求
要通过 TCP 从主机调用 chat/completions OpenAI 端点:
-
从 Docker Desktop GUI 启用主机侧 TCP 支持,或通过 Docker Desktop CLI 启用。 例如:
docker desktop enable model-runner --tcp <port>。如果你在 Windows 上运行,还需启用 GPU 支持的推理。 参见 启用 Docker Model Runner。
-
按照上一节的文档使用
localhost和正确的端口与其交互。
#!/bin/sh
curl http://localhost:12434/engines/llama.cpp/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "ai/smollm2",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Please write 500 words about the fall of Rome."
}
]
}'使用 Unix 套接字从主机请求
要通过 Docker 套接字使用 curl 从主机调用 chat/completions OpenAI 端点:
#!/bin/sh
curl --unix-socket $HOME/.docker/run/docker.sock \
localhost/exp/vDD4.40/engines/llama.cpp/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "ai/smollm2",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Please write 500 words about the fall of Rome."
}
]
}'