| | |
| | | package cc.mrbird.febs.ai.service.impl; |
| | | |
| | | import cc.mrbird.febs.ai.entity.*; |
| | | import cc.mrbird.febs.ai.mapper.AiProductQuestionJobMapper; |
| | | import cc.mrbird.febs.ai.mapper.AiProductQuestionMapper; |
| | | import cc.mrbird.febs.ai.req.AiProductQuestionAiDto; |
| | | import cc.mrbird.febs.ai.service.AiProductQuestionItemService; |
| | | import cc.mrbird.febs.ai.service.AiProductQuestionService; |
| | | import cc.mrbird.febs.ai.service.AiService; |
| | | import cc.mrbird.febs.ai.util.UUID; |
| | | import cc.mrbird.febs.common.entity.FebsResponse; |
| | | import cc.mrbird.febs.common.entity.QueryRequest; |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cc.mrbird.febs.mall.entity.MallMember; |
| | | import cc.mrbird.febs.rabbit.producter.AgentProducer; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | 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; |
| | |
| | | public class AiProductQuestionServiceImpl extends ServiceImpl<AiProductQuestionMapper, AiProductQuestion> implements AiProductQuestionService { |
| | | |
| | | private final AiProductQuestionMapper aiProductQuestionMapper; |
| | | private final AiProductQuestionJobMapper aiProductQuestionJobMapper; |
| | | private final AiProductQuestionItemService aiProductQuestionItemService; |
| | | private final AiService aiService; |
| | | private final AgentProducer agentProducer; |
| | | |
| | | @Override |
| | | public AiProductQuestion getById(String id) { |
| | |
| | | public IPage<AiProductQuestion> listInPage(AiProductQuestion dto, QueryRequest request) { |
| | | Page<AiProductQuestion> page = new Page<>(request.getPageNum(), request.getPageSize()); |
| | | LambdaQueryWrapper<AiProductQuestion> query = Wrappers.lambdaQuery(AiProductQuestion.class); |
| | | if (StrUtil.isNotEmpty(dto.getCompanyId())){ |
| | | query.eq(AiProductQuestion::getCompanyId, dto.getCompanyId()); |
| | | } |
| | | if (StrUtil.isNotEmpty(dto.getProductCategoryId())){ |
| | | query.eq(AiProductQuestion::getProductCategoryId, dto.getProductCategoryId()); |
| | | } |
| | | query.ne(AiProductQuestion::getState, 2); |
| | | query.orderByDesc(AiProductQuestion::getCreatedTime); |
| | | Page<AiProductQuestion> pages = aiProductQuestionMapper.selectPage(page, query); |
| | | return pages; |
| | | } |
| | | |
| | | @Override |
| | | public IPage<AiProductQuestionJob> listJobInPage(AiProductQuestionJob dto, QueryRequest request) { |
| | | Page<AiProductQuestionJob> page = new Page<>(request.getPageNum(), request.getPageSize()); |
| | | LambdaQueryWrapper<AiProductQuestionJob> query = Wrappers.lambdaQuery(AiProductQuestionJob.class); |
| | | if (StrUtil.isNotEmpty(dto.getCompanyId())){ |
| | | query.eq(AiProductQuestionJob::getCompanyId, dto.getCompanyId()); |
| | | } |
| | | if (StrUtil.isNotEmpty(dto.getProductCategoryId())){ |
| | | query.eq(AiProductQuestionJob::getProductCategoryId, dto.getProductCategoryId()); |
| | | } |
| | | query.orderByDesc(AiProductQuestionJob::getCreatedTime); |
| | | Page<AiProductQuestionJob> pages = aiProductQuestionJobMapper.selectPage(page, query); |
| | | return pages; |
| | | } |
| | | |
| | |
| | | for (AiProductQuestionItem item : dto.getAiProductQuestionItems()){ |
| | | item.setId(UUID.getSimpleUUIDString()); |
| | | item.setProductQuestionId(entity.getId()); |
| | | item.setCompanyId(dto.getCompanyId()); |
| | | item.setTitle(entity.getTitle()); |
| | | item.setCreatedTime(new Date()); |
| | | aiProductQuestionItemService.getBaseMapper().insert(item); |
| | |
| | | if (ObjectUtil.isNotNull( entity)){ |
| | | this.update(null, |
| | | Wrappers.lambdaUpdate(AiProductQuestion.class) |
| | | .set(AiProductQuestion::getCompanyId, dto.getCompanyId()) |
| | | .set(AiProductQuestion::getProductCategoryId, dto.getProductCategoryId()) |
| | | .set(AiProductQuestion::getTitle, dto.getTitle()) |
| | | .set(AiProductQuestion::getDifficulty, dto.getDifficulty()) |
| | |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse stateUpdate(String ids, Integer type) { |
| | | if (StrUtil.isEmpty(ids)) { |
| | | throw new FebsException("参数错误"); |
| | | } |
| | | List<String> idList = StrUtil.split(ids, ','); |
| | | this.update(null, |
| | | Wrappers.lambdaUpdate(AiProductQuestion.class) |
| | | .set(AiProductQuestion::getState, type) |
| | | .set(AiProductQuestion::getUpdatedTime, new Date()) |
| | | .in(AiProductQuestion::getId, idList)); |
| | | return new FebsResponse().success().message("操作成功"); |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse delete(String id) { |
| | | AiProductQuestion entity = this.getById(id); |
| | | if(ObjectUtil.isNotNull( entity)){ |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProductQuestion> questionTree() { |
| | | public List<AiProductQuestion> questionTree(String companyId) { |
| | | LambdaQueryWrapper<AiProductQuestion> query = Wrappers.lambdaQuery(AiProductQuestion.class); |
| | | if (StrUtil.isNotEmpty(companyId)){ |
| | | query.eq(AiProductQuestion::getCompanyId, companyId); |
| | | } |
| | | |
| | | return aiProductQuestionMapper.selectList(null); |
| | | return aiProductQuestionMapper.selectList(query); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return aiProductQuestionMapper.selectList(aiProductQuestionLambdaQueryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse aiAdd(AiProductQuestionAiDto dto) { |
| | | |
| | | 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 productCategoryId = dto.getProductCategoryId(); |
| | | Integer difficulty = dto.getDifficulty(); |
| | | Date createdTime = new Date(); |
| | | // 处理解析后的问题列表 |
| | | for (JSONObject questionObj : questionList) { |
| | | String title = questionObj.getStr("title"); |
| | | |
| | | AiProductQuestion aiProductQuestion = new AiProductQuestion(); |
| | | aiProductQuestion.setId(UUID.getSimpleUUIDString()); |
| | | aiProductQuestion.setCompanyId(dto.getCompanyId()); |
| | | aiProductQuestion.setProductCategoryId(productCategoryId); |
| | | aiProductQuestion.setTitle(title); |
| | | aiProductQuestion.setDifficulty(difficulty); |
| | | aiProductQuestion.setCreatedTime(createdTime); |
| | | this.save(aiProductQuestion); |
| | | |
| | | JSONArray answerList = questionObj.getJSONArray("answer_list"); |
| | | System.out.println("问题: " + title); |
| | | for (int i = 0; i < answerList.size(); i++) { |
| | | JSONObject answer = answerList.getJSONObject(i); |
| | | System.out.println("答案" + (i+1) + ": " + answer.getStr("answer")); |
| | | System.out.println("类型: " + answer.getStr("type")); |
| | | System.out.println("分析: " + answer.getStr("analysis")); |
| | | AiProductQuestionItem aiProductQuestionItem = new AiProductQuestionItem(); |
| | | aiProductQuestionItem.setId(UUID.getSimpleUUIDString()); |
| | | aiProductQuestionItem.setCompanyId(dto.getCompanyId()); |
| | | aiProductQuestionItem.setProductQuestionId(aiProductQuestion.getId()); |
| | | aiProductQuestionItem.setTitle(aiProductQuestion.getTitle()); |
| | | aiProductQuestionItem.setAnswer(answer.getStr("answer")); |
| | | aiProductQuestionItem.setCorrectAnswer(answer.getInt("type") == 2 ? 1 : 0); |
| | | aiProductQuestionItem.setAnswerAnalysis(answer.getStr("analysis")); |
| | | aiProductQuestionItem.setCreatedTime(createdTime); |
| | | aiProductQuestionItemService.getBaseMapper().insert(aiProductQuestionItem); |
| | | } |
| | | } |
| | | return new FebsResponse().success().message("操作成功"); |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse aiAddV2(AiProductQuestionAiDto dto) { |
| | | String companyId = dto.getCompanyId(); |
| | | String productCategoryId = dto.getProductCategoryId(); |
| | | Integer questionCnt = dto.getQuestionCnt(); |
| | | String query = dto.getQuery(); |
| | | Integer difficulty = dto.getDifficulty(); |
| | | String promptAiSystem = dto.getPromptAiSystem(); |
| | | Date createdTime = new Date(); |
| | | AiProductQuestionJob aiProductQuestionJob = new AiProductQuestionJob(); |
| | | aiProductQuestionJob.setId(UUID.getSimpleUUIDString()); |
| | | aiProductQuestionJob.setCompanyId(companyId); |
| | | aiProductQuestionJob.setProductCategoryId(productCategoryId); |
| | | aiProductQuestionJob.setTitle(query); |
| | | aiProductQuestionJob.setPromptAiSystem(promptAiSystem); |
| | | aiProductQuestionJob.setQuestionCnt(questionCnt); |
| | | aiProductQuestionJob.setDifficulty(difficulty); |
| | | aiProductQuestionJob.setCreatedTime(createdTime); |
| | | aiProductQuestionJobMapper.insert(aiProductQuestionJob); |
| | | |
| | | agentProducer.sendAddQuestionJob(aiProductQuestionJob.getId()); |
| | | return new FebsResponse().success().message("操作成功"); |
| | | } |
| | | |
| | | @Override |
| | | public void getAddQuestion(String id) { |
| | | try { |
| | | AiProductQuestionJob aiProductQuestionJob = aiProductQuestionJobMapper.selectById(id); |
| | | if (ObjectUtil.isEmpty(aiProductQuestionJob)){ |
| | | return; |
| | | } |
| | | Integer state = aiProductQuestionJob.getState(); |
| | | if (2 == state){ |
| | | return; |
| | | } |
| | | Integer questionCnt = aiProductQuestionJob.getQuestionCnt(); |
| | | Integer questionDoneCnt = aiProductQuestionJob.getQuestionDoneCnt(); |
| | | if (questionCnt <= questionDoneCnt){ |
| | | aiProductQuestionJob.setState(2); |
| | | aiProductQuestionJobMapper.update(null, |
| | | Wrappers.lambdaUpdate(AiProductQuestionJob.class) |
| | | .set(AiProductQuestionJob::getState, 2) |
| | | .set(AiProductQuestionJob::getUpdatedTime, new Date()) |
| | | .eq(AiProductQuestionJob::getId, id)); |
| | | return; |
| | | } |
| | | int i = questionCnt - questionDoneCnt; |
| | | if (i >= 5){ |
| | | i = 5; |
| | | } |
| | | AiProductQuestionAiDto dto = new AiProductQuestionAiDto(); |
| | | String jsonFormat = "{\"question_list\":[{\"title\": \"消费者对透明质酸的主要担忧是什么?\",\"answer_list\":[{\"answer\": \"消费者担心透明质酸维持时间短,效果不明显。\",\"type\": 1,\"analysis\": \"\"},{\"answer\": \"消费者担心透明质酸注射后可能出现移位、凹陷、馒化、僵硬以及炎症反应。\",\"type\": 1,\"analysis\": \"\"},{\"answer\": \"消费者的主要担忧包括效果不明显、维持时间短、注射后出现移位、凹陷、馒化、僵硬以及炎症反应。他们希望既达到理想效果,又避免这些副作用。\",\"type\": 2,\"analysis\": \"标准答案涵盖了消费者对透明质酸的所有主要担忧,既包括效果问题,也涵盖副作用风险,全面反映消费者心理需求。\"}]}]} "; |
| | | dto.setJsonFormat(jsonFormat); |
| | | dto.setCompanyId(aiProductQuestionJob.getCompanyId()); |
| | | dto.setProductCategoryId(aiProductQuestionJob.getProductCategoryId()); |
| | | dto.setQuery(aiProductQuestionJob.getTitle()); |
| | | dto.setPromptAiSystem(aiProductQuestionJob.getPromptAiSystem()); |
| | | dto.setQuestionCnt(i); |
| | | dto.setDifficulty(aiProductQuestionJob.getDifficulty()); |
| | | |
| | | String questionAndAnswerStr = aiService.llmInvokeNonStreaming(dto); |
| | | log.info("问题答案: " + questionAndAnswerStr); |
| | | // 解析AI返回的结果 |
| | | List<JSONObject> questionList = parseAiQuestionResponse(questionAndAnswerStr); |
| | | if (CollUtil.isNotEmpty(questionList)){ |
| | | String productCategoryId = dto.getProductCategoryId(); |
| | | Integer difficulty = dto.getDifficulty(); |
| | | Date createdTime = new Date(); |
| | | // 处理解析后的问题列表 |
| | | for (JSONObject questionObj : questionList) { |
| | | String title = questionObj.getStr("title"); |
| | | |
| | | AiProductQuestion aiProductQuestion = new AiProductQuestion(); |
| | | aiProductQuestion.setId(UUID.getSimpleUUIDString()); |
| | | aiProductQuestion.setCompanyId(dto.getCompanyId()); |
| | | aiProductQuestion.setProductCategoryId(productCategoryId); |
| | | aiProductQuestion.setTitle(title); |
| | | aiProductQuestion.setDifficulty(difficulty); |
| | | aiProductQuestion.setCreatedTime(createdTime); |
| | | this.save(aiProductQuestion); |
| | | JSONArray answerList = questionObj.getJSONArray("answer_list"); |
| | | 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")); |
| | | AiProductQuestionItem aiProductQuestionItem = new AiProductQuestionItem(); |
| | | aiProductQuestionItem.setId(UUID.getSimpleUUIDString()); |
| | | aiProductQuestionItem.setCompanyId(dto.getCompanyId()); |
| | | aiProductQuestionItem.setProductQuestionId(aiProductQuestion.getId()); |
| | | aiProductQuestionItem.setTitle(aiProductQuestion.getTitle()); |
| | | aiProductQuestionItem.setAnswer(answer.getStr("answer")); |
| | | aiProductQuestionItem.setCorrectAnswer(answer.getInt("type") == 2 ? 1 : 0); |
| | | aiProductQuestionItem.setAnswerAnalysis(answer.getStr("analysis")); |
| | | aiProductQuestionItem.setCreatedTime(createdTime); |
| | | aiProductQuestionItemService.getBaseMapper().insert(aiProductQuestionItem); |
| | | } |
| | | } |
| | | aiProductQuestionJobMapper.update(null, |
| | | Wrappers.lambdaUpdate(AiProductQuestionJob.class) |
| | | .set(AiProductQuestionJob::getQuestionDoneCnt, questionDoneCnt + questionList.size()) |
| | | .set(AiProductQuestionJob::getUpdatedTime, new Date()) |
| | | .set(AiProductQuestionJob::getState, 1) |
| | | .eq(AiProductQuestionJob::getId, id)); |
| | | } |
| | | AiProductQuestionJob aiProductQuestionJobDone = aiProductQuestionJobMapper.selectById(id); |
| | | if (2 != aiProductQuestionJobDone.getState()){ |
| | | agentProducer.sendAddQuestionJob(id); |
| | | } |
| | | } catch (Exception e) { |
| | | agentProducer.sendAddQuestionJob(id); |
| | | log.error("知识库异常", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解析AI返回的包含问题和答案的JSON字符串 |
| | | * @param aiResponse AI返回的原始响应字符串 |
| | | * @return 解析后的问题列表 |
| | | */ |
| | | public static List<JSONObject> parseAiQuestionResponse(String aiResponse) { |
| | | |
| | | try { |
| | | // 解析外层JSON |
| | | JSONObject outerJson = JSONUtil.parseObj(aiResponse); |
| | | |
| | | // 提取output字段 |
| | | String output = outerJson.getStr("output"); |
| | | log.info("原始输出内容: {}", output); |
| | | |
| | | // 去除<start>和</start>标签,并清理多余字符 |
| | | String jsonContent = output.replace("<start>", "") |
| | | .replace("</start>", "") |
| | | .replace("\\n", "") |
| | | .replace("\\\"", "\"") |
| | | .trim(); |
| | | |
| | | log.info("清理后JSON内容: {}", jsonContent); |
| | | // 解析内部JSON |
| | | JSONObject innerJson = JSONUtil.parseObj(jsonContent); |
| | | |
| | | // 提取question_list |
| | | JSONArray questionList = innerJson.getJSONArray("question_list"); |
| | | |
| | | // 转换为List<JSONObject>返回 |
| | | return questionList.toList(JSONObject.class); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("解析AI问题响应失败: ", e); |
| | | throw new RuntimeException("解析AI响应失败", e); |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String s = "{\"output\":\"<start>{\\\"question_list\\\":[{\\\"title\\\":\\\"根据知识库内容,调整T区的三大作用分别是什么?请逐条准确列举。\\\",\\\"answer_list\\\":[{\\\"answer\\\":\\\"1.提升折叠度;2.调整面部比例;3.提升骨性支撑力\\\",\\\"type\\\":2,\\\"analysis\\\":\\\"原文明确列出:'1.提升折叠度(增加正面、侧面转折落差,减少扁平感);2.调整面部比例(通过T区的纵向与横线的调整,优化三庭五眼、四高三底);3.提升骨性支撑力(为软组织提供坚实的\\\\\\\"地基\\\\\\\"抗衰并提升高级的骨感)'\\\"},{\\\"answer\\\":\\\"1.提升折叠度;2.调整三庭五眼;3.增强软组织弹性\\\",\\\"type\\\":1,\\\"analysis\\\":\\\"\\\"},{\\\"answer\\\":\\\"1.提升立体感;2.优化面部比例;3.强化韧带支撑\\\",\\\"type\\\":1,\\\"analysis\\\":\\\"\\\"}]}]}</start>\"}"; |
| | | List<JSONObject> questionList = parseAiQuestionResponse(s); |
| | | System.out.println(questionList); |
| | | |
| | | } |
| | | |
| | | } |