陈昶聿
8 小时以前 5db7759db3fa6792155d72139f9d5d9e56eb3436
【丽水】回写语言判断 增加AI识别
已修改2个文件
80 ■■■■ 文件已修改
smartor/src/main/java/com/smartor/common/DeepSeekApi.java 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java 46 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
smartor/src/main/java/com/smartor/common/DeepSeekApi.java
@@ -19,8 +19,9 @@
    public static void main(String[] args) {
        String questionText = "您好,请问您是患者本人还是家属?\n";
        String voiceText = "我自己";
        List<String> options = new ArrayList<>(Arrays.asList("本人","亲属","其他"));
        Integer resp = matchOptionIndex(questionText, voiceText, options);
//        List<String> options = new ArrayList<>(Arrays.asList("本人","亲属","其他"));
        String optionText = "亲属";
        Integer resp = matchQuestionAnswer(questionText, voiceText, optionText);
        System.out.println("DeepSeek返回结果:\n" + resp);
    }
@@ -120,6 +121,35 @@
    }
    /**
     * 判断语音文本最接近哪个选项,返回选项在列表中的下标。
     *
     * @param questionText 语音识别得到的文本
     * @param answerText   答案文本
     * @param optionText   选项文本
     * @return 命中选项的下标(从 0 开始);无法匹配任一选项时返回 {@code -1}
     */
    public static int matchQuestionAnswer(String questionText, String answerText, String optionText) {
        if (StringUtils.isBlank(questionText) || StringUtils.isBlank(answerText) || StringUtils.isBlank(optionText)) {
            return -1;
        }
        String systemPrompt = "\"你是一个专业的语音识别文本语义匹配助手。你的任务是判断用户的语音文本在语义上最符合的选项。用户会给出一段语音识别文本和选项文本,"
                + "请判断这段文本在语义上是否匹配选项, 匹配则输出 1,没有匹配则输出 0, 只允许输出1或者0,"
                + "不要做任何解释。直接输出最匹配选项的编号数字;若没有任何选项与文本相关,则输出 0。";
        String userPrompt = "请根据以下信息进行语义匹配判断:\n" +
                "- 问题文本:" + questionText + "\n\n"
                + "- 语言识别文本:" + answerText + "\n\n"
                + "- 选项文本:" + optionText + "\n\n"
                + "\n请只输出一个数字(匹配则输出 1,没有匹配则输出 0,只允许输出1或者0)。";
        String content = chatCompletion(systemPrompt, userPrompt);
        if (StringUtils.isBlank(content)) {
            return -1;
        }
        return extractFirstNumber(content);
    }
    /**
     * 从模型回复中提取第一个整数。模型偶尔会回复 “选项2” “2。” 之类,做一次兜底解析。
     */
    private static Integer extractFirstNumber(String text) {
smartor/src/main/java/com/smartor/service/impl/ServiceSubtaskServiceImpl.java
@@ -16,6 +16,7 @@
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.smartor.common.DeepSeekApi;
import com.smartor.common.FtpService;
import com.smartor.common.MtSubmitSmUtil;
import com.smartor.config.PhoneUtils;
@@ -1666,20 +1667,39 @@
                    if (StringUtils.isEmpty(phoneCallReqYQVO.getAsrtext())) {
                        continue;
                    }
                    //包含
                    Matcher matcher = null;
                    if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex())) {
                        Pattern pattern = Pattern.compile(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex());
                        matcher = pattern.matcher(phoneCallReqYQVO.getAsrtext());
                    //2-AI识别
                    SysConfig configVoiceMatchType = new SysConfig();
                    configVoiceMatchType.setOrgid(phoneCallReqYQVO.getOrgid());
                    configVoiceMatchType.setConfigKey("sys.voice.match.type");
                    SysConfig matchType = sysConfigMapper.selectConfig(configVoiceMatchType);
                    boolean matchedFlag = false;
                    if(ObjectUtils.isNotEmpty(matchType) && StringUtils.isNotEmpty(matchType.getConfigValue()) && matchType.getConfigValue().equals("2")){
                        int checkFlag = DeepSeekApi.matchQuestionAnswer(ivrTaskTemplateScriptVO.getScriptContent(),
                                phoneCallReqYQVO.getAsrtext(),
                                ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetvalue());
                        if(checkFlag == 1){
                            matchedFlag = true;
                        }
                    }else {
                        //包含
                        Matcher matcher = null;
                        if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex())) {
                            Pattern pattern = Pattern.compile(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex());
                            matcher = pattern.matcher(phoneCallReqYQVO.getAsrtext());
                        }
                        //不包含
                        Matcher matcher2 = null;
                        if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2())) {
                            Pattern pattern2 = Pattern.compile(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2());
                            matcher2 = pattern2.matcher(phoneCallReqYQVO.getAsrtext());
                        }
                        if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches() || StringUtils.isEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() || StringUtils.isEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches()) {
                            matchedFlag = true;
                        }
                    }
                    //不包含
                    Matcher matcher2 = null;
                    if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2())) {
                        Pattern pattern2 = Pattern.compile(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2());
                        matcher2 = pattern2.matcher(phoneCallReqYQVO.getAsrtext());
                    }
                    if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches() || StringUtils.isEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() || StringUtils.isEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches()) {
                        //说明匹配正确了
                    //说明匹配正确了
                    if(matchedFlag){
                        //这里应该先判断类型,去再修改,设置IsUserOperation是单选题的改法
                        log.info("匹配正确了吗--------------");
                        ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).setIsUserOperation(1);