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/domain/po/CaseSimilarityExplanatory.java | 72 ++++
dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/service/AiChatService.java | 242 ++++++++++++++++
dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/AiController.java | 252 ++++++++++++++++
dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/AiConversation.java | 90 ++++++
dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/dto/AiConversationDto.java | 19 +
dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/CaseSimilarityRelations.java | 102 ++++++
dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/AiMessage.java | 78 +++++
7 files changed, 845 insertions(+), 10 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 bffe485..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) {
@@ -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;
+ }
}
}
}
diff --git a/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/dto/AiConversationDto.java b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/dto/AiConversationDto.java
new file mode 100644
index 0000000..612983e
--- /dev/null
+++ b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/dto/AiConversationDto.java
@@ -0,0 +1,19 @@
+package cn.huge.module.ai.controller.domain.dto;
+
+import lombok.Data;
+
+@Data
+public class AiConversationDto {
+ /**
+ * 案件标题
+ */
+ private String caseName;
+ /**
+ * 案件类型 1 调解类 2 判决类
+ */
+ private int caseType;
+ /**
+ * 案件ID
+ */
+ private String caseId;
+}
diff --git a/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/AiConversation.java b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/AiConversation.java
new file mode 100644
index 0000000..eccec60
--- /dev/null
+++ b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/AiConversation.java
@@ -0,0 +1,90 @@
+package cn.huge.module.ai.controller.domain.po;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ *
+ * @description: PO中的属性与数据表是一一对应关系,如需根据业务处理不同,请使用BO对象。
+ * @company:hugeinfo
+ * @author: huangh
+ * @time: 2024-09-23 11:10:05
+ * @version 1.0.0
+ */
+@TableName(value = "ai_conversation")
+@Data
+public class AiConversation {
+
+ /**
+ * 对话主表id编号
+ */
+ @TableField(value = "ai_conversation_id")
+ private String aiConversationId;
+
+ /**
+ * 案件编号
+ */
+ @TableField(value = "case_id")
+ private String caseId;
+
+ /**
+ * 用户编号
+ */
+ @TableField(value = "user_id")
+ private String userId;
+
+ /**
+ * 用户编号
+ */
+ @TableField(value = "conversation_title")
+ private String conversationTitle;
+
+ /**
+ * 对话状态,1:进行中(默认值),2:已完成,3:暂停,4:已删除
+ */
+ @TableField(value = "conversation_status")
+ private String conversationStatus;
+
+ /**
+ * 对话开始时间
+ */
+ @TableField(value = "start_time")
+ private Date startTime;
+
+ /**
+ * 最后一次对话时间
+ */
+ @TableField(value = "end_time")
+ private Date endTime;
+
+ /**
+ * 删除状态,1:未删除(默认值),99:已删除
+ */
+ @TableLogic
+ @TableField(value = "delete_status")
+ private String deleteStatus;
+
+ /**
+ * 备注
+ */
+ @TableField(value = "note")
+ private String note;
+
+ /**
+ * 创建时间
+ */
+ @TableField(value = "create_time")
+ private Date createTime;
+
+ /**
+ * 修改时间
+ */
+ @TableField(value = "update_time")
+ private Date updateTime;
+
+}
diff --git a/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/AiMessage.java b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/AiMessage.java
new file mode 100644
index 0000000..15a1829
--- /dev/null
+++ b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/AiMessage.java
@@ -0,0 +1,78 @@
+package cn.huge.module.ai.controller.domain.po;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ *
+ * @description: PO中的属性与数据表是一一对应关系,如需根据业务处理不同,请使用BO对象。
+ * @company:hugeinfo
+ * @author: huangh
+ * @time: 2024-09-25 14:26:58
+ * @version 1.0.0
+ */
+@TableName(value = "ai_message")
+@Data
+public class AiMessage {
+
+ /**
+ * 唯一标识每一条消息
+ */
+ @TableField(value = "ai_message_id")
+ private String aiMessageId;
+
+ /**
+ * 关联到对话主表,确定这条消息属于哪次对话
+ */
+ @TableField(value = "conversation_id")
+ private String conversationId;
+
+ /**
+ * 发送者类型:1:用户,2:系统,3:助手,4:知识库
+ */
+ @TableField(value = "sender_type")
+ private String senderType;
+
+ /**
+ * 消息的具体内容
+ */
+ @TableField(value = "message_content")
+ private String messageContent;
+
+ /**
+ * 点赞状态,0:未操作,1:点赞,-1:点踩
+ */
+ @TableField(value = "like_status")
+ private String likeStatus;
+
+ /**
+ * 删除状态,1:未删除(默认值),99:已删除
+ */
+ @TableLogic
+ @TableField(value = "delete_status")
+ private String deleteStatus;
+
+ /**
+ * 创建时间
+ */
+ @TableField(value = "create_time")
+ private Date createTime;
+
+ /**
+ * 更新时间
+ */
+ @TableField(value = "update_time")
+ private Date updateTime;
+
+ /**
+ * 备注
+ */
+ @TableField(value = "note")
+ private String note;
+
+}
diff --git a/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/CaseSimilarityExplanatory.java b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/CaseSimilarityExplanatory.java
new file mode 100644
index 0000000..50d11c8
--- /dev/null
+++ b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/CaseSimilarityExplanatory.java
@@ -0,0 +1,72 @@
+package cn.huge.module.ai.controller.domain.po;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ *
+ * @description: PO中的属性与数据表是一一对应关系,如需根据业务处理不同,请使用BO对象。
+ * @company:hugeinfo
+ * @author: huangh
+ * @time: 2024-09-25 15:13:34
+ * @version 1.0.0
+ */
+@TableName(value = "case_similarity_explanatory")
+@Data
+public class CaseSimilarityExplanatory {
+
+ /**
+ * 唯一标识
+ */
+ @TableField(value = "case_similarity_explanatory_id")
+ private String caseSimilarityExplanatoryId;
+
+ /**
+ * 查询案件id
+ */
+ @TableField(value = "case_id")
+ private String caseId;
+
+ /**
+ * 查询案件id
+ */
+ @TableField(value = "similarity_case_id")
+ private String similarityCaseId;
+
+ /**
+ * 案件相关性解释正文
+ */
+ @TableField(value = "explanatory_content")
+ private String explanatoryContent;
+
+ /**
+ * 删除状态,1:未删除(默认值),99:已删除
+ */
+ @TableLogic
+ @TableField(value = "delete_status")
+ private String deleteStatus;
+
+ /**
+ * 创建时间
+ */
+ @TableField(value = "create_time")
+ private Date createTime;
+
+ /**
+ * 更新时间
+ */
+ @TableField(value = "update_time")
+ private Date updateTime;
+
+ /**
+ * 备注
+ */
+ @TableField(value = "note")
+ private String note;
+
+}
diff --git a/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/CaseSimilarityRelations.java b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/CaseSimilarityRelations.java
new file mode 100644
index 0000000..7023a06
--- /dev/null
+++ b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/domain/po/CaseSimilarityRelations.java
@@ -0,0 +1,102 @@
+package cn.huge.module.ai.controller.domain.po;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ *
+ * @description: PO中的属性与数据表是一一对应关系,如需根据业务处理不同,请使用BO对象。
+ * @company:hugeinfo
+ * @author: huangh
+ * @time: 2024-09-25 16:40:08
+ * @version 1.0.0
+ */
+@TableName(value = "case_similarity_relations")
+@Data
+public class CaseSimilarityRelations {
+
+ /**
+ * 关系编号
+ */
+ @TableField(value = "case_similarity_relations_id")
+ private String caseSimilarityRelationsId;
+
+ /**
+ * 查询案件标题
+ */
+ @TableField(value = "case_title")
+ private String caseTitle;
+
+ /**
+ * 案件编号
+ */
+ @TableField(value = "case_id")
+ private String caseId;
+
+ /**
+ * 类似案件标题
+ */
+ @TableField(value = "similarity_case_title")
+ private String similarityCaseTitle;
+
+ /**
+ * 类似案件id
+ */
+ @TableField(value = "similarity_case_id")
+ private String similarityCaseId;
+
+ /**
+ * 类似案件类别,1:判决类,2:调解类
+ */
+ @TableField(value = "similarity_case_type")
+ private String similarityCaseType;
+
+ /**
+ * 案件对在本次类案推荐中的排序
+ */
+ @TableField(value = "top")
+ private String top;
+
+ /**
+ * 案件与查询案件的相似度
+ */
+ @TableField(value = "similarity")
+ private Float similarity;
+
+ /**
+ * 点赞状态,0:未操作,1:点赞,-1:点踩
+ */
+ @TableField(value = "like_status")
+ private String likeStatus;
+
+ /**
+ * 删除状态,1:未删除(默认值),99:已删除
+ */
+ @TableLogic
+ @TableField(value = "delete_status")
+ private String deleteStatus;
+
+ /**
+ * 创建时间
+ */
+ @TableField(value = "create_time")
+ private Date createTime;
+
+ /**
+ * 更新时间
+ */
+ @TableField(value = "update_time")
+ private Date updateTime;
+
+ /**
+ * 备注
+ */
+ @TableField(value = "note")
+ private String note;
+
+}
diff --git a/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/service/AiChatService.java b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/service/AiChatService.java
new file mode 100644
index 0000000..e9e0998
--- /dev/null
+++ b/dyh-service/dyh-mediate/src/main/java/cn/huge/module/ai/controller/service/AiChatService.java
@@ -0,0 +1,242 @@
+package cn.huge.module.ai.controller.service;
+
+import cn.huge.base.common.exception.ServiceException;
+import cn.huge.base.common.utils.HttpClientUtils;
+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 com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.buffer.DataBuffer;
+import org.springframework.http.MediaType;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.reactive.function.BodyInserters;
+import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
+
+import java.io.IOException;
+import java.util.*;
+
+@Slf4j
+@Service
+@Transactional(rollbackFor = Exception.class)
+public class AiChatService {
+
+ @Value("${ai.url}")
+ private String aiUrl;
+
+ /**
+ * 创建会话
+ * @param
+ * @return Object
+ */
+ public AiConversation createAiChat(Map<String, Object> terms){
+ try{
+ String caseId = terms.get("caseId").toString();
+ String userId = terms.get("userId").toString();
+ String conversationTitle = terms.get("conversationTitle").toString();
+ AiConversation aiConversation = new AiConversation();
+ Map<String, String> params = new HashMap<>();
+ params.put("caseId", caseId);
+ params.put("userId", userId);
+ params.put("conversationTitle", conversationTitle);
+ String s = HttpClientUtils.httpPostForm(aiUrl + "/createAiChat", params, new HashMap<>(), "utf-8");
+ JSONObject object = JSONObject.parseObject(s);
+ int code = object.getIntValue("code");
+ if (code == 200) {
+ JSONObject data = object.getJSONObject("data");
+ aiConversation.setAiConversationId(data.getString("aiConversationId"));
+ }
+ return aiConversation;
+ }catch (Exception e){
+ log.error("[AiChatService.updateCaseAgent]调用失败,异常信息:"+e, e);
+ throw new ServiceException("AiChatService.updateCaseAgent", e);
+ }
+ }
+
+ /**
+ * 获取智能会话列表
+ * @param
+ * @return Object
+ */
+ public List<AiConversation> queryAiChatList(String caseId,String userId){
+ try{
+ String message = String.format("/queryAiChatList?caseId=%s&userId=%s", caseId, userId);
+ String s = HttpClientUtils.httpGet(aiUrl + message, new HashMap<>(), "utf-8");
+ JSONObject object = JSONObject.parseObject(s);
+ int code = object.getIntValue("code");
+ List<AiConversation> aiConversationList = new ArrayList<>();
+ if (code == 200) {
+ JSONArray data = object.getJSONArray("data");
+ for (int i = 0; i < data.size(); i++) {
+ JSONObject jsonObject = data.getJSONObject(i);
+ AiConversation aiConversation = new AiConversation();
+ aiConversation.setAiConversationId(jsonObject.getString("ai_conversation_id"));
+ aiConversation.setCaseId(jsonObject.getString("case_id"));
+ aiConversation.setConversationTitle(jsonObject.getString("conversation_title"));
+ aiConversationList.add(aiConversation);
+ }
+ }
+ return aiConversationList;
+ }catch (Exception e){
+ log.error("[AiChatService.updateCaseAgent]调用失败,异常信息:"+e, e);
+ throw new ServiceException("AiChatService.updateCaseAgent", e);
+ }
+ }
+
+ /**
+ * 创建对话
+ * @param
+ * @return Object
+ */
+ public StreamingResponseBody createAiChatMessage(Map<String, Object> terms){
+ try{
+ String aiConversationId = terms.get("aiConversationId").toString();
+ String caseDes = terms.get("caseDes").toString();
+ String caseClaim = terms.get("caseClaim").toString();
+ String userMessage = terms.get("userMessage").toString();
+ MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
+ formData.add("aiConversationId", aiConversationId);
+ formData.add("caseDes", caseDes);
+ formData.add("caseClaim", caseClaim);
+ formData.add("userMessage", userMessage);
+
+ WebClient client = WebClient.create(aiUrl + "/createAiChatMessage");
+
+ return outputStream -> client.post()
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+ .body(BodyInserters.fromFormData(formData))
+ .retrieve()
+ .bodyToFlux(DataBuffer.class)
+ .doOnNext(dataBuffer -> {
+ byte[] bytes = new byte[dataBuffer.readableByteCount()];
+ dataBuffer.read(bytes);
+ try {
+ outputStream.write(bytes);
+ // 刷新输出流以确保数据立即发送
+ outputStream.flush();
+ } catch (IOException e) {
+ // 异常处理
+ e.printStackTrace();
+ }
+ })
+ .blockLast();
+ }catch (Exception e){
+ log.error("[AiChatService.createAiChatMessage]调用失败,异常信息:"+e, e);
+ throw new ServiceException("AiChatService.updateCaseAgent", e);
+ }
+ }
+
+ /**
+ * 获取智能对话列表
+ */
+ public List<AiMessage> getAiChatMessageList(String aiConversationId){
+ try {
+ String message = String.format("/getAiChatMessageList?aiConversationId=%s", aiConversationId);
+ String s = HttpClientUtils.httpGet(aiUrl + message, new HashMap<>(), "utf-8");
+ JSONObject object = JSONObject.parseObject(s);
+ int code = object.getIntValue("code");
+ List<AiMessage> aiMessageList = new ArrayList<>();
+ if (code == 200) {
+ JSONArray data = object.getJSONArray("data");
+ for (int i = 0; i < data.size(); i++) {
+ JSONObject jsonObject = data.getJSONObject(i);
+ AiMessage aiMessage = new AiMessage();
+ aiMessage.setAiMessageId(jsonObject.getString("ai_message_id"));
+ aiMessage.setConversationId(jsonObject.getString("conversation_id"));
+ aiMessage.setMessageContent(jsonObject.getString("message_content"));
+ aiMessage.setSenderType(jsonObject.getString("sender_type"));
+ aiMessageList.add(aiMessage);
+ }
+ }
+ return aiMessageList;
+ }catch (Exception e) {
+ log.error("[AiChatService.getAiChatMessageList]调用失败,异常信息:"+e, e);
+ throw new ServiceException("AiChatService.updateCaseAgent", e);
+ }
+ }
+
+ /**
+ * 删除会话(修改状态)
+ */
+ public Object deleteConversation(Map<String, Object> terms){
+ try {
+ String aiConversationId = terms.get("aiConversationId").toString();
+ Map<String, String> params = new HashMap<>();
+ params.put("aiConversationId", aiConversationId);
+ String s = HttpClientUtils.httpPostForm(aiUrl + "/deleteConversation", params, new HashMap<>(), "utf-8");
+ JSONObject object = JSONObject.parseObject(s);
+ int code = object.getIntValue("code");
+ if (code == 200) {
+ return object.getString("data");
+ }
+ return null;
+ }catch (Exception e) {
+ log.error("[AiChatService.deleteConversation]调用失败,异常信息:"+e, e);
+ throw new ServiceException("AiChatService.updateCaseAgent", e);
+ }
+ }
+
+ /**
+ * 判决简介接口
+ */
+ public CaseSimilarityExplanatory getJudgmentSummarize(String similarityCaseId, String caseContent, String caseId) {
+ try {
+
+ Map<String, String> params = new HashMap<>();
+ params.put("similarityCaseId", similarityCaseId);
+ params.put("caseContent", caseContent);
+ params.put("caseId", caseId);
+ String s = HttpClientUtils.httpPostForm(aiUrl + "/getJudgmentSummarize", params, new HashMap<>(), "utf-8");
+ JSONObject object = JSONObject.parseObject(s);
+ int code = object.getIntValue("code");
+ CaseSimilarityExplanatory caseSimilarityExplanatory = new CaseSimilarityExplanatory();
+
+ if (code == 200) {
+ JSONObject data = object.getJSONObject("data");
+ caseSimilarityExplanatory.setCaseId(data.getString("case_id"));
+ caseSimilarityExplanatory.setSimilarityCaseId(data.getString("similarity_case_id"));
+ caseSimilarityExplanatory.setCaseSimilarityExplanatoryId(data.getString("case_similarity_explanatory_id"));
+ caseSimilarityExplanatory.setExplanatoryContent(data.getString("explanatory_content"));
+ }
+ return caseSimilarityExplanatory;
+ }catch (Exception e) {
+ log.error("[AiChatService.getJudgmentSummarize]调用失败,异常信息:"+e, e);
+ throw new ServiceException("AiChatService.updateCaseAgent", e);
+ }
+ }
+
+ /**
+ * 类案推荐评价
+ */
+ public Object setLikeStatus(Map<String, Object> terms) {
+ try {
+ String similarityCaseId = terms.get("similarityCaseId").toString();
+ String caseContent = terms.get("likeStatus").toString();
+ String caseId = terms.get("caseId").toString();
+
+ Map<String, String> params = new HashMap<>();
+ params.put("similarityCaseId", similarityCaseId);
+ params.put("caseContent", caseContent);
+ params.put("caseId", caseId);
+ String s = HttpClientUtils.httpPostForm(aiUrl + "/setLikeStatus", params, new HashMap<>(), "utf-8");
+ JSONObject object = JSONObject.parseObject(s);
+ int code = object.getIntValue("code");
+ if (code == 200) {
+ return object.getJSONObject("data");
+ }
+ return null;
+ } catch (Exception e) {
+ log.error("[AiChatService.setLikeStatus]调用失败,异常信息:" + e, e);
+ throw new ServiceException("AiChatService.updateCaseAgent", e);
+ }
+ }
+
+
+
+}
--
Gitblit v1.8.0