|
@@ -6,6 +6,7 @@ import com.storlead.framework.common.result.Result;
|
|
|
import com.storlead.knowledge.config.DifyProperties;
|
|
import com.storlead.knowledge.config.DifyProperties;
|
|
|
import com.storlead.knowledge.pojo.dto.ChunkDTO;
|
|
import com.storlead.knowledge.pojo.dto.ChunkDTO;
|
|
|
import com.storlead.knowledge.pojo.dto.QueryPageDTO;
|
|
import com.storlead.knowledge.pojo.dto.QueryPageDTO;
|
|
|
|
|
+import com.storlead.knowledge.pojo.vo.KnowledgeStatisticsVO;
|
|
|
import com.storlead.knowledge.utils.HttpService;
|
|
import com.storlead.knowledge.utils.HttpService;
|
|
|
import com.storlead.knowledge.utils.JacksonHolder;
|
|
import com.storlead.knowledge.utils.JacksonHolder;
|
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
@@ -13,6 +14,8 @@ import io.swagger.annotations.ApiOperation;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -93,8 +96,74 @@ public class ChunkController {
|
|
|
JacksonHolder.OBJECT_MAPPER.writeValueAsString(request),
|
|
JacksonHolder.OBJECT_MAPPER.writeValueAsString(request),
|
|
|
new TypeReference<>() {}
|
|
new TypeReference<>() {}
|
|
|
);
|
|
);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ @GetMapping("getAllChunks")
|
|
|
|
|
+ @ApiOperation("获取所有的文本块")
|
|
|
|
|
+ public Result<Object> getAllChunks() {
|
|
|
|
|
+ KnowledgeStatisticsVO statistics = new KnowledgeStatisticsVO();
|
|
|
|
|
+ // 1. 调用Dify API获取所有知识库列表(使用较大分页以获取全部数据)
|
|
|
|
|
+ String url = difyProperties.getBaseUrl() + "datasets";
|
|
|
|
|
+ Result<Object> datasetsResult = httpService.get(
|
|
|
|
|
+ url,
|
|
|
|
|
+ Map.of("page", 1, "limit", 1000),
|
|
|
|
|
+ "Bearer " + difyProperties.getDatasetApiKey(),
|
|
|
|
|
+ new TypeReference<>() {}
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ List<Object> results = new ArrayList<>();
|
|
|
|
|
+ if (datasetsResult.isSuccess()) {
|
|
|
|
|
+ Object responseData = datasetsResult.getResult();
|
|
|
|
|
+ if (responseData instanceof Map) {
|
|
|
|
|
+ Map<?, ?> responseMap = (Map<?, ?>) responseData;
|
|
|
|
|
+ // 遍历数据集,累加文档数和文本块数
|
|
|
|
|
+ Object dataObj = responseMap.get("data");
|
|
|
|
|
+ if (dataObj instanceof List<?> dataList) {
|
|
|
|
|
+ for (Object item : dataList) {
|
|
|
|
|
+ if (item instanceof Map<?, ?> datasetMap) {
|
|
|
|
|
+ String datasets_id = datasetMap.get("id").toString();
|
|
|
|
|
+ //获取知识库中文档的列表
|
|
|
|
|
+ String url1 = difyProperties.getBaseUrl() + "datasets/"+datasets_id+"/documents";
|
|
|
|
|
+ Result<Object> documents_list = httpService.get(
|
|
|
|
|
+ url1,
|
|
|
|
|
+ Map.of("page", 1, "limit", 10000),
|
|
|
|
|
+ "Bearer " + difyProperties.getDatasetApiKey(),
|
|
|
|
|
+ new TypeReference<>() {}
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if (documents_list.isSuccess()) {
|
|
|
|
|
+ Object responseData1 = documents_list.getResult();
|
|
|
|
|
+ Map<?, ?> responseMap2 = (Map<?, ?>) responseData1;
|
|
|
|
|
+ Object dataObj1 = responseMap2.get("data");
|
|
|
|
|
+ if (dataObj1 instanceof List<?> dataList1) {
|
|
|
|
|
+ for (Object item1 : dataList1) {
|
|
|
|
|
+ if (item1 instanceof Map<?, ?> datasetMap1) {
|
|
|
|
|
+ String document_id = datasetMap1.get("id").toString();
|
|
|
|
|
+ String url2 = difyProperties.getBaseUrl() + "datasets/" + datasets_id + "/documents/" + document_id+"/segments";
|
|
|
|
|
+ Result<Object> chunks_result = httpService.get(
|
|
|
|
|
+ url2,
|
|
|
|
|
+ Map.of("page", 1, "limit", 10000),
|
|
|
|
|
+ "Bearer " + difyProperties.getDatasetApiKey(),
|
|
|
|
|
+ new TypeReference<>() {
|
|
|
|
|
+ });
|
|
|
|
|
+ if (chunks_result.isSuccess()) {
|
|
|
|
|
+ Object responseData2 = chunks_result.getResult();
|
|
|
|
|
+ Map<?, ?> responseMap3 = (Map<?, ?>) responseData2;
|
|
|
|
|
+ Object dataObj2 = responseMap3.get("data");
|
|
|
|
|
+ if (dataObj2 instanceof List<?> dataList2) {
|
|
|
|
|
+ results.addAll(dataList2);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ return Result.ok(results);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|