Todos los productos
Search
Centro de documentos

Alibaba Cloud Model Studio:Modo parcial

Última actualización: Oct 12, 2026

Para escenarios como la finalización de código y la continuación de texto, puede generar nuevo contenido a partir de un fragmento de texto existente (prefijo). El Modo Parcial garantiza que la salida del modelo se conecte perfectamente con su prefijo para mejorar la precisión y el control.

Funcionamiento

Para usar el Modo Parcial, configure el arreglo messages. En el último mensaje del arreglo, establezca el role como assistant y proporcione el prefijo en el campo content. También debe establecer el parámetro "partial": true en ese mensaje. El formato de messages es el siguiente:

[
    {
        "role": "user",
        "content": "Complete this Fibonacci function. Do not add anything else."
    },
    {
        "role": "assistant",
        "content": "def calculate_fibonacci(n):\n    if n <= 1:\n        return n\n    else:\n",
        "partial": true
    }
]

A continuación, el modelo comienza a generar texto a partir del prefijo especificado.

Modelos admitidos

  • Modelos de generación de texto
    • Qwen-Max (modo sin razonamiento): serie Qwen3.7-Max, serie Qwen3.6-Max, serie Qwen3-Max, serie Qwen-Max
    • Qwen-Plus (modo sin razonamiento): serie Qwen3.7-Plus, serie Qwen3.6-Plus, serie Qwen3.5-Plus, serie Qwen-Plus
    • Qwen-Flash (modo sin razonamiento): serie Qwen3.7-Flash, serie Qwen3.6-Flash, serie Qwen3.5-Flash, serie Qwen-Flash
    • Qwen-Coder: serie Qwen3-Coder, serie Qwen2.5-Coder, serie Qwen-Coder
    • Qwen-Turbo (modo sin razonamiento): serie Qwen-Turbo
    • Serie de código abierto Qwen3.6 (modo sin razonamiento)
    • Serie de código abierto Qwen3.5 (modo sin razonamiento)
    • Serie de código abierto Qwen3 (modo sin razonamiento)
    • Serie de código abierto Qwen2.5
    • Qwen-Math: serie Qwen-Math, serie Qwen2.5-Math
    • DeepSeek (despliegue en SiliconFlow): siliconflow/deepseek-v3.1-terminus (modo sin razonamiento), siliconflow/deepseek-v3-0324
    • DeepSeek (despliegue en Vanchin): vanchin/deepseek-v3.2-think
  • Modelos multimodales
    • Qwen-VL: serie Qwen3-VL-Plus, serie Qwen3-VL-Flash, serie Qwen-VL-Max, serie Qwen-VL-Plus
    • Serie de código abierto Qwen3-VL (modo sin razonamiento)
    • Kimi (desplegado en Moonshot AI): kimi/kimi-k3, kimi/kimi-k2.6, kimi/kimi-k2.5

Introducción

Requisitos previos

Antes de comenzar, obtenga una clave API y configure la clave API como variable de entorno. Si llama al servicio mediante el SDK de OpenAI o el SDK de DashScope, debe instalar el SDK. Si es miembro de un sub-espacio de trabajo, asegúrese de que el superadministrador haya concedido acceso al modelo a su espacio de trabajo.

NotaEl SDK de Java de DashScope no es compatible.

Código de ejemplo

La finalización de código es el caso de uso principal del Modo Parcial. El siguiente ejemplo muestra cómo completar una función de Python.

Compatible con OpenAI

Python

import os
from openai import OpenAI

# 1. Initialize the client
client = OpenAI(
    # If not set in environment, replace the next line with: api_key="sk-xxx"
    # API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # Beijing region. For Singapore models use: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)

# 2. Define the code prefix to complete
prefix = """def calculate_fibonacci(n):
    if n <= 1:
        return n
    else:
"""

# 3. Make a Partial Mode request
# Note: The last message in the messages array must have role "assistant" and include "partial": True
completion = client.chat.completions.create(
    model="qwen3.8-max",
    messages=[
        {"role": "user", "content": "Complete this Fibonacci function. Do not add anything else."},
        {"role": "assistant", "content": prefix, "partial": True},
    ],
)

# 4. Manually join the prefix and the model's generated content
generated_code = completion.choices[0].message.content
complete_code = prefix + generated_code

print(complete_code)

Respuesta

def calculate_fibonacci(n):
    if n <= 1:
        return n
    else:
        return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    // If not set in environment, replace the next line with: apiKey: "sk-xxx"
    // API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    apiKey: process.env.DASHSCOPE_API_KEY,
    // Beijing region. For Singapore models use: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1
    baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
});

// Define the code prefix to complete
const prefix = `def calculate_fibonacci(n):
    if n <= 1:
        return n
    else:
`;

const completion = await openai.chat.completions.create({
    model: "qwen3.8-max",  // Use a code model
    messages: [
        { role: "user", content: "Complete this Fibonacci function. Do not add anything else." },
        { role: "assistant", content: prefix, partial: true }
    ],
});

// Manually join the prefix and the model's generated content
const generatedCode = completion.choices[0].message.content;
const completeCode = prefix + generatedCode;

console.log(completeCode);

curl

# ======= Important notice =======
# API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# Beijing region. For Singapore models use: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions
# === Remove this comment before running ===
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.8-max",
    "messages": [
        {
            "role": "user",
            "content": "Complete this Fibonacci function. Do not add anything else."
        },
        {
            "role": "assistant",
            "content": "def calculate_fibonacci(n):\n    if n <= 1:\n        return n\n    else:\n",
            "partial": true
        }
    ]
}'

Respuesta

{
    "choices": [
        {
            "message": {
                "content": "        return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)",
                "role": "assistant"
            },
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null
        }
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 48,
        "completion_tokens": 19,
        "total_tokens": 67,
        "prompt_tokens_details": {
            "cache_type": "implicit",
            "cached_tokens": 0
        }
    },
    "created": 1756800231,
    "system_fingerprint": null,
    "model": "qwen3.8-max",
    "id": "chatcmpl-d103b1cf-4bda-942f-92d6-d7ecabfeeccb"
}

DashScope

Python

import os
import dashscope

# The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"
messages = [
    {
        "role": "user",
        "content": "Complete this Fibonacci function. Do not add anything else."
    },
    {
        "role": "assistant",
        "content": "def calculate_fibonacci(n):\n    if n <= 1:\n        return n\n    else:\n",
        "partial": True
    }
]
response = dashscope.Generation.call(
    # If not set in environment, replace the next line with: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model='qwen3.8-max',
    messages=messages,
    result_format='message',
)

# Manually join the prefix and the model's generated content
prefix = "def calculate_fibonacci(n):\n    if n <= 1:\n        return n\n    else:\n"
generated_code = response.output.choices[0].message.content
complete_code = prefix + generated_code

print(complete_code)

Respuesta

def calculate_fibonacci(n):
    if n <= 1:
        return n
    else:
        return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)

curl

# ======= Important notice =======
# API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# Beijing region. For Singapore models use: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation
# === Remove this comment before running ===
curl -X POST "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.8-max",
    "input":{
        "messages":[
            {
                "role": "user",
                "content": "Complete this Fibonacci function. Do not add anything else."
            },
            {
                "role": "assistant",
                "content": "def calculate_fibonacci(n):\n    if n <= 1:\n        return n\n    else:\n",
                "partial": true
            }
        ]
    },
    "parameters": {
        "result_format": "message"
    }
}'

Respuesta

{
    "output": {
        "choices": [
            {
                "message": {
                    "content": "        return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)",
                    "role": "assistant"
                },
                "finish_reason": "stop"
            }
        ]
    },
    "usage": {
        "total_tokens": 67,
        "output_tokens": 19,
        "input_tokens": 48,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    },
    "request_id": "c61c62e5-cf97-90bc-a4ee-50e5e117b93f"
}

Casos de uso

Pasar imágenes o videos

Los modelos Qwen-VL admiten el Modo Parcial con datos de imagen o video, lo cual es útil para escenarios como descripciones de productos, publicaciones en redes sociales, artículos de noticias y redacción creativa.

Compatible con OpenAI

Python

import os
from openai import OpenAI

client = OpenAI(
    # If not set in environment, replace the next line with: api_key="sk-xxx",
    # API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # Beijing region. For Singapore models use: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    model="qwen3-vl-plus",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://img.alicdn.com/imgextra/i3/O1CN01zFX2Bs1Q0f9pESgPC_!!6000000001914-2-tps-450-450.png"
                    },
                },
                {"type": "text", "text": "I want to post this on social media. Help me write a caption."},
            ],
        },
        {
            "role": "assistant",
            "content": "Today I discovered a hidden-gem café",
            "partial": True,
        },
    ],
)
print(completion.choices[0].message.content)

Respuesta

— the tiramisu here is pure bliss! Every bite delivers perfect harmony between coffee and cream. Pure joy! #FoodShare #Tiramisu #CoffeeTime

Hope you like this caption! Let me know if you need any changes.

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    // If you have not set the environment variable, replace the next line with: apiKey: "sk-xxx"
    // API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    apiKey: process.env.DASHSCOPE_API_KEY,
    // Beijing region. For Singapore models use: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1
    baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
});

async function main() {
    const response = await openai.chat.completions.create({
        model: "qwen3-vl-plus",
        messages: [
            {
                role: "user",
                content: [
                    {
                        type: "image_url",
                        image_url: {
                            "url": "https://img.alicdn.com/imgextra/i3/O1CN01zFX2Bs1Q0f9pESgPC_!!6000000001914-2-tps-450-450.png"
                        }
                    },
                    {
                        type: "text",
                        text: "I want to post this on social media. Help me write a caption."
                    }
                ]
            },
            {
                role: "assistant",
                content: "Today I discovered a hidden-gem café",
                "partial": true
            }
        ]
    });
    console.log(response.choices[0].message.content);
}

main();

curl

# ======= Important notice =======
# API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# This is the base URL for the Beijing region. For Singapore region models, replace base_url with: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions
# === Remove this comment before running ===
curl -X POST 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
  "model": "qwen3-vl-plus",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "image_url",
          "image_url": {
            "url": "https://img.alicdn.com/imgextra/i3/O1CN01zFX2Bs1Q0f9pESgPC_!!6000000001914-2-tps-450-450.png"
          }
        },
        {
          "type": "text",
          "text": "I want to post this on social media. Help me write a caption."
        }
      ]
    },
    {
      "role": "assistant",
      "content": "Today I discovered a hidden-gem café",
      "partial": true
    }
  ]
}'

Respuesta

{
    "choices": [
        {
            "message": {
                "content": "— the tiramisu here is pure bliss! Every bite delivers perfect harmony between coffee and cream. Pure joy! #FoodShare #Tiramisu #CoffeeTime\n\nHope you like this caption! Let me know if you need any changes.",
                "role": "assistant"
            },
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null
        }
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 282,
        "completion_tokens": 56,
        "total_tokens": 338,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    },
    "created": 1756802933,
    "system_fingerprint": null,
    "model": "qwen3-vl-plus",
    "id": "chatcmpl-5780cbb7-ebae-9c63-b098-f8cc49e321f0"
}

DashScope

Python

import os
import dashscope

# The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"

messages = [
    {
        "role": "user",
        "content": [
            {
                "image": "https://img.alicdn.com/imgextra/i3/O1CN01zFX2Bs1Q0f9pESgPC_!!6000000001914-2-tps-450-450.png"
            },
            {"text": "I want to post this on social media. Help me write a caption."},
        ],
    },
    {"role": "assistant", "content": "Today I discovered a hidden-gem café", "partial": True},
]

response = dashscope.MultiModalConversation.call(
    # If you have not set the environment variable, replace the next line with: api_key ="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model="qwen3-vl-plus",
    messages=messages
)

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

Respuesta

— the tiramisu here is pure bliss! Every bite delivers perfect harmony between coffee and cream. Pure joy! #FoodShare #Tiramisu #CoffeeTime

Hope you like this caption! Let me know if you need any changes.

curl

# ======= Important notice =======
# API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# Beijing region. For Singapore models use: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation
# === Remove this comment before running ===
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
    "model": "qwen3-vl-plus",
    "input":{
        "messages":[
            {"role": "user",
             "content": [
               {"image": "https://img.alicdn.com/imgextra/i3/O1CN01zFX2Bs1Q0f9pESgPC_!!6000000001914-2-tps-450-450.png"},
               {"text": "I want to post this on social media. Help me write a caption."}]
            },
            {"role": "assistant",
             "content": "Today I discovered a hidden-gem café",
             "partial": true
            }
        ]
    }
}'

Respuesta

{
    "output": {
        "choices": [
            {
                "message": {
                    "content": [
                        {
                            "text": "— the tiramisu here is pure bliss! Every bite delivers perfect harmony between coffee and cream. Pure joy! #FoodShare #Tiramisu #CoffeeTime\n\nHope you like this caption! Let me know if you need any changes."
                        }
                    ],
                    "role": "assistant"
                },
                "finish_reason": "stop"
            }
        ]
    },
    "usage": {
        "total_tokens": 339,
        "input_tokens_details": {
            "image_tokens": 258,
            "text_tokens": 24
        },
        "output_tokens": 57,
        "input_tokens": 282,
        "output_tokens_details": {
            "text_tokens": 57
        },
        "image_tokens": 258
    },
    "request_id": "c741328c-23dc-9286-bfa7-626a4092ca09"
}

Continue desde una salida incompleta

Puede utilizar el Modo Parcial para continuar desde una salida incompleta del LLM y garantizar resultados semánticamente completos. La salida incompleta puede ocurrir por las siguientes razones:

  • El valor del parámetro max_tokens es demasiado pequeño, lo que provoca que el modelo trunque su salida.

  • Una respuesta sin streaming agota el tiempo de espera. En este caso, el modelo devuelve el contenido que ha generado hasta ese momento.

    Los tiempos de espera ya no causan errores. En su lugar, el modelo devuelve el contenido generado hasta ese momento. Consulte Cómo manejar los tiempos de espera del modelo.

Compatible con OpenAI

Python

import os
from openai import OpenAI

# Initialize the client
client = OpenAI(
    # If not set in environment, replace the next line with: api_key="sk-xxx",
    # API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # Beijing region. For Singapore models use: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)

def chat_completion(messages,max_tokens=None):
    response = client.chat.completions.create(
        model="qwen-plus",
        messages=messages,
        max_tokens=max_tokens
    )
    print(f"### Reason generation stopped: {response.choices[0].finish_reason}")

    return response.choices[0].message.content

# Example usage
messages = [{"role": "user", "content": "Write a short sci-fi story"}]

# First call with max_tokens set to 40
first_content = chat_completion(messages, max_tokens=40)
print(first_content)
# Add the first response as an assistant message and set partial=True
messages.append({"role": "assistant", "content": first_content, "partial": True})

# Second call
second_content = chat_completion(messages)
print("### Complete content:")
print(first_content+second_content)

Respuesta

length: Se alcanzó el límite de max_tokens. stop: El modelo finalizó naturalmente o encontró una palabra de parada del parámetro stop.

### Reason generation stopped: length
**"The End of Memory"**

In the distant future, Earth is no longer fit for human life. The atmosphere is polluted, oceans are dry, and cities lie in ruins. Humans migrated to a habitable planet named "Eden," with blue skies, fresh air, and endless resources.

However, Eden is not a true paradise. It holds no human history, no past, and no memory.

...
**"If we forget who we are, are we still human?"**

— End —

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    // If not set in environment, replace the next line with: apiKey: "sk-xxx"
    // API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    apiKey: process.env.DASHSCOPE_API_KEY,
    // Beijing region. For Singapore models use: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1
    baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
});

async function chatCompletion(messages, maxTokens = null) {
    const completion = await openai.chat.completions.create({
        model: "qwen-plus",
        messages: messages,
        max_tokens: maxTokens
    });

    console.log(`### Reason generation stopped: ${completion.choices[0].finish_reason}`);
    return completion.choices[0].message.content;
}

// Example usage
async function main() {
    let messages = [{"role": "user", "content": "Write a short sci-fi story"}];

    try {
        // First call with max_tokens set to 40
        const firstContent = await chatCompletion(messages, 40);
        console.log(firstContent);

        // Add the first response as an assistant message and set partial=true
        messages.push({"role": "assistant", "content": firstContent, "partial": true});

        // Second call
        const secondContent = await chatCompletion(messages);
        console.log("### Complete content:");
        console.log(firstContent + secondContent);

    } catch (error) {
        console.error('Execution error:', error);
    }
}

// Run the example
main();

DashScope

Python

Código de ejemplo

import os
import dashscope

# The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"

def chat_completion(messages, max_tokens=None):
    response = dashscope.Generation.call(
        # If not set in environment, replace the next line with: api_key="sk-xxx",
        # API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        model='qwen-plus',
        messages=messages,
        max_tokens=max_tokens,
        result_format='message',
    )

    print(f"### Reason generation stopped: {response.output.choices[0].finish_reason}")
    return response.output.choices[0].message.content

# Example usage
messages = [{"role": "user", "content": "Write a short sci-fi story"}]

# First call with max_tokens set to 40
first_content = chat_completion(messages, max_tokens=40)
print(first_content)

# Add the first response as an assistant message and set partial=True
messages.append({"role": "assistant", "content": first_content, "partial": True})

# Second call
second_content = chat_completion(messages)
print("### Complete content:")
print(first_content + second_content)

Respuesta

### Reason generation stopped: length
Title: **"Origami Time"**

---

In 2179, humanity finally mastered time travel. But this technology did not rely on massive machines or complex energy fields. It relied on paper.

A single sheet of paper.

It was called "Origami Time," made from an unknown alien material. Scientists could not explain how it worked. They only knew that drawing a scene on the paper and folding it in a specific way opened a door to the past or future.

...

"You are not the key to time. You are just a reminder that our future is always in our hands."

Then I tore it into pieces.

---

**(End)**

Facturación

La facturación cubre los tokens de entrada y salida. El prefijo cuenta como parte de los tokens de entrada.

Códigos de error

Si la llamada al modelo falla y devuelve un mensaje de error, consulte Códigos de error para su resolución.