All Products
Search
Document Center

Alibaba Cloud Model Studio:Streaming output

Last Updated:Sep 01, 2026

Pada aplikasi chat real-time atau generasi teks panjang, waktu tunggu yang lama dapat menurunkan pengalaman pengguna dan memicu timeout di sisi server, sehingga menyebabkan tugas gagal. Streaming output mengatasi masalah ini dengan terus-menerus mengembalikan fragmen teks saat model menghasilkannya.

Cara kerja

Streaming output menggunakan protokol Server-Sent Events (SSE). Setelah permintaan streaming dimulai, server membentuk koneksi HTTP persisten dengan klien. Setiap kali model menghasilkan blok teks (disebut chunk), server segera mendorongnya melalui koneksi tersebut. Setelah seluruh konten dihasilkan, server mengirimkan sinyal akhir.

Klien mendengarkan aliran event dan menerima serta memproses chunk teks secara real-time—misalnya, merender karakter satu per satu pada antarmuka. Ini berbeda dari panggilan non-streaming yang mengembalikan seluruh konten sekaligus.

Komponen di atas hanya sebagai referensi dan tidak mengirimkan permintaan aktual.

Billing

Streaming output menggunakan aturan penagihan yang sama dengan panggilan non-streaming, yaitu dikenakan biaya berdasarkan jumlah token input dan token output dalam permintaan.

Jika permintaan terputus, token output hanya dihitung untuk bagian yang telah dihasilkan sebelum server menerima permintaan penghentian.

Cara menggunakan

PentingEdisi open-source Qwen3, edisi komersial dan open-source QwQ, QVQ, dan Qwen-Omni hanya mendukung streaming output.

Langkah 1: Konfigurasikan Kunci API Anda dan pilih wilayah

Anda harus telah memperoleh Kunci API dan mengonfigurasikannya sebagai variabel lingkungan.

Mengonfigurasi Kunci API Anda sebagai variabel lingkungan (DASHSCOPE_API_KEY) lebih aman daripada melakukan hardcoding di kode Anda.

Langkah 2: Lakukan permintaan streaming

Kompatibel dengan OpenAI

  • Cara mengaktifkan

    Atur stream ke true.

  • Lihat penggunaan token

    Protokol OpenAI tidak mengembalikan informasi penggunaan token secara default. Atur stream_options={"include_usage": true} agar chunk data terakhir yang dikembalikan mencakup informasi penggunaan token.

Python

import os
from openai import OpenAI

# 1. Persiapan: Inisialisasi client
client = OpenAI(
    # Konfigurasikan Kunci API menggunakan variabel lingkungan untuk menghindari hardcoding.
    api_key=os.environ["DASHSCOPE_API_KEY"],
    # Kunci API terikat erat pada wilayah. Pastikan base_url sesuai dengan wilayah Kunci API Anda.
    # URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

# 2. Lakukan permintaan streaming
completion = client.chat.completions.create(
    model="qwen-plus",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Please introduce yourself"}
    ],
    stream=True,
    stream_options={"include_usage": True}
)

# 3. Tangani respons streaming
# Simpan fragmen respons dalam daftar. Menggabungkannya di akhir lebih efisien daripada penggabungan string berulang.
content_parts = []
print("AI: ", end="", flush=True)

for chunk in completion:
    if chunk.choices:
        content = chunk.choices[0].delta.content or ""
        print(content, end="", flush=True)
        content_parts.append(content)
    elif chunk.usage:
        print("\n--- Request usage ---")
        print(f"Input Tokens: {chunk.usage.prompt_tokens}")
        print(f"Output Tokens: {chunk.usage.completion_tokens}")
        print(f"Total Tokens: {chunk.usage.total_tokens}")

full_response = "".join(content_parts)
# print(f"\n--- Full response ---\n{full_response}")

Respons

AI: Hello! I am Qwen, a large-scale language model independently developed by Tongyi Lab under Alibaba Group. I can answer questions, create content such as stories, official documents, emails, scripts, perform logical reasoning, programming, express opinions, play games, and more. I support multiple languages, including but not limited to Chinese, English, German, French, and Spanish. If you have any questions or need help, feel free to ask me anytime!
--- Request usage ---
Input Tokens: 26
Output Tokens: 87
Total Tokens: 113

Node.js

import OpenAI from "openai";

async function main() {
    // 1. Persiapan: Inisialisasi client
    // Konfigurasikan Kunci API menggunakan variabel lingkungan untuk menghindari hardcoding.
    if (!process.env.DASHSCOPE_API_KEY) {
        throw new Error("Set the DASHSCOPE_API_KEY environment variable");
    }
    // Kunci API terikat erat pada wilayah. Pastikan baseURL sesuai dengan wilayah Kunci API Anda.
    // URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
    const client = new OpenAI({
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
    });

    try {
        // 2. Lakukan permintaan streaming
        const stream = await client.chat.completions.create({
            model: "qwen-plus",
            messages: [
                { role: "system", content: "You are a helpful assistant." },
                { role: "user", content: "Please introduce yourself" },
            ],
            stream: true,
            // Tujuan: Mendapatkan penggunaan token pada chunk terakhir.
            stream_options: { include_usage: true },
        });

        // 3. Tangani respons streaming
        const contentParts = [];
        process.stdout.write("AI: ");

        for await (const chunk of stream) {
            // Chunk terakhir tidak berisi choices tetapi mencakup informasi penggunaan.
            if (chunk.choices && chunk.choices.length > 0) {
                const content = chunk.choices[0]?.delta?.content || "";
                process.stdout.write(content);
                contentParts.push(content);
            } else if (chunk.usage) {
                // Permintaan selesai. Cetak penggunaan token.
                console.log("\n--- Request usage ---");
                console.log(`Input Tokens: ${chunk.usage.prompt_tokens}`);
                console.log(`Output Tokens: ${chunk.usage.completion_tokens}`);
                console.log(`Total Tokens: ${chunk.usage.total_tokens}`);
            }
        }

        const fullResponse = contentParts.join("");
        // console.log(`\n--- Full response ---\n${fullResponse}`);

    } catch (error) {
        console.error("Request failed:", error);
    }
}

main();

Respons

AI: Hello! I am Qwen, a large-scale language model independently developed by Tongyi Lab under Alibaba Group. I can answer questions, create content such as stories, official documents, emails, scripts, perform logical reasoning, programming, express opinions, play games, and more. I support multiple languages, including but not limited to Chinese, English, German, French, and Spanish. If you have any questions or need help, feel free to ask me anytime!
--- Request usage ---
Input Tokens: 26
Output Tokens: 89
Total Tokens: 115

curl

Permintaan

# ======= Catatan penting =======
# Pastikan variabel lingkungan DASHSCOPE_API_KEY telah disetel
# Kunci API berbeda berdasarkan wilayah. Dapatkan Kunci API Anda: https://www.alibabacloud.com/help/zh/model-studio/get-api-key

# === Hapus komentar ini sebelum menjalankan ===

curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
--no-buffer \
-d '{
    "model": "qwen-plus",
    "messages": [
        {"role": "user", "content": "Who are you?"}
    ],
    "stream": true,
    "stream_options": {"include_usage": true}
}'

Respons

Respons mengikuti protokol SSE. Setiap baris yang diawali dengan data: merepresentasikan chunk data.

data: {"choices":[{"delta":{"content":"","role":"assistant"},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1726132850,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-428b414f-fdd4-94c6-b179-8f576ad653a8"}

data: {"choices":[{"finish_reason":null,"delta":{"content":"I am"},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1726132850,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-428b414f-fdd4-94c6-b179-8f576ad653a8"}

data: {"choices":[{"delta":{"content":" from"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1726132850,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-428b414f-fdd4-94c6-b179-8f576ad653a8"}

data: {"choices":[{"delta":{"content":" Alibaba"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1726132850,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-428b414f-fdd4-94c6-b179-8f576ad653a8"}

data: {"choices":[{"delta":{"content":"'s large-scale language"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1726132850,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-428b414f-fdd4-94c6-b179-8f576ad653a8"}

data: {"choices":[{"delta":{"content":" model, my name is Qwen"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1726132850,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-428b414f-fdd4-94c6-b179-8f576ad653a8"}

data: {"choices":[{"delta":{"content":"."},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1726132850,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-428b414f-fdd4-94c6-b179-8f576ad653a8"}

data: {"choices":[{"finish_reason":"stop","delta":{"content":""},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1726132850,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-428b414f-fdd4-94c6-b179-8f576ad653a8"}

data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":22,"completion_tokens":17,"total_tokens":39},"created":1726132850,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-428b414f-fdd4-94c6-b179-8f576ad653a8"}

data: [DONE]
  • data:: Muatan pesan, biasanya berupa string JSON.
  • [DONE]: Menandakan akhir dari seluruh respons streaming.

DashScope

  • Cara mengaktifkan

    Metode untuk mengaktifkan streaming output bervariasi berdasarkan SDK atau alat:

    • SDK Python: Atur parameter stream ke True.
    • SDK Java: Gunakan antarmuka streamCall.
    • cURL: Atur header X-DashScope-SSE ke enable.
  • Aktifkan output inkremental

    Protokol DashScope mendukung output streaming inkremental maupun non-inkremental:

    • Inkremental (direkomendasikan): Setiap chunk data hanya berisi konten yang baru dihasilkan. Atur incremental_output ke true untuk mengaktifkan output streaming inkremental.

      Contoh: ["I love","eating","apples"]

    • Non-inkremental: Setiap chunk data berisi seluruh konten yang telah dihasilkan sebelumnya, sehingga membuang lebar pita jaringan dan meningkatkan beban pemrosesan klien. Atur incremental_output ke false untuk mengaktifkan output streaming non-inkremental.

      Contoh: ["I love","I love eating","I love eating apples"]

  • Lihat penggunaan token

    Setiap chunk data mencakup informasi penggunaan token secara real-time.

Python

import os
from http import HTTPStatus
import dashscope
from dashscope import Generation

# 1. Persiapan: Konfigurasikan Kunci API dan wilayah
# Konfigurasikan Kunci API menggunakan variabel lingkungan untuk menghindari hardcoding.
try:
    dashscope.api_key = os.environ["DASHSCOPE_API_KEY"]
except KeyError:
    raise ValueError("Set the DASHSCOPE_API_KEY environment variable")

# Kunci API terikat erat pada wilayah. Pastikan base_url sesuai dengan wilayah Kunci API Anda.

dashscope.base_http_api_url = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1"

# 2. Lakukan permintaan streaming
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Please introduce yourself"},
]

try:
    responses = Generation.call(
        model="qwen-plus",
        messages=messages,
        result_format="message",
        stream=True,
        # Penting: Atur ke True untuk output inkremental, yang menawarkan kinerja lebih baik.
        incremental_output=True,
    )

    # 3. Tangani respons streaming
    content_parts = []
    print("AI: ", end="", flush=True)

    for resp in responses:
        if resp.status_code == HTTPStatus.OK:
            content = resp.output.choices[0].message.content
            print(content, end="", flush=True)
            content_parts.append(content)

            # Periksa apakah ini paket terakhir
            if resp.output.choices[0].finish_reason == "stop":
                usage = resp.usage
                print("\n--- Request usage ---")
                print(f"Input Tokens: {usage.input_tokens}")
                print(f"Output Tokens: {usage.output_tokens}")
                print(f"Total Tokens: {usage.total_tokens}")
        else:
            # Tangani kesalahan
            print(
                f"\nRequest failed: request_id={resp.request_id}, code={resp.code}, message={resp.message}"
            )
            break

    full_response = "".join(content_parts)
    # print(f"\n--- Full response ---\n{full_response}")

except Exception as e:
    print(f"An unknown error occurred: {e}")
Respons
AI: Hello! I am Qwen, a large-scale language model independently developed by Tongyi Lab under Alibaba Group. I can help you answer questions, create content such as stories, official documents, emails, scripts, perform logical reasoning, programming, express opinions, play games, and more. I support multiple languages, including but not limited to Chinese, English, German, French, and Spanish. If you have any questions or need help, feel free to ask me anytime!
--- Request usage ---
Input Tokens: 26
Output Tokens: 91
Total Tokens: 117

Java

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import com.alibaba.dashscope.protocol.Protocol;

public class Main {
    public static void main(String[] args) {
        // 1. Dapatkan Kunci API
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        if (apiKey == null || apiKey.isEmpty()) {
            System.err.println("Set the DASHSCOPE_API_KEY environment variable");
            return;
        }

        // 2. Inisialisasi instance Generation
        // Kunci API terikat erat pada wilayah. Pastikan baseUrl sesuai dengan wilayah Kunci API Anda.

        Generation gen = new Generation(Protocol.HTTP.getValue(), "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1");
        CountDownLatch latch = new CountDownLatch(1);

        // 3. Bangun parameter permintaan
        GenerationParam param = GenerationParam.builder()
                .apiKey(apiKey)
                .model("qwen-plus")
                .messages(Arrays.asList(
                        Message.builder()
                                .role(Role.USER.getValue())
                                .content("Introduce yourself")
                                .build()
                ))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .incrementalOutput(true) // Aktifkan output inkremental untuk streaming
                .build();
        // 4. Lakukan panggilan streaming dan tangani respons
        try {
            Flowable<GenerationResult> result = gen.streamCall(param);
            StringBuilder fullContent = new StringBuilder();
            System.out.print("AI: ");
            result
                    .subscribeOn(Schedulers.io()) // Jalankan permintaan pada thread IO
                    .observeOn(Schedulers.computation()) // Proses respons pada thread computation
                    .subscribe(
                            // onNext: Tangani setiap fragmen respons
                            message -> {
                                String content = message.getOutput().getChoices().get(0).getMessage().getContent();
                                String finishReason = message.getOutput().getChoices().get(0).getFinishReason();
                                // Output konten
                                System.out.print(content);
                                fullContent.append(content);
                                // Ketika finishReason tidak null, itu menandakan chunk terakhir. Output info penggunaan.
                                if (finishReason != null && !"null".equals(finishReason)) {
                                    System.out.println("\n--- Request usage ---");
                                    System.out.println("Input Tokens: " + message.getUsage().getInputTokens());
                                    System.out.println("Output Tokens: " + message.getUsage().getOutputTokens());
                                    System.out.println("Total Tokens: " + message.getUsage().getTotalTokens());
                                }
                                System.out.flush(); // Flush output segera
                            },
                            // onError: Tangani kesalahan
                            error -> {
                                System.err.println("\nRequest failed: " + error.getMessage());
                                latch.countDown();
                            },
                            // onComplete: Callback penyelesaian
                            () -> {
                                System.out.println(); // Baris baru
                                // System.out.println("Full response: " + fullContent.toString());
                                latch.countDown();
                            }
                    );
            // Thread utama menunggu tugas async selesai
            latch.await();
            System.out.println("Program execution complete");
        } catch (Exception e) {
            System.err.println("Request exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Respons

AI: Hello! I am Qwen, a large-scale language model independently developed by Tongyi Lab under Alibaba Group. I can help you answer questions, create content such as stories, official documents, emails, scripts, perform logical reasoning, programming, express opinions, play games, and more. I support multiple languages, including but not limited to Chinese, English, German, French, and Spanish. If you have any questions or need help, feel free to ask me anytime!
--- Request usage ---
Input Tokens: 26
Output Tokens: 91
Total Tokens: 117

curl

Permintaan

# ======= Catatan penting =======
# Pastikan variabel lingkungan DASHSCOPE_API_KEY telah disetel
# Kunci API berbeda berdasarkan wilayah. Dapatkan Kunci API Anda: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
# === Hapus komentar ini sebelum menjalankan ===
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-SSE: enable" \
-d '{
    "model": "qwen-plus",
    "input":{
        "messages":[
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Who are you?"
            }
        ]
    },
    "parameters": {
        "result_format": "message",
        "incremental_output":true
    }
}'

Respons

Respons mengikuti format Server-Sent Events (SSE). Setiap pesan mencakup:

  • id: Nomor chunk data.
  • event: Jenis event, selalu "result".
  • Informasi kode status HTTP.
  • data: Data berformat JSON.
id:1
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"I am","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":27,"output_tokens":1,"input_tokens":26,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"d30a9914-ac97-9102-b746-ce0cb35e3fa2"}

id:2
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"Qwen","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":30,"output_tokens":4,"input_tokens":26,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"d30a9914-ac97-9102-b746-ce0cb35e3fa2"}

id:3
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":" from Alibaba","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":33,"output_tokens":7,"input_tokens":26,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"d30a9914-ac97-9102-b746-ce0cb35e3fa2"}

...

id:13
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"or need help, feel free to","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":90,"output_tokens":64,"input_tokens":26,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"d30a9914-ac97-9102-b746-ce0cb35e3fa2"}

id:14
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"ask me!","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":92,"output_tokens":66,"input_tokens":26,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"d30a9914-ac97-9102-b746-ce0cb35e3fa2"}

id:15
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"","role":"assistant"},"finish_reason":"stop"}]},"usage":{"total_tokens":92,"output_tokens":66,"input_tokens":26,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"d30a9914-ac97-9102-b746-ce0cb35e3fa2"}

Streaming output untuk model multimodal

Model multimodal mendukung penambahan gambar, audio, dan konten lain ke dalam percakapan. Implementasi streaming output-nya berbeda dari model teks saja dalam hal-hal berikut:

  • Konstruksi pesan pengguna: Input model multimodal tidak hanya mencakup teks, tetapi juga gambar, audio, dan informasi multimodal lainnya.
  • Antarmuka SDK DashScope: Gunakan antarmuka MultiModalConversation di SDK Python DashScope. Gunakan kelas MultiModalConversation di SDK Java DashScope.

Untuk model multimodal, lihat Pemahaman gambar dan video, Ekstraksi teks, Pemahaman audio—Qwen3-Omni-Captioner, Kimi, dll. Model Qwen-Omni hanya mendukung streaming output karena output-nya dapat mencakup teks atau audio dan konten multimodal lainnya. Penguraian hasilnya berbeda dari model lain. Untuk detailnya, lihat Omni-modal.

Kompatibel dengan OpenAI

from openai import OpenAI
import os

client = OpenAI(
    # Kunci API berbeda berdasarkan wilayah. Dapatkan Kunci API Anda: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan Kunci API Model Studio Anda: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    model="qwen3-vl-plus",  # Ganti dengan model multimodal lain sesuai kebutuhan dan sesuaikan messages
    messages=[
        {"role": "user",
        "content": [{"type": "image_url",
                    "image_url": {"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},},
                    {"type": "text", "text": "What scene is depicted in the image?"}]}],
    stream=True,
  # stream_options={"include_usage": True}
)
full_content = ""
print("Streaming output content:")
for chunk in completion:
    # Jika stream_options.include_usage adalah True, field choices pada chunk terakhir adalah daftar kosong dan harus dilewati (penggunaan token dapat diperoleh melalui chunk.usage)
    if chunk.choices and chunk.choices[0].delta.content != "":
        full_content += chunk.choices[0].delta.content
        print(chunk.choices[0].delta.content)
print(f"Full content: {full_content}")
import OpenAI from "openai";

const openai = new OpenAI(
    {
        // Kunci API berbeda berdasarkan wilayah. Dapatkan Kunci API Anda: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
        // Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan Kunci API Model Studio Anda: apiKey: "sk-xxx"
        apiKey: process.env.DASHSCOPE_API_KEY,
        // URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
        baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
    }
);

const completion = await openai.chat.completions.create({
    model: "qwen3-vl-plus",  //  Ganti dengan model multimodal lain sesuai kebutuhan dan sesuaikan messages
    messages: [
        {role: "user",
        content: [{"type": "image_url",
                    "image_url": {"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},},
                    {"type": "text", "text": "What scene is depicted in the image?"}]}],
    stream: true,
    // stream_options: { include_usage: true },
});

let fullContent = ""
console.log("Streaming output content:")
for await (const chunk of completion) {
    // Jika stream_options.include_usage adalah true, field choices pada chunk terakhir adalah array kosong dan harus dilewati (penggunaan token dapat diperoleh melalui chunk.usage)
    if (chunk.choices[0] && chunk.choices[0].delta.content != null) {
      fullContent += chunk.choices[0].delta.content;
      console.log(chunk.choices[0].delta.content);
    }
}
console.log(`Full output content: ${fullContent}`)
# ======= Catatan penting =======
# URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
# Kunci API berbeda berdasarkan wilayah. Dapatkan Kunci API Anda: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# === Hapus komentar ini sebelum menjalankan ===

curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "model": "qwen3-vl-plus",
    "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "image_url",
          "image_url": {
            "url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
          }
        },
        {
          "type": "text",
          "text": "What scene is depicted in the image?"
        }
      ]
    }
  ],
    "stream":true,
    "stream_options":{"include_usage":true}
}'

DashScope

import os
from dashscope import MultiModalConversation
import dashscope
# URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'

messages = [
    {
        "role": "user",
        "content": [
            {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
            {"text": "What scene is depicted in the image?"}
        ]
    }
]

responses = MultiModalConversation.call(
    # Kunci API berbeda berdasarkan wilayah. Dapatkan Kunci API Anda: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan Kunci API Model Studio Anda: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model='qwen3-vl-plus',  #  Ganti dengan model multimodal lain sesuai kebutuhan dan sesuaikan messages
    messages=messages,
    stream=True,
    incremental_output=True)

full_content = ""
print("Streaming output content:")
for response in responses:
    if response["output"]["choices"][0]["message"].content:
        print(response.output.choices[0].message.content[0]['text'])
        full_content += response.output.choices[0].message.content[0]['text']
print(f"Full content: {full_content}")
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import io.reactivex.Flowable;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        // URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
        Constants.baseHttpApiUrl="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1";
    }
    public static void streamCall()
            throws ApiException, NoApiKeyException, UploadFileException {
        MultiModalConversation conv = new MultiModalConversation();
        // must create mutable map.
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                .content(Arrays.asList(Collections.singletonMap("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"),
                        Collections.singletonMap("text", "What scene is depicted in the image?"))).build();
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                // Kunci API berbeda berdasarkan wilayah. Dapatkan Kunci API Anda: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
                // Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan Kunci API Model Studio Anda: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .model("qwen3-vl-plus")  //  Ganti dengan model multimodal lain sesuai kebutuhan dan sesuaikan messages
                .messages(Arrays.asList(userMessage))
                .incrementalOutput(true)
                .build();
        Flowable<MultiModalConversationResult> result = conv.streamCall(param);
        result.blockingForEach(item -> {
            try {
                List<Map<String, Object>> content = item.getOutput().getChoices().get(0).getMessage().getContent();
                    // Periksa apakah konten ada dan tidak kosong
                if (content != null &&  !content.isEmpty()) {
                    System.out.println(content.get(0).get("text"));
                    }
            } catch (Exception e){
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        try {
            streamCall();
        } catch (ApiException | NoApiKeyException | UploadFileException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}
# ======= Catatan penting =======
# Kunci API berbeda berdasarkan wilayah. Dapatkan Kunci API Anda: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
# === Hapus komentar ini sebelum menjalankan ===

curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-H 'X-DashScope-SSE: enable' \
-d '{
    "model": "qwen3-vl-plus",
    "input":{
        "messages":[
            {
                "role": "user",
                "content": [
                    {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
                    {"text": "What scene is depicted in the image?"}
                ]
            }
        ]
    },
    "parameters": {
        "incremental_output": true
    }
}'

Streaming output untuk model berpikir

Model berpikir pertama-tama mengembalikan reasoning_content (proses berpikir), lalu mengembalikan content (respons). Tentukan apakah tahap saat ini adalah berpikir atau merespons berdasarkan status paket data.

Untuk detail tentang model berpikir, lihat Pemikiran mendalam, Pemahaman gambar dan video, Penalaran visual.

Untuk implementasi streaming output Qwen3-Omni-Flash (mode berpikir), lihat Omni-modal.

Kompatibel dengan OpenAI

Berikut adalah format respons saat memanggil mode berpikir model qwen-plus menggunakan SDK Python OpenAI dalam mode streaming:

# Tahap berpikir
...
ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None, reasoning_content='Cover all key points while')
ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None, reasoning_content='remaining natural and fluent.')
# Tahap respons
ChoiceDelta(content='Hello! I am **Qwen', function_call=None, refusal=None, role=None, tool_calls=None, reasoning_content=None)
ChoiceDelta(content='** (', function_call=None, refusal=None, role=None, tool_calls=None, reasoning_content=None)
...
  • Jika reasoning_content tidak None dan content adalah None, tahap saat ini adalah berpikir.
  • Jika reasoning_content adalah None dan content tidak None, tahap saat ini adalah merespons.
  • Jika keduanya adalah None, tahap tetap sama seperti paket sebelumnya.

Python

Kode contoh

from openai import OpenAI
import os

# Inisialisasi client OpenAI
client = OpenAI(
    # Jika Anda belum mengonfigurasi variabel lingkungan, ganti dengan Kunci API Alibaba Cloud Model Studio Anda: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

messages = [{"role": "user", "content": "Who are you"}]

completion = client.chat.completions.create(
    model="qwen-plus",  # Ganti dengan model berpikir mendalam lain sesuai kebutuhan
    messages=messages,
    # Parameter enable_thinking mengaktifkan proses berpikir. Parameter ini tidak berpengaruh pada model qwen3-30b-a3b-thinking-2507, qwen3-235b-a22b-thinking-2507, dan QwQ.
    extra_body={"enable_thinking": True},
    stream=True,
    # stream_options={
    #     "include_usage": True
    # },
)

reasoning_content = ""  # Proses berpikir lengkap
answer_content = ""  # Respons lengkap
is_answering = False  # Apakah sedang dalam tahap respons
print("\n" + "=" * 20 + "Thought process" + "=" * 20 + "\n")

for chunk in completion:
    if not chunk.choices:
        print("\nUsage:")
        print(chunk.usage)
        continue

    delta = chunk.choices[0].delta

    # Kumpulkan hanya konten berpikir
    if hasattr(delta, "reasoning_content") and delta.reasoning_content is not None:
        if not is_answering:
            print(delta.reasoning_content, end="", flush=True)
        reasoning_content += delta.reasoning_content

    # Menerima konten, mulai merespons
    if hasattr(delta, "content") and delta.content:
        if not is_answering:
            print("\n" + "=" * 20 + "Full response" + "=" * 20 + "\n")
            is_answering = True
        print(delta.content, end="", flush=True)
        answer_content += delta.content

Respons

====================Thought process====================

Okay, the user asked "Who are you," so I need to give an accurate and friendly answer. First, I should confirm my identity as Qwen, developed by Tongyi Lab under Alibaba Group. Next, explain my main functions, like answering questions, creating text, logical reasoning, etc. Keep the tone approachable and avoid overly technical terms so the user feels comfortable. Also, avoid complex jargon and ensure the answer is concise. Additionally, include some interactive elements to encourage further questions. Finally, check for any missing key information, such as my Chinese name "Tongyi Qianwen" and English name "Qwen," along with my company and lab. Make sure the response is comprehensive and meets user expectations.
====================Full response====================

Hello! I am Qwen, a large-scale language model independently developed by Tongyi Lab under Alibaba Group. I can answer questions, create text, perform logical reasoning, programming, and more, aiming to provide high-quality information and services. You can call me Qwen or simply Tongyi Qianwen. How can I help you?

Node.js

Kode contoh

import OpenAI from "openai";
import process from 'process';

// Inisialisasi klien openai
const openai = new OpenAI({
    apiKey: process.env.DASHSCOPE_API_KEY, // Baca dari variabel lingkungan
    baseURL: 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1'
});

let reasoningContent = '';
let answerContent = '';
let isAnswering = false;

async function main() {
    try {
        const messages = [{ role: 'user', content: 'Who are you' }];
        const stream = await openai.chat.completions.create({
            // Ganti dengan model Qwen3 atau model QwQ lain sesuai kebutuhan
            model: 'qwen-plus',
            messages,
            stream: true,
            // Parameter enable_thinking mengaktifkan proses berpikir. Parameter ini tidak berpengaruh pada model qwen3-30b-a3b-thinking-2507, qwen3-235b-a22b-thinking-2507, dan QwQ.
            enable_thinking: true
        });
        console.log('\n' + '='.repeat(20) + 'Proses berpikir' + '='.repeat(20) + '\n');

        for await (const chunk of stream) {
            if (!chunk.choices?.length) {
                console.log('\nUsage:');
                console.log(chunk.usage);
                continue;
            }

            const delta = chunk.choices[0].delta;

            // Kumpulkan hanya konten pemikiran
            if (delta.reasoning_content !== undefined && delta.reasoning_content !== null) {
                if (!isAnswering) {
                    process.stdout.write(delta.reasoning_content);
                }
                reasoningContent += delta.reasoning_content;
            }

            // Konten diterima, mulai berikan tanggapan
            if (delta.content !== undefined && delta.content) {
                if (!isAnswering) {
                    console.log('\n' + '='.repeat(20) + 'Tanggapan lengkap' + '='.repeat(20) + '\n');
                    isAnswering = true;
                }
                process.stdout.write(delta.content);
                answerContent += delta.content;
            }
        }
    } catch (error) {
        console.error('Error:', error);
    }
}

main();

Respons

====================Thought process====================

Okay, the user asked "Who are you," so I need to state my identity. First, I should clearly say I am Qwen, a large-scale language model developed by Alibaba Cloud. Next, mention my main functions, like answering questions, creating text, logical reasoning, etc. Also emphasize my multilingual support, including Chinese and English, so users know I can handle requests in different languages. Additionally, explain my application scenarios, such as helping with learning, work, and daily life. However, since the user's question is direct, detailed information might not be necessary—keep it concise. Also, ensure a friendly tone and invite further questions. Check for any missing key information, like my version or latest updates, but the user probably doesn't need that much detail. Finally, confirm the response is accurate and error-free.
====================Full response====================

I am Qwen, a large-scale language model independently developed by Tongyi Lab under Alibaba Group. I can handle various tasks like answering questions, creating text, logical reasoning, and programming, supporting multiple languages including Chinese and English. If you have any questions or need help, feel free to tell me anytime!

HTTP

Kode contoh

curl

Untuk model open-source Qwen3, atur enable_thinking ke true untuk mengaktifkan mode berpikir. Parameter enable_thinking tidak berpengaruh pada model qwen3-30b-a3b-thinking-2507, qwen3-235b-a22b-thinking-2507, QwQ .

curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.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": "Who are you"
        }
    ],
    "stream": true,
    "stream_options": {
        "include_usage": true
    },
    "enable_thinking": true
}'

Respons

data: {"choices":[{"delta":{"content":null,"role":"assistant","reasoning_content":""},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1745485391,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-e2edaf2c-8aaf-9e54-90e2-b21dd5045503"}

.....

data: {"choices":[{"finish_reason":"stop","delta":{"content":"","reasoning_content":null},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1745485391,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-e2edaf2c-8aaf-9e54-90e2-b21dd5045503"}

data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":10,"completion_tokens":360,"total_tokens":370},"created":1745485391,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-e2edaf2c-8aaf-9e54-90e2-b21dd5045503"}

data: [DONE]

DashScope

Berikut adalah format respons streaming saat memanggil mode berpikir model qwen-plus menggunakan SDK Python DashScope:

# Tahap berpikir
...
{"role": "assistant", "content": "", "reasoning_content": "High information density,"}
{"role": "assistant", "content": "", "reasoning_content": "making users feel helped."}
# Tahap respons
{"role": "assistant", "content": "I am Qwen", "reasoning_content": ""}
{"role": "assistant", "content": ", developed by Tongyi Lab", "reasoning_content": ""}
...
  • Jika reasoning_content tidak "", dan content adalah "", tahap saat ini adalah berpikir.
  • Jika reasoning_content adalah "", dan content tidak "", tahap saat ini adalah merespons.
  • Jika keduanya adalah "", tahap tetap sama seperti paket sebelumnya.

Python

Kode contoh

import os
from dashscope import Generation
import dashscope
dashscope.base_http_api_url = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/"

messages = [{"role": "user", "content": "Who are you?"}]

completion = Generation.call(
    # Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan Kunci API Alibaba Cloud Model Studio Anda: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # Ganti dengan model berpikir mendalam lain sesuai kebutuhan
    model="qwen-plus",
    messages=messages,
    result_format="message", # Model open-source Qwen3 hanya mendukung "message"; untuk pengalaman lebih baik, kami sarankan mengatur ini ke "message" untuk model lain juga.
    # Aktifkan pemikiran mendalam. Parameter ini tidak berpengaruh pada model qwen3-30b-a3b-thinking-2507, qwen3-235b-a22b-thinking-2507, dan QwQ.
    enable_thinking=True,
    stream=True,
    incremental_output=True, # Model open-source Qwen3 hanya mendukung true; untuk pengalaman lebih baik, kami sarankan mengatur ini ke true untuk model lain juga.
)

# Definisikan proses berpikir lengkap
reasoning_content = ""
# Definisikan respons lengkap
answer_content = ""
# Tentukan apakah sudah selesai berpikir dan mulai merespons
is_answering = False

print("=" * 20 + "Thought process" + "=" * 20)

for chunk in completion:
    # Jika proses berpikir dan respons keduanya kosong, lewati
    if (
        chunk.output.choices[0].message.content == ""
        and chunk.output.choices[0].message.reasoning_content == ""
    ):
        pass
    else:
        # Jika saat ini dalam tahap berpikir
        if (
            chunk.output.choices[0].message.reasoning_content != ""
            and chunk.output.choices[0].message.content == ""
        ):
            print(chunk.output.choices[0].message.reasoning_content, end="", flush=True)
            reasoning_content += chunk.output.choices[0].message.reasoning_content
        # Jika saat ini dalam tahap respons
        elif chunk.output.choices[0].message.content != "":
            if not is_answering:
                print("\n" + "=" * 20 + "Full response" + "=" * 20)
                is_answering = True
            print(chunk.output.choices[0].message.content, end="", flush=True)
            answer_content += chunk.output.choices[0].message.content

# Untuk mencetak proses berpikir lengkap dan respons lengkap, hapus komentar baris berikut dan jalankan
# print("=" * 20 + "Full thought process" + "=" * 20 + "\n")
# print(f"{reasoning_content}")
# print("=" * 20 + "Full response" + "=" * 20 + "\n")
# print(f"{answer_content}")

Respons

====================Thought process====================
Okay, the user asked: "Who are you?" I need to answer this question. First, clarify my identity as Qwen, a large-scale language model developed by Alibaba Cloud. Next, explain my functions and purposes, like answering questions, creating text, logical reasoning, etc. Also, emphasize my goal of being a helpful assistant.

Keep the expression conversational, avoiding professional jargon or complex sentence structures. Add friendly phrases like "Hello there~" to make the conversation natural. Also, ensure accuracy and don't omit key points like my developer, main functions, and usage scenarios.

Consider possible follow-up questions from the user, such as specific application examples or technical details, so subtly hint at further inquiries in the response. For example, mention "Whether it's everyday questions or professional issues, I'll do my best to help," which is both comprehensive and open-ended.

Finally, check for fluency, repetition, or redundant information to keep the response concise. Maintain a balance between friendliness and professionalism so users feel both approachable and reliable.
====================Full response====================
Hello there~ I'm Qwen, a large-scale language model developed by Alibaba Cloud. I can answer questions, create text, perform logical reasoning, programming, and more, aiming to provide help and support. Whether it's everyday questions or professional issues, I'll do my best to help. How can I assist you?

Java

Kode contoh

// dashscope SDK version >= 2.19.4
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import io.reactivex.Flowable;
import java.lang.System;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        Constants.baseHttpApiUrl="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1";
    }
    private static final Logger logger = LoggerFactory.getLogger(Main.class);
    private static StringBuilder reasoningContent = new StringBuilder();
    private static StringBuilder finalContent = new StringBuilder();
    private static boolean isFirstPrint = true;

    private static void handleGenerationResult(GenerationResult message) {
        String reasoning = message.getOutput().getChoices().get(0).getMessage().getReasoningContent();
        String content = message.getOutput().getChoices().get(0).getMessage().getContent();

        if (!reasoning.isEmpty()) {
            reasoningContent.append(reasoning);
            if (isFirstPrint) {
                System.out.println("====================Thought process====================");
                isFirstPrint = false;
            }
            System.out.print(reasoning);
        }

        if (!content.isEmpty()) {
            finalContent.append(content);
            if (!isFirstPrint) {
                System.out.println("\n====================Full response====================");
                isFirstPrint = true;
            }
            System.out.print(content);
        }
    }
    private static GenerationParam buildGenerationParam(Message userMsg) {
        return GenerationParam.builder()
                // Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan Kunci API Alibaba Cloud Model Studio Anda: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .model("qwen-plus")
                .enableThinking(true)
                .incrementalOutput(true)
                .resultFormat("message")
                .messages(Arrays.asList(userMsg))
                .build();
    }
    public static void streamCallWithMessage(Generation gen, Message userMsg)
            throws NoApiKeyException, ApiException, InputRequiredException {
        GenerationParam param = buildGenerationParam(userMsg);
        Flowable<GenerationResult> result = gen.streamCall(param);
        result.blockingForEach(message -> handleGenerationResult(message));
    }

    public static void main(String[] args) {
        try {
            Generation gen = new Generation();
            Message userMsg = Message.builder().role(Role.USER.getValue()).content("Who are you?").build();
            streamCallWithMessage(gen, userMsg);
//             Cetak hasil akhir
//            if (reasoningContent.length() > 0) {
//                System.out.println("\n====================Full response====================");
//                System.out.println(finalContent.toString());
//            }
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            logger.error("An exception occurred: {}", e.getMessage());
        }
        System.exit(0);
    }
}

Respons

====================Thought process====================
Okay, the user asked "Who are you?", so I need to answer based on previous settings. First, my role is Qwen, a large-scale language model under Alibaba Group. Keep it conversational and simple.

The user might be new to me or confirming my identity. Start by directly stating who I am, then briefly explain my functions and purposes, like answering questions, creating text, programming, etc. Also mention multilingual support so users know I handle different languages.

Also, per guidelines, maintain human-like qualities, so use a friendly tone, maybe add emojis for warmth. Guide users to ask further questions or use my features, like asking what they need help with.

Avoid complex terms and keep it concise. Check for missing key points like multilingual support and specific capabilities. Ensure the response meets all requirements, including conversational style and simplicity.
====================Full response====================
Hello! I'm Qwen, a large-scale language model under Alibaba Group. I can answer questions, create text like stories, official documents, emails, scripts, perform logical reasoning, programming, express opinions, play games, and more. I'm proficient in multiple languages, including but not limited to Chinese, English, German, French, and Spanish. How can I help you?

HTTP

Kode contoh

curl

Untuk model berpikir hibrida, atur enable_thinking ke true untuk mengaktifkan mode berpikir. Parameter enable_thinking tidak berpengaruh pada model qwen3-30b-a3b-thinking-2507, qwen3-235b-a22b-thinking-2507, QwQ .

# ======= Catatan penting =======
# Kunci API berbeda berdasarkan wilayah. Dapatkan Kunci API Anda: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# URL wilayah Singapura. Ganti {WorkspaceId} dengan ID ruang kerja aktual Anda. URL bervariasi berdasarkan wilayah.
# === Hapus komentar ini sebelum menjalankan ===
curl -X POST "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-SSE: enable" \
-d '{
    "model": "qwen-plus",
    "input":{
        "messages":[
            {
                "role": "user",
                "content": "Who are you?"
            }
        ]
    },
    "parameters":{
        "enable_thinking": true,
        "incremental_output": true,
        "result_format": "message"
    }
}'

Respons

id:1
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"","reasoning_content":"Hmm","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":14,"input_tokens":11,"output_tokens":3},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:2
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"","reasoning_content":",","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":15,"input_tokens":11,"output_tokens":4},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:3
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"","reasoning_content":"user","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":16,"input_tokens":11,"output_tokens":5},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:4
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"","reasoning_content":"asked","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":17,"input_tokens":11,"output_tokens":6},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:5
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"","reasoning_content":"\"","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":18,"input_tokens":11,"output_tokens":7},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}
......

id:358
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"help","reasoning_content":"","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":373,"input_tokens":11,"output_tokens":362},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:359
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":",","reasoning_content":"","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":374,"input_tokens":11,"output_tokens":363},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:360
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"welcome","reasoning_content":"","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":375,"input_tokens":11,"output_tokens":364},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:361
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"anytime","reasoning_content":"","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":376,"input_tokens":11,"output_tokens":365},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:362
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"tell","reasoning_content":"","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":377,"input_tokens":11,"output_tokens":366},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:363
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"me","reasoning_content":"","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":378,"input_tokens":11,"output_tokens":367},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:364
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"!","reasoning_content":"","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":378,"input_tokens":11,"output_tokens":367},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

id:365
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"","reasoning_content":"","role":"assistant"},"finish_reason":"stop"}]},"usage":{"total_tokens":378,"input_tokens":11,"output_tokens":367},"request_id":"25d58c29-c47b-9e8d-a0f1-d6c309ec58b1"}

Going live

  • Kinerja dan manajemen sumber daya: Pada layanan backend, mempertahankan koneksi HTTP persisten untuk setiap permintaan streaming mengonsumsi sumber daya. Konfigurasikan layanan Anda dengan ukuran kolam koneksi dan nilai timeout yang sesuai. Pada skenario konkurensi tinggi, pantau penggunaan deskriptor file untuk mencegah kehabisan.

  • Rendering sisi klien: Pada antarmuka depan web, gunakan API ReadableStream dan TextDecoderStream untuk menangani dan merender aliran event SSE dengan lancar, memberikan pengalaman pengguna terbaik.

  • Pemantauan model:

    • Metrik utama: Pantau Time to First Token (TTFT), metrik inti untuk pengalaman streaming. Juga pantau tingkat kesalahan API dan waktu respons rata-rata.
    • Peringatan: Atur peringatan untuk tingkat kesalahan API yang tidak normal, terutama kesalahan 4xx dan 5xx.
  • Konfigurasi proxy Nginx: Jika menggunakan Nginx sebagai reverse proxy, buffering output default-nya (proxy_buffering) mengganggu sifat real-time dari respons streaming. Untuk memastikan data didorong ke klien segera, nonaktifkan fitur ini dengan mengatur proxy_buffering off dalam file konfigurasi Nginx Anda.

Kode kesalahan

Jika panggilan model gagal dan mengembalikan pesan kesalahan, lihat Kode kesalahan untuk resolusi.

FAQ

T: Mengapa tidak ada informasi penggunaan dalam respons?

J: Protokol OpenAI tidak mengembalikan informasi penggunaan secara default. Atur parameter stream_options untuk menyertakan informasi penggunaan dalam paket terakhir yang dikembalikan.

T: Apakah mengaktifkan streaming output memengaruhi kualitas respons model?

J: Tidak. Namun, beberapa model hanya mendukung streaming output, dan panggilan non-streaming dapat menyebabkan kesalahan timeout. Kami merekomendasikan menggunakan streaming output.

T: Apa perbedaan antara panggilan non-streaming dan streaming?

J: Perbedaan utama:

  • Batas waktu timeout: Untuk panggilan non-streaming, timeout maksimum minimal 300 detik dan bervariasi berdasarkan wilayah dan model. Jika tidak selesai tepat waktu, permintaan akan dihentikan.
  • Struktur output: Panggilan non-streaming mengembalikan respons lengkap (objek JSON tunggal) sekaligus. Panggilan streaming mengembalikan chunk data secara progresif melalui protokol SSE, dengan setiap chunk berisi bagian dari konten yang dihasilkan. Klien harus merakit chunk-chunk tersebut.
  • Kompatibilitas fitur: Keduanya mendukung fitur seperti JSON Mode dan Function Call tanpa perbedaan fungsional.

Kami merekomendasikan menggunakan streaming output untuk menghindari timeout dan meningkatkan pengalaman pengguna.

T: Apakah streaming output mendukung JSON Mode (output terstruktur)?

J: Ya. Atur stream ke true dan response_format ke {"type": "json_object"} dalam permintaan. Model akan mengembalikan fragmen konten berformat JSON secara progresif. Output akhir yang dirakit akan menjadi JSON yang valid.