Text generation is an AI technology that uses deep learning algorithms to create logical and coherent text content based on given prompts.
The prompt required for text generation can be simple keywords, one-sentence summaries, or more complex instructions and contextual information. Large language models (LLMs) analyze large amounts of existing data to learn language patterns and can be used in the following scenarios:
Content creation: Generate news reports, product descriptions, short video scripts, and others.
Customer service: Work as chatbots to provide 24-hour customer support and answer frequently asked questions.
Text translation: Quickly and accurately translate texts from one language to another.
Summary generation: Generate summaries for long articles, reports, and customer emails.
Legal document drafting: Generate contract templates and the basic framework of legal opinions.
Example of text generation: Extract key information from customer emails based on requirements
Prompt |
Please extract the following information from this customer feedback email: 1. Customer information 2. Software version 3. Error code 4. Problem description 5. Operating system 6. Other relevant details
---------------
Email content:
Subject: [Help Request] Smart Assistant Startup Issue
Dear Customer Service and Technical Support Team,
I am Tom from xxx Technology, and my user ID is 12345. I am using the Smart Assistant software version V3.1.8, and I am reporting a technical issue that severely affects work progress. I hope you can assist in resolving it as soon as possible.
Since yesterday afternoon, I have encountered a severe obstacle when starting the Smart Assistant. The software startup process is stuck on the initialization screen, with an error code of ERR-2007, and an error message "Database connection failed," causing all functional modules to be unusable. This situation has persisted until now, severely affecting our office efficiency.
I am currently using a server operating system of Windows 10 version 1909, optimized for 64-bit architecture. After encountering the issue, I have taken several preliminary troubleshooting measures, including completely shutting down and restarting the system to clear any potential temporary software conflicts or system hang states, as well as thoroughly uninstalling and reinstalling the relevant software to eliminate possible software corruption or configuration errors. However, despite these routine solutions, the issue persists without any improvement.
To facilitate a more in-depth diagnosis of the issue, I have provided detailed error screenshots and system log files as attachments. These materials should accurately demonstrate the software state, exception details, and any related error codes at the time of the error, providing key information for quickly locating and resolving the current technical obstacle.
Looking forward to your response!
|
Output |
1. Customer information: Tom from xxx Technology (ID12345)
2. Software version: Smart Assistant V3.1.8
3. Error code: ERR-2007
4. Problem description: The Smart Assistant is stuck on the initialization screen during startup, with an error message "Database connection failed," causing all functional modules to be unusable. The issue has persisted since yesterday afternoon.
5. Operating system: Win10 1909 64-bit
6. Other relevant details: Attempted solutions include restarting the system and reinstalling the software, but the issue remains unresolved. Attachments include error screenshots and log files.
|
Text generation models
Alibaba Cloud Model Studio supports Qwen commercial models and open source models. For a detailed model list, see Text generation models.
Flagship models | Qwen-Max
| Qwen-Plus
| Qwen-Turbo
|
Model name for API calls (Stable version) | qwen-max | qwen-plus | qwen-turbo |
Maximum context (Tokens) | 32,768 | 131,072 | 1,000,000 |
Input unit price (1,000 tokens) | $0.0016 | $0.0004 | $0.00005 |
Output unit price (1,000 tokens) | $0.0064 | $0.0012 | $0.0002 |
Open-source models | Qwen2.5
| Qwen2
| Qwen1.5
|
Maximum context (Tokens) | 131,072 - 1,000,000 | 65,536 - 131,072 | 8,000 |
Output length (Tokens) | 8,192 | 6,144 | 2,000 |
Parameter scale (B: Billion) | 7B - 72B | 7B - 72B | 7B - 110B |
How to choose
Qwen-Max, Qwen-Plus, and Qwen-Turbo are all suitable for various scenarios such as customer service, text creation (such as writing articles and copywriting), text polishing, and summarization. If you are not sure, we recommend that you start with Qwen-Plus for its balance of effectiveness, speed, and cost.
Inference capabilities: Qwen-Max > Qwen-Plus > Qwen-Turbo
Response speed: Qwen-Turbo > Qwen-Plus > Qwen-Max
All three models are compatible with the OpenAI SDK. For more information, see Use OpenAI interface.
For summarization and analysis of extensive documents, you can try Qwen-Plus, which boasts up to 131,072 context tokens.
You can also try and evaluate the models against specific tasks and before making a decision. Quickly and intuitively compare model performance in Playground. You can select multiple text generation models and make a horizontal comparison of model capabilities based on the same input.
How to use
The text generation model takes a prompt as input and generates an output based on that prompt. Model Studio supports the following types of connection: OpenAI SDK, DashScope SDK, and HTTP.
For a complete list of parameters for the OpenAI SDK, see OpenAI. For parameters for the DashScope SDK, see DashScope.
Message types
When you interact with a LLM through API, the input and output are called messages. Each message belongs to one of the following roles: system, user, and assistant.
System message (also known as system prompt): Tells the model the role to play or the behavior to make. The default value is "You are a helpful assistant." You can also place such instructions in the user message, but placing them in the system message is more effective.
User message: The text you input to the model.
Assistant message: The response from the model. You can also preset an assistant message as an example for subsequent assistant messages.
Get started
To use the API, you must first obtain an API Key and set the API key as an environment variable.
OpenAI compatible
DashScope
You can use the OpenAI SDK or OpenAI-compatible HTTP method to try Qwen models.
For a complete list of parameters, see OpenAI.
Sample code
import os
from openai import OpenAI
try:
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-plus",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Who are you?'}
]
)
print(completion.choices[0].message.content)
except Exception as e:
print(f"Error message: {e}")
print("For more information, see: https://www.alibabacloud.com/help/zh/model-studio/developer-reference/error-code")
Sample response
I am a large language model created by Alibaba Cloud. You can call me Qwen.
Sample code
import OpenAI from "openai";
const openai = new OpenAI(
{
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen-plus",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Who are you?" }
],
});
console.log(JSON.stringify(completion))
Sample response
{
"choices": [
{
"message": {
"role": "assistant",
"content": "I am a large language model created by Alibaba Cloud. You can call me Qwen."
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 22,
"completion_tokens": 17,
"total_tokens": 39
},
"created": 1728455191,
"system_fingerprint": null,
"model": "qwen-plus",
"id": "chatcmpl-3a8c00cc-9c9f-9aba-b2d9-dc431e27d1b5"
}
Sample code
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-plus",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Who are you?"
}
]
}'
<?php
$url = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions';
$apiKey = getenv('DASHSCOPE_API_KEY');
$headers = [
'Authorization: Bearer '.$apiKey,
'Content-Type: application/json'
];
$data = [
<?php
$url = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions';
$apiKey = getenv('DASHSCOPE_API_KEY');
$headers = [
'Authorization: Bearer '.$apiKey,
'Content-Type: application/json'
];
$data = [
"model" => "qwen-plus",
"messages" => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who are you?"
]
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
echo $response;
?>
"model" => "qwen-plus",
"messages" => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who are you?"
]
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
echo $response;
?>
using System.Net.Http.Headers;
using System.Text;
class Program
{
private static readonly HttpClient httpClient = new HttpClient();
static async Task Main(string[] args)
{
string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("API Key is not set. Please ensure the environment variable 'DASHSCOPE_API_KEY' is set.");
return;
}
string url = "https://dashscope.aliyuncs-intl.com/compatible-mode/v1/chat/completions";
string jsonContent = @"{
""model"": ""qwen-plus"",
""messages"": [
{
""role"": ""system"",
""content"": ""You are a helpful assistant.""
},
{
""role"": ""user"",
""content"": ""Who are you?""
}
]
}";
string result = await SendPostRequestAsync(url, jsonContent, apiKey);
Console.WriteLine(result);
}
private static async Task<string> SendPostRequestAsync(string url, string jsonContent, string apiKey)
{
using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpClient.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
return $"Request failed: {response.StatusCode}";
}
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type RequestBody struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
}
func main() {
client := &http.Client{}
requestBody := RequestBody{
Model: "qwen-plus",
Messages: []Message{
{
Role: "system",
Content: "You are a helpful assistant.",
},
{
Role: "user",
Content: "Who are you?",
},
},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest("POST", "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions", bytes.NewBuffer(jsonData))
if err != nil {
log.Fatal(err)
}
apiKey := os.Getenv("DASHSCOPE_API_KEY")
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", bodyText)
}
OpenAI does not have an SDK for Java. If you need to use Java, see DashScope SDK for Java.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import com.google.gson.Gson;
public class Main {
static class Message {
String role;
String content;
public Message(String role, String content) {
this.role = role;
this.content = content;
}
}
static class RequestBody {
String model;
Message[] messages;
public RequestBody(String model, Message[] messages) {
this.model = model;
this.messages = messages;
}
}
public static void main(String[] args) {
try {
RequestBody requestBody = new RequestBody(
"qwen-plus",
new Message[] {
new Message("system", "You are a helpful assistant."),
new Message("user", "Who are you?")
}
);
Gson gson = new Gson();
String jsonInputString = gson.toJson(requestBody);
URL url = new URL("https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json; utf-8");
httpURLConnection.setRequestProperty("Accept", "application/json");
String apiKey = System.getenv("DASHSCOPE_API_KEY");
String auth = "Bearer " + apiKey;
httpURLConnection.setRequestProperty("Authorization", auth);
httpURLConnection.setDoOutput(true);
try (OutputStream os = httpURLConnection.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
int responseCode = httpURLConnection.getResponseCode();
System.out.println("Response Code: " + responseCode);
try (BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println("Response Body: " + response);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
Sample response
{
"choices": [
{
"message": {
"role": "assistant",
"content": "I am a large language model created by Alibaba Cloud. You can call me Qwen."
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 22,
"completion_tokens": 17,
"total_tokens": 39
},
"created": 1726127645,
"system_fingerprint": null,
"model": "qwen-plus",
"id": "chatcmpl-81951b98-28b8-9659-ab07-cd30d25600e7"
}
You can use the DashScope SDK or HTTP method to try Qwen models.
For a complete parameter list, see DashScope. For information about how to install the SDK, see DashScope SDK.
Sample code
import os
from dashscope import Generation
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Who are you?'}
]
response = Generation.call(
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="qwen-plus",
messages=messages,
result_format="message"
)
if response.status_code == 200:
print(response.output.choices[0].message.content)
else:
print(f"HTTP return code: {response.status_code}")
print(f"Error code: {response.code}")
print(f"Error message: {response.message}")
print("For more information, see: https://www.alibabacloud.com/help/zh/model-studio/developer-reference/error-code")
Sample response
I am Qwen, an AI assistant developed by Alibaba Cloud. I am designed to answer various questions, provide information, and engage in conversations with users. How can I assist you?
Sample code
import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.protocol.Protocol;
public class Main {
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
Message systemMsg = Message.builder()
.role(Role.SYSTEM.getValue())
.content("You are a helpful assistant.")
.build();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("Who are you?")
.build();
GenerationParam param = GenerationParam.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-plus")
.messages(Arrays.asList(systemMsg, userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.build();
return gen.call(param);
}
public static void main(String[] args) {
try {
GenerationResult result = callWithMessage();
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("Error message: "+e.getMessage());
System.out.println("For more information, see: https://www.alibabacloud.com/help/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
Sample response
I am a large language model created by Alibaba Cloud. You can call me Qwen.
Sample code
curl --location "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-plus",
"input":{
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Who are you?"
}
]
},
"parameters": {
"result_format": "message"
}
}'
<?php
$url = "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
$apiKey = getenv('DASHSCOPE_API_KEY');
$data = [
"model" => "qwen-plus",
"input" => [
"messages" => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who are you?"
]
]
],
"parameters" => [
"result_format" => "message"
]
];
$jsonData = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $apiKey",
"Content-Type: application/json"
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == 200) {
echo "Response: " . $response;
} else {
echo "Error: " . $httpCode . " - " . $response;
}
curl_close($ch);
?>
DashScope does not offer an SDK for the Node.js environment. To make calls using the OpenAI Node.js SDK, refer to the Node.js section in this document.
import fetch from 'node-fetch';
const apiKey = process.env.DASHSCOPE_API_KEY;
const data = {
model: "qwen-plus",
input: {
messages: [
{
role: "system",
content: "You are a helpful assistant."
},
{
role: "user",
content: "Who are you?"
}
]
},
parameters: {
result_format: "message"
}
};
fetch('https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
console.log(JSON.stringify(data));
})
.catch(error => {
console.error('Error:', error);
});
using System.Net.Http.Headers;
using System.Text;
class Program
{
private static readonly HttpClient httpClient = new HttpClient();
static async Task Main(string[] args)
{
string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("API Key is not set. Please ensure the environment variable 'DASHSCOPE_API_KEY' is set.");
return;
}
string url = "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
string jsonContent = @"{
""model"": ""qwen-plus"",
""input"": {
""messages"": [
{
""role"": ""system"",
""content"": ""You are a helpful assistant.""
},
{
""role"": ""user"",
""content"": ""Who are you?""
}
]
},
""parameters"": {
""result_format"": ""message""
}
}";
string result = await SendPostRequestAsync(url, jsonContent, apiKey);
Console.WriteLine(result);
}
private static async Task SendPostRequestAsync(string url, string jsonContent, string apiKey)
{
using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpClient.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
return $"Request failed: {response.StatusCode}";
}
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Input struct {
Messages []Message `json:"messages"`
}
type Parameters struct {
ResultFormat string `json:"result_format"`
}
type RequestBody struct {
Model string `json:"model"`
Input Input `json:"input"`
Parameters Parameters `json:"parameters"`
}
func main() {
client := &http.Client{}
requestBody := RequestBody{
Model: "qwen-plus",
Input: Input{
Messages: []Message{
{
Role: "system",
Content: "You are a helpful assistant.",
},
{
Role: "user",
Content: "Who are you?",
},
},
},
Parameters: Parameters{
ResultFormat: "message",
},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest("POST", "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation", bytes.NewBuffer(jsonData))
if err != nil {
log.Fatal(err)
}
apiKey := os.Getenv("DASHSCOPE_API_KEY")
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", bodyText)
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import com.google.gson.Gson;
public class Main {
static class Message {
String role;
String content;
public Message(String role, String content) {
this.role = role;
this.content = content;
}
}
static class Input {
Message[] messages;
public Input(Message[] messages) {
this.messages = messages;
}
}
static class Parameters {
String result_format;
public Parameters(String result_format) {
this.result_format = result_format;
}
}
static class RequestBody {
String model;
Input input;
Parameters parameters;
public RequestBody(String model, Input input, Parameters parameters) {
this.model = model;
this.input = input;
this.parameters = parameters;
}
}
public static void main(String[] args) {
try {
RequestBody requestBody = new RequestBody(
"qwen-plus",
new Input(new Message[] {
new Message("system", "You are a helpful assistant."),
new Message("user", "Who are you?")
}),
new Parameters("message")
);
Gson gson = new Gson();
String jsonInputString = gson.toJson(requestBody);
URL url = new URL("https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json; utf-8");
httpURLConnection.setRequestProperty("Accept", "application/json");
String apiKey = System.getenv("DASHSCOPE_API_KEY");
String auth = "Bearer " + apiKey;
httpURLConnection.setRequestProperty("Authorization", auth);
httpURLConnection.setDoOutput(true);
try (OutputStream os = httpURLConnection.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
Sample response
{
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "I am a large language model created by Alibaba Cloud. You can call me Qwen."
}
}
]
},
"usage": {
"total_tokens": 38,
"output_tokens": 16,
"input_tokens": 22
},
"request_id": "09dceb20-ae2e-999b-85f9-c5ab266198c0"
}
Asynchronous calling
You can achieve concurrency and enhance program efficiency using the Asyncio interface. Sample code:
OpenAI compatible
DashScope
Sample code
import os
import asyncio
from openai import AsyncOpenAI
import platform
client = AsyncOpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
)
async def task(question):
print(f"Sending question: {question}")
response = await client.chat.completions.create(
messages=[
{"role": "user", "content": question}
],
model="qwen-plus",
)
print(f"Received answer: {response.choices[0].message.content}")
async def main():
questions = ["Who are you?", "What can you do?", "How is the weather?"]
tasks = [task(q) for q in questions]
await asyncio.gather(*tasks)
if __name__ == '__main__':
if platform.system() == 'Windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main(), debug=False)
Sample code
Your DashScope SDK for Python must be at least version 1.19.0.
import asyncio
import platform
from dashscope.aigc.generation import AioGeneration
import os
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
async def task(question):
print(f"Sending question: {question}")
response = await AioGeneration.call(
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="qwen-plus",
prompt=question
)
print(f"Received answer: {response.output.text}")
async def main():
questions = ["Who are you?", "What can you do?", "How is the weather?"]
tasks = [task(q) for q in questions]
await asyncio.gather(*tasks)
if __name__ == '__main__':
if platform.system() == 'Windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main(), debug=False)
Common parameters for controlling generation
temperature and top_p
These parameters are used to control the diversity of text generated by the model. The higher the temperature or top_p, the more diverse the generated text. The lower the values, the more deterministic the text.
Diverse texts are suitable for scenarios such as creative writing (novels or advertisement), brainstorming, chat applications, and others.
Deterministic texts are suitable for scenarios with clear answers (such as problem analysis, multiple-choice questions, factual queries) or requiring precise wording (such as technical documents, legal texts, news reports, academic papers).
temperature
The higher the temperature parameter, the flatter the probability distribution of tokens is (The chance of high-probability token decreases, and the chance of low-probability increases), making the model more random in selecting the next token.
The lower the temperature parameter, the steeper the probability distribution of tokens is (The chance of high-probability tokens increases, and the chance of low-probability tokens decreases), making the model more inclined to choose a few high-probability tokens.
top_p
Top-p sampling refers to sampling from the set of tokens with the highest probabilities (the core set). This sampling method sorts all possible tokens by probability from high to low, then accumulates probabilities starting from the highest probability token until the cumulative probability reaches a threshold (For example, if top_p is set to 0.8, the threshold is 80%). Finally, the model randomly selects one token from these high-probability tokens for output.
The higher the top_p parameter, the more tokens are considered, resulting in more diverse generated text.
The lower the top_p parameter, the fewer tokens are considered, resulting in more focused and deterministic generated text.
API reference
For a complete parameter list for the OpenAI SDK, see OpenAI. For a complete parameter list for the DashScope SDK, see DashScope.
Learn more
Prompt engineering
A prompt is a textual input given to an LLM about the problem to be solved or the task to be completed. Prompt is the foundation for the LLM to comprehend user requirements and generate relevant and precise responses. The process of designing and optimizing prompts to enhance LLM response is called prompt engineering.
If you are interested in prompt engineering, see Best practices for prompt engineering to learn how to build effective prompts to enhance model performance.
You can also go to the Prompt Engineering page of the Model Studio console to quickly use templates for text generation.
Multimodal capability
Multimodal capability refers to the ability of a model to process and integrate various types of data modalities (such as text, images, audio, and video) for understanding, processing, and generating information. This capability enables the model to understand and generate content more comprehensively, enhance contextual understanding, and improve model performance.
Alibaba Cloud Model Studio supports:
Qwen-VL (Text + Image -> Text): A Qwen model with image understanding capabilities that can perform tasks such as OCR, visual inference, and text understanding. It supports resolutions of over a million pixels and images with any aspect ratio.
FAQ
What do the suffixes in text generation models, like -chat and -instruct, mean? Do they impact model performance?
These suffixes indicate that the model has undergone fine-tuning and reinforcement learning, with specialization in specific scenarios. You can choose the appropriate model based on your business scenario.
-chat indicates that the model is designed specifically for handling human-computer interaction, excelling at understanding context and generating coherent and contextually relevant responses. It is suitable for dialog tasks, such as chatbots, virtual assistants, or customer support scenarios, and is adept at providing natural, fluent, and conversationally appropriate responses.
-Instruct indicates that the model can understand and execute complex natural language instructions and has strong tool calling capabilities, suitable for executing specific instructions, such as answering questions, generating texts, translating, and other tasks.
What do the numerical suffixes in Qwen models, like -0428 or -0206, represent?
The suffix indicates that the model is a snapshot version. For example, -0428 means the model is a snapshot from April 28.
When should I to select a snapshot version?
The Qwen models are periodically updated (versions without numeric suffixes).
If you require consistent performance for your application without being impacted by model revisions, you can use a snapshot version. (Currently, snapshot versions are accessible only via API calls to create applications)
We recommend that you assess the performance differences between the snapshot version in use and the newly released versions. Then, you can switch between versions to maintain optimal performance.
When I use Web-based Qwen, it can parse the links I enter. Why cannot the Qwen API do the same thing?
The web version has additional engineering optimizations on top of the Qwen API, and parsing web links is not a capability of the Qwen API itself. You can use web scraping tools such as Beautiful Soup in Python to read web content.
The Web-based Qwen and the Qwen API show significant differences in their responses. Why is this?
The web version has additional engineering optimizations on top of the Qwen API, thus enabling features such as web page parsing, online searching, drawing, and creating slides. These features are not the capabilities of the model API itself.