Я использую Spring.ai и использую структурированные выходы, и в настоящее время он выводит для меня массив объектов в строковой форме, но я получаю приведенную ниже ошибку, и я не слишком уверен, почему. Я пытался преобразовать строку в объект, который ее соответствует, но она не работает.
Сообщение об ошибке
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.ArrayList` from String value (token `JsonToken.VALUE_STRING`)]
< /code>
Какой набор тестового набора печатает json.{"tests":[{"code":"int arr[] = {0, 5, 10, 15};\nint result = CountBetween(arr, 4, 3, 12);\nprintf(\"%d\", result);","expectedStdOut":"2"},{"code":"int arr[] = {0, 5, 10, 15};\nint result = CountBetween(arr, 4, 12, 3);\nprintf(\"%d\", result);","expectedStdOut":"2"},{"code":"int arr[] = {3, 12, 6, 9};\nint result = CountBetween(arr, 4, 3, 12);\nprintf(\"%d\", result);","expectedStdOut":"2"},{"code":"int arr[] = {4, 4, 4, 4};\nint result = CountBetween(arr, 4, 1, 5);\nprintf(\"%d\", result);","expectedStdOut":"4"},{"code":"int arr[] = {1, 2, 3};\nint result = CountBetween(arr, 0, 0, 10);\nprintf(\"%d\", result);","expectedStdOut":"0"},{"code":"int arr[] = {1, 2, 3};\nint result = CountBetween(arr, -5, 0, 10);\nprintf(\"%d\", result);","expectedStdOut":"0"},{"code":"int arr[] = {1, 2, 3};\nint result = CountBetween(arr, 3, 5, 5);\nprintf(\"%d\", result);","expectedStdOut":"0"},{"code":"int arr[] = {INT_MIN, INT_MIN + 1, 0, INT_MAX - 1, INT_MAX};\nint result = CountBetween(arr, 5, INT_MIN, INT_MAX);\nprintf(\"%d\", result);","expectedStdOut":"3"},{"code":"int result = CountBetween(NULL, 0, 0, 10);\nprintf(\"%d\", result);","expectedStdOut":"0"},{"code":"int result = CountBetween(NULL, 5, 0, 10);\nprintf(\"%d\", result);","expectedStdOut":"0"}]}
классы
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Test {
String code;
String expectedStdOut;
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestResponse {
List tests;
}
@Data
@Document("problems")
public class Problem {
@Id private String id;
private String problemStatement;
private String modelAnswer;
private String constraints;
private List testSuite;
private ProgramLanguage programLanguage;
private ProblemType problemType;
private String defaultProbe;
}
контроллер
@PostMapping("test-suite")
public ResponseEntity
generateProblemTestSuite(@RequestBody Problem problem) throws JsonMappingException, JsonProcessingException {
String modelAnswer = problem.getModelAnswer();
String constraints = problem.getConstraints();
String basePrompt = Prompts.getTestSuiteGenerationPrompt();
String sysPrompt = basePrompt.replace("//VAR_MODEL_SOLUTION", modelAnswer)
.replace("//VAR_CONSTRAINTS", constraints);
String testSuite = aiService.executeOneTimeLLMCall(sysPrompt, JsonSchemaDefinition.getTestCaseSchema());
System.out.println(testSuite);
ObjectMapper objectMapper = new ObjectMapper();
TestResponse testResponse = objectMapper.readValue(testSuite, TestResponse.class);
problem.setTestSuite(testResponse.getTests());
return ResponseEntity.ok(problem);
}
json схема
private static final String TEST_CASE_SCHEMA = """
{
"type": "object",
"properties": {
"tests": {
"type": "array",
"items": {
"type": "object",
"properties": {
"code": { "type": "string" },
"expectedStdOut": { "type": "string" }
},
"required": ["code", "expectedStdOut"],
"additionalProperties": false
}
}
},
"required": ["tests"],
"additionalProperties": false
}
""";
public static String getTestCaseSchema() {
return TEST_CASE_SCHEMA;
}
вспомогательная функция с OpenAI
public String executeOneTimeLLMCall(String systemPrompt, @Nullable String responseSchema) {
var responseFormat = getResponseType(responseSchema);
List history = new ArrayList();
history.add(new SystemMessage(systemPrompt));
OpenAiChatOptions options = OpenAiChatOptions.builder()
.model(OpenAiApi.ChatModel.O3)
.temperature(1D)
.responseFormat(responseFormat)
.build();
String assistantReply = chatClient.prompt().options(options).messages(history).call().content();
return assistantReply;
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... of-objects
Ошибка при опустошении массива в качестве струнного JSON в массив объектов с Springai ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение