SGLang v1 对话接口调用结果解析

10次阅读
没有评论

一、原始 curl 请求说明

你调用的是 SGLang 兼容 OpenAI /v1/chat/completions 对话补全接口,地址本地端口 30000,入参仅传入单轮用户提问:法国首都是什么?

bash

运行

curl http://localhost:30000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "What is the capital of France?"}
    ]
  }'

二、返回 JSON 逐字段拆解

json

{
  "id": "f63280849be740baa013da223fd60c33",
  "object": "chat.completion",
  "created": 1783434321,
  "model": "default",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Paris",
        "reasoning_content": null,
        "tool_calls": null
      },
      "logprobs": null,
      "finish_reason": "stop",
      "matched_stop": 151645
    }
  ],
  "usage": {
    "prompt_tokens": 26,
    "total_tokens": 28,
    "completion_tokens": 2,
    "prompt_tokens_details": null,
    "reasoning_tokens": 0
  },
  "metadata": {
    "weight_version": "default"
  }
}

1. 基础元信息

表格

字段 含义
id 本次请求唯一会话 ID,用于日志、追踪、流式断点续传
object 固定标识为对话补全返回体,兼容 OpenAI 格式
created 时间戳,Unix 秒级时间
model 加载模型标识,未手动指定则为 default

2. choices 核心回答内容

  • index:0:多候选生成时序号,默认单条结果
  • message.content: Paris:模型最终回答,法国首都巴黎
  • reasoning_content:思考链内容,仅开启深度思考 / CoT 模型才会有值
  • tool_calls:工具调用字段,未配置函数调用则为空
  • finish_reason: stop:生成正常结束,命中终止符停止输出;常见其他值:length(超长截断)、abort(主动中断)
  • matched_stop:SGLang 内部匹配到的停止 token 编码 ID

3. usage 计费 / 统计 Token

  • prompt_tokens:26:输入提示词消耗 token 数
  • completion_tokens:2:模型输出内容占用 token(Paris 计 2 个 token)
  • total_tokens:28:输入 + 输出合计 token
  • reasoning_tokens:0:推理思考部分 token 消耗,无思考过程即为 0

4. metadata

SGLang 自定义扩展字段,weight_version 标记模型权重版本,多权重热切换场景使用。

三、常用扩展调用示例

1. 开启流式输出 stream

bash

运行

curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
    "stream": true,
    "messages": [{"role": "user", "content": "What is the capital of France?"}]
}'

2. 指定温度、最大生成长度

bash

运行

curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
    "temperature": 0.7,
    "max_tokens": 128,
    "messages": [{"role": "user", "content": "What is the capital of France?"}]
}'

四、小结

本次请求完全正常执行,SGLang 服务运行正常,接口兼容 OpenAI 格式,模型正确返回答案 Paris,token 计数与终止状态均无异常。

如果需要 Python 调用示例、批量请求、多轮对话、函数调用、LoRA 权重指定写法可以告诉我。

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)
验证码