Tag categorization
This topic describes the syntax and provides examples for the tag categorization feature of the Tongyi Data Mining API.
Request syntax
POST /zhiwen-chat/tagging HTTP/1.1
Request parameters
Name | Type | Required | Description | Example value |
|---|---|---|---|---|
capabilityType | string | Yes | The type of capability. Tag classification: TAG_CLASSIFY | TAG_CLASSIFY |
userPrompt | string | Yes | The prompt. | Analyze the business scope of a company, extract key information, and provide the output in a structured format. Stop after the output is complete and do not add new content. |
fileIdList | array | Yes | A list of document IDs. | [ "file_zhiwen_XXX" ] |
stream | boolean | Yes | Specifies whether to return the response in a stream. | true |
Response parameters
Name | Type | Description |
|---|---|---|
output | object | |
-choices | array | |
-finishReason | string | The reason the process finished. |
-message | object | The message body. |
-role | string | The role. |
-content | string | The message content. |
requestId | string | The request ID. |
usage | object | Information about token usage. |
-inputTokens | int | The number of input tokens. |
-outputTokens | int | The number of output tokens. |
-totalTokens | int | The total number of tokens. |
Examples
Request example
import os
import requests
'''
API name: Tag Categorization
API path: https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-chat/tagging
Environment requirements: Python >= 3.7
'''
url = "https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-chat/tagging"
request_headers = {
"Authorization": os.environ.get("DASHSCOPE_API_KEY"), # If you have not configured the environment variable, replace this with your API key.
"Content-Type": "application/json"
}
# Tag categorization: TAG_CLASSIFY
request_body = {
"capabilityType": "TAG_CLASSIFY",
"userPrompt": "Analyze the business scope of a company, extract key information, and provide the output in a structured format. Stop after the output is complete and do not add new content.",
"stream": True,
"fileIdList": [
"file_zhiwen_XXX" # Replace this with your document ID.
]
}
with requests.post(url, json=request_body, headers=request_headers) as response:
if request_body["stream"]:
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data:'):
print("Received:", line[5:].strip())
else:
print("Received:", response.json())
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/**
* API name: Tag Categorization
* API path: https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-chat/tagging
* Environment requirements: Java 8 or later
*/
public class TaggingDemo {
// Step 1. API address and configuration
private static final String API_KEY = System.getenv("DASHSCOPE_API_KEY"); // Enter the API key from the Alibaba Cloud Model Studio platform.
private static final String API_URL = "https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-chat/tagging";
private static final String CHARSET = "UTF-8";
private static final ObjectMapper objectMapper = new ObjectMapper();
public static void main(String[] args) throws IOException, InterruptedException {
// Step 2. Configure request parameters.
// The fileId field returned by the document upload API. The format is file_zhiwen_XXX.
String fileId = "file_zhiwen_XXX";
// Custom prompt: Modify this as needed. For examples, see the Effect Debugging page of the Tongyi ZoneWit lightweight application.
String userPrompt = "Analyze the business scope of a company, extract key information, and provide the output in a structured format. Stop after the output is complete and do not add new content.";
// Streaming: This example uses streaming output. Configure this as needed.
boolean isStreaming = true;
// Step 3. Build the request body.
Map<String, Object> requestBodyMap = new HashMap<>();
requestBodyMap.put("userPrompt", userPrompt);
requestBodyMap.put("stream", isStreaming);
requestBodyMap.put("fileIdList", new String[]{fileId});
String requestBody = objectMapper.writeValueAsString(requestBodyMap);
// Step 4. Configure the request timeout period.
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).build();
// Step 5. Execute the request. Choose between a streaming or non-streaming request as needed.
try (CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.build()) {
// Construct the HTTP POST request.
HttpPost httpPost = new HttpPost(API_URL);
buildRequestHeaders(httpPost, requestBody);
// Execute the request. This example uses streaming output. Configure the stream parameter as needed.
buildStreamingRequest(httpClient, httpPost); // Streaming request
// buildNonStreamingRequest(httpClient, httpPost); // Non-streaming request
}
}
/**
* Build common request headers.
*
* @param httpPost HTTP request object
* @param requestBody Request body string
*/
private static void buildRequestHeaders(HttpPost httpPost, String requestBody) {
httpPost.setHeader("Authorization", API_KEY);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(requestBody, CHARSET));
}
/**
* Build a streaming request.
* @param client HTTP client
* @param httpPost HTTP request object
* @return Asynchronously executed CompletableFuture object
*/
private static void buildStreamingRequest(CloseableHttpClient client, HttpPost httpPost) {
System.out.println("Streaming request:");
try (CloseableHttpResponse response = client.execute(httpPost)) {
HttpEntity entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 && entity != null) {
try (InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("data:")) {
String json = line.substring(5).trim();
System.out.println(json);
}
}
}
} else {
String errorBody = EntityUtils.toString(response.getEntity());
System.err.println("Request failed: " + errorBody);
}
} catch (IOException e) {
System.err.println("Request failed: " + e.getMessage());
e.printStackTrace();
}
}
/**
* Build a non-streaming request.
* @param client HTTP client
* @param httpPost HTTP request object
* @throws IOException Thrown when a network request or data read error occurs.
*/
private static void buildNonStreamingRequest(CloseableHttpClient client, HttpPost httpPost) throws IOException {
System.out.println("Non-streaming request:");
try (CloseableHttpResponse response = client.execute(httpPost)) {
HttpEntity entity = response.getEntity();
if (entity == null) {
System.err.println("The response body is empty.");
return;
}
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 && entity != null){
String responseBody = EntityUtils.toString(entity);
System.out.println(responseBody);
}else{
String errorBody = EntityUtils.toString(entity);
System.err.println("Request failed: " + errorBody);
}
}catch (IOException e) {
System.err.println("Request failed: " + e.getMessage());
e.printStackTrace();
}
}
}
Successful response example
JSON format
{
"output": {
"choices": [{
"finishReason": "stop",
"message": {
"role": "assistant",
"content": "The business scope includes: tag categorization results."
}
}]
},
"requestId": "e1b36ec0-225e-9b0a-9851-f5ea0f1fa5d4",
"usage": {
"inputTokens": 100,
"outputTokens": 100,
"totalTokens": 200
}
}
Error codes
For more information, see Error codes for Tongyi Data Mining.