由于前端响应超时,历史对话用数据库数据
This commit is contained in:
@@ -16,6 +16,7 @@ import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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;
|
||||
@@ -64,44 +65,35 @@ public class RagFlowApiUtil {
|
||||
//聊天助手下对话列表
|
||||
public List<Map<String,Object>> getChats(Map<String,Object> params) throws Exception{
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
String id = params.get("chatId").toString();
|
||||
List<String> chatIds = new ArrayList<>();
|
||||
if (StringUtils.isEmpty(id)){
|
||||
List<Map<String,Object>> assistants = getChatAssistants("");
|
||||
for (Map<String, Object> map:assistants) {
|
||||
chatIds.add(map.get("id").toString());
|
||||
}
|
||||
}else {
|
||||
chatIds.add(id);
|
||||
}
|
||||
String chatId = params.get("chatId").toString();
|
||||
List<Map<String,Object>> list = new ArrayList();
|
||||
for (String chatId : chatIds){
|
||||
String page = params.get("page").toString();
|
||||
String pageSize = params.get("pageSize").toString();
|
||||
String sessionId = params.get("sessionId").toString();
|
||||
HttpGet get = new HttpGet(url+"/api/v1/chats/"+chatId+"/sessions?" +
|
||||
"page="+page+"&page_size="+pageSize+"&id="+sessionId+"&user_id="+ShiroUtils.getUId());
|
||||
get.setHeader("Authorization", authorization);
|
||||
get.setHeader("Content-Type", "application/json;chartset=utf-8");
|
||||
CloseableHttpResponse response = httpClient.execute(get);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode >= 400) {
|
||||
throw new RuntimeException("API调用失败,状态码:" + statusCode);
|
||||
}
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
String responseString = EntityUtils.toString(responseEntity, Consts.UTF_8);
|
||||
JSONObject jsonObject = JSONObject.parseObject(responseString);
|
||||
if ("0".equals(jsonObject.get("code").toString())){
|
||||
List l = jsonObject.getJSONArray("data");
|
||||
for (Object o : l) {
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
Map<String,Object> m = (Map<String,Object>)o;
|
||||
map.put("id",m.get("id"));
|
||||
map.put("name",m.get("name"));
|
||||
map.put("messages",m.get("messages"));
|
||||
list.add(map);
|
||||
}
|
||||
}
|
||||
String page = params.get("page").toString();
|
||||
String pageSize = params.get("pageSize").toString();
|
||||
String sessionId = params.get("sessionId").toString();
|
||||
HttpGet get = new HttpGet(url+"/api/v1/chats/"+chatId+"/sessions?" +
|
||||
"page="+page+"&page_size="+pageSize+"&id="+sessionId+"&user_id="+ShiroUtils.getUId());
|
||||
get.setHeader("Authorization", authorization);
|
||||
get.setHeader("Content-Type", "application/json;chartset=utf-8");
|
||||
CloseableHttpResponse response = httpClient.execute(get);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode >= 400) {
|
||||
throw new RuntimeException("API调用失败,状态码:" + statusCode);
|
||||
}
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
String responseString = EntityUtils.toString(responseEntity, Consts.UTF_8);
|
||||
JSONObject jsonObject = JSONObject.parseObject(responseString);
|
||||
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;
|
||||
}
|
||||
@@ -202,6 +194,7 @@ public class RagFlowApiUtil {
|
||||
});
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,25 @@
|
||||
package com.peanut.modules.common.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.peanut.common.utils.R;
|
||||
import com.peanut.common.utils.RagFlowApiUtil;
|
||||
import com.peanut.common.utils.ShiroUtils;
|
||||
import com.peanut.modules.common.entity.AiChatContent;
|
||||
import com.peanut.modules.common.service.AiChatContentService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@Slf4j
|
||||
@RestController("commonRagFlowApi")
|
||||
@@ -19,6 +28,8 @@ public class RagFlowApiController {
|
||||
|
||||
@Autowired
|
||||
private RagFlowApiUtil ragFlowApiUtil;
|
||||
@Autowired
|
||||
private AiChatContentService aiChatContentService;
|
||||
|
||||
//聊天助手列表
|
||||
@RequestMapping("/getChatAssistants")
|
||||
@@ -47,7 +58,21 @@ public class RagFlowApiController {
|
||||
//聊天助手下对话列表
|
||||
@RequestMapping("/getChats")
|
||||
public R getChats(@RequestBody Map<String,Object> params) throws Exception{
|
||||
return R.ok().put("list",ragFlowApiUtil.getChats(params));
|
||||
LambdaQueryWrapper<AiChatContent> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(AiChatContent::getUserId, ShiroUtils.getUId());
|
||||
if (StringUtils.isNotEmpty(params.get("chatId").toString())||
|
||||
StringUtils.isNotEmpty(params.get("sessionId").toString())){
|
||||
wrapper.eq(StringUtils.isNotEmpty(params.get("chatId").toString()),
|
||||
AiChatContent::getChatAssistantId,params.get("chatId"));
|
||||
wrapper.eq(StringUtils.isNotEmpty(params.get("sessionId").toString()),
|
||||
AiChatContent::getChatId,params.get("sessionId"));
|
||||
wrapper.orderByAsc(AiChatContent::getCreateTime);
|
||||
}else {
|
||||
wrapper.groupBy(AiChatContent::getChatId);
|
||||
wrapper.orderByDesc(AiChatContent::getCreateTime);
|
||||
}
|
||||
List<AiChatContent> contents = aiChatContentService.list(wrapper);
|
||||
return R.ok().put("list",contents);
|
||||
}
|
||||
|
||||
//创建会话
|
||||
@@ -58,6 +83,7 @@ public class RagFlowApiController {
|
||||
|
||||
//与助手聊天流式
|
||||
@RequestMapping(value = "/chatToAssistantStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
@Transactional
|
||||
public Flux<String> chatToAssistantStream(String chatId,String chatName,String sessionId,String sessionName,String question,String patientName){
|
||||
return ragFlowApiUtil.chatToAssistantStream(chatId,chatName,sessionId,sessionName,question,patientName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user