引言

  • 在本地部署好 vLLM 之后,它会启动一个 兼容 OpenAI 格式的 API 服务

  • vLLM 在内部用 FastAPI 写了一个 Web 服务,挂载了和 OpenAI API 风格一致的路由,比如:

    • /v1/completions
    • /v1/chat/completions
    • /v1/models
  • 本文将以部署在 http://172.22.215.77:6080 的服务为例,详细介绍 vLLM 的 API 路由结构、各个地址的含义,以及常用接口的调用示例。

  • http://172.22.215.77:6080/docs 可以查看由 FastAPI 自动生成的交互式 API 文档界面。


API 路由结构

vLLM 的接口分为以下几类:

1. 基础运维类

  • GET /health → 健康检查,返回服务状态。(一般不需要 Key)
  • GET /ping / POST /ping → Ping 测试。
  • GET /load → 查看服务负载信息。
  • GET /metrics → 导出监控指标。
  • GET /version → 查看服务版本。

2. Token 工具类

  • POST /tokenize → 文本转 token。
  • POST /detokenize → token 转文本。

3. 模型与响应

  • GET /v1/models → 列出可用模型。
  • POST /v1/responses → 创建生成任务。
  • GET /v1/responses/{response_id} → 获取生成结果。
  • POST /v1/responses/{response_id}/cancel → 取消生成任务。

4. 核心 NLP 能力

  • POST /v1/chat/completions → 聊天对话接口。
  • POST /v1/completions → 文本补全接口。
  • POST /v1/embeddings → 获取文本向量嵌入。
  • POST /pooling → 向量池化。
  • POST /classify → 分类任务。
  • POST /score / POST /v1/score → 文本评分。

5. 音频相关

  • POST /v1/audio/transcriptions → 语音转文本。
  • POST /v1/audio/translations → 语音翻译。

6. Rerank 与检索增强

  • POST /rerank / /v1/rerank / /v2/rerank → 文档重排序接口。

7. 系统管理

  • POST /scale_elastic_ep → 动态扩缩容。
  • POST /is_scaling_elastic_ep → 查询是否在扩容。
  • POST /invocations → 通用推理调用入口。

常用接口与示例

1. 查看可用模型

1
2
curl http://172.22.215.77:6080/v1/models \
-H "Authorization: Bearer 123456"

返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
"object": "list",
"data": [
{
"id": "Qwen3-235B-A22B-2507",
"object": "model",
"created": 1755654682,
"owned_by": "vllm",
"root": "/mnt/data/hot4/wenjin/models/Qwen3-235B-A22B-2507",
"parent": null,
"max_model_len": 163840,
"permission": [
{
"id": "modelperm-ff40983d7a8e414989dff2f553521184",
"object": "model_permission",
"created": 1755654682,
"allow_create_engine": false,
"allow_sampling": true,
"allow_logprobs": true,
"allow_search_indices": false,
"allow_view": true,
"allow_fine_tuning": false,
"organization": "*",
"group": null,
"is_blocking": false
}
]
}
]
}

2. Chat Completions

1
2
3
4
5
6
7
8
9
curl http://172.22.215.77:6080/v1/chat/completions \
-H "Authorization: Bearer 123456" \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen3-235B-A22B-2507",
"messages": [
{"role": "user", "content": "你好,请介绍一下你自己"}
]
}'

返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
"id": "chatcmpl-7512a95a51cb4712b1ffd72a2da19c0b",
"object": "chat.completion",
"created": 1755654762,
"model": "Qwen3-235B-A22B-2507",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!我是Qwen,是阿里巴巴集团旗下的义实验室自主研发的超大规模语言模型。我可以帮助你回答问题、创作文字,比如写故事、写公文、写邮件、写剧本、逻辑推理、编程等等,还能表达观点,玩游戏等。我支持多种语言,包括但不限于中文、英文、德语、法语、西班牙语等。\n\n如果你有任何问题或需要帮助,尽管告诉我,我会尽力提供支持!😊",
"refusal": null,
"annotations": null,
"audio": null,
"function_call": null,
"tool_calls": [],
"reasoning_content": null
},
"logprobs": null,
"finish_reason": "stop",
"stop_reason": null
}
],
"service_tier": null,
"system_fingerprint": null,
"usage": {
"prompt_tokens": 12,
"total_tokens": 106,
"completion_tokens": 94,
"prompt_tokens_details": null
},
"prompt_logprobs": null,
"kv_transfer_params": null
}

3. Tokenize / Detokenize

3.1 Tokenize,使用 prompt 字段

1
2
3
4
curl http://172.22.215.77:6080/tokenize \
-H "Authorization: Bearer 123456" \
-H "Content-Type: application/json" \
-d '{"prompt": "Hello world"}'

返回:

1
{"count":2,"max_model_len":163840,"tokens":[9707,1879],"token_strs":null}

3.2 Tokenize,使用 messages(类似 Chat Completion)

1
2
3
4
5
6
7
8
curl http://172.22.215.77:6080/tokenize \
-H "Authorization: Bearer 123456" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "Hello world"}
]
}'

返回:

1
{"count":10,"max_model_len":163840,"tokens":[151644,872,198,9707,1879,151645,198,151644,77091,198],"token_strs":null}

3.3 Detokenize

1
2
3
4
curl http://172.22.215.77:6080/detokenize \
-H "Authorization: Bearer 123456" \
-H "Content-Type: application/json" \
-d '{"tokens": [151644,872,198,9707,1879,151645,198,151644,77091,198]}'

返回:

1
{"prompt":"<|im_start|>user\nHello world<|im_end|>\n<|im_start|>assistant\n"}

4. Embeddings

1
2
3
4
5
6
7
curl http://172.22.215.77:6080/v1/embeddings \
-H "Authorization: Bearer 123456" \
-H "Content-Type: application/json" \
-d '{
"model": "bge-large",
"input": "自然语言处理很有趣"
}'

示例返回:

1
2
3
4
5
{
"data": [
{"embedding": [0.012, -0.031, ...], "index": 0}
]
}

5. Rerank

1
2
3
4
5
6
7
8
9
10
11
12
curl http://172.22.215.77:6080/v1/rerank \
-H "Authorization: Bearer 123456" \
-H "Content-Type: application/json" \
-d '{
"model": "bge-reranker-large",
"query": "人工智能的应用有哪些?",
"documents": [
"人工智能用于医疗诊断。",
"人工智能和足球没有关系。",
"人工智能推动自动驾驶。"
]
}'

示例返回:

1
2
3
4
5
6
7
{
"results": [
{"index": 0, "score": 0.95},
{"index": 2, "score": 0.88},
{"index": 1, "score": 0.05}
]
}

三、总结

  • vLLM 提供了 与 OpenAI API 高度兼容的接口

  • 常用接口:

    • /v1/chat/completions → 聊天对话
    • /v1/completions → 文本补全
    • /v1/embeddings → 向量嵌入
    • /v1/rerank → 文档重排序