Completions API

更新时间:
复制 MD 格式

The Completions API is designed for text completion scenarios, such as code completion and content continuation.

Note

This document applies only to the Chinese mainland (Beijing region). You must use an API key from the China (Beijing) region.

Supported models

The following Qwen Coder models are supported:

qwen-coder-turbo

Prerequisites

Obtain an API key and configure it as an environment variable. If you use the OpenAI software development kit (SDK), install the SDK.

Getting started

You can use the Completions API for text completion. Two text completion scenarios are supported:

  1. Generate content that follows a given prefix.

  2. Generate content between a given prefix and suffix.

Generating content that precedes a given suffix is not supported.

Quick start

Pass information such as a function name, input parameters, and usage instructions in the prefix. The Completions API returns the generated code.

The prompt template is:

<|fim_prefix|>{prefix_content}<|fim_suffix|>

In the template, {prefix_content} is the prefix information that you pass.

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="qwen-coder-turbo",
  prompt="<|fim_prefix|>Write a Python quick sort function, def quick_sort(arr):<|fim_suffix|>",
)

print(completion.choices[0].text)
import OpenAI from "openai";

const openai = new OpenAI(
    {
        // If you have not configured environment variables, 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: "qwen-coder-turbo",
        prompt: "<|fim_prefix|>Write a Python quick sort function, def quick_sort(arr):<|fim_suffix|>",
    });
    console.log(completion.choices[0].text)
}

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

Generate content between a prefix and a suffix

The Completions API can generate content between a given prefix and suffix. Pass information such as a function name, input parameters, and usage instructions in the prefix. Pass information such as the function's return parameters in the suffix. The Completions API returns the generated code.

The prompt template is:

<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|>

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

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):
'''
Reverse each word in the string (preserving the position of non-alphabetic characters) and maintain the word order.
    Example:
    reverse_words_with_special_chars("Hello, world!") -> "olleH, dlrow!"
    Parameters:
        s (str): The input string, which may contain punctuation.
    Returns:
        str: The processed string, where words are reversed but non-alphabetic characters remain in their original positions.
'''
"""

suffix_content = "return result"

completion = client.completions.create(
  model="qwen-coder-turbo",
  prompt=f"<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|>",
)

print(completion.choices[0].text)
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):
'''
Reverse each word in the string (preserving the position of non-alphabetic characters) and maintain the word order.
    Example:
    reverse_words_with_special_chars("Hello, world!") -> "olleH, dlrow!"
    Parameters:
        s (str): The input string, which may contain punctuation.
    Returns:
        str: The processed string, where words are reversed but non-alphabetic characters remain in their original positions.
'''
`;

const suffixContent = "return result";

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

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

main();
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-coder-turbo",
    "prompt": "<|fim_prefix|>def reverse_words_with_special_chars(s):\n\"\"\"\nReverse each word in the string (preserving the position of non-alphabetic characters) and maintain the word order.\n    Example:\n    reverse_words_with_special_chars(\"Hello, world!\") -> \"olleH, dlrow!\"\n    Parameters:\n        s (str): The input string, which may contain punctuation.\n    Returns:\n        str: The processed string, where words are reversed but non-alphabetic characters remain in their original positions.\n\"\"\"\n<|fim_suffix|>return result<|fim_middle|>"
}'

Input and output parameters

Input parameters

Parameter

Type

Required

Description

model

string

Yes

The name of the model to call.

prompt

string

Yes

The prompt for which to generate completions.

max_tokens

integer

No

The maximum number of tokens to return in the request.

max_tokens does not affect the generation process of the Large Language Model (LLM). If the number of tokens generated by the model exceeds max_tokens, the returned content is truncated.

temperature

float

No

The sampling temperature, which controls the diversity of the generated text.

A higher temperature results in more diverse text, while a lower temperature results in more deterministic text.

Valid values: [0, 2.0).

Because both temperature and top_p control text diversity, set only one of them.

top_p

float

No

The probability threshold for nucleus sampling, which controls the diversity of the generated text.

A higher top_p results in more diverse text, while a lower top_p results in more deterministic text.

Valid values: (0, 1.0].

Because both temperature and top_p control text diversity, set only one of them.

stream

boolean

No

Specifies whether to stream the response. Valid values:

  • false (default): Returns the result after all content is generated.

  • true: Streams the output as it is generated. A chunk is output immediately after a part of the content is generated.

stream_options

object

No

When streaming output is enabled, set this parameter to {"include_usage": true} to display the number of tokens used in the last line of the output.

stop

string or array

No

The model automatically stops generating text when it is about to include the string or token_id specified in the stop parameter.

You can pass sensitive words in the stop parameter to control the model's output.

seed

integer

No

Setting the seed parameter makes the text generation process more deterministic. This is typically used to ensure that the model produces consistent results for each run.

If you pass the same seed value for each model call and keep other parameters unchanged, the model returns the same result as much as possible.

The value must be in the range of 0 to 231−1.

presence_penalty

float

No

Controls the degree of content repetition when the model generates text.

The value must be in the range of [-2.0, 2.0]. A positive value reduces repetition, and a negative value increases repetition.

Output 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 content generated by the request.

choices[0].finish_reason

string

The reason why the model stopped generating content.

choices[0].index

integer

The index of the current element in the array. The value is fixed at 0.

choices[0].logprobs

object

This is currently fixed at null.

created

integer

The time when the request was created.

model

string

The name of the model used for the request.

system_fingerprint

string

This parameter is currently fixed at null.

object

string

The object type. This is always "text_completion".

usage

object

The usage statistics for the 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 codes for resolution.