|
@@ -1,20 +1,23 @@
|
|
|
package com.bosshand.virgo.api.workark.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.bosshand.virgo.api.workark.dao.DifyDatasetDao;
|
|
|
import com.bosshand.virgo.api.workark.dao.DifyDatasetDocumentDao;
|
|
|
import com.bosshand.virgo.api.workark.model.DifyDataset;
|
|
|
import com.bosshand.virgo.api.workark.model.DifyDatasetDocument;
|
|
|
-import com.bosshand.virgo.api.workark.model.RetrieveDatasetDto;
|
|
|
import io.github.imfangs.dify.client.DifyClientFactory;
|
|
|
import io.github.imfangs.dify.client.DifyDatasetsClient;
|
|
|
import io.github.imfangs.dify.client.exception.DifyApiException;
|
|
|
import io.github.imfangs.dify.client.model.datasets.*;
|
|
|
+import okhttp3.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
@Service
|
|
|
public class DifyDatasetService {
|
|
@@ -45,9 +48,9 @@ public class DifyDatasetService {
|
|
|
getClient().deleteDataset(datasetId);
|
|
|
difyDatasetDao.delete(datasetId);
|
|
|
difyDatasetDocumentDao.deleteDatasetId(datasetId);
|
|
|
- System.out.println("删除测试知识库成功,ID: " + datasetId);
|
|
|
+ System.out.println("删除知识库成功,ID: " + datasetId);
|
|
|
} catch (Exception e) {
|
|
|
- System.err.println("删除测试知识库失败: " + e.getMessage());
|
|
|
+ System.err.println("删除知识库失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -55,7 +58,7 @@ public class DifyDatasetService {
|
|
|
/**
|
|
|
* 创建知识库
|
|
|
*/
|
|
|
- public void createDataset(DifyDataset difyDataset) {
|
|
|
+ public void createDataset(DifyDataset difyDataset) throws IOException, DifyApiException {
|
|
|
// 创建知识库请求
|
|
|
CreateDatasetRequest request = CreateDatasetRequest.builder()
|
|
|
.name(difyDataset.getName())
|
|
@@ -64,15 +67,9 @@ public class DifyDatasetService {
|
|
|
.build();
|
|
|
|
|
|
// 发送请求
|
|
|
- try {
|
|
|
- DatasetResponse response = getClient().createDataset(request);
|
|
|
- difyDataset.setDatasetId(response.getId());
|
|
|
- difyDatasetDao.save(difyDataset);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (DifyApiException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ DatasetResponse response = getClient().createDataset(request);
|
|
|
+ difyDataset.setDatasetId(response.getId());
|
|
|
+ difyDatasetDao.save(difyDataset);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -199,10 +196,12 @@ public class DifyDatasetService {
|
|
|
/**
|
|
|
* 获取文档列表
|
|
|
*/
|
|
|
- public List<DifyDatasetDocument> getDocument(String datasetId) {
|
|
|
- return difyDatasetDocumentDao.getDatasetId(datasetId);
|
|
|
+ public DocumentListResponse getDocument(String datasetId, int page, int limit) throws DifyApiException, IOException {
|
|
|
+ DocumentListResponse response = getClient().getDocuments(datasetId, null, page, limit);
|
|
|
+ return response;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
public RetrieveResponse retrieveDataset(String datasetId, RetrieveDatasetDto dto) {
|
|
|
|
|
|
String query = dto.getQuery();
|
|
@@ -224,6 +223,99 @@ public class DifyDatasetService {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+ */
|
|
|
+
|
|
|
+ private static final OkHttpClient client = new OkHttpClient();
|
|
|
+
|
|
|
+ public String uploadFile(String apiUrl, String apiKey, String datasetId, MultipartFile multipartFile) throws IOException {
|
|
|
+
|
|
|
+ String finalUrl = apiUrl.replace("{dataset_id}", datasetId);
|
|
|
+
|
|
|
+ // 构建请求体
|
|
|
+ RequestBody requestBody = new MultipartBody.Builder()
|
|
|
+ .setType(MultipartBody.FORM)
|
|
|
+ // 添加 JSON 数据部分
|
|
|
+ .addFormDataPart(
|
|
|
+ "data",
|
|
|
+ null,
|
|
|
+ RequestBody.create(
|
|
|
+ MediaType.parse("text/plain"),
|
|
|
+ "{\"indexing_technique\": \"economy\",\"process_rule\": {\"mode\": \"automatic\"}}"
|
|
|
+ )
|
|
|
+ )
|
|
|
+ // 添加文件部分
|
|
|
+ .addFormDataPart(
|
|
|
+ "file",
|
|
|
+ Objects.requireNonNull(multipartFile.getOriginalFilename()),
|
|
|
+ RequestBody.create(
|
|
|
+ MediaType.parse(Objects.requireNonNull(multipartFile.getContentType())),
|
|
|
+ multipartFile.getBytes()
|
|
|
+ )
|
|
|
+ )
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 构建请求
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(finalUrl)
|
|
|
+ .header("Authorization", "Bearer " + apiKey)
|
|
|
+ .post(requestBody)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 发送请求并获取响应
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ throw new IOException("请求失败: " + response.code() + " " + response.message());
|
|
|
+ }
|
|
|
+ return Objects.requireNonNull(response.body()).string();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void uploadFileToDataset(String datasetId, MultipartFile multipartFile) throws IOException {
|
|
|
+ String apiUrl = "http://203.110.233.149:80/v1/datasets/{dataset_id}/document/create-by-file";
|
|
|
+ String response = uploadFile(apiUrl, "dataset-SWjJp6FOFqT85n7KxxyCFPSS", datasetId, multipartFile);
|
|
|
+ DocumentResponse documentResponse = JSONObject.parseObject(response, DocumentResponse.class);
|
|
|
+ // 保存文档ID
|
|
|
+ String documentId = documentResponse.getDocument().getId();
|
|
|
+ DifyDatasetDocument difyDatasetDocument = new DifyDatasetDocument();
|
|
|
+ difyDatasetDocument.setDatasetId(datasetId);
|
|
|
+ difyDatasetDocument.setDocumentId(documentId);
|
|
|
+ difyDatasetDocument.setName(multipartFile.getOriginalFilename());
|
|
|
+ difyDatasetDocumentDao.save(difyDatasetDocument);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String retrieve(String apiUrl, String apiKey, String datasetId, String json) throws IOException {
|
|
|
+
|
|
|
+ String finalUrl = apiUrl.replace("{dataset_id}", datasetId);
|
|
|
+
|
|
|
+ // 构建请求体
|
|
|
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
|
|
|
+
|
|
|
+ // 构建请求
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(finalUrl)
|
|
|
+ .header("Authorization", "Bearer " + apiKey)
|
|
|
+ .post(body)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 发送请求并获取响应
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ throw new IOException("请求失败: " + response.code() + " " + response.message());
|
|
|
+ }
|
|
|
+ return Objects.requireNonNull(response.body()).string();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public RetrieveResponse retrieveDataset(String datasetId, JSONObject jsonobject) throws IOException {
|
|
|
+ String apiUrl = "http://203.110.233.149:80/v1/datasets/{dataset_id}/retrieve";
|
|
|
+ String json = JSONObject.toJSONString(jsonobject);
|
|
|
+ String response = retrieve(apiUrl, "dataset-SWjJp6FOFqT85n7KxxyCFPSS", datasetId, json);
|
|
|
+ RetrieveResponse retrieveResponse = JSONObject.parseObject(response, RetrieveResponse.class);
|
|
|
+ return retrieveResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
}
|