创建 Chat Completion

提交对话消息,取得模型生成的回复。

POST https://token.moi.matrixorigin.cn/v1/chat/completions

调用前准备

  1. 准备个人访问令牌或服务账号 API Key。创建方法见管理 Genesis 访问凭据。

  2. 将 type 设为 chat,调用查询可调用模型。

  3. 将所选模型的 id 填入请求体的 model。

认证方式见 Genesis 凭据规则。

请求体

将示例中的 $GENESIS_ACCESS_TOKEN、$MODEL_ID 替换为实际值。

curl -X POST \
  "https://token.moi.matrixorigin.cn/v1/chat/completions" \
  -H "Authorization: Bearer $GENESIS_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "'"$MODEL_ID"'",
  "messages": [
    {
      "role": "user",
      "content": "Reply with OK."
    }
  ],
  "max_tokens": 64
}'

参数

类型

是否必填

说明

model

string

是

所选模型的 ID,取自模型查询结果中的 data[].id。

messages

array of object

是

按对话顺序排列的非空消息列表。

messages[].role

string

是

文本对话使用 system、user 或 assistant。

messages[].content

string 或 array of object

否

文本内容或文本块列表。

messages[].content[].type

string

文本块中必填

文本块填写 text。

messages[].content[].text

string

文本块中必填

非空文本内容。

max_tokens

integer

否

输出 Token 上限。推理模型的预算还可能包含推理 Token。按模型支持的范围设置。

temperature

number

否

采样温度,仅在模型支持时设置。

top_p

number

否

核采样参数,仅在模型支持时设置。

stream

boolean

否

设为 true 返回流式增量。

成功响应

{
  "id": "chatcmpl-example",
  "object": "chat.completion",
  "model": "MODEL_ID",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "OK"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 1,
    "total_tokens": 13
  }
}

字段

类型

说明

id

string

本次生成的标识。

object

string

对象类型 chat.completion。

model

string

处理请求的模型。

choices

array of object

生成结果。

choices[].index

integer

结果序号,从 0 开始。

choices[].message

object

生成的助手消息。

choices[].message.role

string

消息角色 assistant。

choices[].message.content

string 或 null

生成文本。某些非文本结果中可能为空。

choices[].finish_reason

string 或 null

结束原因。stop 表示正常停止,length 表示达到长度限制。

usage

object

模型返回的 Token 用量。

usage.prompt_tokens

integer

输入 Token 数。

usage.completion_tokens

integer

生成 Token 数。

usage.total_tokens

integer

输入与生成 Token 总数。

流式响应

在请求体中将 stream 设为 true,逐段接收模型生成的内容。服务通过服务器发送事件(SSE)返回增量结果,发送 data: [DONE] 后结束响应。

流式请求

curl -N -X POST \
  "https://token.moi.matrixorigin.cn/v1/chat/completions" \
  -H "Authorization: Bearer $GENESIS_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "'"$MODEL_ID"'",
  "messages": [
    {
      "role": "user",
      "content": "Reply with OK."
    }
  ],
  "max_tokens": 64,
  "stream": true
}'

流式事件

data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"OK"},"finish_reason":null}]}

data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]

字段

类型

说明

choices[].index

integer

生成结果的序号,用于区分不同结果。

choices[].delta.content

string

本次返回的增量文本,按接收顺序追加到对应结果。

choices[].finish_reason

string 或 null

对应结果的生成结束原因。尚未结束时为 null。

每次网络读取可能包含部分事件或多个事件。

  1. 缓冲每次读取的数据。

  2. 按完整 SSE 事件解析缓冲区中的数据。

如果连接在收到 data: [DONE] 前断开,生成结果可能不完整。

错误响应

缺少必填模型 ID 时返回 HTTP 400。补充模型 ID 后重新提交。

所选模型没有可用的 Chat Completions 调用入口时,返回 HTTP 503。error.code 和 error.type 均为 endpoint_capability_mismatch。遇到此错误时,选择其他 chat 模型重试。

{
  "error": {
    "message": "missing required parameter: model",
    "type": "invalid_request",
    "code": "invalid_request"
  }
}

字段

类型

说明

error

object

错误信息。

error.message

string

错误原因。示例表示缺少模型 ID。

error.type

string

错误类别,例如 invalid_request 或 endpoint_capability_mismatch。模型服务返回时可能省略。

error.code

string 或 null

错误代码,例如 invalid_request 或 endpoint_capability_mismatch。模型服务返回时可能省略。

最后更新于