|
@@ -1,15 +1,24 @@
|
|
|
package com.ruoyi.web.controller.moonshot;
|
|
|
|
|
|
|
|
|
+import cn.moonshot.platform.util.MoonshotAiUtils;
|
|
|
+import com.google.gson.Gson;
|
|
|
import com.ruoyi.common.config.RuoYiConfig;
|
|
|
import com.ruoyi.common.core.controller.BaseController;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.ruoyi.common.enums.FileType;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.common.utils.file.FileUploadUtils;
|
|
|
import com.ruoyi.common.utils.file.FileUtils;
|
|
|
import com.ruoyi.common.utils.moonshot.Client;
|
|
|
+import com.ruoyi.common.utils.moonshot.vo.ChatCompletionRequest;
|
|
|
+import com.ruoyi.common.utils.moonshot.vo.ChatCompletionStreamChoice;
|
|
|
import com.ruoyi.common.utils.moonshot.vo.FileUploadResult;
|
|
|
+import com.ruoyi.common.utils.uuid.IdUtils;
|
|
|
import com.ruoyi.framework.config.ServerConfig;
|
|
|
+import com.ruoyi.system.domain.resume.Resume;
|
|
|
+import com.ruoyi.system.service.resume.IResumeService;
|
|
|
import com.ruoyi.tool.domain.TUnifyFile;
|
|
|
import com.ruoyi.tool.service.ITUnifyFileService;
|
|
|
import com.ruoyi.web.controller.common.CommonController;
|
|
@@ -37,6 +46,9 @@ public class MoonshotFileController extends BaseController {
|
|
|
@Value("${moonshot.api_key}")
|
|
|
private String API_KEY;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IResumeService resumeService;
|
|
|
+
|
|
|
/**
|
|
|
* 通用上传请求(单个)
|
|
|
*/
|
|
@@ -44,6 +56,10 @@ public class MoonshotFileController extends BaseController {
|
|
|
public AjaxResult uploadFile(MultipartFile file) throws Exception
|
|
|
{
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
+ String corpId = "";
|
|
|
+ if (StringUtils.isNotEmpty(SecurityUtils.getLoginUser().getCorpid())) {
|
|
|
+ corpId = SecurityUtils.getLoginUser().getCorpid();
|
|
|
+ }
|
|
|
try
|
|
|
{
|
|
|
if (API_KEY == null) {
|
|
@@ -61,24 +77,72 @@ public class MoonshotFileController extends BaseController {
|
|
|
String fileName = FileUploadUtils.upload(filePath, file);
|
|
|
String url = serverConfig.getUrl() + fileName;
|
|
|
TUnifyFile tUnifyFile = new TUnifyFile();
|
|
|
+ tUnifyFile.setId(IdUtils.fastSimpleUUID());
|
|
|
tUnifyFile.setUploadPath(url);
|
|
|
tUnifyFile.setUploadName(fileName);
|
|
|
tUnifyFile.setNewUploadName(FileUtils.getName(fileName));
|
|
|
tUnifyFile.setUploadFormat(file.getOriginalFilename());
|
|
|
tUnifyFile.setUploadType(String.valueOf(FileType.RESUME.ordinal()));//简历附件
|
|
|
tUnifyFile.setCreateBy(getNickName());
|
|
|
+ tUnifyFile.setCorpId(corpId);
|
|
|
|
|
|
//moonshot AI 附件上传返回附件信息
|
|
|
if(null != resuat.getError()){
|
|
|
- ajax.put("message", resuat.getError().getMessage());
|
|
|
tUnifyFile.setStatus_details(resuat.getError().getMessage());
|
|
|
}else if(resuat.getStatus().equals("ok")) {
|
|
|
tUnifyFile.copyFrom(resuat,true);
|
|
|
+ /**
|
|
|
+ * 根据moonshot获取附件解析内容
|
|
|
+ */
|
|
|
+ if(StringUtils.isNotEmpty(tUnifyFile.getFileId())){
|
|
|
+ tUnifyFile.setContent(MoonshotAiUtils.getFileContent(tUnifyFile.getFileId(),client.getApiKey()));
|
|
|
+ //读取内容后删除AI存储附件
|
|
|
+ MoonshotAiUtils.deleteFile(tUnifyFile.getFileId(),client.getApiKey());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // todo 开始简历解析
|
|
|
+ if(StringUtils.isNotEmpty(tUnifyFile.getContent())){
|
|
|
+
|
|
|
+ StringBuilder str = new StringBuilder();
|
|
|
+
|
|
|
+ client.ChatCompletionStream(new ChatCompletionRequest(tUnifyFile.getContent())).subscribe(
|
|
|
+ streamResponse -> {
|
|
|
+ if (streamResponse.getChoices().isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (ChatCompletionStreamChoice choice : streamResponse.getChoices()) {
|
|
|
+ String finishReason = choice.getFinishReason();
|
|
|
+ if (finishReason != null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 将每个choice的delta内容添加到StringBuilder中
|
|
|
+ str.append(choice.getDelta().getContent());
|
|
|
+ }
|
|
|
+ },
|
|
|
+ error -> {
|
|
|
+ error.printStackTrace();
|
|
|
+ },
|
|
|
+ () -> {
|
|
|
+ System.out.println(str);
|
|
|
+ // todo 格式解析完成-存储简历内容整理并返回
|
|
|
+ if(isValidJson((str.toString()))){
|
|
|
+ Gson gson = new Gson();
|
|
|
+ Resume resume = gson.fromJson(str.toString(), Resume.class); // 使用 Gson 转换 JSON 字符串为 Resume 对象
|
|
|
+ resumeService.insertResume(resume);
|
|
|
+ tUnifyFile.setFileBusinessId(resume.getResumeId().toString());
|
|
|
+ }else{
|
|
|
+ // 返回值不是json字符串
|
|
|
+ ajax.put("error","解析失败,AI解析返回格式有误,请重试!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
tUnifyFileService.insertTUnifyFile(tUnifyFile);
|
|
|
|
|
|
- ajax.put("FileUploadResult", resuat);
|
|
|
+ //根据上传附件已经moonshot返回上传内容
|
|
|
+ ajax.put(tUnifyFile.getFileId(),tUnifyFile);
|
|
|
return ajax;
|
|
|
}
|
|
|
catch (Exception e)
|
|
@@ -88,4 +152,15 @@ public class MoonshotFileController extends BaseController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
+ public static boolean isValidJson(String test) {
|
|
|
+ try {
|
|
|
+ new Gson().fromJson(test, Resume.class);
|
|
|
+ return true;
|
|
|
+ } catch (com.google.gson.JsonSyntaxException ex) {
|
|
|
+ // 如果捕获到JsonSyntaxException异常,则字符串不是有效的JSON
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|