Use the AI Search Open Platform Java SDK to call the content generation model service and generate text responses.
Prerequisites
Before you begin, ensure that you have:
An activated AI Search Open Platform service. See Activate the service
An API key for authentication. See Obtain an API key. Your API key is in the format
OS-****and is passed as a Bearer token when initializing the client
Usage notes
The request body cannot exceed 8 MB.
For a complete list of parameters, see Content generation service.
Send a single-turn request
The following example shows the minimum code required to send one message and print the response.
package com.aliyun.sample;
import com.aliyun.searchplat20240529.Client;
import com.aliyun.searchplat20240529.models.GetTextGenerationRequest;
import com.aliyun.searchplat20240529.models.GetTextGenerationResponse;
import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
public class TextGenerationDemo {
public static void main(String[] args) throws Exception {
Config config = new Config();
// Pass your API key as a Bearer token
config.setBearerToken("your-api-key: OS-****");
// Endpoint: the unified request endpoint, without the http:// prefix
config.setEndpoint("***.opensearch.aliyuncs.com");
config.setProtocol("http");
Client client = new Client(config);
final ObjectMapper objectMapper = new ObjectMapper();
List<GetTextGenerationRequest.GetTextGenerationRequestMessages> messages =
objectMapper.readValue("[]", List.class);
messages.add(
new GetTextGenerationRequest.GetTextGenerationRequestMessages()
.setRole("user")
.setContent("What are some fun things to do in Shanghai?")
);
GetTextGenerationRequest request = new GetTextGenerationRequest();
request.setMessages(messages);
GetTextGenerationResponse response = client.getTextGeneration("default", "ops-qwen-turbo", request);
System.out.println(response.getBody().getResult().getText());
}
}Replace the following placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
your-api-key: OS-**** | Your API key | OS-AbCdEf123456 |
***.opensearch.aliyuncs.com | Your service endpoint | my-app.opensearch.aliyuncs.com |
Send a multi-turn conversation
Each call to the model is stateless — it has no memory of previous exchanges. To build a conversation, pass the full message history with every request. Use the user role for the human side and the assistant role to include the model's previous responses.
package com.aliyun.sample;
import com.aliyun.searchplat20240529.Client;
import com.aliyun.searchplat20240529.models.GetTextGenerationRequest;
import com.aliyun.searchplat20240529.models.GetTextGenerationResponse;
import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
public class TextGenerationDemo {
public static void main(String[] args) throws Exception {
Config config = new Config();
config.setBearerToken("your-api-key: OS-****");
config.setEndpoint("***.opensearch.aliyuncs.com");
config.setProtocol("http");
Client client = new Client(config);
// Build the conversation history. Each entry has a "role" (user or assistant)
// and "content" (the message text).
String history = "[{\"role\":\"user\",\"content\":\"hello\"},{\"role\":\"assistant\",\"content\":\"hello\"}]";
final ObjectMapper objectMapper = new ObjectMapper();
List<GetTextGenerationRequest.GetTextGenerationRequestMessages> messages =
objectMapper.readValue(history, List.class);
// Append the next user message
messages.add(
new GetTextGenerationRequest.GetTextGenerationRequestMessages()
.setRole("user")
.setContent("What are some fun things to do in Shanghai?")
);
GetTextGenerationRequest request = new GetTextGenerationRequest();
request.setMessages(messages);
GetTextGenerationResponse response = client.getTextGeneration("default", "ops-qwen-turbo", request);
System.out.println(response.getBody().getResult().getText());
}
}What's next
Content generation service — Full parameter reference for the text generation API