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

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

Loading…
Cancel
Save