1. 添加 Qanything api 接口

main
xueqingkun 11 months ago
parent 11c6d0b421
commit 80bba22a99

@ -1,8 +1,11 @@
package com.supervision.qanything;
import com.supervision.qanything.dto.ChatResult;
import com.supervision.qanything.dto.CreateKBResult;
import com.supervision.qanything.dto.ResultWrapper;
import com.supervision.qanything.dto.UploadResult;
import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.util.List;
@ -12,6 +15,10 @@ import java.util.List;
public interface QanythingService {
ResultWrapper<CreateKBResult> createKB(String kbName) throws NoSuchAlgorithmException;
ResultWrapper<String> deleteKB(String kbId) throws NoSuchAlgorithmException;
/**
*
* @param question
@ -20,4 +27,21 @@ public interface QanythingService {
* @throws NoSuchAlgorithmException
*/
ResultWrapper<ChatResult> chat(String question,List<String> kbIds) throws NoSuchAlgorithmException;
/**
*
* @param kbId ID
* @param file
* @return
* @throws NoSuchAlgorithmException
*/
ResultWrapper<List<UploadResult>> uploadDoc(String kbId, File file) throws NoSuchAlgorithmException;
ResultWrapper<List<UploadResult>> uploadUrl(String kbId, String url) throws NoSuchAlgorithmException;
ResultWrapper<List<String>> deleteFile(String kbId, List<String> fileIds) throws NoSuchAlgorithmException;
ResultWrapper<List<CreateKBResult>> kbList() throws NoSuchAlgorithmException;
ResultWrapper<List<UploadResult>> fileList(String kbId) throws NoSuchAlgorithmException;
}

@ -0,0 +1,44 @@
package com.supervision.qanything.constant;
public interface QanythingConstant {
/**
*
*/
String URL_CREATE_KB = "/q_anything/paas/create_kb";
/**
*
*/
String URL_DELETE_KB = "/q_anything/paas/delete_kb";
/**
*
*/
String URL_CHAT = "/q_anything/paas/chat";
/**
*
*/
String URL_UPLOAD_DOC = "/q_anything/paas/upload_file";
/**
* url
*/
String URL_UPLOAD_URL = "/q_anything/paas/upload_url";
/**
*
*/
String URL_DELETE_FILE = "/q_anything/paas/delete_file";
/**
*
*/
String URL_KB_LIST = "/q_anything/paas/kb_list";
/**
*
*/
String URL_FILE_LIST = "/q_anything/paas/file_list";
}

@ -2,6 +2,10 @@ package com.supervision.qanything.dto;
import lombok.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
public class BaseParam {
@ -32,7 +36,25 @@ public class BaseParam {
private String q;
private String url;
private List<String> fileIds;
private String[] img;
public Map<String,Object> toMap(){
Map<String, Object> map = new HashMap<>();
map.put("appKey",appKey);
map.put("curtime",curtime);
map.put("salt",salt);
map.put("sign",sign);
map.put("signType",signType);
map.put("q",q);
map.put("img",img);
return map;
}
}

@ -0,0 +1,17 @@
package com.supervision.qanything.dto;
import lombok.Data;
@Data
public class CreateKBResult {
/**
* id
*/
private String kbId;
/**
*
*/
private String kbName;
}

@ -6,7 +6,7 @@ import lombok.Data;
public class ResultWrapper<T> {
/**
*
* https://ai.youdao.com/DOCSIRMA/html/aigc/api/qanything/index.html 错误码
*/
private String errorCode;

@ -0,0 +1,23 @@
package com.supervision.qanything.dto;
import lombok.Data;
@Data
public class UploadResult {
/**
* id
*/
String fileId;
/**
*
*/
String fileName;
/**
* 0: 1:() 2: 3:
*/
String status;
}

@ -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);
}
}
}

@ -1,10 +1,13 @@
package com.supervision;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.supervision.qanything.QanythingService;
import com.supervision.qanything.dto.ChatResult;
import com.supervision.qanything.dto.CreateKBResult;
import com.supervision.qanything.dto.ResultWrapper;
import com.supervision.qanything.dto.UploadResult;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -12,7 +15,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ -28,9 +36,86 @@ public class YoudaoTest {
@Test
public void qanythingChatTest() throws NoSuchAlgorithmException {
/*ResultWrapper<ChatResult> chat = qanythingService.chat("怎样创建创建知识库", CollUtil.newArrayList("KB249d43fdb96f4e1399340b8f95e1baa0_240328"));
/* ResultWrapper<ChatResult> chat = qanythingService.chat("怎样创建创建知识库", CollUtil.newArrayList("KB249d43fdb96f4e1399340b8f95e1baa0_240328"));
System.out.println("返回结果:"+ JSONUtil.toJsonStr(chat));
System.out.println(chat.getResult().getResponse());*/
}
@Test
public void qanythingUploadDocTest() throws NoSuchAlgorithmException {
//
ResultWrapper<List<UploadResult>> resultWrapper = qanythingService.fileList("KB403a6543629648a3a74b60be5707398f_240328");
List<UploadResult> result = resultWrapper.getResult();
Set<String> fileNames = result.stream().map(UploadResult::getFileName).collect(Collectors.toSet());
FileUtil.walkFiles(new File("F:\\supervision\\doc\\深圳人社\\养老退休文档"),(f)->{
if (fileNames.contains(f.getName()) || f.getName().contains(".mp4")){
return;
}
try {
qanythingService.uploadDoc("KB403a6543629648a3a74b60be5707398f_240328",f);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
});
}
@Test
public void qanythingKbListTest() throws NoSuchAlgorithmException {
ResultWrapper<List<CreateKBResult>> listResultWrapper = qanythingService.kbList();
// {"errorCode":"0","msg":"SUCCESS","requestId":"a7281218-17eb-44dc-9817-d5deb812c16b","result":[{"kbId":"KB249d43fdb96f4e1399340b8f95e1baa0_240328","kbName":"测试知识库"}]}
System.out.println(listResultWrapper.getResult());
}
@Test
public void qanythingFileListTest() throws NoSuchAlgorithmException {
ResultWrapper<List<UploadResult>> resultWrapper = qanythingService.fileList("KB249d43fdb96f4e1399340b8f95e1baa0_240328");
// {"errorCode":"0","msg":"SUCCESS","requestId":"5f651ab1-43ca-4d04-af05-794cf55e4a5d","result":[{"fileId":"7c1af28235284934aabb1daeb16f4383","fileName":"qanthing.md","status":"1","url":"https://ai-virtual-person.nos-jd.163yun.com/6baa3e021848020ec8afc699decba96c?Signature=oUJTnEH6ZNl8QYiYRqkGj%2F9PeTGUzpxFJD%2FfTVm%2B2vs%3D&Expires=1714311144&NOSAccessKeyId=22d00336bf1e47b2a159c4ad33b9b762&download=qanthing.md"},{"fileId":"09eab2d3de084c3babbad7cf913c37a6","fileName":"aaa.md","status":"1","url":"https://ai-virtual-person.nos-jd.163yun.com/62dc3fb272e75eb9d0d659a0ff12845c?Signature=o6yPXcUu0xxWCkRXX32KGSXeVFKE02Ti5gKgBn2cVgM%3D&Expires=1714402794&NOSAccessKeyId=22d00336bf1e47b2a159c4ad33b9b762&download=aaa.md"}]}
System.out.println(resultWrapper.getResult());
}
@Test
public void qanythingUploadUrlTest() throws NoSuchAlgorithmException {
ResultWrapper<List<UploadResult>> resultWrapper = qanythingService.uploadUrl("KB249d43fdb96f4e1399340b8f95e1baa0_240328","https://blog.csdn.net/kangli_zhang/article/details/131209634");
// {"errorCode":"0","msg":"SUCCESS","requestId":"30665c3a-ecc9-44d6-8f85-dc7f38bf3753","result":{"fileId":"96ea740652394b3a991ca336f4a678b2","fileName":"https://blog.csdn.net/kangli_zhang/article/details/131209634","status":"0"}}
System.out.println(resultWrapper.getResult());
}
@Test
public void qanythingDeleteFileTest() throws NoSuchAlgorithmException {
ResultWrapper<List<String>> resultWrapper = qanythingService.deleteFile("KB249d43fdb96f4e1399340b8f95e1baa0_240328", CollUtil.newArrayList("96ea740652394b3a991ca336f4a678b2"));
// {"errorCode":"0","msg":"SUCCESS","requestId":"3ec1278d-7d55-463d-89a4-89123f8d1e77","result":null}
System.out.println(resultWrapper.getResult());
}
@Test
public void qanythingCreateKBTest() throws NoSuchAlgorithmException {
ResultWrapper<CreateKBResult> resultWrapper = qanythingService.createKB("养老退休知识库");
// {"errorCode":"0","msg":"SUCCESS","requestId":"a9ff5740-e644-4c8a-b6d8-64959de7a135","result":{"kbId":"KB32ac03789c464cba878a390e566b053b_240328","kbName":"测试库2"}}
System.out.println(resultWrapper.getResult());
}
//deleteKB
@Test
public void qanythingDeleteKBTest() throws NoSuchAlgorithmException {
ResultWrapper<String> resultWrapper = qanythingService.deleteKB("KB32ac03789c464cba878a390e566b053b_240328");
// {"errorCode":"0","msg":"SUCCESS","requestId":"6d1c37a6-f10f-438a-a372-57ab6aeabba0","result":null}
System.out.println(resultWrapper.getResult());
}
}

Loading…
Cancel
Save