1. qanyting 集成本地接口调用
parent
c54ea4571b
commit
5250456282
@ -0,0 +1,35 @@
|
||||
package com.supervision.qanything.dto.offline;
|
||||
|
||||
import com.supervision.qanything.dto.HistoryDTO;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class ChatParam {
|
||||
|
||||
private String userId;
|
||||
|
||||
private List<String> kbIds;
|
||||
|
||||
private List<HistoryDTO> history;
|
||||
|
||||
private String question;
|
||||
|
||||
private boolean streaming;
|
||||
|
||||
private String productSource;
|
||||
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("user_id", userId);
|
||||
map.put("kb_ids", kbIds);
|
||||
map.put("history", history);
|
||||
map.put("question", question);
|
||||
map.put("streaming", streaming);
|
||||
map.put("product_source", productSource);
|
||||
return map;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.supervision.qanything.dto.offline;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.supervision.qanything.dto.HistoryDTO;
|
||||
import com.supervision.qanything.dto.ResultWrapper;
|
||||
import com.supervision.qanything.dto.SourceDTO;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Data
|
||||
public class ChatResult {
|
||||
private String code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private String question;
|
||||
|
||||
private String response;
|
||||
|
||||
private List<List<String>> history;
|
||||
|
||||
private List<SourceDocument> source_documents;
|
||||
|
||||
public ResultWrapper<com.supervision.qanything.dto.ChatResult> toResultWrapper() {
|
||||
ResultWrapper<com.supervision.qanything.dto.ChatResult> resultWrapper = new ResultWrapper<>();
|
||||
resultWrapper.setMsg("200".equals(code)?"0":"500");
|
||||
resultWrapper.setMsg(msg);
|
||||
com.supervision.qanything.dto.ChatResult chatResult = new com.supervision.qanything.dto.ChatResult();
|
||||
chatResult.setQuestion(question);
|
||||
if (StrUtil.isNotEmpty(response)){
|
||||
String jsonString = StrUtil.removePrefix(response, "data: ");
|
||||
String answer = JSONUtil.parseObj(jsonString).getStr("answer");
|
||||
chatResult.setResponse(answer);
|
||||
}
|
||||
|
||||
// setHistory
|
||||
if (CollUtil.isNotEmpty(history)){
|
||||
List<HistoryDTO> historyDTOS = history.stream().map(item -> {
|
||||
if (CollUtil.isNotEmpty(item)) {
|
||||
HistoryDTO historyDTO = new HistoryDTO();
|
||||
historyDTO.setQuestion(item.get(0));
|
||||
if (item.size() > 1) {
|
||||
historyDTO.setResponse(item.get(1));
|
||||
}
|
||||
return historyDTO;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
chatResult.setHistory(historyDTOS);
|
||||
}
|
||||
|
||||
// setSource
|
||||
if (CollUtil.isNotEmpty(source_documents)){
|
||||
List<SourceDTO> sourceDTOS = this.source_documents.stream().map(SourceDocument::toSourceDTO).collect(Collectors.toList());
|
||||
chatResult.setSource(sourceDTOS);
|
||||
}
|
||||
|
||||
resultWrapper.setResult(chatResult);
|
||||
return resultWrapper;
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.supervision.qanything.dto.offline;
|
||||
|
||||
import com.supervision.qanything.dto.SourceDTO;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SourceDocument {
|
||||
private String file_id;
|
||||
|
||||
private String file_name;
|
||||
|
||||
private String content;
|
||||
|
||||
private String retrieval_query;
|
||||
|
||||
private String file_path;
|
||||
|
||||
private String score;
|
||||
|
||||
private String embed_version;
|
||||
|
||||
public SourceDTO toSourceDTO() {
|
||||
SourceDTO sourceDTO = new SourceDTO();
|
||||
sourceDTO.setFileId(this.file_id);
|
||||
sourceDTO.setFileName(this.file_name);
|
||||
sourceDTO.setContent(this.content);
|
||||
sourceDTO.setScore(this.score);
|
||||
return sourceDTO;
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
package com.supervision.qanything.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
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.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(name = "youdao.qanthing.online", havingValue = "false")
|
||||
public class OfflineQanythingServiceImpl implements QanythingService {
|
||||
|
||||
@Value("${youdao.qanthing.baseUrl}")
|
||||
private String BASE_URL;
|
||||
|
||||
@Value("${youdao.qanthing.default_user_id:zzp}")
|
||||
private String defaultUserId;
|
||||
@Override
|
||||
public ResultWrapper<CreateKBResult> createKB(String kbName) throws NoSuchAlgorithmException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultWrapper<String> deleteKB(String kbId) throws NoSuchAlgorithmException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultWrapper<ChatResult> chat(String question, List<String> kbIds) {
|
||||
|
||||
kbIds = CollUtil.newArrayList("KB90b7c7ca27d543eab714e746c5f6f1f0");
|
||||
Assert.notEmpty(kbIds, "kbIds不能为空");
|
||||
Assert.notEmpty(question, "问题不能为空");
|
||||
|
||||
com.supervision.qanything.dto.offline.ChatParam chatParam = new com.supervision.qanything.dto.offline.ChatParam();
|
||||
chatParam.setQuestion(question);
|
||||
chatParam.setKbIds(kbIds);
|
||||
chatParam.setUserId(defaultUserId);
|
||||
String paramString = JSONUtil.toJsonStr(chatParam.toMap());
|
||||
log.info("chat:请求参数:{}", paramString);
|
||||
HttpRequest request = HttpRequest.post(BASE_URL + "/api/local_doc_qa/local_doc_chat")
|
||||
.body(paramString);
|
||||
try (HttpResponse response = request.execute()){
|
||||
String body = response.body();
|
||||
log.info("chat:响应结果:{}", body);
|
||||
com.supervision.qanything.dto.offline.ChatResult offLineChatResult = JSONUtil.toBean(body, com.supervision.qanything.dto.offline.ChatResult.class);
|
||||
return offLineChatResult.toResultWrapper();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultWrapper<ChatResult> chat(String question, List<HistoryDTO> history, List<String> kbIds) throws NoSuchAlgorithmException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void chatStream(HttpServletResponse response, String question, List<String> kbIds) throws NoSuchAlgorithmException, IOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultWrapper<List<UploadResult>> uploadDoc(String kbId, File file) throws NoSuchAlgorithmException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultWrapper<List<UploadResult>> uploadDoc(String kbId, String fileName, byte[] bytes) throws NoSuchAlgorithmException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultWrapper<List<UploadResult>> uploadUrl(String kbId, String url) throws NoSuchAlgorithmException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultWrapper<List<String>> deleteFile(String kbId, List<String> fileIds) throws NoSuchAlgorithmException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultWrapper<List<CreateKBResult>> kbList() throws NoSuchAlgorithmException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultWrapper<List<UploadResult>> fileList(String kbId) throws NoSuchAlgorithmException {
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue