模型调用

This commit is contained in:
wkc
2026-01-21 15:58:39 +08:00
parent c4a05e1338
commit 0d061155ed
25 changed files with 1134 additions and 55 deletions

View File

@@ -7,14 +7,15 @@ import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.loanratepricing.domain.RatePricingProperties;
import com.ruoyi.loanratepricing.domain.dto.ModelInvokeDTO;
import com.ruoyi.loanratepricing.domain.entity.ModelOutputFields;
import com.ruoyi.loanpricing.domain.dto.ModelInvokeDTO;
import com.ruoyi.loanpricing.domain.entity.LoanPricingWorkflow;
import org.springframework.beans.factory.annotation.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -24,28 +25,28 @@ import java.util.Objects;
**/
@Service
@Slf4j
@EnableAsync
public class ModelService {
@Resource
private RatePricingProperties ratePricingProperties;
@Value("${model.url}")
private String modelUrl;
@Resource
private RedisCache redisCache;
public ModelOutputFields invokeModel(ModelInvokeDTO modelInvokeDTO) {
public JSONObject invokeModel(ModelInvokeDTO modelInvokeDTO) {
Map<String, String> requestBody = entityToMap(modelInvokeDTO);
JSONObject response = HttpUtils.doPostFormUrlEncoded(ratePricingProperties.getModelUrl(), requestBody, null, JSONObject.class);
JSONObject response = HttpUtils.doPostFormUrlEncoded(modelUrl, requestBody, null, JSONObject.class);
log.info("------------------->调用模型返回结果:" + JSON.toJSONString(response));
if(Objects.nonNull(response) && response.containsKey("code") && response.getInteger("code") == 10000){
JSONObject mappingOutputFields = response.getJSONObject("data").getJSONObject("mappingOutputFields");
return JSON.parseObject(mappingOutputFields.toJSONString(), ModelOutputFields.class);
// return JSON.parseObject(mappingOutputFields.toJSONString(), ModelOutputFields.class);
return mappingOutputFields;
}else{
log.error("------------------->调用模型失败,失败原因为:" + response.getString("message"));
throw new ServiceException("调用模型失败");
}
}
/**
* 使用FastJSON将实体类转换为Map<String, String>
* @param obj 待转换的实体类对象
@@ -60,16 +61,6 @@ public class ModelService {
return JSON.parseObject(jsonStr, new TypeReference<Map<String, String>>() {});
}
private void replaceIndicatorToVariableName(JSONObject jsonObject) {
List<SysDictData> variableNameList = redisCache.getCacheObject("sys_dict:model_indicator_metric");
variableNameList.forEach(sysDictData -> {
if (jsonObject.containsKey(sysDictData.getDictLabel())) {
jsonObject.put(sysDictData.getDictValue(), jsonObject.get(sysDictData.getDictLabel()));
jsonObject.remove(sysDictData.getDictLabel());
}
});
}