O Qwen-Long processa documentos de até 10 milhões de tokens por meio de um mecanismo de upload e referência de arquivos, superando os limites de contexto dos modelos padrão.
ObservaçãoEste documento se aplica apenas à região da China continental (Beijing). Para usar o modelo, utilize uma chave de API da região da China continental (Beijing).
Como usar
Use o Qwen-Long em duas etapas: faça o upload dos arquivos e depois chame a API.
-
Upload e análise de arquivos:
- Faça o upload de um arquivo usando a API. Para obter detalhes sobre os formatos de arquivo suportados e limites de tamanho, consulte Formatos suportados.
- Após um upload bem-sucedido, o sistema retorna um
file-idúnico para sua conta e inicia a análise. Não há cobrança pelo upload, armazenamento ou análise do arquivo.
-
Chamada de API e faturamento:
- Ao chamar o modelo, referencie um ou mais
file-ids na mensagemsystem. - O modelo executa a inferência com base no conteúdo de texto associado ao
file-id. - Em cada chamada de API, o número de tokens no conteúdo do arquivo referenciado conta como tokens de entrada daquela solicitação.
- Ao chamar o modelo, referencie um ou mais
Essa abordagem evita a transferência de arquivos grandes em cada solicitação, mas observe que os tokens do arquivo são cobrados por chamada de API.
Primeiros passos
Pré-requisitos
- Obtenha uma chave de API e configure-a como variável de ambiente.
- Para chamar o modelo via SDK, instale o OpenAI SDK.
Fazer upload de um documento
Este exemplo faz o upload do arquivo Model_Studio_Phone_Product_Introduction.docx para o armazenamento seguro do Model Studio por meio da interface compatível com OpenAI e obtém um file-id. Consulte a documentação da API para ver os parâmetros de upload.
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"'
Execute o código para obter o file-id do arquivo enviado.
Passar informações e conversar usando um ID de arquivo
Passe o file-id nas mensagens do sistema: a primeira mensagem define a função, a segunda contém o file-id e, em seguida, adicione as perguntas do usuário.
Documentos mais longos exigem maior tempo de análise. Aguarde a conclusão da análise antes de fazer a chamada.
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/en/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/en/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/en/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/en/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
}
}'
Passar vários documentos
Passe vários file-id s em uma única mensagem do sistema ou adicione mensagens de sistema separadas para cada documento.
Pass multiple documents
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/en/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/en/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/en/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/en/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
}
}'
Append documents
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/en/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/en/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/en/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/en/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/en/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
}
}'
Passar informações como texto simples
Em vez de usar file-id s, passe o conteúdo do documento diretamente como string. Adicione as definições de função na primeira mensagem para evitar confusão com o conteúdo do documento.
Se o conteúdo do documento exceder 1 milhão de tokens, use um ID de arquivo devido aos limites de tamanho da API.
Simple example
Insira o conteúdo do documento diretamente na Mensagem do 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/en/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/en/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/en/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
}
}'
Pass multiple documents
Para passar vários documentos em um único turno de conversa, coloque o conteúdo de cada documento em uma Mensagem do Sistema separada.
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/en/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/en/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/en/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
}
}'
Append documents
Durante a interação com o modelo, talvez seja necessário adicionar novas informações de documentos. Para isso, anexe o novo conteúdo do documento como uma Mensagem do Sistema ao array 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/en/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/en/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/en/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/en/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/en/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
}
}'
Preços do modelo
China continental
Se você selecionar o escopo de implantação China continental, os recursos de computação para inferência do modelo ficarão restritos à China continental. Os dados estáticos são armazenados na região selecionada. Região suportada: China (Beijing).
| Modelo | Versão | Janela de contexto | Entrada máx. | Saída máx. | Custo de entrada | Custo de saída | Cota gratuita |
|---|---|---|---|---|---|---|---|
| (tokens) | (por 1M tokens) | ||||||
qwen-long
| Estável | 10.000.000 | 10.000.000 | 32.768 | 0,5 CNY | 2 CNY | 1 milhão de tokens cada Válido por 90 dias após ativar o Model Studio |
qwen-long-latest
| Mais recente | ||||||
qwen-long-2025-01-25
| Snapshot | 0,5 CNY | 2 CNY | ||||
| Nome do modelo | Versão | Tamanho do contexto | Entrada máx. | Saída máx. | Custo de entrada | Custo de saída |
|---|---|---|---|---|---|---|
| (Tokens) | (por 1 milhão de tokens) | |||||
qwen-long-latest
| Mais recente | 10.000.000 | 10.000.000 | 32.768 | $0,072 | $0,287 |
qwen-long-2025-01-25
| Snapshot | |||||
Perguntas frequentes
-
O modelo Qwen-Long suporta o envio de jobs em lote?
Sim. O Qwen-Long suporta a API Batch da OpenAI com 50% das taxas de chamadas em tempo real. Envie jobs em lote como arquivos; os jobs são executados assincronicamente e retornam resultados após a conclusão ou timeout.
-
Onde os arquivos são salvos após o upload usando a API de arquivos compatível com OpenAI?
Os arquivos são enviados para o bucket do Model Studio sem custo. Consulte a API de Arquivos OpenAI para consultar e gerenciar arquivos.
-
O que é
qwen-long-2025-01-25?Trata-se de um snapshot de versão congelado em um ponto específico no tempo. É mais estável que
lateste não tem data de expiração. -
Como garantir que o modelo gere uma string JSON em formato padrão?
O
qwen-longe todos os snapshots suportam saída estruturada. Especifique um JSON Schema para garantir um JSON válido que corresponda à sua estrutura.
Referência da API
Consulte Detalhes da API Qwen para ver os parâmetros de entrada e saída do modelo Qwen-Long.
Códigos de erro
Se a chamada do modelo falhar e retornar uma mensagem de erro, consulte Códigos de erro para resolução.
Limites
-
Dependências do SDK:
- Operações de arquivo (upload, exclusão, consulta) exigem um SDK compatível com OpenAI.
- Invoque modelos usando um SDK compatível com OpenAI ou Dashscope SDK.
-
Upload de arquivos:
- Formatos suportados: TXT, DOCX, PDF, XLSX, EPUB, MOBI, MD, CSV, JSON, BMP, PNG, JPG/JPEG e GIF.
- Tamanho do arquivo: O tamanho máximo para arquivos de imagem é 20 MB. Para outros formatos de arquivo, o limite é 150 MB.
- Cota da conta: Máximo de 10.000 arquivos ou 100 GB por conta. Os uploads falham quando qualquer um dos limites é atingido. Exclua arquivos para liberar cota. Consulte Compatível com OpenAI - Arquivo.
- Período de armazenamento: Atualmente, não há limite de expiração para arquivos armazenados.
-
Entradas da API:
- A primeira mensagem
systemdefine a função. A segunda contém o conteúdo do documento oufileid://xxx. A mensagemusercontém a consulta. - Ao referenciar arquivos usando um
file-id, uma única solicitação pode referenciar no máximo 100 arquivos. - Com uma segunda mensagem
system, o limite da mensagemuseré de 9.000 tokens. Não há limite com apenas uma mensagem de sistema. - O tamanho total do contexto é limitado a 10 milhões de tokens.
- A primeira mensagem
-
Saídas da API:
- O comprimento máximo de saída é 32.768 tokens.
-
Compartilhamento de arquivos:
- Os
file-ids são específicos da conta e não podem ser usados entre contas diferentes ou com chaves de API de usuários RAM.
- Os
-
Limitação de taxa: Para obter informações sobre as condições de limitação de taxa do modelo, consulte Limitação de taxa.