Appearance
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:
Generate text continuation from a given prefix.
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
| Parameter | Type | Required | Description |
|---|---|---|---|
| model | string | Yes | The name of the model to use. |
| prompt | string | Yes | The prompt to use for generating completions. |
| max_tokens | integer | No | The 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. |
| temperature | float | No | The 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_p | float | No | The 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. |
| stream | boolean | No | Specifies 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_options | object | No | When streaming is enabled, set this parameter to {"include_usage": true} to include token usage in the last output line. |
| stop | string or array | No | The model stops generating when it encounters a string or token_id in the `stop` parameter.Use stop sequences to filter out unwanted content. |
| seed | integer | No | Setting 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_penalty | float | No | Controls 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** | Type | Description |
|---|---|---|
| id | string | The unique identifier for the call. |
| choices | array | An array of model-generated content. |
| choices[0].text | string | The model-generated content for this request. |
| choices[0].finish_reason | string | Why the model stopped generating. |
| choices[0].index | integer | The index of the current element in the array, always 0. |
| choices[0].logprobs | object | This parameter is always null. |
| created | integer | The UNIX timestamp of request creation. |
| model | string | The model name used for this request. |
| system_fingerprint | string | This parameter is always null. |
| object | string | The object type, which is always "text_completion". |
| usage | object | Usage statistics for this request. |
| usage.prompt_tokens | integer | The number of tokens in the prompt. |
| usage.completion_tokens | integer | The number of tokens in choices\[0\].text. |
| usage.total_tokens | integer | The 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.