import com.volcengine.ark.runtime.models.chat.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.volcengine.ark.runtime.service.ArkService;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
public class ChatCompletionsStructuredOutputsExamplev4 {
static String apiKey = System.getenv("ARK_API_KEY");
static ArkService service = ArkService.builder()
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
.baseUrl("https://ark.cn-beijing.volces.com/api/v3")
.dispatcher(new Dispatcher())
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
// 构造消息列表(包含 system 和 user 角色)
List<ChatCompletionRequestMessage> messages = new ArrayList<>();
messages.add(ChatCompletionRequestSystemMessage.builder()
.role(ChatCompletionRequestMessageType.SYSTEM)
.content(ChatCompletionMessageContent.ofString("你是一位数学辅导老师,需详细展示解题步骤"))
messages.add(ChatCompletionRequestUserMessage.builder()
.role(ChatCompletionRequestMessageType.USER)
.content(ChatCompletionMessageContent.ofString("用中文解方程组:8x + 9 = 32 和 x + y = 1"))
// Build the JSON Schema with Java collections.
java.util.Map<String, Object> stepProperties = new java.util.HashMap<>();
stepProperties.put("explanation", java.util.Collections.singletonMap("type", "string"));
stepProperties.put("output", java.util.Collections.singletonMap("type", "string"));
java.util.Map<String, Object> stepSchema = new java.util.HashMap<>();
stepSchema.put("type", "object");
stepSchema.put("properties", stepProperties);
java.util.Map<String, Object> properties = new java.util.HashMap<>();
java.util.Map<String, Object> stepsSchema = new java.util.HashMap<>();
stepsSchema.put("type", "array");
stepsSchema.put("items", stepSchema);
properties.put("steps", stepsSchema);
properties.put("finalAnswer", java.util.Collections.singletonMap("type", "string"));
java.util.Map<String, Object> schema = new java.util.HashMap<>();
schema.put("type", "object");
schema.put("properties", properties);
ChatCompletionResponseFormat responseFormat = ChatCompletionResponseFormat.builder()
.type(ResponseFormatType.JSON_SCHEMA)
.jsonSchema(ChatCompletionResponseFormatJsonSchema.builder()
.description("Structured response for math problem solutions")
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model("doubao-seed-2-1-pro-260915") // 替换为实际使用模型
.responseFormat(responseFormat)
.thinking(Thinking.builder().type(ThinkingMode.DISABLED).build()) // 关闭模型深度思考能力
ChatCompletionResponse response = service.createChatCompletion(request);
if (!response.getChoices().isEmpty()) {
String content = String.valueOf(response.getChoices().get(0).getMessage().getContent());
JsonNode jsonNode = mapper.readTree(content);
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode));
service.shutdownExecutor();