ragflow接口
This commit is contained in:
13
src/main/java/com/peanut/common/utils/RagFlowApiUtil.java
Normal file
13
src/main/java/com/peanut/common/utils/RagFlowApiUtil.java
Normal file
@@ -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 {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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<String> 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<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);
|
||||
}
|
||||
|
||||
//聊天助手下对话列表
|
||||
@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);
|
||||
}
|
||||
|
||||
//创建会话
|
||||
@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("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<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);
|
||||
}
|
||||
|
||||
//与助手聊天流式
|
||||
@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", "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);//输出格式
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user