Create a Chat Completion

Submit conversation messages and receive a generated reply.

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

Before you begin

  1. Prepare a personal access token or service account API key for Genesis. See Access credentials for credential selection and permissions.

  2. Use List callable models to select a model that supports Chat Completions.

Request body

Replace $GENESIS_ACCESS_TOKEN and $MODEL_ID with your credential and the selected 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
}'

The table covers text conversation fields. Fields for image input and tool calls are not expanded here.

Parameter

Type

Required

Description

model

string

Yes

Selected model ID from data[].id in the model list.

messages

array of object

Yes

Nonempty messages in conversation order.

messages[].role

string

Yes

Use system, user, or assistant for text conversations.

messages[].content

string or array of object

No

Text or text blocks.

messages[].content[].type

string

Required for a text block

Use text for a text block.

messages[].content[].text

string

Required for a text block

Nonempty text content.

max_tokens

integer

No

Output token limit; reasoning models may also use this budget for reasoning tokens. Use a model-supported value.

temperature

number

No

Sampling temperature, when supported by the model.

top_p

number

No

Nucleus sampling parameter, when supported by the model.

stream

boolean

No

Set to true for streaming deltas.

Successful response

{
  "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
  }
}

Field

Type

Description

id

string

Completion identifier.

object

string

Object type chat.completion.

model

string

Model that processed the request.

choices

array of object

Generated choices.

choices[].index

integer

Zero-based choice index.

choices[].message

object

Generated assistant message.

choices[].message.role

string

Message role assistant.

choices[].message.content

string or null

Generated text; it can be null in non-text results.

choices[].finish_reason

string or null

Stop reason; stop indicates a normal stop and length a length limit.

usage

object

Token usage returned by the model.

usage.prompt_tokens

integer

Input tokens.

usage.completion_tokens

integer

Generated tokens.

usage.total_tokens

integer

Total input and generated tokens.

Streaming response

Set stream to true in the request body to receive generated content incrementally. The service sends server-sent events (SSE) and ends the response with 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]

Field

Type

Description

choices[].index

integer

Choice index used to distinguish generated results.

choices[].delta.content

string

Incremental text to append to the corresponding result in receive order.

choices[].finish_reason

string or null

Reason the corresponding result ended; null while generation is in progress.

Parse complete SSE events. A network read can contain part of an event or multiple events, so buffer data before parsing. If the connection closes before data: [DONE], the content received so far may be incomplete.

Error response

A missing model ID returns HTTP 400. Supply the model ID before sending the request again.

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

Field

Type

Description

error

object

Error details.

error.message

string

Error description; the example indicates a missing model ID.

error.type

string

Error type; may be omitted by a model service.

error.code

string or null

Error code; may be omitted by a model service.

Last updated on