ai问答改成websocket
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package com.peanut.common.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.peanut.config.WebSocket;
|
||||
import com.peanut.modules.common.entity.AiChatContent;
|
||||
import com.peanut.modules.common.service.AiChatContentService;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@@ -18,7 +18,6 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Flux;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -33,6 +32,8 @@ public class RagFlowApiUtil {
|
||||
private String authorization;
|
||||
@Autowired
|
||||
private AiChatContentService aiChatContentService;
|
||||
@Autowired
|
||||
private WebSocket webSocket;
|
||||
|
||||
//聊天助手列表
|
||||
public List<Map<String,Object>> getChatAssistants(String chatId) throws Exception{
|
||||
@@ -85,15 +86,6 @@ public class RagFlowApiUtil {
|
||||
if ("0".equals(jsonObject.get("code").toString())){
|
||||
List l = jsonObject.getJSONArray("data");
|
||||
list.addAll(l);
|
||||
// for (Object o : l) {
|
||||
// Map<String,Object> map = new HashMap<>();
|
||||
// Map<String,Object> m = (Map<String,Object>)o;
|
||||
// map.put("chatId",chatId);
|
||||
// map.put("id",m.get("id"));
|
||||
// map.put("name",m.get("name"));
|
||||
// map.put("messages",m.get("messages"));
|
||||
// list.add(map);
|
||||
// }
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@@ -146,7 +138,7 @@ public class RagFlowApiUtil {
|
||||
}
|
||||
|
||||
//与助手聊天流式
|
||||
public Flux<String> chatToAssistantStream(String chatId,String chatName,String sessionId,String sessionName,String question,String patientName) {
|
||||
public void chatToAssistantStream(String chatId,String chatName,String sessionId,String sessionName,String question,String patientName) {
|
||||
try {
|
||||
String userId = ShiroUtils.getUId()+"";
|
||||
Map<String, Object> entity = new HashMap<>();
|
||||
@@ -165,7 +157,7 @@ public class RagFlowApiUtil {
|
||||
content.setContent(question);
|
||||
aiChatContentService.save(content);
|
||||
List<String> list = new ArrayList<>();
|
||||
return WebClient.create().post()
|
||||
WebClient.create().post()
|
||||
.uri(url+"/api/v1/chats/"+chatId+"/completions")
|
||||
.header("Authorization", authorization)
|
||||
.header("Content-Type", "application/json;chartset=utf-8")
|
||||
@@ -176,11 +168,13 @@ public class RagFlowApiUtil {
|
||||
JSONObject jsonObject = JSONObject.parseObject(data);
|
||||
if ("0".equals(jsonObject.get("code").toString())){
|
||||
if (!"true".equals(jsonObject.get("data").toString())){
|
||||
webSocket.sendMessage(data);
|
||||
list.add(((JSONObject)jsonObject.get("data")).get("answer").toString());
|
||||
}
|
||||
}
|
||||
})
|
||||
.doFinally(data -> {
|
||||
webSocket.sendMessage("{\"code\":0,\"data\":true}");
|
||||
AiChatContent answer = new AiChatContent();
|
||||
answer.setUserId(Integer.parseInt(userId));
|
||||
answer.setChatAssistantId(chatId);
|
||||
@@ -191,12 +185,11 @@ public class RagFlowApiUtil {
|
||||
answer.setType(1);
|
||||
answer.setContent(list.get(list.size()-1));
|
||||
aiChatContentService.save(answer);
|
||||
});
|
||||
}).subscribe();
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user