|
|
@@ -2,6 +2,7 @@ package com.inspur.device.service.impl;
|
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
@@ -45,7 +46,8 @@ public class SmsbDifyDatasetsServiceImpl implements ISmsbDifyDatasetsService {
|
|
|
@Value("${dify.datasets.apiKey}")
|
|
|
private String datasetsApiKey;
|
|
|
|
|
|
- private final static String API_DATASETS_LIST = "/v1/datasets";
|
|
|
+ /** 知识库API */
|
|
|
+ private final static String API_DATASETS_COMMON = "/v1/datasets";
|
|
|
|
|
|
|
|
|
private final SmsbDifyDatasetsMapper baseMapper;
|
|
|
@@ -58,7 +60,7 @@ public class SmsbDifyDatasetsServiceImpl implements ISmsbDifyDatasetsService {
|
|
|
*/
|
|
|
@Override
|
|
|
public boolean syncFromDify() {
|
|
|
- String requestUrl = difyUrl + API_DATASETS_LIST + "?page=1&limit=100";
|
|
|
+ String requestUrl = difyUrl + API_DATASETS_COMMON + "?page=1&limit=100";
|
|
|
HttpRequest request = HttpRequest.get(requestUrl)
|
|
|
.header("Authorization", "Bearer " + datasetsApiKey);
|
|
|
HttpResponse response = request.execute();
|
|
|
@@ -171,11 +173,27 @@ public class SmsbDifyDatasetsServiceImpl implements ISmsbDifyDatasetsService {
|
|
|
@Override
|
|
|
public Boolean insertByBo(SmsbDifyDatasetsBo bo) {
|
|
|
SmsbDifyDatasets add = MapstructUtils.convert(bo, SmsbDifyDatasets.class);
|
|
|
- boolean flag = baseMapper.insert(add) > 0;
|
|
|
- if (flag) {
|
|
|
- bo.setId(add.getId());
|
|
|
+ // 1、 发送请求,请求创建知识库
|
|
|
+ cn.hutool.json.JSONObject data = JSONUtil.createObj()
|
|
|
+ .set("name", add.getName())
|
|
|
+ .set("permission", "only_me")
|
|
|
+ .set("description",add.getDescription())
|
|
|
+ .set("indexing_technique","high_quality")
|
|
|
+ .set("provider","vendor");
|
|
|
+ String requestUrl = difyUrl + API_DATASETS_COMMON;
|
|
|
+ HttpRequest request = HttpRequest.post(requestUrl)
|
|
|
+ .header("Authorization", "Bearer " + datasetsApiKey)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .body(data.toString());
|
|
|
+
|
|
|
+ HttpResponse response = request.execute();
|
|
|
+ // 2 接口请求失败
|
|
|
+ if (!response.isOk()) {
|
|
|
+ return false;
|
|
|
}
|
|
|
- return flag;
|
|
|
+ // 3 接口请求成功 重新同步知识库列表
|
|
|
+ this.syncFromDify();
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|