163 lines
7.5 KiB
Java
163 lines
7.5 KiB
Java
package com.peanut.common.utils;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
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.Value;
|
||
import org.springframework.stereotype.Component;
|
||
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;
|
||
|
||
@Component
|
||
public class RagFlowApiUtil {
|
||
|
||
@Value("${ragflow.url}")
|
||
private String url;
|
||
@Value("${ragflow.authorization}")
|
||
private String authorization;
|
||
|
||
//聊天助手列表
|
||
public R getChatAssistants(String chatId) throws Exception{
|
||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||
HttpGet get = new HttpGet(url+"/api/v1/chats?id="+chatId);
|
||
get.setHeader("Authorization", authorization);
|
||
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);
|
||
}
|
||
|
||
//聊天助手下对话列表
|
||
public R 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);
|
||
get.setHeader("Authorization", authorization);
|
||
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);
|
||
}
|
||
|
||
//创建会话
|
||
public String createChat(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(url+"/api/v1/chats/"+chatId+"/sessions");
|
||
post.setHeader("Authorization", authorization);
|
||
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);
|
||
JSONObject jsonObject = JSONObject.parseObject(responseString);
|
||
if ("0".equals(jsonObject.get("code").toString())){
|
||
return ((JSONObject)jsonObject.get("data")).get("id").toString();
|
||
}
|
||
return "";
|
||
}
|
||
|
||
//与助手聊天
|
||
public R chatToAssistant(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(url+"/api/v1/chats/"+chatId+"/completions");
|
||
post.setHeader("Authorization", authorization);
|
||
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);
|
||
}
|
||
|
||
//与助手聊天流式
|
||
public Flux<String> chatToAssistantStream(Map<String,Object> params) {
|
||
try {
|
||
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(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);
|
||
}catch (Exception e){
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
}
|