|
|
@@ -9,19 +9,24 @@ import com.takai.bigmodel.mapper.*;
|
|
|
import com.takai.bigmodel.service.IBigModelService;
|
|
|
import com.takai.common.config.BigModelConfig;
|
|
|
import com.takai.common.core.redis.RedisCache;
|
|
|
+import com.takai.common.enums.BackgroundColor;
|
|
|
+import com.takai.common.enums.IconImg;
|
|
|
import com.takai.common.utils.uuid.IdUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
|
import okhttp3.sse.EventSource;
|
|
|
import okhttp3.sse.EventSourceListener;
|
|
|
import okhttp3.sse.EventSources;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
import javax.validation.constraints.NotNull;
|
|
|
+import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
@@ -63,6 +68,9 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
@Autowired
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private QuestionMapper questionMapper;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public List<BmMediaReplacement> selectMediaList(BmMediaReplacement mData) {
|
|
|
@@ -429,6 +437,8 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
.model(json.getString("model"))
|
|
|
.iconColor(json.getString("icon_color"))
|
|
|
.iconType(json.getString("icon_type"))
|
|
|
+ .paramDesc(json.getString("param_desc"))
|
|
|
+ .maxToken(json.getInteger("max_token"))
|
|
|
.build();
|
|
|
applicationList.add(bmApplication);
|
|
|
}
|
|
|
@@ -716,7 +726,7 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
}
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ log.info("获取切片信息失败", e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -788,7 +798,7 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
}
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ log.info("异步提示词获取失败", e);
|
|
|
}
|
|
|
return resultList;
|
|
|
}
|
|
|
@@ -945,6 +955,598 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
return dialogMapper.selectDialogExport(id);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String createKnowledge(KnowledgeParams knowledge) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getKnowledge();
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("embedding_id",knowledge.getEmbeddingId());
|
|
|
+ json.put("name", knowledge.getName());
|
|
|
+ json.put("description", knowledge.getDescription());
|
|
|
+ RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , json.toJSONString());
|
|
|
+
|
|
|
+ Request request = buildPostRequest(url,requestBody);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ String id = null;
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ String body = response.body().string();
|
|
|
+ boolean status = isJsonObject(body);
|
|
|
+ if(status){
|
|
|
+ JSONObject jsonObj = JSON.parseObject(body);
|
|
|
+ if(jsonObj != null){
|
|
|
+ JSONObject dataObj = jsonObj.getJSONObject("data");
|
|
|
+ if(dataObj != null){
|
|
|
+ id = String.valueOf(dataObj.get("id"));
|
|
|
+ BmKnowledge bm = BmKnowledge.builder()
|
|
|
+ .knowledgeId(id).build();
|
|
|
+ BmKnowledge vo = bmKnowledgeMapper.selectTargetKnowledge(bm);
|
|
|
+ if(vo == null){
|
|
|
+ //保存知识库信息
|
|
|
+ BmKnowledge bmKnowledge = BmKnowledge.builder()
|
|
|
+ .knowledgeId(id)
|
|
|
+ .embeddingId(knowledge.getEmbeddingId())
|
|
|
+ .name(knowledge.getName())
|
|
|
+ .description(knowledge.getDescription())
|
|
|
+ .icon(knowledge.getIcon() == null ? IconImg.QUESTION.getValue() : knowledge.getIcon())
|
|
|
+ .background(knowledge.getBackground() == null ? BackgroundColor.BLUE.getValue() : knowledge.getBackground()).build();
|
|
|
+ bmKnowledgeMapper.insertKnowledge(bmKnowledge);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateKnowledge(KnowledgeParams knowledge, String knowledgeId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getKnowledge() + "/" + knowledgeId;
|
|
|
+ JSONObject object = JSONObject.from(knowledge);
|
|
|
+ RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , object.toJSONString());
|
|
|
+
|
|
|
+ Request request = buildPutRequest(url,requestBody);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ BmKnowledge bm = BmKnowledge.builder()
|
|
|
+ .knowledgeId(knowledgeId).build();
|
|
|
+ BmKnowledge vo = bmKnowledgeMapper.selectTargetKnowledge(bm);
|
|
|
+ if(vo != null){
|
|
|
+ //保存知识库信息
|
|
|
+ BmKnowledge bmKnowledge = BmKnowledge.builder()
|
|
|
+ .knowledgeId(knowledgeId)
|
|
|
+ .embeddingId(knowledge.getEmbeddingId())
|
|
|
+ .name(knowledge.getName())
|
|
|
+ .description(knowledge.getDescription())
|
|
|
+ .icon(knowledge.getIcon())
|
|
|
+ .background(knowledge.getBackground())
|
|
|
+ .build();
|
|
|
+ bmKnowledgeMapper.updateKnowledge(bmKnowledge);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> detailKnowledge(String knowledgeId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getKnowledge() + "/" + knowledgeId;
|
|
|
+ Map<String, Object> map = new HashMap();
|
|
|
+ Request request = buildGetRequest(url);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ String data = response.body().string();
|
|
|
+ boolean status = isJsonObject(data);
|
|
|
+ if(status){
|
|
|
+ JSONObject obj = JSON.parseObject(data);
|
|
|
+ if(obj != null){
|
|
|
+
|
|
|
+ JSONObject dataObject = obj.getJSONObject("data");
|
|
|
+ String code = String.valueOf(obj.get("code"));
|
|
|
+ String message = String.valueOf(obj.get("message"));
|
|
|
+ String timestamp = String.valueOf(obj.get("timestamp"));
|
|
|
+ map.put("detail", dataObject);
|
|
|
+ map.put("code", code);
|
|
|
+ map.put("message", message);
|
|
|
+ map.put("timestamp", timestamp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ };
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> delKnowledge(String knowledgeId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getKnowledge() + "/" + knowledgeId;
|
|
|
+ Map<String, Object> map = new HashMap();
|
|
|
+ Request request = buildDeleteRequest(url);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ String data = response.body().string();
|
|
|
+ boolean status = isJsonObject(data);
|
|
|
+ if(status){
|
|
|
+ JSONObject obj = JSON.parseObject(data);
|
|
|
+ if(obj != null){
|
|
|
+ String code = String.valueOf(obj.get("code"));
|
|
|
+ String message = String.valueOf(obj.get("message"));
|
|
|
+ String timestamp = String.valueOf(obj.get("timestamp"));
|
|
|
+ map.put("code", code);
|
|
|
+ map.put("message", message);
|
|
|
+ map.put("timestamp", timestamp);
|
|
|
+ if("200".equals(code)){
|
|
|
+ //删除
|
|
|
+ bmKnowledgeMapper.delKnowledge(knowledgeId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ };
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject uploadDocument(MultipartFile[] file, DocumentParams documentParams, String knowledgeId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getDocument() + "/upload_document/" + knowledgeId;
|
|
|
+
|
|
|
+ MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
|
|
|
+ //MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
|
|
|
+ for (int i = 0; i < file.length; i++) {
|
|
|
+
|
|
|
+ File f = new File(file[i].getOriginalFilename());
|
|
|
+ try {
|
|
|
+ FileUtils.writeByteArrayToFile(f, file[i].getBytes());
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ String type = getMimeType(f);
|
|
|
+ // 为每个文件创建RequestBody
|
|
|
+ RequestBody requestBody = FormBody.create(MediaType.parse(type) , f);
|
|
|
+ // 添加文件到MultipartBodyBuilder
|
|
|
+ builder.addFormDataPart("files", f.getName(), requestBody);
|
|
|
+ }
|
|
|
+ MultipartBody body = builder.build();
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ Request request = buildPostFileRequest(url, body);
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ String result = response.body().string();
|
|
|
+ if(isJsonObject(result)){
|
|
|
+ JSONObject jsonObj = JSON.parseObject(result);
|
|
|
+ if(jsonObj != null){
|
|
|
+ JSONObject dataObj = jsonObj.getJSONObject("data");
|
|
|
+ if(dataObj != null){
|
|
|
+ JSONArray array = dataObj.getJSONArray("successInfos");
|
|
|
+ if(array != null){
|
|
|
+ for(int i=0; i<array.size(); i++){
|
|
|
+ JSONObject obj = array.getJSONObject(i);
|
|
|
+ String documentId = String.valueOf(obj.get("documentId"));
|
|
|
+ String fileName = String.valueOf(obj.get("fileName"));
|
|
|
+ String urlStr = String.valueOf(obj.get("url"));
|
|
|
+
|
|
|
+ BmDocument bm = BmDocument.builder()
|
|
|
+ .documentId(documentId).build();
|
|
|
+ BmDocument vo = bmDocumentMapper.selectTargetDocument(bm);
|
|
|
+ if(vo == null){
|
|
|
+ //保存知识信息
|
|
|
+ BmDocument res = BmDocument.builder()
|
|
|
+ .documentId(documentId)
|
|
|
+ .knowledgeId(knowledgeId)
|
|
|
+ .name(fileName)
|
|
|
+ .url(urlStr)
|
|
|
+ .customSeparator(String.format("[\"%s\"", "\\n")+ "]")
|
|
|
+ .sentenceSize("300")
|
|
|
+ .build();
|
|
|
+ bmDocumentMapper.insertDocument(res);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("code", jsonObj.getJSONObject("code"));
|
|
|
+ json.put("message", jsonObj.getJSONObject("message"));
|
|
|
+ json.put("timestamp", jsonObj.getJSONObject("timestamp"));
|
|
|
+ return json;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject updateDocument(DocumentParams documentParams, String documentId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getDocument() + "/" + documentId;
|
|
|
+ JSONObject object = JSONObject.from(documentParams);
|
|
|
+ RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , object.toJSONString());
|
|
|
+
|
|
|
+ Request request = buildPutRequest(url, requestBody);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ JSONObject obj = JSON.parseObject(response.body().string());
|
|
|
+ String code = String.valueOf(obj.get("code"));
|
|
|
+ if("200".equals(code)){
|
|
|
+ //更新知识数据
|
|
|
+ BmDocument vo = BmDocument.builder().documentId(documentId).build();
|
|
|
+ BmDocument result = bmDocumentMapper.selectTargetDocument(vo);
|
|
|
+ if(result != null){
|
|
|
+ BmDocument bm = BmDocument.builder()
|
|
|
+ .documentId(documentId)
|
|
|
+ .sentenceSize(String.valueOf(documentParams.getSentence_size()))
|
|
|
+ .knowledgeType(documentParams.getKnowledge_type())
|
|
|
+ .wordNum(documentParams.getWord_num_limit() == null ? null : Integer.valueOf(documentParams.getWord_num_limit()))
|
|
|
+ .build();
|
|
|
+ bmDocumentMapper.updateDocument(bm);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("更新知识失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONArray documentList(DocumentParams documentParams) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getDocument();
|
|
|
+ if(com.takai.common.utils.StringUtils.isNotEmpty(documentParams.getKnowledge_id())){
|
|
|
+ url += "?knowledge_id=" + documentParams.getKnowledge_id();
|
|
|
+ }
|
|
|
+ if (documentParams.getPage() >= 1) {
|
|
|
+ url += "&page=" + documentParams.getPage();
|
|
|
+ }
|
|
|
+ if (documentParams.getSize() >= 10) {
|
|
|
+ url += "&size=" + documentParams.getSize();
|
|
|
+ }
|
|
|
+ Request request = buildGetRequest(url);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ JSONObject obj = JSON.parseObject(response.body().string());
|
|
|
+ JSONObject dataObj = obj.getJSONObject("data");
|
|
|
+ JSONArray array = dataObj.getJSONArray("list");
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("获取知识列表失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject documentDetail(String documentId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getDocument() + "/" + documentId;
|
|
|
+
|
|
|
+ Request request = buildGetRequest(url);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ JSONObject obj = JSON.parseObject(response.body().string());
|
|
|
+ JSONObject dataObje = obj.getJSONObject("data");
|
|
|
+ return dataObje;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("获取知识详情失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject delDocument(String documentId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getDocument() + "/" + documentId;
|
|
|
+
|
|
|
+ Request request = buildDeleteRequest(url);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ JSONObject obj = JSON.parseObject(response.body().string());
|
|
|
+ Map<String, Object> map = new HashMap();
|
|
|
+ if("200".equals(String.valueOf(obj.get("code")))){
|
|
|
+ bmDocumentMapper.delDocument(documentId);
|
|
|
+ }
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("删除知识失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void uploadUrl(DocumentObject object) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getDocument() + "/upload_url";
|
|
|
+ JSONObject jsonObject = JSONObject.from(object);
|
|
|
+ RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , jsonObject.toJSONString());
|
|
|
+ Request request = buildPostRequest(url, requestBody);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ JSONObject obj = JSON.parseObject(response.body().string());
|
|
|
+ JSONObject dataObj = obj.getJSONObject("data");
|
|
|
+ JSONArray array = dataObj.getJSONArray("successInfos");
|
|
|
+ if(array != null){
|
|
|
+ for(int i=0; i<array.size(); i++){
|
|
|
+ JSONObject aObj = array.getJSONObject(i);
|
|
|
+ String documentId = String.valueOf(aObj.get("documentId"));
|
|
|
+ String urlStr = String.valueOf(aObj.get("url"));
|
|
|
+
|
|
|
+ BmDocument bm = BmDocument.builder()
|
|
|
+ .documentId(documentId).build();
|
|
|
+ BmDocument vo = bmDocumentMapper.selectTargetDocument(bm);
|
|
|
+ if(vo != null) {
|
|
|
+ //保存知识信息
|
|
|
+ BmDocument res = BmDocument.builder()
|
|
|
+ .documentId(documentId)
|
|
|
+ .url(urlStr)
|
|
|
+ .build();
|
|
|
+ bmDocumentMapper.updateDocument(res);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("上传url构建知识失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject documentEmbedding(DocumentParams params, String documentId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getDocument() + "/embedding/" + documentId;
|
|
|
+ JSONObject jsonObject = JSONObject.from(params);
|
|
|
+ RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , jsonObject.toJSONString());
|
|
|
+ Request request = buildPostRequest(url, requestBody);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ JSONObject object = JSON.parseObject(response.body().string());
|
|
|
+ if(object != null){
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("重新向量化失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> documentSliceImageList(String documentId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getDocument() + "/slice/image_list/" + documentId;
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , jsonObject.toJSONString());
|
|
|
+ Request request = buildPostRequest(url, requestBody);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ JSONObject object = JSON.parseObject(response.body().string());
|
|
|
+ JSONObject dataObj = object.getJSONObject("data");
|
|
|
+ JSONArray array = dataObj.getJSONArray("images");
|
|
|
+ if(array != null){
|
|
|
+ BmMediaReplacement condition = BmMediaReplacement.builder().documentId(documentId).build();
|
|
|
+ List<BmMediaReplacement> dbList = bmMediaReplacementMapper.selectMediaList(condition);
|
|
|
+ for(int i=0; i<array.size(); i++){
|
|
|
+ JSONObject aObj = array.getJSONObject(i);
|
|
|
+ Optional<BmMediaReplacement> optObj = dbList.stream().filter(a -> a.getOriginText().equals(String.valueOf(aObj.get("text")))).findFirst();
|
|
|
+ if (!optObj.isPresent()) {
|
|
|
+ BmMediaReplacement image = BmMediaReplacement
|
|
|
+ .builder()
|
|
|
+ .documentId(documentId)
|
|
|
+ .originText(String.valueOf(aObj.get("text")))
|
|
|
+ .mediaUrl(String.valueOf(aObj.get("cos_url")))
|
|
|
+ .build();
|
|
|
+ bmMediaReplacementMapper.insertMedia(image);
|
|
|
+ } else if(!optObj.get().getOriginText().equals(String.valueOf(aObj.get("text")))
|
|
|
+ || !optObj.get().getMediaUrl().equals(String.valueOf(aObj.get("cos_url")))) {
|
|
|
+ BmMediaReplacement updateObj = BmMediaReplacement
|
|
|
+ .builder()
|
|
|
+ .documentId(documentId)
|
|
|
+ .originText(String.valueOf(aObj.get("text")))
|
|
|
+ .mediaUrl(String.valueOf(aObj.get("cos_url")))
|
|
|
+ .build();
|
|
|
+ bmMediaReplacementMapper.updateMedia(updateObj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Map<String, Object> map = new HashMap();
|
|
|
+ map.put("code", object.get("code"));
|
|
|
+ map.put("message", object.get("message"));
|
|
|
+ map.put("timestamp", object.get("timestamp"));
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("获取文件解析到的图片列表失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject createApplication(ApplicationParams params) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getApplication();
|
|
|
+ JSONObject jsonObject = JSONObject.from(params);
|
|
|
+ RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , jsonObject.toJSONString());
|
|
|
+ Request request = buildPostRequest(url, requestBody);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ String result = response.body().string();
|
|
|
+ JSONObject object = JSON.parseObject(result);
|
|
|
+ if(object != null){
|
|
|
+ String code = String.valueOf(object.get("code"));
|
|
|
+ String appId = String.valueOf(object.get("data"));
|
|
|
+ if("200".equals(code)){
|
|
|
+ //调用列表接口将应用数据写入数据库
|
|
|
+ getValidAppList();
|
|
|
+ //预设问题写入数据库
|
|
|
+ List<QuestionInfo> list = params.getQuestionList();
|
|
|
+ if(!list.isEmpty() && list.size() > 0){
|
|
|
+ for(QuestionInfo vo : list){
|
|
|
+ String uuid = IdUtils.simpleUUID();
|
|
|
+ Question question = Question.builder().id(uuid).question(vo.getQuestion()).appId(appId).build();
|
|
|
+ questionMapper.insertQuestion(question);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("应用创建失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject updateApplication(ApplicationParams params, String appId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getApplication() + "/" + appId;
|
|
|
+ JSONObject json = JSONObject.from(params);
|
|
|
+ RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , json.toJSONString());
|
|
|
+ Request request = buildPutRequest(url, requestBody);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ String result = response.body().string();
|
|
|
+ JSONObject object = JSON.parseObject(result);
|
|
|
+ if(object != null){
|
|
|
+ String code = String.valueOf(object.get("code"));
|
|
|
+ if("200".equals(code)){
|
|
|
+ //调用列表接口将应用数据写入数据库
|
|
|
+ getValidAppList();
|
|
|
+ //预设问题写入数据库
|
|
|
+ List<QuestionInfo> list = params.getQuestionList();
|
|
|
+ if(!list.isEmpty() && list.size() > 0){
|
|
|
+ for(QuestionInfo vo : list){
|
|
|
+ //删除预设问题
|
|
|
+ if(com.takai.common.utils.StringUtils.isNotEmpty(vo.getDelFlag()) && "1".equals(vo.getDelFlag())){
|
|
|
+ questionMapper.delQuestion(vo.getId());
|
|
|
+ }else{
|
|
|
+ if(com.takai.common.utils.StringUtils.isNotEmpty(vo.getId())){
|
|
|
+ // 更新预设问题
|
|
|
+ Question question = Question.builder().question(vo.getQuestion()).build();
|
|
|
+ questionMapper.updateQuestion(question);
|
|
|
+ }else{
|
|
|
+ // 新增预设问题
|
|
|
+ String uuid = IdUtils.simpleUUID();
|
|
|
+ Question question = Question.builder().id(uuid).question(vo.getQuestion()).appId(appId).build();
|
|
|
+ questionMapper.insertQuestion(question);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("更新应用失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject getApplicationList(Integer page, Integer size) {
|
|
|
+ String url = null;
|
|
|
+ if(page != null && size != null){
|
|
|
+ url = bigModelConfig.getBaseurl() + bigModelConfig.getApplication() + "?page=" + page +"&size=" + size;
|
|
|
+ }else{
|
|
|
+ url = bigModelConfig.getBaseurl() + bigModelConfig.getApplication();
|
|
|
+ }
|
|
|
+ Request request = buildGetRequest(url);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ String result = response.body().string();
|
|
|
+ JSONObject object = JSON.parseObject(result);
|
|
|
+ if(object != null){
|
|
|
+ return object.getJSONObject("data");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("获取应用列表失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject selectApplication(String appId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getApplication() + "/" + appId;
|
|
|
+ Request request = buildGetRequest(url);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ String result = response.body().string();
|
|
|
+ JSONObject object = JSON.parseObject(result);
|
|
|
+ if(object != null){
|
|
|
+ String code = String.valueOf(object.get("code"));
|
|
|
+ if("200".equals(code)){
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ List<Question> list = questionMapper.getQuestionList(appId);
|
|
|
+ obj.put("detail", object.getJSONObject("data"));
|
|
|
+ obj.put("questionlist", list);
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("获取应用详情失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JSONObject delApplication(String appId) {
|
|
|
+ String url = bigModelConfig.getBaseurl() + bigModelConfig.getApplication() + "/" + appId;
|
|
|
+ Request request = buildDeleteRequest(url);
|
|
|
+ OkHttpClient client = buildOkHttpClient();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if(response.isSuccessful()){
|
|
|
+ String result = response.body().string();
|
|
|
+ JSONObject object = JSON.parseObject(result);
|
|
|
+ String code = String.valueOf(object.get("code"));
|
|
|
+ if("200".equals(code)){
|
|
|
+ //getValidAppList();
|
|
|
+ bmApplicationMapper.delApplication(appId);
|
|
|
+ questionMapper.delQuestionByAppId(appId);
|
|
|
+ }
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("获取应用详情失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
private Request buildGetRequest(String url) {
|
|
|
return new Request.Builder()
|
|
|
.addHeader("accept", "*/*")
|
|
|
@@ -961,6 +1563,14 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
.url(url).post(requestBody).build();
|
|
|
}
|
|
|
|
|
|
+ private Request buildPostFileRequest(String url,RequestBody requestBody) {
|
|
|
+ return new Request.Builder()
|
|
|
+ .addHeader("accept", "*/*")
|
|
|
+ .addHeader("Authorization", bigModelConfig.getBigModelApiKey())
|
|
|
+ .addHeader("Content-Type", "multipart/form-data")
|
|
|
+ .url(url).post(requestBody).build();
|
|
|
+ }
|
|
|
+
|
|
|
private OkHttpClient buildOkHttpClient() {
|
|
|
return new OkHttpClient.Builder()
|
|
|
.connectTimeout(10, TimeUnit.SECONDS)
|
|
|
@@ -969,6 +1579,21 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
+ private Request buildPutRequest(String url, RequestBody requestBody){
|
|
|
+ return new Request.Builder()
|
|
|
+ .addHeader("accept", "*/*")
|
|
|
+ .addHeader("Authorization", bigModelConfig.getBigModelApiKey())
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .url(url).put(requestBody).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Request buildDeleteRequest(String url){
|
|
|
+ return new Request.Builder()
|
|
|
+ .addHeader("accept", "*/*")
|
|
|
+ .addHeader("Authorization", bigModelConfig.getBigModelApiKey())
|
|
|
+ .url(url).delete().build();
|
|
|
+ }
|
|
|
+
|
|
|
private boolean isJsonObject(String data){
|
|
|
try {
|
|
|
JSON.parseObject(data);
|
|
|
@@ -989,4 +1614,21 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
return l;
|
|
|
}
|
|
|
|
|
|
+ private String getMimeType(File file){
|
|
|
+ if(!file.exists()){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String fileName = file.getName().toLowerCase();
|
|
|
+ if(fileName.endsWith(".pdf")){
|
|
|
+ return "application/json";
|
|
|
+ }else if (fileName.endsWith(".doc") || fileName.endsWith(".docx")) {
|
|
|
+ return "application/msword";
|
|
|
+ }else if (fileName.endsWith(".txt")) {
|
|
|
+ return "text/plain";
|
|
|
+ }else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|