From 8f0ef33d52c4b7ba572cb7528233db9b2d58a614 Mon Sep 17 00:00:00 2001 From: wuchunlei Date: Fri, 9 May 2025 11:55:37 +0800 Subject: [PATCH] =?UTF-8?q?ragflow=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../peanut/common/utils/RagFlowApiUtil.java | 13 ++ .../controller/RagFlowApiController.java | 139 ++++++++++++++++-- 2 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/peanut/common/utils/RagFlowApiUtil.java diff --git a/src/main/java/com/peanut/common/utils/RagFlowApiUtil.java b/src/main/java/com/peanut/common/utils/RagFlowApiUtil.java new file mode 100644 index 00000000..d735d2a5 --- /dev/null +++ b/src/main/java/com/peanut/common/utils/RagFlowApiUtil.java @@ -0,0 +1,13 @@ +package com.peanut.common.utils; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class RagFlowApiUtil { + + + + + +} diff --git a/src/main/java/com/peanut/modules/common/controller/RagFlowApiController.java b/src/main/java/com/peanut/modules/common/controller/RagFlowApiController.java index fcea1da8..1e439ded 100644 --- a/src/main/java/com/peanut/modules/common/controller/RagFlowApiController.java +++ b/src/main/java/com/peanut/modules/common/controller/RagFlowApiController.java @@ -1,16 +1,26 @@ package com.peanut.modules.common.controller; import com.alibaba.fastjson.JSONObject; +import com.peanut.common.utils.R; import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; +import org.apache.http.Consts; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +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.http.MediaType; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; @Slf4j @@ -18,21 +28,130 @@ import java.util.Map; @RequestMapping("common/ragFlowApi") public class RagFlowApiController { - //获取地址 - @RequestMapping(value = "/test2", produces = MediaType.TEXT_EVENT_STREAM_VALUE) - public Flux test3() { + @RequestMapping("/getChatAssistants") + public R getChatAssistants() throws Exception{ + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet get = new HttpGet("http://125.39.141.154:81/api/v1/chats"); + get.setHeader("Authorization", "Bearer ragflow-kyM2JjZWM4MjFiNDExZjA5MTgzOGE1Nm"); + 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); + List> list = new ArrayList(); + if ("0".equals(jsonObject.get("code").toString())){ + List l = jsonObject.getJSONArray("data"); + for (Object o : l) { + Map map = new HashMap<>(); + Map m = (Map)o; + map.put("id",m.get("id")); + map.put("name",m.get("name")); + list.add(map); + } + } + return R.ok().put("list",list); + } + + //聊天助手下对话列表 + @RequestMapping("/getChats") + public R getChats(@RequestBody Map 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("http://125.39.141.154:81/api/v1/chats/"+chatId+"/sessions?page="+page+"&page_size="+pageSize+"&id="+sessionId+"&user_id="+userId); + get.setHeader("Authorization", "Bearer ragflow-kyM2JjZWM4MjFiNDExZjA5MTgzOGE1Nm"); + 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); + List> list = new ArrayList(); + if ("0".equals(jsonObject.get("code").toString())){ + List l = jsonObject.getJSONArray("data"); + for (Object o : l) { + Map map = new HashMap<>(); + Map m = (Map)o; + map.put("id",m.get("id")); + map.put("name",m.get("name")); + map.put("messages",m.get("messages")); + list.add(map); + } + } + return R.ok().put("list",list); + } + + //创建会话 + @RequestMapping("/createChat") + public R createChat(@RequestBody Map params) throws Exception{ + CloseableHttpClient httpClient = HttpClients.createDefault(); + String chatId = params.get("chatId").toString(); Map entity = new HashMap<>(); - entity.put("question", "更年期怎么改善"); + entity.put("name", params.get("name").toString()); + entity.put("user_id", params.get("userId").toString()); + HttpPost post = new HttpPost("http://125.39.141.154:81/api/v1/chats/"+chatId+"/sessions"); + post.setHeader("Authorization", "Bearer ragflow-kyM2JjZWM4MjFiNDExZjA5MTgzOGE1Nm"); + post.setHeader("Content-Type", "application/json;chartset=utf-8"); + post.setEntity(new StringEntity(JSONObject.toJSONString(entity),"utf-8")); + CloseableHttpResponse response = httpClient.execute(post); + 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); + return R.ok().put("res",responseString); + } + + //与助手聊天 + @RequestMapping("/chatToAssistant") + public R chatToAssistant(@RequestBody Map params) throws Exception{ + CloseableHttpClient httpClient = HttpClients.createDefault(); + String chatId = params.get("chatId").toString(); + Map 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()); + HttpPost post = new HttpPost("http://125.39.141.154:81/api/v1/chats/"+chatId+"/completions"); + post.setHeader("Authorization", "Bearer ragflow-kyM2JjZWM4MjFiNDExZjA5MTgzOGE1Nm"); + post.setHeader("Content-Type", "application/json;chartset=utf-8"); + post.setEntity(new StringEntity(JSONObject.toJSONString(entity),"utf-8")); + CloseableHttpResponse response = httpClient.execute(post); + 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); + return R.ok().put("res",responseString); + } + + //与助手聊天流式 + @RequestMapping(value = "/chatToAssistantStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) + public Flux chatToAssistantStream(@RequestBody Map params) { + String chatId = params.get("chatId").toString(); + Map entity = new HashMap<>(); + entity.put("question", params.get("question").toString()); entity.put("stream", true); - entity.put("session_id", "397ea7f9033947c3b32296ad4f7688b8"); + entity.put("session_id", params.get("sessionId").toString()); + entity.put("user_id", params.get("userId").toString()); return WebClient.create().post() - .uri("http://125.39.141.154:81/api/v1/chats/3f35f41e233111f080cd8a56b889fe93/completions") + .uri("http://125.39.141.154:81/api/v1/chats/"+chatId+"/completions") .header("Authorization", "Bearer ragflow-kyM2JjZWM4MjFiNDExZjA5MTgzOGE1Nm") .header("Content-Type", "application/json;chartset=utf-8") .bodyValue(JSONObject.toJSONString(entity)) .retrieve() .bodyToFlux(String.class);//输出格式 - }