评估申请调研大模型解析申请信息

This commit is contained in:
2026-05-15 11:23:17 +08:00
parent 5958e8412d
commit 76858d26a9
5 changed files with 197 additions and 6 deletions

View File

@@ -1,15 +1,26 @@
package com.rj.service.impl;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rj.entity.LbAssessmentApply;
import com.rj.mapper.LbAssessmentApplyMapper;
import com.rj.service.ILbAssessmentApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@@ -20,6 +31,123 @@ public class LbAssessmentApplyServiceImpl
extends ServiceImpl<LbAssessmentApplyMapper, LbAssessmentApply>
implements ILbAssessmentApplyService {
@Autowired
private ObjectMapper objectMapper;
@Value("${langchain4j.open-ai.chat-model.model-name:qwen-plus}")
private String dashScopeChatModel;
private static final String ASSESSMENT_PARSE_SYSTEM_PROMPT = """
你是信息抽取助手。用户会提供一段与「申请评估」相关的自然语言描述。
请只输出一个 JSON 对象,不要 markdown 代码块,不要解释性文字。
字段均为可选;无法从原文推断的字段请省略或设为 null。
字段名与含义JSON 键名必须完全一致,与 Java 驼峰一致):
applicantName 申请人姓名;
applicantPhone 申请人电话;
applicantAge 年龄(整数);
workExperience 从业经历;
colleagueName 同事姓名;
colleaguePhone 同事电话;
teamLeaderName 团队长姓名;
assessmentTeacherName 评估老师姓名;
moderatorName 主持人姓名;
meetingNumber 会议号;
applicationDatetime 申请日期时间,优先 ISO-8601如 2026-05-15T14:30:00也可 yyyy-MM-dd HH:mm:ss若仅有日期可设为当天 00:00:00。
不要输出 id、tenantId、createdAt、updatedAt。
""";
@Override
public Map<String, Object> parseFromAssessmentTextByLlm(String assessmentText, String tenantId) {
Map<String, Object> result = new HashMap<>();
try {
if (assessmentText == null || assessmentText.trim().isEmpty()) {
result.put("success", false);
result.put("message", "申请评估文本不能为空");
return result;
}
if (tenantId == null || tenantId.trim().isEmpty()) {
result.put("success", false);
result.put("message", "tenantId不能为空");
return result;
}
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null || apiKey.isEmpty()) {
result.put("success", false);
result.put("message", "未配置环境变量 DASHSCOPE_API_KEY无法调用大模型");
return result;
}
Generation gen = new Generation();
Message systemMsg = Message.builder()
.role(Role.SYSTEM.getValue())
.content(ASSESSMENT_PARSE_SYSTEM_PROMPT)
.build();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("申请评估信息如下:\n" + assessmentText.trim())
.build();
GenerationParam param = GenerationParam.builder()
.apiKey(apiKey)
.model(dashScopeChatModel)
.messages(Arrays.asList(systemMsg, userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.build();
GenerationResult call = gen.call(param);
String raw = call.getOutput().getChoices().get(0).getMessage().getContent();
if (raw == null || raw.trim().isEmpty()) {
result.put("success", false);
result.put("message", "大模型返回内容为空");
return result;
}
String json = normalizeJson(raw);
LbAssessmentApply entity = objectMapper.readValue(json, LbAssessmentApply.class);
entity.setTenantId(tenantId.trim());
entity.setId(null);
entity.setCreatedAt(null);
entity.setUpdatedAt(null);
result.put("success", true);
result.put("message", "解析成功");
result.put("data", entity);
return result;
} catch (NoApiKeyException e) {
log.error("DashScope API Key 异常", e);
result.put("success", false);
result.put("message", "API密钥异常" + e.getMessage());
return result;
} catch (InputRequiredException e) {
log.error("DashScope 入参异常", e);
result.put("success", false);
result.put("message", "调用大模型入参错误:" + e.getMessage());
return result;
} catch (Exception e) {
log.error("申请评估信息大模型解析失败", e);
result.put("success", false);
result.put("message", "解析失败:" + e.getMessage());
return result;
}
}
private static String normalizeJson(String content) {
if (content == null) {
return "";
}
String trimmed = content.trim();
if (trimmed.startsWith("```")) {
int firstLineBreak = trimmed.indexOf('\n');
int lastFence = trimmed.lastIndexOf("```");
if (firstLineBreak >= 0 && lastFence > firstLineBreak) {
trimmed = trimmed.substring(firstLineBreak + 1, lastFence).trim();
}
}
return trimmed;
}
@Override
public Map<String, Object> add(LbAssessmentApply entity) {
Map<String, Object> result = new HashMap<>();
@@ -109,7 +237,10 @@ public class LbAssessmentApplyServiceImpl
String applicantName,
String applicantPhone,
String colleagueName,
String teamLeaderName) {
String teamLeaderName,
String assessmentTeacherName,
String moderatorName,
String meetingNumber) {
Map<String, Object> result = new HashMap<>();
try {
if (current == null || current < 1) {
@@ -135,6 +266,15 @@ public class LbAssessmentApplyServiceImpl
if (teamLeaderName != null && !teamLeaderName.trim().isEmpty()) {
queryWrapper.like(LbAssessmentApply::getTeamLeaderName, teamLeaderName.trim());
}
if (assessmentTeacherName != null && !assessmentTeacherName.trim().isEmpty()) {
queryWrapper.like(LbAssessmentApply::getAssessmentTeacherName, assessmentTeacherName.trim());
}
if (moderatorName != null && !moderatorName.trim().isEmpty()) {
queryWrapper.like(LbAssessmentApply::getModeratorName, moderatorName.trim());
}
if (meetingNumber != null && !meetingNumber.trim().isEmpty()) {
queryWrapper.like(LbAssessmentApply::getMeetingNumber, meetingNumber.trim());
}
queryWrapper.orderByDesc(LbAssessmentApply::getUpdatedAt)
.orderByDesc(LbAssessmentApply::getCreatedAt);