Content generation service

Updated at:

This topic describes the API parameters of the OpenAI-compatible content generation service.

URL

{host}/compatible-mode/v1/chat/completions

host: the endpoint used to call the service. You can call the API service over the Internet or a virtual private cloud (VPC). For more information, see Obtain a service endpoint.

AI API key screenshot.png

Request parameters

Parameter

Type

Required

Description

Example

messages

List[Dict]

Yes

The list of messages in the conversation so far:

  • role specifies the role. Valid values: system, user, and assistant.

    • system: a system-level message. It can be used only as the first message in the conversation history (messages[0]). The system role is optional. If it is used, it must be at the very beginning of the list.

    • user and assistant: the conversation between the user and the model. These two roles should alternate in the conversation to simulate an actual conversation flow.

  • content specifies the message content. It cannot be empty.

[

{"role": "system", "content": "You are a robot assistant"},

{"role": "user", "content": "What is the capital of Henan Province?"},

{"role": "assistant", "content": "Zhengzhou"},

{"role": "user", "content": "What fun things are there to do there?"}

]

model

String

Yes

The service ID. For information about supported service IDs, see

Supported services。

ops-qwen-turbo

max_tokens

Int

No

The maximum number of tokens to generate for the chat completion. If generation has not finished when this value is reached, the value of finish_reason is length. Otherwise, the value is stop.

1024

temperature

Float

No

The temperature value controls the probability distribution over candidate words during text generation and is used to control the randomness and diversity of model responses. Valid values: [0, 2). A value of 0 is meaningless.

A higher temperature value lowers the peak of the probability distribution so that more low-probability words are selected, which produces more diverse results. A lower temperature value sharpens the peak of the probability distribution so that high-probability words are more likely to be selected, which produces more deterministic results.

1

top_p

Float

No

The probability threshold for nucleus sampling during generation. Valid values: (0, 1.0). A larger value results in higher randomness, and a smaller value results in higher determinism.

0.8

presence_penalty

Float

No

Controls the repetition across the entire sequence generated by the model. Valid values: [-2.0, 2.0]. Default value: 0.

Increasing presence_penalty reduces repetition in the generated content.

0

frequency_penalty

Float

No

The frequency penalty. Valid values: [-2.0, 2.0]. Default value: 0.

A positive value penalizes newly generated words based on their current frequency in the text, which reduces the likelihood that the model repeats the same phrases.

0

stop

String, List[String]

No

The stop words. The model automatically stops generating when the generated content is about to include the specified string or token_id. The generated content does not include the specified content. stop can be of the String or Array type.

Default: null

stream

Boolean

No

Specifies whether to use streaming output. When results are output in stream mode, the API returns a generator, and you must iterate over it to obtain the results. Each output is the incremental sequence generated so far. Default value: false.

false

Response parameters

Parameter

Type

Description

Example

id

String

The system-generated ID that identifies this call.

2244F3A8-4201-4F37-BF86-42013B1026D6

object

String

The object type. The value is fixed as chat.completion.

chat.completion

created

Long

The current UNIX timestamp. Unit: seconds.

1719313883

model

String

The name of the model that is called.

ops-qwen-turbo

choices.index

Int

The sequence number of the result generated by the model. 0 indicates the first result.

0

choices.message

Map

The message output by the model.

{

"role":"assistant",

"content":"This is an example"

}

choices.finish_reason

String

Non-streaming + streaming

  • stop: The model returned the complete output.

  • length: Content generation stopped because the generated content was too long. To increase the length of the generated content, adjust the max_tokens request parameter.

  • A value that starts with content_filter indicates the result of security filtering.

stop

usage.completion_tokens

Int

The length of the model-generated response after it is converted into tokens.

150

usage.prompt_tokens

Int

The number of tokens converted from the input content of the user request to the LLM.

180

usage.total_tokens

Int

The total number of tokens used, which is the sum of usage.prompt_tokens and usage.completion_tokens.

330

Sample curl request

curl http://xxxx-cn-shanghai.opensearch.aliyuncs.com/compatible-mode/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer Your-API-Key" \
  -d '{
        "model":"ops-qwen-turbo",
        "messages":[
            {"role": "system", "content": "You are a robot assistant"},
            {"role": "user", "content": "Recommend one science fiction book"}
         ]
  }'

Sample response

{
  "id":"fb4b3860e051ecad0b019971******",
  "object":"chat.completion",
  "created":1749804786,
  "model":"ops-qwen-turbo",
  "choices":
      [
         {
            "index":0,
            "message":
                {
                  "role":"assistant",
                  "content":"The Three-Body Problem series by Liu Cixin. It tells the story of......"
                 },
                  "finish_reason":"stop"
           }
        ],
     "usage":
         {
             "prompt_tokens":22,
             "completion_tokens":48,
             "total_tokens":70
           }
  }           

Status codes

For more information, see API and SDK status codes and error codes of AI Search Open Platform.