This commit is contained in:
wuchunlei
2025-05-13 17:54:49 +08:00
parent e9d1627399
commit e4730b8a2a
4 changed files with 63 additions and 18 deletions

View File

@@ -1,6 +1,8 @@
package com.peanut.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.peanut.modules.common.entity.AiChatContent;
import com.peanut.modules.common.service.AiChatContentService;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -10,6 +12,7 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
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.web.reactive.function.client.WebClient;
@@ -26,9 +29,11 @@ public class RagFlowApiUtil {
private String url;
@Value("${ragflow.authorization}")
private String authorization;
@Autowired
private AiChatContentService aiChatContentService;
//聊天助手列表
public R getChatAssistants(String chatId) throws Exception{
public List<Map<String,Object>> getChatAssistants(String chatId) throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url+"/api/v1/chats?id="+chatId);
get.setHeader("Authorization", authorization);
@@ -52,18 +57,18 @@ public class RagFlowApiUtil {
list.add(map);
}
}
return R.ok().put("list",list);
return list;
}
//聊天助手下对话列表
public R getChats(Map<String,Object> params) throws Exception{
public List<Map<String,Object>> getChats(Map<String,Object> params) throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
String chatId = params.get("chatId").toString();
String page = params.get("page").toString();
String pageSize = params.get("pageSize").toString();
String sessionId = params.get("sessionId").toString();
String userId = params.get("userId").toString();
HttpGet get = new HttpGet(url+"/api/v1/chats/"+chatId+"/sessions?page="+page+"&page_size="+pageSize+"&id="+sessionId+"&user_id="+userId);
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);
@@ -86,7 +91,7 @@ public class RagFlowApiUtil {
list.add(map);
}
}
return R.ok().put("list",list);
return list;
}
//创建会话
@@ -95,7 +100,7 @@ public class RagFlowApiUtil {
String chatId = params.get("chatId").toString();
Map<String, Object> entity = new HashMap<>();
entity.put("name", params.get("name").toString());
entity.put("user_id", params.get("userId").toString());
entity.put("user_id", ShiroUtils.getUId());
HttpPost post = new HttpPost(url+"/api/v1/chats/"+chatId+"/sessions");
post.setHeader("Authorization", authorization);
post.setHeader("Content-Type", "application/json;chartset=utf-8");
@@ -121,7 +126,7 @@ public class RagFlowApiUtil {
Map<String, Object> entity = new HashMap<>();
entity.put("question", params.get("question").toString());
entity.put("session_id", params.get("sessionId").toString());
entity.put("user_id", params.get("userId").toString());
entity.put("user_id", ShiroUtils.getUId());
HttpPost post = new HttpPost(url+"/api/v1/chats/"+chatId+"/completions");
post.setHeader("Authorization", authorization);
post.setHeader("Content-Type", "application/json;chartset=utf-8");
@@ -137,21 +142,52 @@ public class RagFlowApiUtil {
}
//与助手聊天流式
public Flux<String> chatToAssistantStream(Map<String,Object> params) {
public Flux<String> chatToAssistantStream(String chatId,String chatName,String sessionId,String sessionName,String question,String patientName) {
try {
String chatId = params.get("chatId").toString();
String userId = ShiroUtils.getUId()+"";
Map<String, Object> entity = new HashMap<>();
entity.put("question", params.get("question").toString());
entity.put("question", question);
entity.put("stream", true);
entity.put("session_id", params.get("sessionId").toString());
entity.put("user_id", params.get("userId").toString());
entity.put("session_id", sessionId);
entity.put("user_id", userId);
AiChatContent content = new AiChatContent();
content.setUserId(Integer.parseInt(userId));
content.setChatAssistantId(chatId);
content.setChatAssistantName(chatName);
content.setChatId(sessionId);
content.setChatName(sessionName);
content.setPatientName(patientName);
content.setType(0);
content.setContent(question);
aiChatContentService.save(content);
List<String> list = new ArrayList<>();
return WebClient.create().post()
.uri(url+"/api/v1/chats/"+chatId+"/completions")
.header("Authorization", authorization)
.header("Content-Type", "application/json;chartset=utf-8")
.bodyValue(JSONObject.toJSONString(entity))
.retrieve()
.bodyToFlux(String.class);
.bodyToFlux(String.class)
.doOnNext(data -> {
JSONObject jsonObject = JSONObject.parseObject(data);
if ("0".equals(jsonObject.get("code").toString())){
if (!"true".equals(jsonObject.get("data").toString())){
list.add(((JSONObject)jsonObject.get("data")).get("answer").toString());
}
}
})
.doFinally(data -> {
AiChatContent answer = new AiChatContent();
answer.setUserId(Integer.parseInt(userId));
answer.setChatAssistantId(chatId);
answer.setChatAssistantName(chatName);
answer.setChatId(sessionId);
answer.setChatName(sessionName);
answer.setPatientName(patientName);
answer.setType(1);
answer.setContent(list.get(list.size()-1));
aiChatContentService.save(answer);
});
}catch (Exception e){
e.printStackTrace();
}

View File

@@ -39,8 +39,8 @@ public class RagFlowApiController {
//与助手聊天流式
@RequestMapping(value = "/chatToAssistantStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> chatToAssistantStream(@RequestBody Map<String,Object> params){
return ragFlowApiUtil.chatToAssistantStream(params);
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);
}

View File

@@ -28,10 +28,16 @@ public class AiChatContent implements Serializable {
//对话名称
private String chatName;
//患者姓名
private String patientName;
//0问题1答案
private Integer type;
//对话内容
private String content;
private Date startTime;
private Date createTime;
@TableLogic
private Integer delFlag;

View File

@@ -22,10 +22,13 @@ public class AiRecordFolder implements Serializable {
//患者姓名
private String patientName;
//聊天助手id
private String chatAssistantId;
//对话id
private String chatId;
private Date startTime;
private Date createTime;
@TableLogic
private Integer delFlag;