The AI Search Open Platform lets you call the content generation model service using the Java SDK.
Prerequisites
The AI Search Open Platform service is activated. For more information, see Activate the service.
You have authenticated your identity using an API key. For more information, see Obtain an API key.
Parameters
The request body cannot exceed 8 MB.
For more information about the parameters, see Content generation service.
package com.aliyun.sample;
import com.aliyun.searchplat20240529.Client;
import com.aliyun.searchplat20240529.models.GetDocumentRankRequest;
import com.aliyun.searchplat20240529.models.GetDocumentRankResponse;
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.Arrays;
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-****");
// endpoint: The unified request endpoint. Remove the http:// prefix.
config.setEndpoint("***.opensearch.aliyuncs.com");
config.setProtocol("http");
Client client = new Client(config);
String history = "[{\"role\":\"user\",\"content\":\"hello\"},{\"role\":\"assistant\",\"content\":\"hello\"}]";
final ObjectMapper objectMapper = new ObjectMapper();
List<GetTextGenerationRequest.GetTextGenerationRequestMessages> messages = objectMapper.readValue(history, 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());
}
}