From 5ddede46d799e732344c1a2d49ee95721ee73510 Mon Sep 17 00:00:00 2001 From: LuanY77 <2307984361@qq.com> Date: Mon, 11 Aug 2025 23:52:20 +0800 Subject: [PATCH] =?UTF-8?q?Feat(engine):=20=E7=BB=93=E6=9E=84=E5=8C=96?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E8=A7=A3=E6=9E=90=E5=99=A8=E6=8F=90=E4=BE=9B?= =?UTF-8?q?json=E7=94=9F=E6=88=90=E6=8F=90=E7=A4=BA=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../output/structure/parser/OutputParser.java | 21 +++++++++++ .../ai/engine/structure/OutputParserTest.java | 37 ++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/liteflow-ai/liteflow-ai-engine/src/main/java/com/yomahub/liteflow/ai/engine/model/output/structure/parser/OutputParser.java b/liteflow-ai/liteflow-ai-engine/src/main/java/com/yomahub/liteflow/ai/engine/model/output/structure/parser/OutputParser.java index 5a87e981d..a29a220a8 100644 --- a/liteflow-ai/liteflow-ai-engine/src/main/java/com/yomahub/liteflow/ai/engine/model/output/structure/parser/OutputParser.java +++ b/liteflow-ai/liteflow-ai-engine/src/main/java/com/yomahub/liteflow/ai/engine/model/output/structure/parser/OutputParser.java @@ -81,6 +81,27 @@ public class OutputParser { } } + /** + * 获取输出格式的大模型提示词 + * + * @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 * diff --git a/liteflow-testcase-el/liteflow-testcase-el-ai/src/test/java/com/yomahub/liteflow/test/ai/engine/structure/OutputParserTest.java b/liteflow-testcase-el/liteflow-testcase-el-ai/src/test/java/com/yomahub/liteflow/test/ai/engine/structure/OutputParserTest.java index aac54e635..4fb81beb4 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-ai/src/test/java/com/yomahub/liteflow/test/ai/engine/structure/OutputParserTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-ai/src/test/java/com/yomahub/liteflow/test/ai/engine/structure/OutputParserTest.java @@ -36,7 +36,7 @@ public class OutputParserTest { public void testComplexConvert() { OutputParser> parser = new OutputParser<>("com.yomahub.liteflow.test.ai.engine.structure.param.OutputWithT"); 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 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() + ); + } }