Feat(engine): 结构化输出解析器提供json生成提示词

This commit is contained in:
LuanY77
2025-08-11 23:52:20 +08:00
parent c3bbf41b72
commit 5ddede46d7
2 changed files with 57 additions and 1 deletions

View File

@@ -81,6 +81,27 @@ public class OutputParser<T> {
}
}
/**
* 获取输出格式的大模型提示词
*
* @return 提示词
*/
public String getOutputInstruction() {
String template =
"Your response should be in JSON format.\n" +
"Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.\n" +
"Do not include markdown code blocks in your response.\n" +
"Remove the ```json markdown from the output.\n" +
"Here is the JSON Schema instance your output must adhere to:\n" +
"```\n%s\n```";
try {
return String.format(template, objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this.jsonSchema));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
* JsonSchema
*

View File

@@ -36,7 +36,7 @@ public class OutputParserTest {
public void testComplexConvert() {
OutputParser<OutputWithT<Output>> parser = new OutputParser<>("com.yomahub.liteflow.test.ai.engine.structure.param.OutputWithT<com.yomahub.liteflow.test.ai.engine.structure.param.Output>");
String jsonInput =
"{\n" +
"{\n" +
" \"data\": {\n" +
" \"data\": \"This is a sample string.\",\n" +
" \"lst\": [10, 20, 30]\n" +
@@ -136,4 +136,39 @@ public class OutputParserTest {
Assertions.assertTrue(required.isArray());
Assertions.assertEquals(2, required.size());
}
@Test
public void testOutputInstruction() {
OutputParser<Output> parser = new OutputParser<>(Output.class);
Assertions.assertEquals(
"Your response should be in JSON format.\n" +
"Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.\n" +
"Do not include markdown code blocks in your response.\n" +
"Remove the ```json markdown from the output.\n" +
"Here is the JSON Schema instance your output must adhere to:\n" +
"```\n" +
"{\n" +
" \"type\" : \"object\",\n" +
" \"properties\" : {\n" +
" \"data\" : {\n" +
" \"type\" : \"string\",\n" +
" \"description\" : \"a data description\"\n" +
" },\n" +
" \"lst\" : {\n" +
" \"description\" : \"a list description\",\n" +
" \"type\" : \"array\",\n" +
" \"items\" : {\n" +
" \"type\" : \"integer\",\n" +
" \"description\" : \"a list description\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"required\" : [ \"data\", \"lst\" ],\n" +
" \"additionalProperties\" : false\n" +
"}\n" +
"```",
parser.getOutputInstruction()
);
}
}