forked from gzzfw/backEnd/gz-dyh

huangh
2024-09-26 5a4355579f663a0fb8b0c7ca4b3676594b55c7c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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);
        }
    }
 
 
 
}