|
@@ -0,0 +1,238 @@
|
|
|
+package com.jeeplus.modules.knowledgeSharing.dify;
|
|
|
+
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.entity.mime.FormBodyPartBuilder;
|
|
|
+import org.apache.http.entity.mime.HttpMultipartMode;
|
|
|
+import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
+import org.apache.http.entity.mime.content.FileBody;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 知识分享Controller
|
|
|
+ * @author 徐滕
|
|
|
+ * @create 2022-04-11 09:20
|
|
|
+ */
|
|
|
+public class DifyApiClient {
|
|
|
+
|
|
|
+ // Dify API 密钥和端点
|
|
|
+ private static final String API_KEY = "app-rWTbPVHyPDLqWOqqWX1DQ6EQ"; // 替换为你的 Dify API 密钥
|
|
|
+ private static final String API_URL = "http://localhost/v1/chat-messages"; // 替换为实际的 Dify API 端点
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ // 调用 Dify API
|
|
|
+ JSONObject result = selectKnowledgeBaseList();
|
|
|
+ System.out.println("API Response: " + result.toString(4)); // 格式化输出 JSON 响应
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 聊天窗查询
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JSONObject callDifyApi() throws Exception {
|
|
|
+ // 创建 HTTP 客户端
|
|
|
+ try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
+ // 创建 POST 请求
|
|
|
+ HttpPost httpPost = new HttpPost(API_URL);
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ httpPost.setHeader("Authorization", "Bearer " + API_KEY);
|
|
|
+
|
|
|
+ // 构建请求体(根据 Dify API 文档调整参数)
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
+ requestBody.put("inputs", new JSONObject().put("text", "Hello Dify!")); // 输入文本
|
|
|
+ requestBody.put("query", "新修订《审计法》背景下工程审计概述"); // 查询内容
|
|
|
+ requestBody.put("response_mode", "blocking"); // 响应模式
|
|
|
+ requestBody.put("conversation_id", ""); // 会话 ID(可选)
|
|
|
+ requestBody.put("user", "user-123"); // 用户标识(可选)
|
|
|
+
|
|
|
+ // 设置请求体
|
|
|
+ StringEntity entity = new StringEntity(requestBody.toString(), "UTF-8");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+
|
|
|
+ // 执行请求并获取响应
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+
|
|
|
+ if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ // 解析响应
|
|
|
+ String responseString = EntityUtils.toString(responseEntity);
|
|
|
+ return new JSONObject(responseString);
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("API request failed with status code: "
|
|
|
+ + response.getStatusLine().getStatusCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过文本创建文档
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JSONObject createByTextDifyApi() throws Exception {
|
|
|
+ // 创建 HTTP 客户端
|
|
|
+ try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
+ // 创建 POST 请求
|
|
|
+ HttpPost httpPost = new HttpPost("http://localhost/v1/datasets/e2f96c65-8b83-4305-a404-609304ad355e/document/create-by-text");
|
|
|
+
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ httpPost.setHeader("Authorization", "Bearer " + "dataset-rnKsBl9h5FLwajzFPGd38SEu");
|
|
|
+
|
|
|
+ // 请求体
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
+ requestBody.put("name", "text"); // 文档名称
|
|
|
+ requestBody.put("text", "其中有 3 个变电站为暂估价材料,暂估价材料单价平均为"); // 文档内容
|
|
|
+ requestBody.put("indexing_technique", "high_quality"); // 索引方式
|
|
|
+ requestBody.put("process_rule", new JSONObject().put("mode", "automatic")); // 处理规则
|
|
|
+
|
|
|
+ // 设置请求体
|
|
|
+ StringEntity entity = new StringEntity(requestBody.toString(), "UTF-8");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+
|
|
|
+ // 执行请求并获取响应
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+
|
|
|
+ if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ // 解析响应
|
|
|
+ String responseString = EntityUtils.toString(responseEntity);
|
|
|
+ System.out.println(responseString);
|
|
|
+ return new JSONObject(responseString);
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("API request failed with status code: "
|
|
|
+ + response.getStatusLine().getStatusCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过文件创建文档
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JSONObject createByFileDifyApi() throws Exception {
|
|
|
+ try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
+ HttpPost httpPost = new HttpPost("http://localhost/v1/datasets/e2f96c65-8b83-4305-a404-609304ad355e/document/create-by-file");
|
|
|
+
|
|
|
+ // 设置 Authorization 头
|
|
|
+ httpPost.setHeader("Authorization", "Bearer dataset-rnKsBl9h5FLwajzFPGd38SEu");
|
|
|
+
|
|
|
+ // 构建 JSON 参数
|
|
|
+ JSONObject dataJson = new JSONObject();
|
|
|
+ dataJson.put("indexing_technique", "high_quality");
|
|
|
+
|
|
|
+ JSONObject processRule = new JSONObject();
|
|
|
+ JSONObject rules = new JSONObject();
|
|
|
+ rules.put("pre_processing_rules", new JSONArray()
|
|
|
+ .put(new JSONObject().put("id", "remove_extra_spaces").put("enabled", true))
|
|
|
+ .put(new JSONObject().put("id", "remove_urls_emails").put("enabled", true)));
|
|
|
+ JSONObject segmentation = new JSONObject();
|
|
|
+ segmentation.put("separator", "###");
|
|
|
+ segmentation.put("max_tokens", 500);
|
|
|
+ rules.put("segmentation", segmentation);
|
|
|
+ processRule.put("rules", rules);
|
|
|
+ processRule.put("mode", "custom");
|
|
|
+
|
|
|
+ dataJson.put("process_rule", processRule);
|
|
|
+
|
|
|
+ // 中文文件路径
|
|
|
+ File file = new File("D:/项目导入模板.xlsx"); // 上传文件,支持中文
|
|
|
+
|
|
|
+ // 构建 Multipart 请求体
|
|
|
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
+ builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
|
|
+
|
|
|
+ // 添加 JSON 文本部分
|
|
|
+ builder.addTextBody("data", dataJson.toString(), ContentType.APPLICATION_JSON);
|
|
|
+
|
|
|
+ // 构建 FileBody
|
|
|
+ FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
|
|
|
+
|
|
|
+ // 构建支持 UTF-8 文件名的 part
|
|
|
+ FormBodyPartBuilder filePartBuilder = FormBodyPartBuilder.create()
|
|
|
+ .setName("file")
|
|
|
+ .setBody(fileBody);
|
|
|
+
|
|
|
+ // 添加 Content-Disposition 支持 filename* 编码
|
|
|
+ String encodedFileName = URLEncoder.encode(file.getName(), "UTF-8");
|
|
|
+ filePartBuilder.addField("Content-Disposition", "form-data; name=\"file\"; filename*=utf-8''" + encodedFileName);
|
|
|
+
|
|
|
+ // 添加文件部分
|
|
|
+ builder.addPart(filePartBuilder.build());
|
|
|
+
|
|
|
+ // 设置最终请求体
|
|
|
+ httpPost.setEntity(builder.build());
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ String responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
|
|
+
|
|
|
+ if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ System.out.println("上传成功: " + responseString);
|
|
|
+ return new JSONObject(responseString);
|
|
|
+ } else {
|
|
|
+ System.err.println("错误响应: " + responseString);
|
|
|
+ throw new RuntimeException("上传失败,状态码: " + response.getStatusLine().getStatusCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 知识库文档列表
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JSONObject selectKnowledgeBaseList() throws Exception {
|
|
|
+ // 创建 HTTP 客户端
|
|
|
+ try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
+ // 创建 GET 请求
|
|
|
+ HttpGet httpGet = new HttpGet("http://3081089em4.wicp.vip:20667/v1/datasets?page=1&limit=20");
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ httpGet.setHeader("Authorization", "Bearer dataset-TinelGDdnlrXJPyFe38iA0Zh");
|
|
|
+
|
|
|
+ // 执行请求并获取响应
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+
|
|
|
+ String responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
|
|
+ if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ System.out.println(responseString);
|
|
|
+ return new JSONObject(responseString);
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("API request failed with status code: "
|
|
|
+ + response.getStatusLine().getStatusCode() + "\nResponse: " + responseString);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|