| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.fasterxml.jackson.databind.JsonNode; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.databind.SerializationFeature; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | String jsonFormat = "{\"question_list\":[{\"title\": \"消费者对透明质酸的主要担忧是什么?\",\"answer_list\":[{\"answer\": \"消费者担心透明质酸维持时间短,效果不明显。\",\"type\": 1,\"analysis\": \"\"},{\"answer\": \"消费者担心透明质酸注射后可能出现移位、凹陷、馒化、僵硬以及炎症反应。\",\"type\": 1,\"analysis\": \"\"},{\"answer\": \"消费者的主要担忧包括效果不明显、维持时间短、注射后出现移位、凹陷、馒化、僵硬以及炎症反应。他们希望既达到理想效果,又避免这些副作用。\",\"type\": 2,\"analysis\": \"标准答案涵盖了消费者对透明质酸的所有主要担忧,既包括效果问题,也涵盖副作用风险,全面反映消费者心理需求。\"}]}]} "; |
| | | dto.setJsonFormat(jsonFormat); |
| | | String questionAndAnswerStr = aiService.llmInvokeNonStreaming(dto); |
| | | System.out.println(questionAndAnswerStr); |
| | | // 解析AI返回的结果 |
| | | List<JSONObject> questionList = parseAiQuestionResponse(questionAndAnswerStr); |
| | | |
| | |
| | | String jsonContent = output.replace("<start>", "") |
| | | .replace("</start>", "") |
| | | .replace("\\n", "") |
| | | .replace("\\n", "") |
| | | .replace("\\\"", "\"") |
| | | .trim(); |
| | | |
| | |
| | | log.error("解析AI问题响应失败: ", e); |
| | | throw new RuntimeException("解析AI响应失败", e); |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String questionStr = "{\"output\":\"<start>\\n{\\n \\\"question_list\\\": [\\n {\\n \\\"title\\\": \\\"1. 如何通过菲欧曼品牌背景和价值塑造透明质酸产品的高端形象?\\\",\\n \\\"answer_list\\\": [\\n {\\n \\\"answer\\\": \\\"强调菲欧曼拥有45年历史,是法国第一家医学美容实验室,产品行销全球60多个国家,树立其国际权威形象。\\\",\\n \\\"type\\\": \\\"2\\\",\\n \\\"analysis\\\": \\\"该答案全面结合品牌历史、科研投入及市场影响力,系统塑造高端定位,符合标准答案要求。\\\"\\n },\\n {\\n \\\"answer\\\": \\\"通过讲述菲欧曼产品获得欧盟CE、中国NMPA等国际认证,强调其安全性和合规性。\\\",\\n \\\"type\\\": \\\"1\\\",\\n \\\"analysis\\\": \\\"\\\"\\n },\\n {\\n \\\"answer\\\": \\\"通过明星使用、医生推荐等口碑效应,提升菲欧曼在消费者心中的高端认知。\\\",\\n \\\"type\\\": \\\"1\\\",\\n \\\"analysis\\\": \\\"\\\"\\n }\\n ]\\n }\\n ]\\n}\\n</start>\"}"; |
| | | // 第一步:解析外层JSON |
| | | JSONObject outerJson = JSONUtil.parseObj(questionStr); |
| | | |
| | | // 第二步:提取output字段 |
| | | String output = outerJson.getStr("output"); |
| | | |
| | | // 第三步:去除<start>和</start>标签 |
| | | String jsonContent = output.replace("<start>", "").replace("</start>", "").trim(); |
| | | |
| | | // 第四步:解析内部的JSON |
| | | JSONObject innerJson = JSONUtil.parseObj(jsonContent); |
| | | |
| | | // 第五步:提取question_list |
| | | JSONArray questionList = innerJson.getJSONArray("question_list"); |
| | | |
| | | // 遍历问题列表 |
| | | for (int i = 0; i < questionList.size(); i++) { |
| | | JSONObject question = questionList.getJSONObject(i); |
| | | |
| | | System.out.println("问题标题: " + question.getStr("title")); |
| | | |
| | | JSONArray answerList = question.getJSONArray("answer_list"); |
| | | System.out.println("答案列表:"); |
| | | |
| | | // 遍历答案列表 |
| | | for (int j = 0; j < answerList.size(); j++) { |
| | | JSONObject answer = answerList.getJSONObject(j); |
| | | System.out.println(" 答案 " + (j+1) + ": " + answer.getStr("answer")); |
| | | System.out.println(" 类型: " + answer.getStr("type")); |
| | | System.out.println(" 分析: " + answer.getStr("analysis")); |
| | | System.out.println(); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |