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