调用大模型自动解析订单信息
This commit is contained in:
@@ -1,17 +1,28 @@
|
||||
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.LbDailyUserTradeReport;
|
||||
import com.rj.entity.LbPurchaseApply;
|
||||
import com.rj.mapper.LbDailyUserTradeReportMapper;
|
||||
import com.rj.mapper.LbPurchaseApplyMapper;
|
||||
import com.rj.service.ILbPurchaseApplyService;
|
||||
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.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -20,6 +31,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class LbPurchaseApplyServiceImpl
|
||||
extends ServiceImpl<LbPurchaseApplyMapper, LbPurchaseApply>
|
||||
@@ -28,6 +40,120 @@ public class LbPurchaseApplyServiceImpl
|
||||
@Autowired
|
||||
private LbDailyUserTradeReportMapper lbDailyUserTradeReportMapper;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Value("${langchain4j.open-ai.chat-model.model-name:qwen-plus}")
|
||||
private String dashScopeChatModel;
|
||||
|
||||
private static final String PARSE_SYSTEM_PROMPT = """
|
||||
你是信息抽取助手。用户会提供一段「进货申请」相关的自然语言订货信息。
|
||||
请只输出一个 JSON 对象,不要 markdown 代码块,不要解释性文字。
|
||||
字段均为可选;无法从原文推断的字段请省略或设为 null。
|
||||
字段名与含义(JSON 键名必须完全一致):
|
||||
applyUser 申请人姓名;
|
||||
applyPhone 申请人电话;
|
||||
tryDate 试用日期(原文怎么说就怎么填字符串,如 yyyy-MM-dd 或中文描述);
|
||||
age 年龄(整数);
|
||||
workExperience 从业经历;
|
||||
colleagueName 同事姓名;
|
||||
colleaguePhone 同事电话;
|
||||
teamLeader 团队长;
|
||||
applyDate 申请日期,格式 yyyy-MM-dd,若原文无明确日期可省略。
|
||||
不要输出 id、tenantId、createTime、updateTime。
|
||||
""";
|
||||
|
||||
@Override
|
||||
public Map<String, Object> parseFromOrderInfoByLlm(String orderInfo, String tenantId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (orderInfo == null || orderInfo.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(PARSE_SYSTEM_PROMPT)
|
||||
.build();
|
||||
Message userMsg = Message.builder()
|
||||
.role(Role.USER.getValue())
|
||||
.content("订货信息如下:\n" + orderInfo.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);
|
||||
LbPurchaseApply entity = objectMapper.readValue(json, LbPurchaseApply.class);
|
||||
entity.setTenantId(tenantId.trim());
|
||||
entity.setId(null);
|
||||
entity.setCreateTime(null);
|
||||
entity.setUpdateTime(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(LbPurchaseApply entity) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user