Create a Chat Completion¶
Submit conversation messages and receive a generated reply.
POST https://token.moi.matrixorigin.cn/v1/chat/completions
Before you begin¶
Prepare a personal access token or service account API key for Genesis. See Access credentials for credential selection and permissions.
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 |
|---|---|---|---|
|
string |
Yes |
Selected model ID from |
|
array of object |
Yes |
Nonempty messages in conversation order. |
|
string |
Yes |
Use |
|
string or array of object |
No |
Text or text blocks. |
|
string |
Required for a text block |
Use |
|
string |
Required for a text block |
Nonempty text content. |
|
integer |
No |
Output token limit; reasoning models may also use this budget for reasoning tokens. Use a model-supported value. |
|
number |
No |
Sampling temperature, when supported by the model. |
|
number |
No |
Nucleus sampling parameter, when supported by the model. |
|
boolean |
No |
Set to |
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 |
|---|---|---|
|
string |
Completion identifier. |
|
string |
Object type |
|
string |
Model that processed the request. |
|
array of object |
Generated choices. |
|
integer |
Zero-based choice index. |
|
object |
Generated assistant message. |
|
string |
Message role |
|
string or null |
Generated text; it can be null in non-text results. |
|
string or null |
Stop reason; |
|
object |
Token usage returned by the model. |
|
integer |
Input tokens. |
|
integer |
Generated 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 |
|---|---|---|
|
integer |
Choice index used to distinguish generated results. |
|
string |
Incremental text to append to the corresponding result in receive order. |
|
string or null |
Reason the corresponding result ended; |
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 |
|---|---|---|
|
object |
Error details. |
|
string |
Error description; the example indicates a missing model ID. |
|
string |
Error type; may be omitted by a model service. |
|
string or null |
Error code; may be omitted by a model service. |