Skip to content

OpenAI compatible - Completions

The Completions API supports text completion scenarios, including code completion and content continuation. Important

This document applies only to the China (Beijing) region. To use the model, you must use an API key from the China (Beijing) region.

Supported models

The following Qwen-Coder models are supported: qwen2.5-coder-7b-instruct, qwen2.5-coder-14b-instruct, qwen2.5-coder-32b-instruct, qwen-coder-turbo-0919, qwen-coder-turbo-latest, qwen-coder-turbo

Prerequisites

You have got an API key and exported it as an environment variable. If you use the OpenAI SDK, install the SDK first.

Usage

The Completions API supports text completion in the following scenarios:

  1. Generate text continuation from a given prefix.

  2. Generate intermediate content based on a given prefix and suffix.

The API does not support generating content that precedes a given suffix.

Getting started

Pass information such as the function name, input parameters, and usage instructions in the prefix. The API generates the corresponding code.

Use the following prompt template:

HELPCODEESCAPE-plaintext
<|fim_prefix|>{prefix_content}<|fim_suffix|>

In this template, {prefix_content} represents your prefix. Python

HELPCODEESCAPE-python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key=os.getenv("DASHSCOPE_API_KEY")
)

completion = client.completions.create(
  model="qwen2.5-coder-32b-instruct",
  prompt="<|fim_prefix|>Write a Python quick sort function, def quick_sort(arr):<|fim_suffix|>",
)

print(completion.choices[0].text)

Node.js

HELPCODEESCAPE-nodejs
import OpenAI from "openai";

const openai = new OpenAI(
    {
        // If you have not configured an environment variable, replace the following line with your Model Studio API key: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
    }
);

async function main() {
    const completion = await openai.completions.create({
        model: "qwen2.5-coder-32b-instruct",
        prompt: "<|fim_prefix|>Write a Python quick sort function, def quick_sort(arr):<|fim_suffix|>",
    });
    console.log(completion.choices[0].text)
}

main();

curl

HELPCODEESCAPE-curl
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen2.5-coder-32b-instruct",
    "prompt": "<|fim_prefix|>Write a Python quick sort function, def quick_sort(arr):<|fim_suffix|>"
}'

Generate intermediate content based on prefix and suffix

Pass information such as the function name, input parameters, and usage instructions in the prefix, and return parameters in the suffix. The API generates the corresponding code.

Use the following prompt template:

HELPCODEESCAPE-plaintext
<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|>

In this template, {prefix_content} is the prefix and {suffix_content} is the suffix. Python

HELPCODEESCAPE-python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key=os.getenv("DASHSCOPE_API_KEY")
)

prefix_content = f"""def reverse_words_with_special_chars(s):
'''
Reverses each word in a string (preserving the position of non-alphabetic characters) while maintaining the word order.
    Example:
    reverse_words_with_special_chars("Hello, world!") -> "olleH, dlrow!"
    Parameters:
        s (str): The input string (may contain punctuation).
    Returns:
        str: The processed string with words reversed but non-alphabetic characters in their original positions.
'''
"""

suffix_content = "return result"

completion = client.completions.create(
  model="qwen2.5-coder-32b-instruct",
  prompt=f"<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|>",
)

print(completion.choices[0].text)

Node.js

HELPCODEESCAPE-nodejs
import OpenAI from 'openai';


const client = new OpenAI({
  baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
  apiKey: process.env.DASHSCOPE_API_KEY
});

const prefixContent = `def reverse_words_with_special_chars(s):
'''
Reverses each word in a string (preserving the position of non-alphabetic characters) while maintaining the word order.
    Example:
    reverse_words_with_special_chars("Hello, world!") -> "olleH, dlrow!"
    Parameters:
        s (str): The input string (may contain punctuation).
    Returns:
        str: The processed string with words reversed but non-alphabetic characters in their original positions.
'''
`;

const suffixContent = "return result";

async function main() {
  const completion = await client.completions.create({
    model: "qwen2.5-coder-32b-instruct",
    prompt: `<|fim_prefix|>${prefixContent}<|fim_suffix|>${suffixContent}<|fim_middle|>`
  });

  console.log(completion.choices[0].text);
}

main();

curl

HELPCODEESCAPE-curl
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen2.5-coder-32b-instruct",
    "prompt": "<|fim_prefix|>def reverse_words_with_special_chars(s):\n\"\"\"\nReverses each word in a string (preserving the position of non-alphabetic characters) while maintaining the word order.\n    Example:\n    reverse_words_with_special_chars(\"Hello, world!\") -> \"olleH, dlrow!\"\n    Parameters:\n        s (str): The input string (may contain punctuation).\n    Returns:\n        str: The processed string with words reversed but non-alphabetic characters in their original positions.\n\"\"\"\n<|fim_suffix|>return result<|fim_middle|>"
}'

Parameters

Request parameters

ParameterTypeRequiredDescription
modelstringYesThe name of the model to use.
promptstringYesThe prompt to use for generating completions.
max_tokensintegerNoThe maximum number of tokens to include in the response. ** The max_tokens setting does not affect the model's generation process - if the model generates more tokens than the max_tokens value, the response is truncated.
temperaturefloatNoThe sampling temperature that controls the diversity of generated text.Higher values produce more diverse output, lower values produce more deterministic output.Valid values: [0, 2.0).Because both `temperature` and `top_p` control text diversity, specify only one of them.
top_pfloatNoThe probability threshold for nucleus sampling that controls the diversity of generated text.Higher values produce more diverse output, lower values produce more deterministic output.Valid values: (0, 1.0].Because both `temperature` and `top_p` control text diversity, specify only one of them.
streambooleanNoSpecifies whether to enable streaming output for the response. Valid values: - false (default): Return the result after all content is generated. - true: Stream content incrementally as chunks are generated.
stream_optionsobjectNoWhen streaming is enabled, set this parameter to {"include_usage": true} to include token usage in the last output line.
stopstring or arrayNoThe model stops generating when it encounters a string or token_id in the `stop` parameter.Use stop sequences to filter out unwanted content.
seedintegerNoSetting the `seed` parameter makes generation more deterministic for consistent results across runs.Pass the same `seed` value and keep other parameters unchanged to get consistent results.Valid values: 0 to 2 31 -1.
presence_penaltyfloatNoControls the level of content repetition in generated text.Valid values: [-2.0, 2.0]. Positive values reduce repetition, negative values increase repetition.

Response parameters

Parameter**TypeDescription
idstringThe unique identifier for the call.
choicesarrayAn array of model-generated content.
choices[0].textstringThe model-generated content for this request.
choices[0].finish_reasonstringWhy the model stopped generating.
choices[0].indexintegerThe index of the current element in the array, always 0.
choices[0].logprobsobjectThis parameter is always null.
createdintegerThe UNIX timestamp of request creation.
modelstringThe model name used for this request.
system_fingerprintstringThis parameter is always null.
objectstringThe object type, which is always "text_completion".
usageobjectUsage statistics for this request.
usage.prompt_tokensintegerThe number of tokens in the prompt.
usage.completion_tokensintegerThe number of tokens in choices\[0\].text.
usage.total_tokensintegerThe sum of usage.prompt_tokens and usage.completion_tokens.

Error codes

If the model call fails and returns an error message, see Error messages for resolution.

Mirror of Alibaba Cloud Model Studio docs for reference and RAG. Not affiliated with Alibaba Cloud.