Model Qwen di Alibaba Cloud Model Studio mendukung API Responses yang kompatibel dengan OpenAI. API Responses merupakan evolusi dari API Chat Completions yang menyediakan fitur agen bawaan secara lebih ringkas. Kami merekomendasikan penggunaannya untuk semua proyek baru.
Keunggulan dibandingkan API OpenAI Chat Completions:
Alat bawaan: Menyertakan alat bawaan seperti web search, web scraping, dan code interpreter. Anda dapat mengaktifkan alat-alat ini secara bersamaan untuk mencapai hasil terbaik saat menangani tugas kompleks. Untuk informasi selengkapnya, lihat Panggil alat bawaan.
Input yang lebih fleksibel: Mendukung pengiriman string langsung sebagai input model dan juga kompatibel dengan array message dalam format Chat.
Pengelolaan konteks yang disederhanakan: Anda dapat meneruskan
previous_response_iddari respons sebelumnya untuk menghindari pembuatan manual array riwayat pesan lengkap.
Prasyarat
Dapatkan kunci API dan ekspor kunci API sebagai variabel lingkungan. Untuk menggunakan SDK, instal SDK.
Ketersediaan
Saat ini mendukung qwen3-max dan qwen3-max-2026-01-23.
Titik akhir layanan
Saat ini, hanya wilayah Singapore yang didukung.
Singapore
base_url untuk panggilan SDK adalah https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1.
Alamat permintaan HTTP adalah POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses.
Contoh kode
Basic
Anda dapat mengirim pesan dan menerima respons dari model.
Python
import os
from openai import OpenAI
client = OpenAI(
# Jika variabel lingkungan belum diatur, ganti dengan: api_key="sk-xxx"
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)
response = client.responses.create(
model="qwen3-max-2026-01-23",
input="Hello, please introduce yourself in one sentence."
)
# Dapatkan respons model
# print(response.model_dump_json())
print(response.output_text)Node.js
import OpenAI from "openai";
const openai = new OpenAI({
// Jika variabel lingkungan belum diatur, ganti dengan: apiKey: "sk-xxx"
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});
async function main() {
const response = await openai.responses.create({
model: "qwen3-max-2026-01-23",
input: "Hello, please introduce yourself in one sentence."
});
// Dapatkan respons model
console.log(response.output_text);
}
main();curl
curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-max-2026-01-23",
"input": "Hello, please introduce yourself in one sentence."
}'Contoh respons
Berikut ini menunjukkan respons API lengkap.
{
"id": "b8104cd2-ee57-903d-aae0-93d99254axxx",
"created_at": 1769084048.0,
"model": "qwen3-max-2026-01-23",
"object": "response",
"status": "completed",
"output": [
{
"id": "msg_1eb85c78-a627-4c7e-aac6-22235c173xxx",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "Hello! I am Qwen, a large-scale language model developed by Tongyi Lab. I can answer questions, create text, write code, and express opinions. I am committed to providing you with accurate, useful, and friendly help.",
"annotations": []
}
]
}
],
"usage": {
"input_tokens": 39,
"output_tokens": 46,
"total_tokens": 85,
"input_tokens_details": {
"cached_tokens": 0
},
"output_tokens_details": {
"reasoning_tokens": 0
}
}
}Percakapan multi-putaran
Anda dapat menggunakan parameter previous_response_id untuk menautkan konteks secara otomatis tanpa membuat riwayat pesan secara manual. id tanggapan saat ini berlaku selama 7 hari.
Python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)
# Putaran pertama
response1 = client.responses.create(
model="qwen3-max-2026-01-23",
input="My name is John, please remember it."
)
print(f"First response: {response1.output_text}")
# Putaran kedua - gunakan previous_response_id untuk menghubungkan konteks
# ID respons berlaku selama 7 hari
response2 = client.responses.create(
model="qwen3-max-2026-01-23",
input="Do you remember my name?",
previous_response_id=response1.id
)
print(f"Second response: {response2.output_text}")Node.js
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});
async function main() {
// Putaran pertama
const response1 = await openai.responses.create({
model: "qwen3-max-2026-01-23",
input: "My name is John, please remember it."
});
console.log(`First response: ${response1.output_text}`);
// Putaran kedua - gunakan previous_response_id untuk menghubungkan konteks
// ID respons berlaku selama 7 hari
const response2 = await openai.responses.create({
model: "qwen3-max-2026-01-23",
input: "Do you remember my name?",
previous_response_id: response1.id
});
console.log(`Second response: ${response2.output_text}`);
}
main();curl
# Putaran pertama
curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-max-2026-01-23",
"input": "My name is John, please remember it."
}'
# Putaran kedua - gunakan id dari respons pertama sebagai previous_response_id
curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-max-2026-01-23",
"input": "Do you remember my name?",
"previous_response_id": "response_id_from_first_round"
}'Contoh respons untuk putaran percakapan kedua
{
"id": "4730f70e-6aa3-9315-b4d1-c43c8e509xxx",
"created_at": 1769173209.0,
"model": "qwen3-max-2026-01-23",
"object": "response",
"status": "completed",
"output": [
{
"id": "msg_869508e7-590f-46c0-bd8d-e3b5e970exxx",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "Yes, John! I remember your name. How can I assist you today?",
"annotations": []
}
]
}
],
"usage": {
"input_tokens": 78,
"output_tokens": 16,
"total_tokens": 94,
"input_tokens_details": {
"cached_tokens": 0
},
"output_tokens_details": {
"reasoning_tokens": 0
}
}
}Catatan: input_tokens untuk putaran percakapan kedua adalah 78. Nilai ini mencakup konteks dari putaran pertama, dan model berhasil mengingat nama "John".
Keluaran streaming
Anda dapat menerima konten yang dihasilkan model secara real-time melalui keluaran streaming. Fitur ini cocok untuk skenario generasi teks panjang.
Python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)
stream = client.responses.create(
model="qwen3-max-2026-01-23",
input="Please briefly introduce artificial intelligence.",
stream=True
)
print("Receiving stream output:")
for event in stream:
# print(event.model_dump_json()) # Hapus komentar untuk melihat respons event mentah
if event.type == 'response.output_text.delta':
print(event.delta, end='', flush=True)
elif event.type == 'response.completed':
print("\nStream completed")
print(f"Total tokens: {event.response.usage.total_tokens}")Node.js
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});
async function main() {
const stream = await openai.responses.create({
model: "qwen3-max-2026-01-23",
input: "Please briefly introduce artificial intelligence.",
stream: true
});
console.log("Receiving stream output:");
for await (const event of stream) {
// console.log(JSON.stringify(event)); // Hapus komentar untuk melihat respons event mentah
if (event.type === 'response.output_text.delta') {
process.stdout.write(event.delta);
} else if (event.type === 'response.completed') {
console.log("\nStream completed");
console.log(`Total tokens: ${event.response.usage.total_tokens}`);
}
}
}
main();curl
curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-max-2026-01-23",
"input": "Please briefly introduce artificial intelligence.",
"stream": true
}'Contoh respons
{"response":{"id":"47a71e7d-868c-4204-9693-ef8ff9058xxx","created_at":1769417481.0,"error":null,"incomplete_details":null,"instructions":null,"metadata":null,"model":"","object":"response","output":[],"parallel_tool_calls":false,"temperature":null,"tool_choice":"auto","tools":[],"top_p":null,"background":null,"completed_at":null,"conversation":null,"max_output_tokens":null,"max_tool_calls":null,"previous_response_id":null,"prompt":null,"prompt_cache_key":null,"prompt_cache_retention":null,"reasoning":null,"safety_identifier":null,"service_tier":null,"status":"queued","text":null,"top_logprobs":null,"truncation":null,"usage":null,"user":null},"sequence_number":0,"type":"response.created"}
{"response":{"id":"47a71e7d-868c-4204-9693-ef8ff9058xxx","created_at":1769417481.0,"error":null,"incomplete_details":null,"instructions":null,"metadata":null,"model":"","object":"response","output":[],"parallel_tool_calls":false,"temperature":null,"tool_choice":"auto","tools":[],"top_p":null,"background":null,"completed_at":null,"conversation":null,"max_output_tokens":null,"max_tool_calls":null,"previous_response_id":null,"prompt":null,"prompt_cache_key":null,"prompt_cache_retention":null,"reasoning":null,"safety_identifier":null,"service_tier":null,"status":"in_progress","text":null,"top_logprobs":null,"truncation":null,"usage":null,"user":null},"sequence_number":1,"type":"response.in_progress"}
{"item":{"id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","content":[],"role":"assistant","status":"in_progress","type":"message"},"output_index":0,"sequence_number":2,"type":"response.output_item.added"}
{"content_index":0,"item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","output_index":0,"part":{"annotations":[],"text":"","type":"output_text","logprobs":null},"sequence_number":3,"type":"response.content_part.added"}
{"content_index":0,"delta":"Artificial intelligence","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":4,"type":"response.output_text.delta"}
{"content_index":0,"delta":" (Art","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":5,"type":"response.output_text.delta"}
{"content_index":0,"delta":"ificial Intelligence,","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":6,"type":"response.output_text.delta"}
{"content_index":0,"delta":" or AI for short)","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":7,"type":"response.output_text.delta"}
... (intermediate events omitted) ...
{"content_index":0,"delta":" fields, and is profoundly changing our","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":38,"type":"response.output_text.delta"}
{"content_index":0,"delta":" lives and work","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":39,"type":"response.output_text.delta"}
{"content_index":0,"delta":".","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":40,"type":"response.output_text.delta"}
{"content_index":0,"item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":41,"text":"Artificial intelligence (AI) is the technology and science of computer systems that simulate human intelligent behavior. xxxx","type":"response.output_text.done"}
{"content_index":0,"item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","output_index":0,"part":{"annotations":[],"text":"Artificial intelligence (AI) is the technology and science of computer systems that simulate human intelligent behavior. xxx","type":"output_text","logprobs":null},"sequence_number":42,"type":"response.content_part.done"}
{"item":{"id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","content":[{"annotations":[],"text":"Artificial intelligence (AI) is the technology and science of computer systems that simulate human intelligent behavior. It aims to enable machines to perform tasks that typically require human intelligence, such as:\n\n- **Learning** (such as training models with data)\n- **Reasoning** (such as logical judgment and problem-solving)\n- **Perception** (such as recognizing images, speech, or text)\n- **Understanding language** (such as natural language processing)\n- **Decision-making** (such as making optimal choices in complex environments)\n\nArtificial intelligence can be divided into **weak AI** (focused on specific tasks, such as voice assistants and recommendation systems) and **strong AI** (possessing general intelligence similar to humans, which has not yet been achieved).\n\nCurrently, AI is widely used in many fields, such as healthcare, finance, transportation, education, and entertainment, and is profoundly changing the way we live and work.","type":"output_text","logprobs":null}],"role":"assistant","status":"completed","type":"message"},"output_index":0,"sequence_number":43,"type":"response.output_item.done"}
{"response":{"id":"47a71e7d-868c-4204-9693-ef8ff9058xxx","created_at":1769417481.0,"error":null,"incomplete_details":null,"instructions":null,"metadata":null,"model":"qwen3-max-2026-01-23","object":"response","output":[{"id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","content":[{"annotations":[],"text":"Artificial intelligence (AI) is xxxxxx","type":"output_text","logprobs":null}],"role":"assistant","status":"completed","type":"message"}],"parallel_tool_calls":false,"temperature":null,"tool_choice":"auto","tools":[],"top_p":null,"background":null,"completed_at":null,"conversation":null,"max_output_tokens":null,"max_tool_calls":null,"previous_response_id":null,"prompt":null,"prompt_cache_key":null,"prompt_cache_retention":null,"reasoning":null,"safety_identifier":null,"service_tier":null,"status":"completed","text":null,"top_logprobs":null,"truncation":null,"usage":{"input_tokens":37,"input_tokens_details":{"cached_tokens":0},"output_tokens":166,"output_tokens_details":{"reasoning_tokens":0},"total_tokens":203},"user":null},"sequence_number":44,"type":"response.completed"}Panggil alat bawaan
Anda dapat mengaktifkan alat bawaan secara bersamaan untuk mencapai hasil terbaik saat menangani tugas kompleks. Alat web scraping dan code interpreter saat ini gratis untuk waktu terbatas. Untuk informasi selengkapnya, lihat Web search, Web scraping, dan Code interpreter.
Python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)
response = client.responses.create(
model="qwen3-max-2026-01-23",
input="Find the Alibaba Cloud website and extract key information",
# Untuk hasil terbaik, aktifkan semua alat bawaan
tools=[
{"type": "web_search"},
{"type": "code_interpreter"},
{"type": "web_extractor"}
],
extra_body={"enable_thinking": True}
)
# Hapus komentar pada baris di bawah untuk melihat output antara
# print(response.output)
print(response.output_text)
Node.js
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});
async function main() {
const response = await openai.responses.create({
model: "qwen3-max-2026-01-23",
input: "Find the Alibaba Cloud website and extract key information",
tools: [
{ type: "web_search" },
{ type: "code_interpreter" },
{ type: "web_extractor" }
],
enable_thinking: true
});
for (const item of response.output) {
if (item.type === "reasoning") {
console.log("Model is thinking...");
} else if (item.type === "web_search_call") {
console.log(`Search query: ${item.action.query}`);
} else if (item.type === "web_extractor_call") {
console.log("Extracting web content...");
} else if (item.type === "message") {
console.log(`Response: ${item.content[0].text}`);
}
}
}
main();curl
curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-max-2026-01-23",
"input": "Find the Alibaba Cloud website and extract key information",
"tools": [
{
"type": "web_search"
},
{
"type": "code_interpreter"
},
{
"type": "web_extractor"
}
],
"enable_thinking": true
}'Contoh respons
{
"id": "69258b21-5099-9d09-92e8-8492b1955xxx",
"object": "response",
"status": "completed",
"output": [
{
"type": "reasoning",
"summary": [
{
"type": "summary_text",
"text": "The user wants to find the Alibaba Cloud official website and extract information..."
}
]
},
{
"type": "web_search_call",
"status": "completed",
"action": {
"query": "Alibaba Cloud official website",
"type": "search",
"sources": [
{
"type": "url",
"url": "https://cn.aliyun.com/"
},
{
"type": "url",
"url": "https://www.alibabacloud.com/zh"
}
]
}
},
{
"type": "reasoning",
"summary": [
{
"type": "summary_text",
"text": "The search results show the URL of the Alibaba Cloud official website..."
}
]
},
{
"type": "web_extractor_call",
"status": "completed",
"goal": "Extract key information from the Alibaba Cloud official website home page",
"output": "Qwen LLM, complete product system, AI solutions...",
"urls": [
"https://cn.aliyun.com/"
]
},
{
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "Key information from the Alibaba Cloud official website: Qwen LLM, cloud computing services..."
}
]
}
],
"usage": {
"input_tokens": 40836,
"output_tokens": 2106,
"total_tokens": 42942,
"output_tokens_details": {
"reasoning_tokens": 677
},
"x_tools": {
"web_extractor": {
"count": 1
},
"web_search": {
"count": 1
}
}
}
}Migrasi dari API Chat Completions ke API Responses
Jika Anda menggunakan API OpenAI Chat Completions, Anda dapat mengikuti langkah-langkah berikut untuk bermigrasi ke API Responses. API Responses kompatibel dengan API Chat Completions tetapi menawarkan antarmuka yang lebih sederhana dan fitur yang lebih kuat.
1. Perbarui alamat endpoint dan base_url
Anda harus memperbarui kedua hal berikut:
Jalur endpoint: Perbarui jalur dari
/v1/chat/completionsmenjadi/v1/responses.base_url:
Singapore: Perbarui URL dari
https://dashscope-intl.aliyuncs.com/compatible-mode/v1menjadihttps://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1.
Python
# API Chat Completions
completion = client.chat.completions.create(
model="qwen3-max-2026-01-23",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(completion.choices[0].message.content)
# API Responses - dapat menggunakan format pesan yang sama
response = client.responses.create(
model="qwen3-max-2026-01-23",
input=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(response.output_text)
# API Responses - atau gunakan format yang lebih ringkas
response = client.responses.create(
model="qwen3-max-2026-01-23",
input="Hello!"
)
print(response.output_text)Node.js
// API Chat Completions
const completion = await client.chat.completions.create({
model: "qwen3-max-2026-01-23",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" }
]
});
console.log(completion.choices[0].message.content);
// API Responses - dapat menggunakan format pesan yang sama
const response = await client.responses.create({
model: "qwen3-max-2026-01-23",
input: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" }
]
});
console.log(response.output_text);
// API Responses - atau gunakan format yang lebih ringkas
const response2 = await client.responses.create({
model: "qwen3-max-2026-01-23",
input: "Hello!"
});
console.log(response2.output_text);curl
# API Chat Completions
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": "qwen3-max-2026-01-23",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
}'
# API Responses - gunakan format yang lebih ringkas
curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-max-2026-01-23",
"input": "Hello!"
}'2. Perbarui penanganan respons
Struktur respons API Responses berbeda. Anda dapat menggunakan metode pintasan output_text untuk mengambil output teks atau mengakses detail melalui array output.
Perbandingan respons
| |
3. Sederhanakan pengelolaan percakapan multi-putaran
Di Chat Completions API, Anda harus mengelola array riwayat pesan secara manual. Sebaliknya, Responses API menyediakan parameter previous_response_id untuk menautkan konteks secara otomatis. id tanggapan saat ini berlaku selama 7 hari.
Python
| |
Node.js
| |
4. Gunakan alat bawaan
API Responses memiliki beberapa alat bawaan yang tidak perlu Anda implementasikan sendiri. Anda dapat menentukannya dalam parameter tools. Alat code interpreter dan web scraping saat ini gratis untuk waktu terbatas. Untuk informasi selengkapnya, lihat Web search, Code interpreter, dan Web scraping.
Python
| |
Node.js
| |
curl
| |
FAQ
T: Bagaimana cara meneruskan konteks untuk percakapan multi-putaran?
J: Untuk memulai giliran baru dalam percakapan, cukup teruskan id dari tanggapan sebelumnya sebagai previous_response_id.