将ragflow参数写道配置文件

This commit is contained in:
wuchunlei
2025-05-12 13:10:50 +08:00
parent 8f0ef33d52
commit 1ba1c6a418
5 changed files with 197 additions and 123 deletions

View File

@@ -11,12 +11,16 @@ package com.peanut.modules.app.config;
import com.peanut.modules.app.interceptor.AuthorizationInterceptor;
import com.peanut.modules.app.resolver.LoginUserHandlerMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
import java.util.concurrent.Executor;
/**
* MVC配置
@@ -30,6 +34,15 @@ public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;
@Autowired
@Qualifier("fluxTaskExecutor")
private AsyncTaskExecutor taskExecutor;
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setTaskExecutor(taskExecutor);
configurer.setDefaultTimeout(30_000); // 超时时间设为30秒
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authorizationInterceptor).addPathPatterns("/app/**");
@@ -39,4 +52,5 @@ public class WebMvcConfig implements WebMvcConfigurer {
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
}
}

View File

@@ -1,26 +1,14 @@
package com.peanut.modules.common.controller;
import com.alibaba.fastjson.JSONObject;
import com.peanut.common.utils.R;
import com.peanut.common.utils.RagFlowApiUtil;
import lombok.extern.slf4j.Slf4j;
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.beans.factory.annotation.Autowired;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@@ -28,130 +16,31 @@ import java.util.Map;
@RequestMapping("common/ragFlowApi")
public class RagFlowApiController {
@Autowired
private RagFlowApiUtil ragFlowApiUtil;
//聊天助手列表
@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<Map<String,Object>> list = new ArrayList();
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"));
list.add(map);
}
}
return R.ok().put("list",list);
return R.ok().put("list",ragFlowApiUtil.getChatAssistants(""));
}
//聊天助手下对话列表
@RequestMapping("/getChats")
public R getChats(@RequestBody 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("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<Map<String,Object>> list = new ArrayList();
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);
}
}
return R.ok().put("list",list);
return R.ok().put("list",ragFlowApiUtil.getChats(params));
}
//创建会话
@RequestMapping("/createChat")
public R createChat(@RequestBody Map<String,Object> params) throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
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());
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<String,Object> params) throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
String chatId = params.get("chatId").toString();
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());
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);
return R.ok().put("id",ragFlowApiUtil.createChat(params));
}
//与助手聊天流式
@RequestMapping(value = "/chatToAssistantStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> chatToAssistantStream(@RequestBody Map<String,Object> params) {
String chatId = params.get("chatId").toString();
Map<String, Object> entity = new HashMap<>();
entity.put("question", params.get("question").toString());
entity.put("stream", true);
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/"+chatId+"/completions")
.header("Authorization", "Bearer ragflow-kyM2JjZWM4MjFiNDExZjA5MTgzOGE1Nm")
.header("Content-Type", "application/json;chartset=utf-8")
.bodyValue(JSONObject.toJSONString(entity))
.retrieve()
.bodyToFlux(String.class);//输出格式
public Flux<String> chatToAssistantStream(@RequestBody Map<String,Object> params){
return ragFlowApiUtil.chatToAssistantStream(params);
}