Qwen-Long gestiona documentos de hasta 10 millones de tokens mediante un mecanismo de carga y referencia de archivos, superando los límites de contexto de los modelos estándar.
NotaEste documento se aplica únicamente a la región de China continental (Pekín). Para utilizar el modelo, debe usar una clave de API de la región de China continental (Pekín).
Cómo usar
Utilice Qwen-Long en dos pasos: cargue los archivos y luego llame a la API.
-
Carga y análisis de archivos:
- Cargue un archivo mediante la API. Para obtener detalles sobre los formatos de archivo admitidos y los límites de tamaño, consulte Formatos admitidos.
- Después de una carga exitosa, el sistema devuelve un único
file-idpara su cuenta y comienza el análisis. No se cobran tarifas por la carga, el almacenamiento o el análisis de archivos.
-
Llamada a la API y facturación:
- Cuando llame al modelo, haga referencia a uno o más
file-ids en el mensajesystem. - El modelo realiza la inferencia basándose en el contenido de texto asociado al
file-id. - Para cada llamada a la API, el número de tokens en el contenido del archivo referenciado se cuenta como tokens de entrada para esa solicitud.
- Cuando llame al modelo, haga referencia a uno o más
Esto evita transferir archivos grandes en cada solicitud, pero tenga en cuenta que los tokens de archivo se facturan por cada llamada a la API.
Introducción
Requisitos previos
- Obtenga una clave de API y configúrela como variable de entorno.
- Para llamar al modelo mediante SDK, instale el OpenAI SDK.
Cargar un documento
Este ejemplo carga Model_Studio_Phone_Product_Introduction.docx en el almacenamiento seguro de Model Studio a través de la interfaz compatible con OpenAI y obtiene un file-id. Consulte la documentación de la API para ver los parámetros de carga.
import os
from pathlib import Path
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # If not configured, replace with your API key.
# The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
file_object = client.files.create(file=Path("Model_Studio_Phone_Product_Introduction.docx"), purpose="file-extract")
print(file_object.id)
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.files.*;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
// Create a client and use the API key from the environment variable.
OpenAIClient client = OpenAIOkHttpClient.builder()
// If you have not configured the environment variable, replace the following line with: .apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
// Set the file path. Modify the path and filename as needed.
Path filePath = Paths.get("src/main/java/org/example/Model_Studio_Phone_Product_Introduction.docx");
// Create file upload parameters.
FileCreateParams fileParams = FileCreateParams.builder()
.file(filePath)
.purpose(FilePurpose.of("file-extract"))
.build();
// Upload the file and print the file-id.
FileObject fileObject = client.files().create(fileParams);
System.out.println(fileObject.id());
}
}
# The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
curl --location --request POST 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/files' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--form 'file=@"Alibaba Cloud Model Studio Phone Series Product Introduction.docx"' \
--form 'purpose="file-extract"'
Ejecute el código para obtener el file-id del archivo cargado.
Pasar información y chatear mediante un ID de archivo
Pase el file-id en los mensajes del sistema: el primer mensaje define el rol, el segundo contiene el file-id y luego agregue las preguntas del usuario.
Los documentos más extensos requieren más tiempo de análisis. Espere a que finalice el análisis antes de realizar la llamada.
import os
from openai import OpenAI, BadRequestError
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # If not configured, replace with your API key.
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
try:
# Initialize messages list.
completion = client.chat.completions.create(
model="qwen-long",
messages=[
# sys1: Role definition.
{'role': 'system', 'content': 'You are a helpful assistant.'},
# sys2: Document content (plain text or file-id).
# Replace '{FILE_ID}' with the file-id used in your conversation.
{'role': 'system', 'content': f'fileid://{FILE_ID}'},
# When the request includes a second system message, the user message content is limited to 9,000 tokens.
{'role': 'user', 'content': 'What is this article about?'}
],
# All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
stream=True,
stream_options={"include_usage": True}
)
full_content = ""
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
# Concatenate the output content.
full_content += chunk.choices[0].delta.content
print(chunk.model_dump())
# Get token usage.
if chunk.usage:
print(f"Total tokens: {chunk.usage.total_tokens}")
print(full_content)
except BadRequestError as e:
print(f"Error: {e}")
print("See documentation: https://www.alibabacloud.com/help/model-studio/error-code")
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.*;
public class Main {
public static void main(String[] args) {
// Create a client and use the API key from the environment variable.
OpenAIClient client = OpenAIOkHttpClient.builder()
// If you have not configured the environment variable, replace the following line with: .apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
// Create a chat request.
ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
//sys1: Role definition.
.addSystemMessage("You are a helpful assistant.")
//sys2: Document content (plain text or file-id).
//Replace '{FILE_ID}' with the file-id used in your conversation.
.addSystemMessage("fileid://{FILE_ID}")
//When the request includes a second system message, the user message content is limited to 9,000 tokens.
.addUserMessage("What is this article about?")
.model("qwen-long")
.build();
StringBuilder fullResponse = new StringBuilder();
// All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
streamResponse.stream().forEach(chunk -> {
// Print and concatenate the content of each chunk.
System.out.println(chunk);
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
fullResponse.append(content);
}
});
System.out.println(fullResponse);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.err.println("See documentation: https://www.alibabacloud.com/help/model-studio/error-code");
}
}
}
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-long",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "fileid://file-fe-xxx"},
{"role": "user","content": "What is this article about?"}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
Pasar varios documentos
Pase múltiples file-ids en un solo mensaje del sistema o agregue mensajes del sistema separados para cada documento.
Pasar varios documentos
import os
from openai import OpenAI, BadRequestError
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # If not configured, replace with your API key.
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
try:
# Initialize messages list.
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
# Replace '{FILE_ID1}' and '{FILE_ID2}' with the file-ids used in your conversation.
{'role': 'system', 'content': f"fileid://{FILE_ID1},fileid://{FILE_ID2}"},
{'role': 'user', 'content': 'What are these articles about?'}
],
# All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
stream=True,
stream_options={"include_usage": True}
)
full_content = ""
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
# Concatenate the output content.
full_content += chunk.choices[0].delta.content
print(chunk.model_dump())
print(full_content)
except BadRequestError as e:
print(f"Error: {e}")
print("See documentation: https://www.alibabacloud.com/help/model-studio/error-code")
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.*;
public class Main {
public static void main(String[] args) {
// Create a client and use the API key from the environment variable.
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
// Create a chat request.
ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful assistant.")
//Replace '{FILE_ID1}' and '{FILE_ID2}' with the file-ids used in your conversation.
.addSystemMessage("fileid://{FILE_ID1},fileid://{FILE_ID2}")
.addUserMessage("What are these two articles about?")
.model("qwen-long")
.build();
StringBuilder fullResponse = new StringBuilder();
// All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
streamResponse.stream().forEach(chunk -> {
// The content of each chunk.
System.out.println(chunk);
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
fullResponse.append(content);
}
});
System.out.println("\nFull response content:");
System.out.println(fullResponse);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.err.println("See documentation: https://www.alibabacloud.com/help/model-studio/error-code");
}
}
}
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-long",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "fileid://file-fe-xxx1"},
{"role": "system","content": "fileid://file-fe-xxx2"},
{"role": "user","content": "What are these two articles about?"}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
Añadir documentos
import os
from openai import OpenAI, BadRequestError
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # If not configured, replace with your API key.
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
# Initialize the messages list.
messages = [
{'role': 'system', 'content': 'You are a helpful assistant.'},
# Replace '{FILE_ID1}' with the file-id used in your conversation.
{'role': 'system', 'content': f'fileid://{FILE_ID1}'},
{'role': 'user', 'content': 'What is this article about?'}
]
try:
# First-round response
completion_1 = client.chat.completions.create(
model="qwen-long",
messages=messages,
stream=False
)
# Print first-round response.
# To stream: set stream=True, concatenate segments, and pass to assistant_message content.
print(f"First-round response: {completion_1.choices[0].message.model_dump()}")
except BadRequestError as e:
print(f"Error: {e}")
print("See documentation: https://www.alibabacloud.com/help/model-studio/error-code")
# Construct the assistant_message.
assistant_message = {
"role": "assistant",
"content": completion_1.choices[0].message.content}
# Add assistant_message to messages.
messages.append(assistant_message)
# Add the file-id of the appended document to messages.
# Replace '{FILE_ID2}' with the file-id used in your conversation.
system_message = {'role': 'system', 'content': f'fileid://{FILE_ID2}'}
messages.append(system_message)
# Add the user's question.
messages.append({'role': 'user', 'content': 'What are the similarities and differences between the methods discussed in these two articles?'})
# Response after appending the document.
completion_2 = client.chat.completions.create(
model="qwen-long",
messages=messages,
# All code examples use streaming output to clearly and intuitively show the model output process. For non-streaming output examples, see https://www.alibabacloud.com/help/model-studio/text-generation
stream=True,
stream_options={
"include_usage": True
}
)
# Stream and print the response after appending the document.
print("Response after appending the document:")
for chunk in completion_2:
print(chunk.model_dump())
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.*;
import com.openai.core.http.StreamResponse;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
// Initialize messages list.
List<ChatCompletionMessageParam> messages = new ArrayList<>();
// Add information for role setting.
ChatCompletionSystemMessageParam roleSet = ChatCompletionSystemMessageParam.builder()
.content("You are a helpful assistant.")
.build();
messages.add(ChatCompletionMessageParam.ofSystem(roleSet));
// Replace '{FILE_ID1}' with the file-id used in your conversation.
ChatCompletionSystemMessageParam systemMsg1 = ChatCompletionSystemMessageParam.builder()
.content("fileid://{FILE_ID1}")
.build();
messages.add(ChatCompletionMessageParam.ofSystem(systemMsg1));
// User question message (USER role).
ChatCompletionUserMessageParam userMsg1 = ChatCompletionUserMessageParam.builder()
.content("Please summarize the article content.")
.build();
messages.add(ChatCompletionMessageParam.ofUser(userMsg1));
// Construct the first-round request and handle exceptions.
ChatCompletion completion1;
try {
completion1 = client.chat().completions().create(
ChatCompletionCreateParams.builder()
.model("qwen-long")
.messages(messages)
.build()
);
} catch (Exception e) {
System.err.println("Request error. See error code page:");
System.err.println("https://www.alibabacloud.com/help/model-studio/error-code");
System.err.println("Error details: " + e.getMessage());
e.printStackTrace();
return;
}
// First-round response.
String firstResponse = completion1 != null ? completion1.choices().get(0).message().content().orElse("") : "";
System.out.println("First-round response: " + firstResponse);
// Construct AssistantMessage.
ChatCompletionAssistantMessageParam assistantMsg = ChatCompletionAssistantMessageParam.builder()
.content(firstResponse)
.build();
messages.add(ChatCompletionMessageParam.ofAssistant(assistantMsg));
// Replace '{FILE_ID2}' with the file-id used in your conversation.
ChatCompletionSystemMessageParam systemMsg2 = ChatCompletionSystemMessageParam.builder()
.content("fileid://{FILE_ID2}")
.build();
messages.add(ChatCompletionMessageParam.ofSystem(systemMsg2));
// Second-round user question (USER role).
ChatCompletionUserMessageParam userMsg2 = ChatCompletionUserMessageParam.builder()
.content("Please compare the structural differences between the two articles.")
.build();
messages.add(ChatCompletionMessageParam.ofUser(userMsg2));
// All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
StringBuilder fullResponse = new StringBuilder();
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(
ChatCompletionCreateParams.builder()
.model("qwen-long")
.messages(messages)
.build())) {
streamResponse.stream().forEach(chunk -> {
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
fullResponse.append(content);
}
});
System.out.println("\nFinal response:");
System.out.println(fullResponse.toString().trim());
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.err.println("See documentation: https://www.alibabacloud.com/help/model-studio/error-code");
}
}
}
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-long",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "fileid://file-fe-xxx1"},
{"role": "user","content": "What is this article about?"},
{"role": "system","content": "fileid://file-fe-xxx2"},
{"role": "user","content": "What are the similarities and differences between the methods discussed in these two articles?"}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
Pasar información como texto sin formato
En lugar de utilizar file-ids, pase el contenido del documento directamente como una cadena. Agregue la configuración de roles en el primer mensaje para evitar confusiones con el contenido del documento.
Si el contenido del documento supera 1 millón de tokens, utilice un ID de archivo en su lugar debido a los límites de tamaño de la API.
Ejemplo sencillo
Puede introducir el contenido del documento directamente en el mensaje del sistema.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # Replace your API key here if you haven't set the environment variable
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
# Initialize the messages list
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'system', 'content': 'Alibaba Cloud Model Studio smartphone product introduction: Alibaba Cloud Model Studio X1 —————— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen...'},
{'role': 'user', 'content': 'What does the article talk about?'}
],
# All code examples use streaming output to clearly and intuitively show the model's output process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
stream=True,
stream_options={"include_usage": True}
)
full_content = ""
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
# Append output content
full_content += chunk.choices[0].delta.content
print(chunk.model_dump())
print(full_content)
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.*;
public class Main {
public static void main(String[] args) {
// Create a client using the API key from the environment variable
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
// Create a chat request
ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful assistant.")
.addSystemMessage("Alibaba Cloud Model Studio smartphone product introduction: Alibaba Cloud Model Studio X1 —————— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen...")
.addUserMessage("What does this article talk about?")
.model("qwen-long")
.build();
StringBuilder fullResponse = new StringBuilder();
// All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
streamResponse.stream().forEach(chunk -> {
// Print and append each chunk's content
System.out.println(chunk);
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
fullResponse.append(content);
}
});
System.out.println(fullResponse);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.err.println("For more information, see https://www.alibabacloud.com/help/model-studio/error-code");
}
}
}
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-long",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate, ..."},
{"role": "user","content": "What does this article talk about?"}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
Pasar varios documentos
Para pasar varios documentos en un solo turno de conversación, coloque el contenido de cada documento en un mensaje del sistema separado.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # Replace your API key here if you haven't set the environment variable
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
# Initialize the messages list
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'system', 'content': 'Alibaba Cloud Model Studio X1————Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate...'},
{'role': 'system', 'content': 'Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design...'},
{'role': 'user', 'content': 'What are the similarities and differences between the products discussed in these two articles?'}
],
# All code examples use streaming output to clearly and intuitively show the model's output process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
stream=True,
stream_options={"include_usage": True}
)
full_content = ""
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
# Append output content
full_content += chunk.choices[0].delta.content
print(chunk.model_dump())
print(full_content)
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.*;
public class Main {
public static void main(String[] args) {
// Create a client using the API key from the environment variable
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
// Create a chat request
ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful assistant.")
.addSystemMessage("Alibaba Cloud Model Studio smartphone product introduction: Alibaba Cloud Model Studio X1 —————— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen...")
.addSystemMessage("Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design...")
.addUserMessage("What are the similarities and differences between the products discussed in these two articles?")
.model("qwen-long")
.build();
StringBuilder fullResponse = new StringBuilder();
// All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
streamResponse.stream().forEach(chunk -> {
// Print and append each chunk's content
System.out.println(chunk);
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
fullResponse.append(content);
}
});
System.out.println(fullResponse);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.err.println("For more information, see https://www.alibabacloud.com/help/model-studio/error-code");
}
}
}
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-long",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate..."},
{"role": "system","content": "Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design..."},
{"role": "user","content": "What are the similarities and differences between the products discussed in these two articles?"}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
Añadir documentos
Durante su interacción con el modelo, es posible que necesite agregar nueva información de documentos. Para hacerlo, añada el nuevo contenido del documento como un mensaje del sistema al arreglo Messages.
import os
from openai import OpenAI, BadRequestError
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # Replace your API key here if you haven't set the environment variable
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
# Initialize the messages list
messages = [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'system', 'content': 'Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate...'},
{'role': 'user', 'content': 'What does this article talk about?'}
]
try:
# First-round response
completion_1 = client.chat.completions.create(
model="qwen-long",
messages=messages,
stream=False
)
# Print the first-round response
# For streaming output in the first round, set stream=True and concatenate each segment's content. Pass the concatenated string as the content when constructing assistant_message
print(f"First-round response: {completion_1.choices[0].message.model_dump()}")
except BadRequestError as e:
print(f"Error: {e}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/error-code")
# Construct assistant_message
assistant_message = {
"role": "assistant",
"content": completion_1.choices[0].message.content}
# Append assistant_message to messages
messages.append(assistant_message)
# Append new document content to messages
system_message = {
'role': 'system',
'content': 'Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design, delivering an immersive visual experience...'}
messages.append(system_message)
# Add user question
messages.append({
'role': 'user',
'content': 'What are the similarities and differences between the products discussed in these two articles?'
})
# Response after appending the document
completion_2 = client.chat.completions.create(
model="qwen-long",
messages=messages,
# All code examples use streaming output to clearly and intuitively show the model's output process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
stream=True,
stream_options={"include_usage": True}
)
# Stream and print the response after appending the document
print("Response after appending the document:")
for chunk in completion_2:
print(chunk.model_dump())
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.*;
import com.openai.core.http.StreamResponse;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
// Initialize the messages list
List<ChatCompletionMessageParam> messages = new ArrayList<>();
// Add role-setting information
ChatCompletionSystemMessageParam roleSet = ChatCompletionSystemMessageParam.builder()
.content("You are a helpful assistant.")
.build();
messages.add(ChatCompletionMessageParam.ofSystem(roleSet));
// First-round content
ChatCompletionSystemMessageParam systemMsg1 = ChatCompletionSystemMessageParam.builder()
.content("Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate, 256GB storage, 12GB RAM, and a 5000mAh long-lasting battery...")
.build();
messages.add(ChatCompletionMessageParam.ofSystem(systemMsg1));
// User question (USER role)
ChatCompletionUserMessageParam userMsg1 = ChatCompletionUserMessageParam.builder()
.content("Please summarize the article content")
.build();
messages.add(ChatCompletionMessageParam.ofUser(userMsg1));
// Build the first-round request and handle exceptions
ChatCompletion completion1;
try {
completion1 = client.chat().completions().create(
ChatCompletionCreateParams.builder()
.model("qwen-long")
.messages(messages)
.build()
);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.err.println("For more information, see https://www.alibabacloud.com/help/model-studio/error-code");
e.printStackTrace();
return;
}
// First-round response
String firstResponse = completion1 != null ? completion1.choices().get(0).message().content().orElse("") : "";
System.out.println("First-round response: " + firstResponse);
// Construct AssistantMessage
ChatCompletionAssistantMessageParam assistantMsg = ChatCompletionAssistantMessageParam.builder()
.content(firstResponse)
.build();
messages.add(ChatCompletionMessageParam.ofAssistant(assistantMsg));
// Second-round content
ChatCompletionSystemMessageParam systemMsg2 = ChatCompletionSystemMessageParam.builder()
.content("Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design, delivering an immersive visual experience...")
.build();
messages.add(ChatCompletionMessageParam.ofSystem(systemMsg2));
// Second-round user question (USER role)
ChatCompletionUserMessageParam userMsg2 = ChatCompletionUserMessageParam.builder()
.content("Please compare the structural differences between the two descriptions")
.build();
messages.add(ChatCompletionMessageParam.ofUser(userMsg2));
// All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
StringBuilder fullResponse = new StringBuilder();
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(
ChatCompletionCreateParams.builder()
.model("qwen-long")
.messages(messages)
.build())) {
streamResponse.stream().forEach(chunk -> {
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
fullResponse.append(content);
}
});
System.out.println("\nFinal response:");
System.out.println(fullResponse.toString().trim());
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.err.println("For more information, see https://www.alibabacloud.com/help/model-studio/error-code");
}
}
}
# Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-long",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate..."},
{"role": "user","content": "What does this article talk about?"},
{"role": "system","content": "Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design, delivering an immersive visual experience..."},
{"role": "user","content": "What are the similarities and differences between the products discussed in these two articles"}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
Precios del modelo
| Nombre del modelo | Versión | Longitud del contexto | Entrada máxima | Salida máxima | Costo de entrada | Costo de salida |
|---|---|---|---|---|---|---|
| (Tokens) | (por 1 millón de tokens) | |||||
qwen-long
| Estable | 10,000,000 | 10,000,000 | 32,768 | $0.072 | $0.287 |
FAQ
-
¿El modelo Qwen-Long admite el envío de trabajos por lotes?
Sí. Qwen-Long admite la Batch API de OpenAI al 50% de las tarifas de llamada en tiempo real. Envíe trabajos por lotes como archivos; los trabajos se ejecutan de forma asíncrona y devuelven resultados al completarse o agotarse el tiempo de espera.
-
¿Dónde se guardan los archivos después de cargarlos mediante la API de archivos compatible con OpenAI?
Los archivos se cargan en su bucket de Model Studio sin costo alguno. Consulte la API de archivos de OpenAI para consultar y gestionar archivos.
-
¿Cómo puedo garantizar que el modelo genere una cadena JSON en un formato estándar?
qwen-longy todas las instantáneas admiten salida estructurada. Especifique un esquema JSON para garantizar un JSON válido que coincida con su estructura.
Referencia de API
Consulte Qwen detalles de la API para conocer los parámetros de entrada y salida del modelo Qwen-Long.
Códigos de error
Si la llamada al modelo falla y devuelve un mensaje de error, consulte Códigos de error para su resolución.
Límites
-
Dependencias del SDK:
- Las operaciones con archivos (carga, eliminación, consulta) requieren un SDK compatible con OpenAI.
- Invoque modelos mediante un SDK compatible con OpenAI o Dashscope SDK.
-
Carga de archivos:
- Formatos admitidos: TXT, DOCX, PDF, XLSX, EPUB, MOBI, MD, CSV, JSON, BMP, PNG, JPG/JPEG y GIF.
- Tamaño de archivo: El tamaño máximo para archivos de imagen es de 20 MB. El tamaño máximo para otros formatos de archivo es de 150 MB.
- Cuota de la cuenta: Máximo 10,000 archivos o 100 GB por cuenta. Las cargas fallan cuando se alcanza cualquiera de los límites. Elimine archivos para liberar cuota. Consulte Compatible con OpenAI - Archivo.
- Periodo de almacenamiento: Actualmente, no existe un límite de caducidad para los archivos almacenados.
-
Entradas de API:
- El primer mensaje
systemdefine el rol. El segundo contiene el contenido del documento ofileid://xxx. El mensajeusercontiene la consulta. - Al hacer referencia a archivos mediante un
file-id, una sola solicitud puede referenciar un máximo de 100 archivos. - Con un segundo mensaje
system, el límite del mensajeuseres de 9,000 tokens. No hay límite si solo hay un mensaje del sistema. - La longitud total del contexto está limitada a 10 millones de tokens.
- El primer mensaje
-
Salidas de API:
- La longitud máxima de salida es de 32,768 tokens.
-
Uso compartido de archivos:
- Los
file-ids son específicos de la cuenta y no pueden utilizarse entre cuentas ni con claves de API de usuarios RAM.
- Los
-
Limitación de tasa: Para obtener información sobre las condiciones de limitación de tasa del modelo, consulte Limitación de tasa.