jinan_dev
DESKTOP-DDTUS3E\yaxin 5 months ago
parent ac8a9ea483
commit 08477d961f

@ -10,10 +10,10 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.supervision.minio.domain.MinioFile;
import com.supervision.minio.service.MinioService;
import com.supervision.police.vo.dify.ChatReqVO;
import com.supervision.police.vo.dify.ChatResVO;
import com.supervision.police.vo.dify.DatasetReqVO;
import com.supervision.police.vo.dify.DatasetResVO;
import com.supervision.police.vo.dify.DifyChatReqVO;
import com.supervision.police.vo.dify.DifyChatResVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
@ -31,6 +31,7 @@ import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.supervision.common.constant.DifyConstants.*;
@ -49,22 +50,23 @@ public class DifyApiUtil {
@Autowired
private MinioService minioService;
public void chat(ChatReqVO chatReqVO) {
public DifyChatResVO chat(DifyChatReqVO difyChatReqVO) {
DifyChatResVO difyChatResVO = null;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(difyUrl + METHOD_CHAT_MESSAGES);
httpPost.setHeader(HttpHeaders.AUTHORIZATION, difyAppAuth);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
log.info("发起对话:{}", chatReqVO);
StringEntity entity = new StringEntity(new JSONObject(chatReqVO).toString(), ContentType.APPLICATION_FORM_URLENCODED);
log.info("发起对话:{}", difyChatReqVO);
StringEntity entity = new StringEntity(com.alibaba.fastjson.JSONObject.toJSON(difyChatReqVO).toString(), StandardCharsets.UTF_8);
httpPost.setEntity(entity);
ChatResVO chatResVO1 = httpClient.execute(httpPost, response -> {
difyChatResVO = httpClient.execute(httpPost, response -> {
final int status = response.getCode();
if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
final HttpEntity responseEntity = response.getEntity();
try {
String responseStr = EntityUtils.toString(responseEntity);
log.info("responseStr:{}", responseStr);
return new ChatResVO();
log.info("发起对话成功!");
return new Gson().fromJson(responseStr, DifyChatResVO.class);
} catch (final ParseException ex) {
throw new ClientProtocolException(ex);
}
@ -73,10 +75,10 @@ public class DifyApiUtil {
throw new ClientProtocolException("Unexpected response status: " + status);
}
});
log.info("chatResVO1:{}", chatResVO1);
} catch (Exception e) {
log.error("create dataset error", e);
log.error("发起对话失败", e);
}
return difyChatResVO;
}
/**
@ -104,8 +106,7 @@ public class DifyApiUtil {
final HttpEntity responseEntity = response.getEntity();
try {
String responseStr = EntityUtils.toString(responseEntity);
Gson gson = new Gson();
DatasetResVO datasetResVO = gson.fromJson(responseStr, DatasetResVO.class);
DatasetResVO datasetResVO = new Gson().fromJson(responseStr, DatasetResVO.class);
log.info("创建知识库成功!ID:【{}】", datasetResVO.getId());
return datasetResVO.getId();
} catch (final ParseException ex) {
@ -225,18 +226,18 @@ public class DifyApiUtil {
}
}
public DocumentResult queryDocuments(String datasetId, int page, int size){
cn.hutool.http.HttpRequest request = HttpUtil.createGet(difyUrl+ METHOD_DATASET + "/" + datasetId + METHOD_DOCUMENTS);
request.auth(difyDatasetAuth).form("page",page,"limit",size);
public DocumentResult queryDocuments(String datasetId, int page, int size) {
cn.hutool.http.HttpRequest request = HttpUtil.createGet(difyUrl + METHOD_DATASET + "/" + datasetId + METHOD_DOCUMENTS);
request.auth(difyDatasetAuth).form("page", page, "limit", size);
try (cn.hutool.http.HttpResponse execute = request.execute()){
try (cn.hutool.http.HttpResponse execute = request.execute()) {
String body = execute.body();
if (200 != execute.getStatus()){
log.error("queryDocuments:请求知识库文件列表接口出错error:{}",body);
throw new RuntimeException("queryDocuments:请求知识库文件列表接口出错error:"+body);
if (200 != execute.getStatus()) {
log.error("queryDocuments:请求知识库文件列表接口出错error:{}", body);
throw new RuntimeException("queryDocuments:请求知识库文件列表接口出错error:" + body);
}
DocumentResult documentResult = new Gson().fromJson(body, DocumentResult.class);
if (CollUtil.isNotEmpty(documentResult.getData())){
if (CollUtil.isNotEmpty(documentResult.getData())) {
documentResult.getData().forEach(document -> document.setFileId(decodeDocumentName(document.getName()).getValue()));
}
return documentResult;
@ -244,12 +245,12 @@ public class DifyApiUtil {
}
public List<Document> queryDocuments(String datasetId){
public List<Document> queryDocuments(String datasetId) {
DocumentResult documentResult = queryDocuments(datasetId, 1, 100);
List<Document> documents = new ArrayList<>(documentResult.getData());
while (documentResult.isHas_more()){
documentResult = queryDocuments(datasetId, documentResult.getPage()+1, 100);
while (documentResult.isHas_more()) {
documentResult = queryDocuments(datasetId, documentResult.getPage() + 1, 100);
documents.addAll(documentResult.getData());
}
@ -259,20 +260,18 @@ public class DifyApiUtil {
/**
*
*
* @param fileName
* @param fileId id
* @param fileId id
* @return + "_" + ID +"." +
*/
public String generateDocumentName(String fileName, String fileId) {
String[] split = fileName.split("\\.");
List<String> nameTrunk = new ArrayList<>();
for (int i = 0; i < split.length-1; i++) {
nameTrunk.add(split[i]);
}
List<String> nameTrunk = new ArrayList<>(Arrays.asList(split).subList(0, split.length - 1));
String documentName = StrUtil.join(".", nameTrunk) + "_" + fileId;
if (split.length > 1) {
documentName = documentName + "." + split[split.length -1];
documentName = documentName + "." + split[split.length - 1];
}
return documentName;
}
@ -280,31 +279,26 @@ public class DifyApiUtil {
/**
*
*
* @param documentName
* @return keyvalueid
*/
public Pair<String,String> decodeDocumentName(String documentName){
public Pair<String, String> decodeDocumentName(String documentName) {
if (StrUtil.isEmpty(documentName)){
return Pair.of(null,null);
if (StrUtil.isEmpty(documentName)) {
return Pair.of(null, null);
}
String[] firstSplit = documentName.split("\\.");
List<String> nameTrunk = new ArrayList<>();
for (int i = 0; i < firstSplit.length -1; i++) {
nameTrunk.add(firstSplit[i]);
}
List<String> nameTrunk = new ArrayList<>(Arrays.asList(firstSplit).subList(0, firstSplit.length - 1));
String name = StrUtil.join(".", nameTrunk);
String[] secondSplit = name.split("_");
nameTrunk = new ArrayList<>();
for (int i = 0; i < secondSplit.length -1; i++) {
nameTrunk.add(secondSplit[i]);
}
nameTrunk = new ArrayList<>(Arrays.asList(secondSplit).subList(0, secondSplit.length - 1));
String completeName = StrUtil.join("_", nameTrunk) + "." + firstSplit[firstSplit.length-1];
String fileId = secondSplit[secondSplit.length-1];
String completeName = StrUtil.join("_", nameTrunk) + "." + firstSplit[firstSplit.length - 1];
String fileId = secondSplit[secondSplit.length - 1];
return Pair.of(completeName,fileId);
return Pair.of(completeName, fileId);
}
}

Loading…
Cancel
Save