You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
8.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package Tools.MaxKb.Service.Impl;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PropKit;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MaxKbImpl {
//MaxKB的url
static final String baseUrl = PropKit.get("baseUrl");
//获取身份识别码接口
static final String Authorization_URL = baseUrl + "/api/user/login";
/**
* 上传QA文件到知识库
*
* @param authCode 身份识别码
* @param uploadFile 上传文件路径
*/
public static String uploadQA(String authCode, String zskId, String uploadFile) {
//知识库url上传地址
String qaUrl = baseUrl + "/api/dataset/" + zskId + "/document/qa";
//使用hutool工具箱里面的httpPost功能向指定的接口发起上传文件的请求在Header中携带参数authCode
HttpResponse response = HttpRequest.post(qaUrl)
.header("Accept", "application/json, text/plain, */*")
.header("Accept-Encoding", "gzip,deflate")
.header("Accept-Language", "zh-CN,zh;q=0.9")
.header("Authorization", authCode)// 添加header
.header("Connection", "keep-alive")
.header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryhEiEDpZDka6byweA")
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
.form("file", new File(uploadFile)).execute();// 添加文件
// 获取响应内容
String result = response.body();
JSONObject jo = JSONObject.parseObject(result);
int code = jo.getInteger("code");
if (code != 200) {
System.out.println("请求失败");
return null;
}
return jo.getJSONArray("data").getJSONObject(0).getString("id");
}
/**
* 上传文档并分段
*
* @param authCode 验证码
* @param uploadFile 上传的文件
* @return
*/
public static JSONObject uploadDocumentSplit(String authCode, String uploadFile) {
//知识库url上传地址
String qaUrl = baseUrl + "/api/dataset/document/split";
//使用hutool工具箱里面的httpPost功能向指定的接口发起上传文件的请求在Header中携带参数authCode
HttpResponse response = HttpRequest.post(qaUrl)
.header("Accept", "application/json, text/plain, */*")
.header("Accept-Encoding", "gzip,deflate")
.header("Accept-Language", "zh-CN,zh;q=0.9")
.header("Authorization", authCode)// 添加header
.header("Connection", "keep-alive")
.header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryhEiEDpZDka6byweA")
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
.form("file", new File(uploadFile)).execute();// 添加文件
// 获取响应内容
String result = response.body();
JSONObject jo = JSONObject.parseObject(result);
int code = jo.getInteger("code");
if (code != 200) {
System.out.println("请求失败");
return null;
}
return jo;
}
/**
* 发送请求获取识别码
*
* @throws Exception
*/
public static String getAuthorization() {
//管理员账号与密码
String username = PropKit.get("MaxKBUsername");
String password = PropKit.get("MaxKBPassword");
JSONObject requestBody = new JSONObject();
requestBody.put("username", username);
requestBody.put("password", password);
HttpResponse response = HttpRequest.post(Authorization_URL)
.header("Content-Type", "application/json")
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36")
.header("Origin", baseUrl)
.header("Referer", baseUrl + "/ui/login")
.body(requestBody.toString())
.timeout(10000) // 可选:设置超时时间(毫秒)
.execute();
//System.out.println(response);
// 详细的状态处理
if (response.isOk()) {
String body = response.body();
if (StrUtil.isBlank(body)) {
throw new RuntimeException("响应内容为空");
}
try {
JSONObject responseJson = JSONObject.parseObject(body);
String data = responseJson.getString("data");
if (StrUtil.isBlank(data)) {
throw new RuntimeException("未获取到data字段");
}
return data;
} catch (Exception e) {
throw new RuntimeException("解析响应JSON失败: " + e.getMessage());
}
} else {
String errorBody = response.body();
throw new RuntimeException(String.format("请求失败 [%d]: %s",
response.getStatus(),
StrUtil.isBlank(errorBody) ? "无错误信息" : errorBody));
}
}
/**
* 删除知识库文档
*
* @param authCode 身份识别码
* @param zskId 知识库id
* @param documentId 文档id
* @return
*/
public static com.alibaba.fastjson.JSONObject delDocument(String authCode, String zskId, String documentId) {
String url = baseUrl + "/api/dataset/" + zskId + "/document/" + documentId + "?dep=[object+Object]&__v_isRef=true&__v_isShallow=false&_rawValue=false&_value=false";
HttpResponse response = HttpRequest.delete(url)
.header("Accept", "application/json, text/plain, */*")
.header("Accept-Encoding", "gzip,deflate")
.header("Accept-Language", "zh-CN,zh;q=0.9")
.header("Authorization", authCode)// 添加header
.header("Connection", "keep-alive")
.header("Content-Type", "application/json")
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
.execute();
return com.alibaba.fastjson.JSONObject.parseObject(response.body());
}
/**
* 生成问题
*
* @param authCode 身份识别码
* @param zskId 知识库id
* @param modelId 模型id
* @param documentId 文档id
* @return
*/
public static com.alibaba.fastjson.JSONObject generateQuestion(String authCode, String zskId, String modelId, String documentId) {
String url = baseUrl + "/api/dataset/" + zskId + "/document/batch_generate_related";
com.alibaba.fastjson.JSONObject jo = new com.alibaba.fastjson.JSONObject();
jo.put("model_id", modelId);
jo.put("prompt", "内容:{data}\\n\\n请总结上面的内容并根据内容总结生成 5 个问题。\\n回答要求\\n- 请只输出问题;\\n- 请将每个问题放置<question></question>标签中。");
List<String> list = new ArrayList<>();
list.add(documentId);
jo.put("document_id_list", list);
HttpResponse response = HttpRequest.put(url)
.header("Accept", "application/json, text/plain, */*")
.header("Accept-Encoding", "gzip,deflate")
.header("Accept-Language", "zh-CN,zh;q=0.9")
.header("Authorization", authCode)// 添加header
.header("Connection", "keep-alive")
.header("Content-Type", "application/json")
.body(jo.toJSONString()).execute();
return com.alibaba.fastjson.JSONObject.parseObject(response.body());
}
/**
* 向知识库批量上传文档
*
* @param authCode 授权码
* @param zskId 知识库ID
* @param body 分段后的文字内容
* @return
*/
public static String _bach(String authCode, String zskId, String body) {
String url = baseUrl + "/api/dataset/" + zskId + "/document/_bach";
HttpResponse response = HttpRequest.post(url)
.header("Accept", "application/json, text/plain, */*")
.header("Accept-Encoding", "gzip,deflate")
.header("Accept-Language", "zh-CN,zh;q=0.9")
.header("Authorization", authCode)// 添加header
.header("Connection", "keep-alive")
.header("Content-Type", "application/json")
.body(body).execute();
return response.body();
}
}