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);
|
}
|
}
|
|
|
|
}
|