From 5a4355579f663a0fb8b0c7ca4b3676594b55c7c7 Mon Sep 17 00:00:00 2001 From: huangh <hshgjzd5@163.com> Date: Thu, 26 Sep 2024 10:32:46 +0800 Subject: [PATCH] AI对话接口+简介接口+评价接口 --- dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/AiController.java | 258 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 245 insertions(+), 13 deletions(-) diff --git a/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/AiController.java b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/AiController.java index fd515eb..82f494b 100644 --- a/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/AiController.java +++ b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/AiController.java @@ -1,25 +1,28 @@ package cn.huge.module.ai.controller; -import cn.huge.base.common.utils.GuavaCacheUtils; -import cn.huge.base.common.utils.HttpClientUtils; -import cn.huge.base.common.utils.ObjectUtils; -import cn.huge.base.common.utils.ReturnSucUtils; +import cn.huge.base.common.utils.*; import cn.huge.module.ai.controller.domain.dto.CaseInfoDetailDto; import cn.huge.module.ai.controller.domain.dto.CaseInfoDto; import cn.huge.module.ai.controller.domain.dto.CaseRiskDto; import cn.huge.module.ai.controller.domain.dto.LawInfoDto; +import cn.huge.module.ai.controller.domain.po.AiConversation; +import cn.huge.module.ai.controller.domain.po.AiMessage; +import cn.huge.module.ai.controller.domain.po.CaseSimilarityExplanatory; import cn.huge.module.ai.controller.domain.vo.AiRequestVo; +import cn.huge.module.ai.controller.service.AiChatService; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.cloud.openfeign.support.FeignHttpClientProperties; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import java.util.*; /** * AI请求调用/get-case获取案件列表(一般包含3个纠纷类和3个判决类),/get-law获取法条推荐列表;案件详情在根据案件列表的id及type调用/get-case-detail获取案件详情 @@ -29,8 +32,236 @@ @RequestMapping("/api/ai/case-law") public class AiController { + @Autowired(required = false) + private HttpServletRequest request; + @Value("${ai.url}") private String aiUrl; + + @Autowired + private AiChatService aiChatService; + + + private Map<String, Object> getParameter(){ + Map<String, Object> terms = Maps.newHashMap(); + // 案件id + String caseId = request.getParameter("caseId"); + if (StringUtils.isNotBlank(caseId)){ + terms.put("caseId", caseId); + } + // 用户id + String userId = request.getParameter("userId"); + if (StringUtils.isNotBlank(userId)){ + terms.put("userId", userId); + } + // 会话标题 + String conversationTitle = request.getParameter("conversationTitle"); + if (StringUtils.isNotBlank(conversationTitle)){ + terms.put("conversationTitle", conversationTitle); + } + // 会话id + String aiConversationId = request.getParameter("aiConversationId"); + if (StringUtils.isNotBlank(aiConversationId)){ + terms.put("aiConversationId", aiConversationId); + } + // 案件描述 + String caseDes = request.getParameter("caseDes"); + if (StringUtils.isNotBlank(caseDes)){ + terms.put("caseDes", caseDes); + } + // 案件诉求 + String caseClaim = request.getParameter("caseClaim"); + if (StringUtils.isNotBlank(caseClaim)){ + terms.put("caseClaim", caseClaim); + } + // 用户消息(提问) + String userMessage = request.getParameter("userMessage"); + if (StringUtils.isNotBlank(userMessage)){ + terms.put("userMessage", userMessage); + } + // 相似案件id + String similarityCaseId = request.getParameter("similarityCaseId"); + if (StringUtils.isNotBlank(similarityCaseId)){ + terms.put("similarityCaseId", similarityCaseId); + } + // 判决案件正文 + String caseContent = request.getParameter("caseContent"); + if (StringUtils.isNotBlank(caseContent)){ + terms.put("caseContent", caseContent); + } + // 评价 + String likeStatus = request.getParameter("likeStatus"); + if (StringUtils.isNotBlank(likeStatus)){ + terms.put("likeStatus", likeStatus); + } + return terms; + } + + /** + * 创建会话 + * @url {ctx}/api/ai/case-law/createAiChat + * @param + * @return Object + */ + @PostMapping("/createAiChat") + public Object createAiChat() { + Map<String, Object> terms = getParameter(); + + try { + AiConversation aiConversation = aiChatService.createAiChat(terms); + return ReturnSucUtils.getRepInfo("请求成功", aiConversation); + } + catch (Exception e) { + log.error("创建AI会话失败:{}", e.getMessage()); + return ReturnFailUtils.getRepInfo(); + } + } + + /** + * 获取智能会话列表 + * @url {ctx}/api/ai/case-law/queryAiChatList + * @param + * @return Object + */ + @GetMapping("/queryAiChatList") + public Object queryAiChatList() { + Map<String, Object> terms = getParameter(); + String caseId = terms.get("caseId").toString(); + String userId = terms.get("userId").toString(); + + // 先从缓存中获取,如果有直接返回 + Object cacheAi = GuavaCacheUtils.getCacheAi("queryAiChatList"+caseId+userId); + if(ObjectUtils.isNotEmpty(cacheAi)){ + return ReturnSucUtils.getRepInfo("请求成功", cacheAi); + } + + try { + List<AiConversation> aiConversations = aiChatService.queryAiChatList(caseId, userId); + // 放入缓存 + GuavaCacheUtils.putCacheAi("queryAiChatList"+caseId+userId, aiConversations); + return ReturnSucUtils.getRepInfo("请求成功", aiConversations); + } + catch (Exception e) { + log.error("查询AI会话列表失败:{}", e.getMessage()); + return ReturnFailUtils.getRepInfo(); + } + } + + /** + * 创建对话 + * @url {ctx}/api/ai/case-law/createAiChatMessage + * @param + * @return Object + */ + @PostMapping(value = "/createAiChatMessage", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) + public StreamingResponseBody createAiChatMessage() { + Map<String, Object> terms = getParameter(); + + try { + return aiChatService.createAiChatMessage(terms); + } + catch (Exception e) { + log.error("创建AI会话消息失败:{}", e.getMessage()); + return null; + } + } + + /** + * 获取智能会话列表 + * @url {ctx}/api/ai/case-law/getAiChatMessageList + * @return + */ + @GetMapping("/getAiChatMessageList") + public Object getAiChatMessageList() { + Map<String, Object> terms = getParameter(); + String aiConversationId = terms.get("aiConversationId").toString(); + + // 先从缓存中获取,如果有直接返回 + Object cacheAi = GuavaCacheUtils.getCacheAi("getAiChatMessageList"+aiConversationId); + if(ObjectUtils.isNotEmpty(cacheAi)){ + return ReturnSucUtils.getRepInfo("请求成功", cacheAi); + } + + try { + List<AiMessage> aiConversationMessages = aiChatService.getAiChatMessageList(aiConversationId); + // 放入缓存 + GuavaCacheUtils.putCacheAi("getAiChatMessageList"+aiConversationId, aiConversationMessages); + return ReturnSucUtils.getRepInfo("请求成功", aiConversationMessages); + } + catch (Exception e) { + log.error("查询AI会话消息列表失败:{}", e.getMessage()); + return ReturnFailUtils.getRepInfo(); + } + } + + /** + * 删除会话 + * @url {ctx}/api/ai/case-law/deleteConversation + * @return + */ + @PostMapping("/deleteConversation") + public Object deleteConversation() { + Map<String, Object> terms = getParameter(); + + try { + return ReturnSucUtils.getRepInfo("请求成功",aiChatService.deleteConversation(terms)); + } + catch (Exception e) { + log.error("删除AI会话失败:{}", e.getMessage()); + return ReturnFailUtils.getRepInfo(); + } + } + + /** + * 获取案件总结 + * @url {ctx}/api/ai/case-law/getJudgmentSummarize + * @return + */ + @PostMapping("/getJudgmentSummarize") + public Object getJudgmentSummarize() { + Map<String, Object> terms = getParameter(); + + String similarityCaseId = terms.get("similarityCaseId").toString(); + String caseContent = terms.get("caseContent").toString(); + String caseId = terms.get("caseId").toString(); + + // 先从缓存中获取,如果有直接返回 + Object cacheAi = GuavaCacheUtils.getCacheAi("getJudgmentSummarize"+caseId+similarityCaseId); + if(ObjectUtils.isNotEmpty(cacheAi)){ + return ReturnSucUtils.getRepInfo("请求成功", cacheAi); + } + try { + CaseSimilarityExplanatory caseSimilarityExplanatory = aiChatService.getJudgmentSummarize(similarityCaseId, caseContent, caseId); + + // 放入缓存 + GuavaCacheUtils.putCacheAi("getJudgmentSummarize"+caseId+similarityCaseId, caseSimilarityExplanatory); + return ReturnSucUtils.getRepInfo("请求成功", caseSimilarityExplanatory); + } + catch (Exception e) { + log.error("获取案件总结失败:{}", e.getMessage()); + return ReturnFailUtils.getRepInfo(); + } + } + + /** + * 类案推荐评价 + * @url {ctx}/api/ai/case-law/setLikeStatus + * @return + */ + @PostMapping("/setLikeStatus") + public Object setLikeStatus() { + Map<String, Object> terms = getParameter(); + + try { + aiChatService.setLikeStatus(terms); + return ReturnSucUtils.getRepInfo(true); + } + catch (Exception e) { + log.error("设置点赞状态失败:{}", e.getMessage()); + return ReturnFailUtils.getRepInfo(); + } + } + @PostMapping("/get-case") public Object getCase(@RequestBody AiRequestVo aiRequestVo) { @@ -43,7 +274,7 @@ params.put("caseDes", aiRequestVo.getCaseDes());//案件描述 params.put("caseClaim", aiRequestVo.getCaseClaim());//案件诉求 params.put("caseId", aiRequestVo.getCaseId());//案件id - params.put("topK", "3"); + params.put("topK", "2"); params.put("metricType", "IP"); String s = HttpClientUtils.httpPostForm(aiUrl + "/queryConciliationCase", params, new HashMap<>(), "utf-8"); JSONObject object = JSONObject.parseObject(s); @@ -59,7 +290,7 @@ caseInfoDtoList.add(caseInfoDto); } } - if (caseInfoDtoList.size() < 6) { + if (caseInfoDtoList.size() < 4) { s = HttpClientUtils.httpPostForm(aiUrl + "/queryJudgmentCase", params, new HashMap<>(), "utf-8"); object = JSONObject.parseObject(s); code = object.getIntValue("code"); @@ -72,8 +303,9 @@ caseInfoDto.setCaseName(jsonObject.getString("case_name")); caseInfoDto.setCaseType(2);//表示判决类案件 caseInfoDtoList.add(caseInfoDto); - if (caseInfoDtoList.size() == 3) + if (caseInfoDtoList.size() == 3) { break; + } } } } @@ -129,7 +361,7 @@ caseInfoDetailDto.setAgreeContent(data.getString("agree_content")); } } else { - s = HttpClientUtils.httpGet(aiUrl + "/getJudgmentCaseText?caseId=" + caseId, new HashMap<>(), "utf-8"); + s = HttpClientUtils.httpGet(aiUrl + "/getJudgmentCaseText?cpwsCaseInfoId=" + caseId, new HashMap<>(), "utf-8"); JSONObject object = JSONObject.parseObject(s); int code = object.getIntValue("code"); if (code == 200) { -- Gitblit v1.8.0