Alibaba Cloud Model Studio prend en charge l'API Responses compatible avec OpenAI. S'appuyant sur l'API Chat Completions, l'API Responses simplifie les fonctionnalités natives des agents.
Avantages par rapport à l'API Chat Completions d'OpenAI :- Outils intégrés : améliorez les résultats pour les tâches complexes grâce à la recherche web, au scraping web, à un interpréteur de code, ainsi qu'aux fonctionnalités de génération d'images à partir de texte et de modification d'images. Pour plus de détails, consultez la section Appeler des outils intégrés.
- Entrées plus flexibles : l'API accepte aussi bien les entrées sous forme de chaînes directes que les tableaux de messages au format de chat standard.
- Gestion du contexte simplifiée : le passage de l'identifiant
previous_response_idélimine le besoin de construire manuellement un tableau complet de l'historique des messages.
Consultez la référence de l'API Responses d'OpenAI pour obtenir des détails sur les paramètres.
Prérequis
Commencez par obtenir une clé API et la configurer comme variable d'environnement. Si vous utilisez le SDK OpenAI, installez le SDK.
ImportantL'ancien chemin /api/v2/apps/protocols/compatible-mode/v1/responses pour l'API Responses compatible avec OpenAI sera bientôt obsolète. Veuillez migrer vers le nouveau chemin /compatible-mode/v1/responses dès que possible.
ImportantAlibaba Cloud Model Studio a publié des domaines spécifiques aux espaces de travail pour les régions Chine (Pékin), Singapour et Chine (Hong Kong). Les nouveaux domaines dédiés offrent des performances supérieures et une stabilité accrue pour les requêtes d'inférence. Nous vous recommandons de migrer vers les nouveaux domaines :
- Chine (Pékin) : passez de
https://dashscope.aliyuncs.comàhttps://{WorkspaceId}.cn-beijing.maas.aliyuncs.com - Singapour : passez de
https://dashscope-intl.aliyuncs.comàhttps://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com - Chine (Hong Kong) : passez de
https://cn-hongkong.dashscope.aliyuncs.comàhttps://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com
{WorkspaceId} correspond à l'ID de votre espace de travail, visible sur la page Workspace Details dans la console Alibaba Cloud Model Studio. Le domaine existant reste entièrement fonctionnel.
Modèles pris en charge
Singapore
International deployment scopeqwen3.8-max, qwen3.7-max, qwen3.7-max-2026-05-20, qwen3.7-max-2026-06-08, qwen3.7-plus, qwen3.7-plus-2026-05-26, qwen3.6-plus, qwen3.6-plus-2026-04-02, qwen3.5-plus, qwen3.5-plus-2026-04-20, qwen3.5-plus-2026-02-15, qwen3.7-flash, qwen3.7-flash-2026-07-15, qwen3.6-flash, qwen3.6-flash-2026-04-16, qwen3.5-flash, qwen3.5-flash-2026-02-23, qwen3.8-2.4t-a95b, qwen3.8-27b, qwen3.6-35b-a3b, qwen3.5-397b-a17b, qwen3.5-122b-a10b, qwen3.5-27b, qwen3.5-35b-a3b, qwen-plus, qwen-flash, qwen3-coder-flash, qwen-plus-character, qwen-flash-character, deepseek-v4.1-flash-1017, deepseek-v4-pro, deepseek-v4-flash-0731、glm-5.2
US (Virginia)
Global deployment scopeqwen3.8-max, qwen3.7-max, qwen3.7-max-2026-05-20, qwen3.7-max-2026-06-08, qwen3.7-plus, qwen3.7-plus-2026-05-26, qwen3.6-plus, qwen3.6-plus-2026-04-02, qwen3.5-plus, qwen3.5-plus-2026-02-15, qwen3.7-flash, qwen3.7-flash-2026-07-15, qwen3.6-flash, qwen3.6-flash-2026-04-16, qwen3.5-flash, qwen3.5-flash-2026-02-23, qwen3.6-35b-a3b, qwen3.5-122b-a10b, qwen3.5-27b, qwen3.5-35b-a3b
Germany (Frankfurt)
Global deployment scopeqwen3.8-max, qwen3.7-max, qwen3.7-max-2026-05-20, qwen3.7-max-2026-06-08, qwen3.7-plus, qwen3.7-plus-2026-05-26, qwen3.7-flash, qwen3.7-flash-2026-07-15,qwen3.5-397b-a17b, qwen3.5-122b-a10b, qwen3.5-35b-a3b, qwen3.5-27b
China (Beijing)
qwen3.8-max, qwen3.7-max, qwen3.7-max-2026-05-20, qwen3.7-max-2026-06-08, qwen3.6-plus, qwen3.6-plus-2026-04-02, qwen3.5-plus, qwen3.5-plus-2026-04-20, qwen3.5-plus-2026-02-15, qwen3.7-flash, qwen3.7-flash-2026-07-15, qwen3.6-flash, qwen3.6-flash-2026-04-16, qwen3.5-flash, qwen3.5-flash-2026-02-23, qwen3.8-2.4t-a95b, qwen3.8-27b, qwen3.6-35b-a3b, qwen3.5-397b-a17b, qwen3.5-122b-a10b, qwen3.5-27b, qwen3.5-35b-a3b, qwen-plus, qwen-flash, qwen3-coder-flash, qwen-plus-character, qwen-flash-character, deepseek-v4.1-flash-1017, deepseek-v4-pro, deepseek-v4-flash-0731, glm-5.2
Hong Kong (China)
Global deployment scopeqwen3.8-max, qwen3.7-plus, qwen3.7-plus-2026-05-26, qwen3.6-plus, qwen3.7-flash, qwen3.7-flash-2026-07-15, qwen3.6-flash, qwen3.5-flash, qwen3.5-flash-2026-02-23
qwen-plus, qwen3.5-flash, qwen3.5-flash-2026-02-23
Japan (Tokyo)
Japan deployment scopeqwen3.7-plus, qwen3.7-plus-2026-05-26
qwen3.8-max, qwen3.7-plus, qwen3.7-plus-2026-05-26, qwen3.7-max, qwen3.7-max-2026-05-20, qwen3.6-plus, qwen3.6-plus-2026-04-02, qwen3.7-flash, qwen3.7-flash-2026-07-15, qwen3.6-flash, qwen3.6-flash-2026-04-16
deepseek-v4.1-flash-1017 prend en charge l’API Responses et ses fonctionnalités d’agent dans les six régions : cn-beijing, ap-southeast-1, us-east-1, eu-central-1, ap-northeast-1 et cn-hongkong. Il prend en charge les trois outils intégrés web_search, web_extractor et code_interpreter.
Endpoints
Singapore
Valeur du paramètre base_url pour la configuration des appels SDK : https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1
URL de la requête HTTP : POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses
Remplacez WorkspaceId par votre Workspace ID réel.
China (Beijing)
Valeur du paramètre base_url pour la configuration du SDK : https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
URL de la requête HTTP : POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/responses
Remplacez WorkspaceId par votre Workspace ID réel.
US (Virginia)
Valeur du paramètre base_url pour la configuration des appels SDK : https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/compatible-mode/v1
URL de la requête HTTP : POST https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/compatible-mode/v1/responses
Remplacez WorkspaceId par votre Workspace ID réel.
China (Hong Kong)
Valeur du paramètre base_url pour la configuration des appels SDK : https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1
URL de la requête HTTP : POST https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1/responses
Remplacez WorkspaceId par votre Workspace ID réel.
Germany (Frankfurt)
Valeur du paramètre base_url pour le SDK : https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/compatible-mode/v1
URL de la requête HTTP : POST https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/compatible-mode/v1/responses
Remplacez WorkspaceId par votre Workspace ID réel.
Japan (Tokyo)
Valeur du paramètre base_url pour la configuration des appels SDK : https://{WorkspaceId}.ap-northeast-1.maas.aliyuncs.com/compatible-mode/v1
URL de la requête HTTP : POST https://{WorkspaceId}.ap-northeast-1.maas.aliyuncs.com/compatible-mode/v1/responses
Remplacez WorkspaceId par votre Workspace ID réel.
Exemples de code
Appel de base
Envoyez un message et obtenez une réponse.
import os
from openai import OpenAI
client = OpenAI(
# If an environment variable is not set, replace with your API key: api_key="sk-xxx"
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
response = client.responses.create(
model="qwen3.8-max",
input="What can you do?"
)
# Get model response
# print(response.model_dump_json())
print(response.output_text)
import OpenAI from "openai";
const openai = new OpenAI({
// If an environment variable is not set, replace with your API key: apiKey: "sk-xxx"
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
const response = await openai.responses.create({
model: "qwen3.8-max",
input: "What can you do?"
});
// Get model response
console.log(response.output_text);
}
main();
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.8-max",
"input": "What can you do?"
}'
Il s'agit d'une réponse API complète.
{
"created_at": 1771226624,
"id": "bf0d5c2e-f14b-9ad7-bc0d-ee0c8c9ee2d8",
"model": "qwen3.8-max",
"object": "response",
"output": [
{
"content": [
{
"annotations": [],
"text": "Hi there! I'm actually quite ......",
"type": "output_text"
}
],
"id": "msg_1e17fdb2-5fc3-4c78-a9e9-cbd78eb043f0",
"role": "assistant",
"status": "completed",
"type": "message"
}
],
"parallel_tool_calls": false,
"status": "completed",
"tool_choice": "auto",
"tools": [],
"usage": {
"input_tokens": 37,
"input_tokens_details": {
"cached_tokens": 0
},
"output_tokens": 220,
"output_tokens_details": {
"reasoning_tokens": 0
},
"total_tokens": 257,
"x_details": [
{
"input_tokens": 37,
"output_tokens": 220,
"total_tokens": 257,
"x_billing_type": "response_api"
}
]
}
}
Conversation multi-tours
Le paramètre previous_response_id conserve automatiquement le contexte de la conversation ; vous n'avez donc pas besoin de construire manuellement l'historique des messages. Chaque id de réponse reste valide pendant 7 jours.
Le paramètre
previous_response_iddoit correspondre à l'idde niveau supérieur de la réponse précédente (par exemple,resp_xxx, au format UUID), et non à l'iddu message situé dans le tableauoutput(par exemple,msg_56c860c4-3ad8-4a96-8553-d2f94c259xxx).
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# First round
response1 = client.responses.create(
model="qwen3.8-max",
input="My name is John, please remember it."
)
print(f"First response: {response1.output_text}")
# Second round - use previous_response_id to link context
# The response id expires in 7 days
response2 = client.responses.create(
model="qwen3.8-max",
input="Do you remember my name?",
previous_response_id=response1.id
)
print(f"Second response: {response2.output_text}")
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
// First round
const response1 = await openai.responses.create({
model: "qwen3.8-max",
input: "My name is John, please remember it."
});
console.log(`First response: ${response1.output_text}`);
// Second round - use previous_response_id to link context
// The response id expires in 7 days
const response2 = await openai.responses.create({
model: "qwen3.8-max",
input: "Do you remember my name?",
previous_response_id: response1.id
});
console.log(`Second response: ${response2.output_text}`);
}
main();
# First round
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.8-max",
"input": "My name is John, please remember it."
}'
# Second round - use the id from the first response as previous_response_id
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.8-max",
"input": "Do you remember my name?",
"previous_response_id": "response_id_from_first_round"
}'
{
"id": "f0dbb153-117f-9bbf-8176-5284b47f3xxx",
"created_at": 1769173209.0,
"model": "qwen3.8-max",
"object": "response",
"status": "completed",
"output": [
{
"id": "msg_56c860c4-3ad8-4a96-8553-d2f94c259xxx",
"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
}
}
}
Remarque : Lors du deuxième tour, le nombre de input_tokens est de 78. Ce total inclut le contexte du premier tour, ce qui confirme que le modèle a bien mémorisé le nom « John ».
Réflexion approfondie
Utilisez le paramètre reasoning pour contrôler l'intensité du raisonnement du modèle. Lorsque vous définissez reasoning.effort, le modèle réfléchit avant de répondre et renvoie le processus de réflexion dans un élément de sortie reasoning. Le paramètre effort prend en charge les valeurs suivantes :
none: désactive la réflexion et fournit une réponse directe.minimal: minimise la réflexion pour obtenir la réponse la plus rapide possible.low: effectue une réflexion légère, en privilégiant la rapidité de la réponse.medium(par défaut) : effectue une réflexion modérée, en équilibrant vitesse et profondeur d'analyse.high: effectue une réflexion approfondie, en se concentrant sur les problèmes complexes et spécialisés.
Vous ne pouvez pas utiliser le paramètre
thinking_budgetpour contrôler la longueur maximale de la réflexion. Le paramètrereasoning.effortest prioritaire surenable_thinking. Utilisezreasoning.effort, carenable_thinkingsera obsolète.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
response = client.responses.create(
model="qwen3.8-max",
input="Which is larger, 9.9 or 9.11?",
reasoning={"effort": "medium"}
)
# Process the output
for item in response.output:
if item.type == "reasoning":
print("=== Thinking Process ===")
for summary in item.summary:
print(summary.text)
elif item.type == "message":
print("\n=== Final Answer ===")
print(item.content[0].text)
# Check the thinking token count
print(f"\nThinking token count: {response.usage.output_tokens_details.reasoning_tokens}")
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
const response = await openai.responses.create({
model: "qwen3.8-max",
input: "Which is larger, 9.9 or 9.11?",
reasoning: { effort: "medium" }
});
for (const item of response.output) {
if (item.type === "reasoning") {
console.log("=== Thinking Process ===");
for (const summary of item.summary) {
console.log(summary.text);
}
} else if (item.type === "message") {
console.log("\n=== Final Answer ===");
console.log(item.content[0].text);
}
}
// Check the thinking token count
console.log(`\nThinking token count: ${response.usage.output_tokens_details.reasoning_tokens}`);
}
main();
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.8-max",
"input": "Which is larger, 9.9 or 9.11?",
"reasoning": {"effort": "medium"}
}'
{
"created_at": 1774498317,
"id": "resp_xxx",
"model": "qwen3.8-max",
"object": "response",
"output": [
{
"id": "msg_xxx",
"summary": [
{
"text": "Thinking Process:\n\n1. Analyze the Request:\n * Question: \"Which is larger, 9.9 or 9.11?\"\n * Context: The user is asking a simple mathematical comparison question.\n * Current Date: Thursday, March 26, 2026 (provided in the system prompt).\n * Knowledge Cutoff: 2026 (provided in the system prompt).\n\n2. Evaluate the Numbers:\n * Number A: 9.9\n * Number B: 9.11\n * These are decimal numbers.\n * Compare the integer part: Both are 9.\n * Compare the tenths place (first decimal digit):\n * 9.9 has 9 in the tenths place.\n * 9.11 has 1 in the tenths place.\n * Since 9 > 1, 9.9 is greater than 9.11.\n\n3. Consider Potential Ambiguities:\n * Could this be version numbering? (e.g., software versions). In versioning, 9.11 is often \"newer\" or \"higher\" than 9.9. However, mathematically, 9.9 > 9.11.\n * Could this be dates? (September 9th vs September 11th). 11th is later.\n * Standard interpretation: Without context, decimal numbers are assumed to be mathematical values.\n * Common pitfall: Some people mistakenly treat decimals like whole numbers (where 11 > 9), leading them to think 9.11 > 9.9. This is a known cognitive bias or misconception in elementary math.\n * Decision: Provide the mathematical answer clearly, but perhaps acknowledge the versioning context if relevant (though usually, for this specific question, it's a math test). Given the simplicity, stick to the mathematical truth first.\n\n4. Formulate the Answer:\n * Direct answer: 9.9 is larger.\n * Explanation: Compare place values. 9.9 = 9.90, 9.11 = 9.11. 90 hundredths > 11 hundredths.\n * Tone: Helpful, clear.\n\n5. Drafting the Response (Internal Monologue/Draft):\n * Okay, mathematically 9.9 is bigger. 9.9 is 9 and 9 tenths. 9.11 is 9 and 11 hundredths (or 1 tenth and 1 hundredth). 9 tenths is more than 1 tenth.\n * So, 9.9 > 9.11.\n * I should answer in English since the question is in English.\n * \"9.9 is larger.\"\n * Add a brief explanation to avoid confusion. \"Because 9.9 equals 9.90, and 9.90 is greater than 9.11.\"\n\n6. Refining based on \"Version Number\" possibility:\n * Sometimes this is a trick question about software versions. In semver, 9.11 > 9.9.\n * However, usually, when asked plainly \"which is larger\", it refers to numerical value.\n * I will provide the numerical answer as primary, maybe add a note about versions if it feels necessary. But keeping it simple is better. Most likely, this is testing the decimal comparison logic.\n * Let's stick to the mathematical value as the default.\n\n7. Final Polish:\n * Answer: 9.9 is larger.\n * Reasoning: When comparing decimals, first look at the integer part, then the tenths place. The tenths digit of 9.9 is 9, and the tenths digit of 9.11 is 1. Since 9 > 1, 9.9 > 9.11.\n\n8. Output Generation: (Matching the thought process)\n * State clearly which is larger.\n * Explain why.\n\n *Self-Correction on Date/Time:* The system prompt mentions the current date is 2026. This doesn't affect the math question, but I should ensure I don't reference anything post-2026 if I were adding context (not needed here).\n\n *Final Decision:* Just answer the math question directly.\n\n \"Numerically, 9.9 is larger.\"\n Explain: 9.9 = 9.90, 9.11 = 9.11. 90 > 11.\n\n Wait, is there any other interpretation?\n - Dates? 9/9 vs 9/11. 11th is later.\n - Versions? 9.11 is newer.\n - But \"who is big/larger\" usually implies magnitude.\n - I will answer based on numerical magnitude but briefly mention the version context if it adds value without confusing. Actually, better to be decisive. Numerical value is the standard interpretation for \"who is larger\" with numbers.\n\n Let's go with the numerical answer.\n\n Plan:\n 1. State 9.9 is larger numerically.\n 2. Explain the decimal comparison.\n 3. (Optional but helpful) Mention that if it's version numbers, 9.11 might be considered \"higher\". But prioritize the math.\n Actually, for a simple query like this, over-explaining versions might be confusing. I'll stick to the math but acknowledge the common confusion.\n\n *Revised Plan:*\n 1. Direct answer: 9.9 is larger (mathematically).\n 2. Explanation: Place value.\n 3. Note: Unless it's a version number.\n\n Let's keep it concise.\n\n \"In terms of mathematical value, 9.9 is larger.\"\n \"Because 9.9 equals 9.90, which is greater than 9.11.\"\n \"If it's a version number, version 9.11 is usually higher than version 9.9.\"\n This covers both bases.\n\n Wait, looking at common AI training, this is often a \"trap\" question to see if the AI treats decimals like integers. The correct mathematical answer is crucial.\n\n Okay, final output structure:\n 1. Mathematical answer.\n 2. Brief explanation.\n 3. Contextual note (versions).\n\n Let's write it.\n \"In terms of mathematical value, 9.9 is larger.\"\n \"When comparing decimals, first compare the integer part (both are 9), then the tenths place: The tenths digit of 9.9 is 9, while the tenths digit of 9.11 is 1. Because 9 > 1, 9.9 > 9.11.\"\n \"However, if these are software version numbers, version 9.11 is typically newer than version 9.9.\"\n\n This is balanced.\n\n Wait, I need to check the instruction again. \"Please remember the current actual time...\". This is just system context. I don't need to mention the date in the answer unless relevant. It's not relevant here.\n\n Okay, ready to generate.\n\n Wait, one more check. Is there any cultural context? In some contexts, people might say 9.11 is bigger because 11 > 9. This is the misconception I need to correct.\n So, emphasizing the mathematical rule is key.\n\n Final check:\n 9.9 = 9 + 9/10 = 9.90\n 9.11 = 9 + 1/10 + 1/100 = 9.11\n 9.90 > 9.11. Correct.\n\n Response will be in English.\n \"In terms of numerical value, 9.9 is larger.\"\n Explain why.\n Mention version exception.\n Done.\n",
"type": "summary_text"
}
],
"type": "reasoning"
},
{
"content": [
{
"annotations": [],
"text": "In terms of mathematical value, 9.9 is larger.\n\nHere's why:\nWhen comparing decimals, you first look at the integer part, which is 9 for both. Then you look at the tenths place (the first digit after the decimal point):\n* The tenths digit of 9.9 is 9\n* The tenths digit of 9.11 is 1\n\nSince 9 is greater than 1, 9.9 > 9.11 (you can think of 9.9 as 9.90 for comparison).\n\nNote: If these were software version numbers, version 9.11 would typically be considered newer (or higher) than version 9.9. However, in a direct numerical comparison, 9.9 is larger.",
"type": "output_text"
}
],
"id": "msg_xxx",
"role": "assistant",
"status": "completed",
"type": "message"
}
],
"parallel_tool_calls": false,
"status": "completed",
"tool_choice": "auto",
"tools": [],
"usage": {
"input_tokens": 57,
"input_tokens_details": {
"cached_tokens": 0
},
"output_tokens": 2018,
"output_tokens_details": {
"reasoning_tokens": 1861
},
"total_tokens": 2075,
"x_details": [
{
"input_tokens": 57,
"output_tokens": 2018,
"output_tokens_details": {
"reasoning_tokens": 1861
},
"total_tokens": 2075,
"x_billing_type": "response_api"
}
]
}
}
Sortie en flux continu
Recevez le contenu généré par le modèle en temps réel, ce qui s'avère particulièrement utile pour la génération de textes longs.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
stream = client.responses.create(
model="qwen3.8-max",
input="Please briefly introduce artificial intelligence.",
stream=True
)
print("Receiving stream output:")
for event in stream:
# print(event.model_dump_json()) # Uncomment to see raw event response
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}")
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
const stream = await openai.responses.create({
model: "qwen3.8-max",
input: "Please briefly introduce artificial intelligence.",
stream: true
});
console.log("Receiving stream output:");
for await (const event of stream) {
// console.log(JSON.stringify(event)); // Uncomment to see raw event response
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 -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.8-max",
"input": "Please briefly introduce artificial intelligence.",
"stream": true
}'
{"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","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":4,"type":"response.output_text.delta"}
{"content_index":0,"delta":" intelligence","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":5,"type":"response.output_text.delta"}
{"content_index":0,"delta":" (","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":6,"type":"response.output_text.delta"}
{"content_index":0,"delta":"AI","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 ways of working","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 simulating human intelligent behavior by using computer systems. 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 simulating human intelligent behavior by using computer systems. 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 simulating human intelligent behavior by using computer systems. It aims to enable machines to perform tasks that typically require human intelligence, such as:\n\n- Learning (for example, training models with data) \n- Reasoning (for example, logical judgment and problem-solving) \n- Perception (for example, recognizing images, speech, or text) \n- Understanding language (for example, natural language processing) \n- Decision-making (for example, making optimal choices in complex environments)\n\nAI can be divided into weak AI (focused on specific tasks, such as voice assistants and recommendation systems) and strong AI (possessing general, human-like intelligence, which has not yet been achieved).\n\nCurrently, AI is widely used in various fields, including 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.8-max","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"}
Utiliser les outils intégrés
Activez les outils intégrés pour réaliser des tâches complexes. L'extracteur web et l'interpréteur de code sont gratuits pour une durée limitée. Consultez la rubrique appel d'outils pour connaître les outils pris en charge.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
response = client.responses.create(
model="qwen3.8-max",
input="Find the Alibaba Cloud website and extract key information from the homepage",
# For best results, enable all the built-in tools.
tools=[
{"type": "web_search"},
{"type": "code_interpreter"},
{"type": "web_extractor"}
],
reasoning={"effort": "medium"}
)
# Uncomment the following line to see the intermediate output.
# print(response.output)
print(response.output_text)
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
const response = await openai.responses.create({
model: "qwen3.8-max",
input: "Find the Alibaba Cloud website and extract key information from the homepage",
tools: [
{ type: "web_search" },
{ type: "code_interpreter" },
{ type: "web_extractor" }
],
reasoning: { effort: "medium" }
});
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 -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.8-max",
"input": "Find the Alibaba Cloud website and extract key information from the homepage",
"tools": [
{
"type": "web_search"
},
{
"type": "code_interpreter"
},
{
"type": "web_extractor"
}
],
"reasoning": {"effort": "medium"}
}'
{
"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 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 Alibaba Cloud website URL..."
}
]
},
{
"type": "web_extractor_call",
"status": "completed",
"goal": "Extract key information from the Alibaba Cloud homepage",
"output": "Tongyi Large Language Model, full product portfolio, AI solutions...",
"urls": [
"https://cn.aliyun.com/"
]
},
{
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "Key information from the Alibaba Cloud website: Tongyi Large Language Model, 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
}
}
}
}
Cache de session
Lors de conversations multi-tours, activez le cache de session pour permettre au serveur de mettre en cache automatiquement le contexte de la conversation. Cette approche réduit la latence et les coûts sans nécessiter de gestion manuelle du cache.
Utilisation : Pour activer le cache de session, ajoutez x-dashscope-session-cache: enable à l'en-tête de la requête. Pour le désactiver, définissez la valeur sur disable. La valeur par défaut est disable.
-
Cache de session activé :
- Le modèle prend en charge le cache explicite : utilise le cache explicite. Pour plus d'informations sur la facturation et les contraintes, consultez la section Cache explicite.
- Le modèle ne prend pas en charge le cache explicite mais prend en charge le cache implicite : utilise le cache implicite. Pour plus d'informations sur la facturation et les contraintes, consultez la section Cache implicite.
-
Cache de session non activé : fonctionnement identique aux appels API standards. Le cache implicite reste automatiquement activé pour les modèles qui le prennent en charge, mais sans les avantages liés au cache de session.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
# Enable session cache via default_headers
default_headers={"x-dashscope-session-cache": "enable"}
)
# Construct a long text exceeding 1024 tokens to trigger cache creation.
# (If the initial prompt is under 1024 tokens, the server creates the cache once the total context exceeds this threshold.)
long_context = "Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence." * 50
# First request
response1 = client.responses.create(
model="qwen3.8-max",
input=long_context + "\n\nBased on the background knowledge above, briefly introduce the random forest algorithm in machine learning.",
)
print(f"First response: {response1.output_text}")
# Second request: Link the context using previous_response_id. The server manages the cache automatically.
response2 = client.responses.create(
model="qwen3.8-max",
input="What are the main differences between it and GBDT?",
previous_response_id=response1.id,
)
print(f"Second response: {response2.output_text}")
# Check the cache hit status
usage = response2.usage
print(f"Input tokens: {usage.input_tokens}")
print(f"Cached tokens: {usage.input_tokens_details.cached_tokens}")
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
// Enable session cache via defaultHeaders
defaultHeaders: {"x-dashscope-session-cache": "enable"}
});
// Construct a long text exceeding 1024 tokens to trigger cache creation.
// (If the initial prompt is under 1024 tokens, the server creates the cache once the total context exceeds this threshold.)
const longContext = "Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence.".repeat(50);
async function main() {
// First request
const response1 = await openai.responses.create({
model: "qwen3.8-max",
input: longContext + "\n\nBased on the background knowledge above, briefly introduce the random forest algorithm in machine learning, including its basic principles and use cases."
});
console.log(`First response: ${response1.output_text}`);
// Second request: Link the context using previous_response_id. The server manages the cache automatically.
const response2 = await openai.responses.create({
model: "qwen3.8-max",
input: "What are the main differences between it and GBDT?",
previous_response_id: response1.id
});
console.log(`Second response: ${response2.output_text}`);
// Check the cache hit status
console.log(`Input tokens: ${response2.usage.input_tokens}`);
console.log(`Cached tokens: ${response2.usage.input_tokens_details.cached_tokens}`);
}
main();
# First request
# The 'input' text must exceed 1024 tokens to trigger cache creation.
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "x-dashscope-session-cache: enable" \
-d '{
"model": "qwen3.8-max",
"input": "Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence. Artificial intelligence is an important branch of computer science that focuses on the research and development of theories, methods, technologies, and application systems that can simulate, extend, and expand human intelligence.\n\nBased on the background knowledge above, briefly introduce the random forest algorithm in machine learning, including its basic principles and use cases."
}'
# Second request - Set 'previous_response_id' to the 'id' from the first response.
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "x-dashscope-session-cache: enable" \
-d '{
"model": "qwen3.8-max",
"input": "What are the main differences between it and GBDT?",
"previous_response_id": "id_from_first_response"
}'
Migrer de l'API Chat Completions vers l'API Responses
L'API Responses simplifie l'interface de l'API Chat Completions tout en maintenant la compatibilité. Pour effectuer la migration, suivez les étapes ci-dessous.
1. Mettez à jour l'adresse du endpoint
Modifiez l'adresse du endpoint de /v1/chat/completions vers /v1/responses.
# Chat Completions API
completion = client.chat.completions.create(
model="qwen3.8-max",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(completion.choices[0].message.content)
# Responses API - can use the same message format
response = client.responses.create(
model="qwen3.8-max",
input=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(response.output_text)
# Responses API - or use a more concise format
response = client.responses.create(
model="qwen3.8-max",
input="Hello!"
)
print(response.output_text)
// Chat Completions API
const completion = await client.chat.completions.create({
model: "qwen3.8-max",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" }
]
});
console.log(completion.choices[0].message.content);
// Responses API - can use the same message format
const response = await client.responses.create({
model: "qwen3.8-max",
input: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" }
]
});
console.log(response.output_text);
// Responses API - or use a more concise format
const response2 = await client.responses.create({
model: "qwen3.8-max",
input: "Hello!"
});
console.log(response2.output_text);
# Chat Completions API
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": "qwen3.8-max",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
}'
# Responses API - use a more concise format
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.8-max",
"input": "Hello!"
}'
2. Mettez à jour la gestion des réponses
L'API Responses renvoie une structure de réponse différente. Utilisez le raccourci output_text pour récupérer la sortie textuelle, ou accédez aux informations détaillées via le tableau output.
| |
3. Simplifiez les conversations multi-tours
Avec l'API Chat Completions, vous devez gérer manuellement le tableau d'historique des messages. L'API Responses simplifie ce processus en utilisant le paramètre previous_response_id pour lier automatiquement le contexte de la conversation. L'id de la réponse est valide pendant 7 jours.
Python
| |
Node.js
| |
4. Utilisez les outils intégrés
L'API Responses inclut des outils intégrés. Spécifiez-les dans le paramètre tools. L'interpréteur de code et les outils de recherche web sont gratuits pour une durée limitée. Consultez la section appel d'outils.
Python
| |
Node.js
| |
Curl
| |
FAQ
Q : Comment transmettre le contexte d'une conversation multi-tours ?
R : Transmettez l'id de la précédente réponse réussie du modèle en tant que paramètre previous_response_id dans votre prochaine requête de conversation.
Q : Pourquoi ne puis-je pas afficher 'output_text' ?
R : Cet attribut est absent dans certaines versions du SDK OpenAI pour Python, comme la version 1.99.2. Pour résoudre cette erreur, mettez à jour le SDK vers la dernière version.
Rubriques connexes
- Réponses OpenAI
- Obtenir votre clé API
- Stocker votre clé API dans une variable d'environnement