|
|
|
@ -1,19 +1,20 @@
|
|
|
|
|
package com.supervision.qanything.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
|
import cn.hutool.core.lang.TypeReference;
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.supervision.qanything.QanythingService;
|
|
|
|
|
import com.supervision.qanything.dto.ChatParam;
|
|
|
|
|
import com.supervision.qanything.dto.ChatResult;
|
|
|
|
|
import com.supervision.qanything.dto.ResultWrapper;
|
|
|
|
|
import com.supervision.qanything.constant.QanythingConstant;
|
|
|
|
|
import com.supervision.qanything.dto.*;
|
|
|
|
|
import com.supervision.qanything.util.AuthV3Util;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
@ -30,8 +31,54 @@ public class QanythingServiceImpl implements QanythingService {
|
|
|
|
|
|
|
|
|
|
@Value("${youdao.qanthing.baseUrl}")
|
|
|
|
|
private String BASE_URL;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ResultWrapper<CreateKBResult> createKB(String kbName) throws NoSuchAlgorithmException {
|
|
|
|
|
|
|
|
|
|
Assert.notEmpty(kbName, "知识库名称不能为空");
|
|
|
|
|
|
|
|
|
|
ChatParam baseParam = new ChatParam();
|
|
|
|
|
baseParam.setQ(kbName);
|
|
|
|
|
// 添加鉴权相关参数
|
|
|
|
|
AuthV3Util.addAuthParams(APP_KEY, APP_SECRET, baseParam);
|
|
|
|
|
String paramString = JSONUtil.toJsonStr(baseParam);
|
|
|
|
|
log.info("createKB:请求参数:{}", paramString);
|
|
|
|
|
|
|
|
|
|
HttpRequest request = HttpRequest.post(BASE_URL + QanythingConstant.URL_CREATE_KB)
|
|
|
|
|
.body(paramString);
|
|
|
|
|
try (HttpResponse response = request.execute()){
|
|
|
|
|
String body = response.body();
|
|
|
|
|
log.info("createKB:响应结果:{}", body);
|
|
|
|
|
return JSONUtil.toBean(body, new TypeReference<ResultWrapper<CreateKBResult>>(){},true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ResultWrapper<String> deleteKB(String kbId) throws NoSuchAlgorithmException {
|
|
|
|
|
|
|
|
|
|
Assert.notEmpty(kbId, "kbId不能为空");
|
|
|
|
|
|
|
|
|
|
ChatParam baseParam = new ChatParam();
|
|
|
|
|
baseParam.setQ(kbId);
|
|
|
|
|
AuthV3Util.addAuthParams(APP_KEY, APP_SECRET, baseParam);
|
|
|
|
|
String paramString = JSONUtil.toJsonStr(baseParam);
|
|
|
|
|
log.info("deleteKB:请求参数:{}", paramString);
|
|
|
|
|
|
|
|
|
|
HttpRequest request = HttpRequest.post(BASE_URL + QanythingConstant.URL_DELETE_KB)
|
|
|
|
|
.body(paramString);
|
|
|
|
|
try (HttpResponse response = request.execute()){
|
|
|
|
|
String body = response.body();
|
|
|
|
|
log.info("deleteKB:响应结果:{}", body);
|
|
|
|
|
return JSONUtil.toBean(body, new TypeReference<ResultWrapper<String>>(){},true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ResultWrapper<ChatResult> chat(String question, List<String> kbIds) throws NoSuchAlgorithmException {
|
|
|
|
|
|
|
|
|
|
Assert.notEmpty(kbIds, "kbIds不能为空");
|
|
|
|
|
Assert.notEmpty(question, "问题不能为空");
|
|
|
|
|
|
|
|
|
|
log.info("chat:请求入参:question:{},kbIds:{}", question, kbIds);
|
|
|
|
|
ChatParam chatParam = new ChatParam();
|
|
|
|
|
chatParam.setQ(question);
|
|
|
|
@ -40,7 +87,7 @@ public class QanythingServiceImpl implements QanythingService {
|
|
|
|
|
AuthV3Util.addAuthParams(APP_KEY, APP_SECRET, chatParam);
|
|
|
|
|
String paramString = JSONUtil.toJsonStr(chatParam);
|
|
|
|
|
log.info("chat:请求参数:{}", paramString);
|
|
|
|
|
HttpRequest request = HttpRequest.post(BASE_URL + "/q_anything/paas/chat")
|
|
|
|
|
HttpRequest request = HttpRequest.post(BASE_URL + QanythingConstant.URL_CHAT)
|
|
|
|
|
.body(paramString);
|
|
|
|
|
try (HttpResponse response = request.execute()){
|
|
|
|
|
String body = response.body();
|
|
|
|
@ -49,5 +96,106 @@ public class QanythingServiceImpl implements QanythingService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ResultWrapper<List<UploadResult>> uploadDoc(String kbId, File file) throws NoSuchAlgorithmException {
|
|
|
|
|
Assert.notNull(file, "文件不能为空");
|
|
|
|
|
Assert.notEmpty(kbId, "kbId不能为空");
|
|
|
|
|
|
|
|
|
|
log.info("uploadDoc:请求入参:kbId:{} fileName:{}", kbId,file.getName());
|
|
|
|
|
BaseParam baseParam = new BaseParam();
|
|
|
|
|
baseParam.setQ(kbId);
|
|
|
|
|
AuthV3Util.addAuthParams(APP_KEY, APP_SECRET,baseParam);
|
|
|
|
|
log.info("uploadDoc:请求参数:{}", JSONUtil.toJsonStr(baseParam));
|
|
|
|
|
HttpRequest request = HttpRequest.post(BASE_URL + QanythingConstant.URL_UPLOAD_DOC).form(baseParam.toMap()).form("file", file);
|
|
|
|
|
|
|
|
|
|
try (HttpResponse execute = request.execute()) {
|
|
|
|
|
String body = execute.body();
|
|
|
|
|
log.info("uploadDoc:响应结果:{}", body);
|
|
|
|
|
return JSONUtil.toBean(body, new TypeReference<ResultWrapper<List<UploadResult>>>(){},true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ResultWrapper<List<UploadResult>> uploadUrl(String kbId, String url) throws NoSuchAlgorithmException {
|
|
|
|
|
|
|
|
|
|
Assert.notEmpty(url, "url不能为空");
|
|
|
|
|
Assert.notEmpty(kbId, "kbId不能为空");
|
|
|
|
|
|
|
|
|
|
log.info("uploadUrl:请求入参:kbId:{}", kbId);
|
|
|
|
|
BaseParam baseParam = new BaseParam();
|
|
|
|
|
baseParam.setQ(kbId);
|
|
|
|
|
baseParam.setUrl(url);
|
|
|
|
|
AuthV3Util.addAuthParams(APP_KEY, APP_SECRET,baseParam);
|
|
|
|
|
|
|
|
|
|
String paramString = JSONUtil.toJsonStr(baseParam);
|
|
|
|
|
log.info("uploadUrl:请求参数:{}", paramString);
|
|
|
|
|
HttpRequest request = HttpRequest.post(BASE_URL + QanythingConstant.URL_UPLOAD_URL)
|
|
|
|
|
.body(paramString);
|
|
|
|
|
try (HttpResponse response = request.execute()){
|
|
|
|
|
String body = response.body();
|
|
|
|
|
log.info("uploadUrl:响应结果:{}", body);
|
|
|
|
|
return JSONUtil.toBean(body, new TypeReference<ResultWrapper<List<UploadResult>>>(){},true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ResultWrapper<List<String>> deleteFile(String kbId,List<String> fileIds) throws NoSuchAlgorithmException {
|
|
|
|
|
|
|
|
|
|
Assert.notEmpty(fileIds, "fileIds不能为空");
|
|
|
|
|
Assert.notEmpty(kbId, "kbId不能为空");
|
|
|
|
|
|
|
|
|
|
BaseParam baseParam = new BaseParam();
|
|
|
|
|
baseParam.setQ(kbId);
|
|
|
|
|
baseParam.setFileIds(fileIds);
|
|
|
|
|
AuthV3Util.addAuthParams(APP_KEY, APP_SECRET, baseParam);
|
|
|
|
|
String paramString = JSONUtil.toJsonStr(baseParam);
|
|
|
|
|
log.info("deleteFile:请求参数:{}", paramString);
|
|
|
|
|
|
|
|
|
|
HttpRequest request = HttpRequest.post(BASE_URL + QanythingConstant.URL_DELETE_FILE)
|
|
|
|
|
.body(paramString);
|
|
|
|
|
try (HttpResponse response = request.execute()){
|
|
|
|
|
String body = response.body();
|
|
|
|
|
log.info("deleteFile:响应结果:{}", body);
|
|
|
|
|
return JSONUtil.toBean(body, new TypeReference<ResultWrapper<List<String>>>(){},true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ResultWrapper<List<CreateKBResult>> kbList() throws NoSuchAlgorithmException {
|
|
|
|
|
|
|
|
|
|
BaseParam baseParam = new BaseParam();
|
|
|
|
|
baseParam.setQ("");
|
|
|
|
|
AuthV3Util.addAuthParams(APP_KEY, APP_SECRET, baseParam);
|
|
|
|
|
String paramString = JSONUtil.toJsonStr(baseParam);
|
|
|
|
|
log.info("kbList:请求参数:{}", paramString);
|
|
|
|
|
HttpRequest request = HttpRequest.post(BASE_URL + QanythingConstant.URL_KB_LIST)
|
|
|
|
|
.body(paramString);
|
|
|
|
|
try (HttpResponse response = request.execute()){
|
|
|
|
|
String body = response.body();
|
|
|
|
|
log.info("kbList:响应结果:{}", body);
|
|
|
|
|
return JSONUtil.toBean(body, new TypeReference<ResultWrapper<List<CreateKBResult>>>(){},true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ResultWrapper<List<UploadResult>> fileList(String kbId) throws NoSuchAlgorithmException {
|
|
|
|
|
|
|
|
|
|
Assert.notEmpty(kbId, "kbId不能为空");
|
|
|
|
|
|
|
|
|
|
BaseParam baseParam = new BaseParam();
|
|
|
|
|
baseParam.setQ(kbId);
|
|
|
|
|
// 添加鉴权相关参数
|
|
|
|
|
AuthV3Util.addAuthParams(APP_KEY, APP_SECRET, baseParam);
|
|
|
|
|
String paramString = JSONUtil.toJsonStr(baseParam);
|
|
|
|
|
log.info("fileList:请求参数:{}", paramString);
|
|
|
|
|
HttpRequest request = HttpRequest.post(BASE_URL + QanythingConstant.URL_FILE_LIST)
|
|
|
|
|
.body(paramString);
|
|
|
|
|
try (HttpResponse response = request.execute()){
|
|
|
|
|
String body = response.body();
|
|
|
|
|
log.info("fileList:响应结果:{}", body);
|
|
|
|
|
return JSONUtil.toBean(body, new TypeReference<ResultWrapper<List<UploadResult>>>(){},true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|