Specify prefix continuation (Partial mode)

Updated at: 2025-02-28 05:37

In certain scenarios, you may require the model to continue its generation after a specified prefix. Partial Mode in the Qwen API enables this feature, allowing the large model to generate content that follows the starting text you provide.

Supported models

  • Qwen-Max

    qwen-max, qwen-max-latest, qwen-max-0125

  • Qwen-Plus

    qwen-plus, qwen-plus-latest, qwen-plus-0125

  • Qwen-Turbo

    qwen-turbo, qwen-turbo-latest, qwen-turbo-1101

  • Open source Qwen

    qwen2.5 models

Get started

Prerequisites

To get started, you must obtain an API key and set the API key as an environment variable. If you're using the OpenAI SDK or DashScope SDK, you must install the appropriate SDK. For members of a sub-workspace, ensure the super administrator has authorized models for the sub-workspace.

How to use

You need to specify the prefix content in the last assistant message of the messages array and set the partial parameter to true.

Usually, the last message in the messages array is a user message. However, in Partial mode, the last message should be an assistant message containing the prefix information.
OpenAI compatible
DashScope
Python
Node.js
cURL

Sample code

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"), # If you have not configured the environment variable, replace DASHSCOPE_API_KEY with your API key
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",  # Fill in the base_url of the DashScope service
)
completion = client.chat.completions.create(
    model="qwen-plus",
    messages=[{
        "role": "user",
        "content": "Please continue the sentence 'Spring has come, the earth' to express the beauty of spring and the author's joy"
    },
    {
        "role": "assistant",
        "content": "Spring has come, the earth",
        "partial": True
    }]
    )
print(completion.choices[0].message.content)

Sample response

 awakens with vibrant life, painting a canvas of lush greens and colorful blooms that dance in the gentle breeze, filling my heart with an indescribable joy and a renewed sense of wonder.

Sample code

import OpenAI from "openai";

const openai = new OpenAI(
    {
        // If you have not configured the environment variable, replace the following line with: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
    }
);
const completion = await openai.chat.completions.create({
    model: "qwen-plus",  //Model list: https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
    messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "Please continue the sentence 'Spring has come, the earth' to express the beauty of spring and the author's joy" },
        { role: "assistant", content: "Spring has come, the earth", partial: true}
    ],
});
console.log(completion.choices[0].message.content)

Sample code

curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-plus",
    "messages": [{
        "role": "user",
        "content": "Please continue the sentence [Spring has come, the earth] to express the beauty of spring and the joy of the author."
    },
    {
        "role": "assistant",
        "content": "Spring has come, the earth",
        "partial": true
    }]
}'

Sample response

{
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": " awakens from its long slumber, bursting into a symphony of colors and life. Flowers bloom in vibrant hues, painting the landscape with strokes of pink, yellow, and purple, while tender green leaves unfurl on the trees, creating a lush canopy that whispers promises of warmth and growth. The air is filled with the sweet fragrance of blossoms and the cheerful songs of returning birds, bringing a sense of renewal and joy to all who witness this beautiful transformation."
            },
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null
        }
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 34,
        "completion_tokens": 125,
        "total_tokens": 159
    },
    "created": 1731898325,
    "system_fingerprint": null,
    "model": "qwen-plus",
    "id": "chatcmpl-244c5496-6a84-9380-951d-49d4691f252a"
}
The Java SDK is not currently supported.
Python
cURL

Sample code

import os
import dashscope
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

messages = [{
    "role": "user",
    "content": "Please continue the sentence 'Spring has come, the earth' to express the beauty of spring and the author's joy"
},
{
    "role": "assistant",
    "content": "Spring has come, the earth",
    "partial": True
}]
response = dashscope.Generation.call(
    # If you have not configured the environment variable, replace the following line with: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model='qwen-plus',
    messages=messages,
    result_format='message',  
)

print(response.output.choices[0].message.content)

Sample response

 awakens from its long slumber, bursting into a vibrant tapestry of colors and life, filling my heart with an indescribable joy and a renewed sense of hope.

Sample code

curl -X POST "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-plus",
    "input":{
        "messages":[{
            "role": "user",
            "content": "Please continue the sentence [Spring has come, the earth] to express the beauty of spring and the joy of the author."
        },
        {
            "role": "assistant",
            "content": "Spring has come, the earth",
            "partial": true
        }]
    },
    "parameters": {
        "result_format": "message"
    }
}'

Sample response

{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": " awakens from its long slumber, donning a vibrant cloak of green dotted with colorful blossoms, and my heart dances with the melody of birdsong, celebrating the rebirth and beauty that fills every corner of the world."
                }
            }
        ]
    },
    "usage": {
        "total_tokens": 177,
        "output_tokens": 143,
        "input_tokens": 34
    },
    "request_id": "bb4dab4a-aba1-9786-9857-d0743bab11e8"
}

The response does not contain the prefix you specified. You must manually concatenate the prefix with the response.

Error code

If the call failed and an error message is returned, see Error messages.

  • On this page (1)
  • Supported models
  • Get started
  • Prerequisites
  • How to use
  • Error code
Feedback