RunRealTestMain.java 13.1 KB
//package com.glsk.llm;
//
//import com.alibaba.dashscope.exception.InputRequiredException;
//import com.alibaba.dashscope.exception.NoApiKeyException;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.glsk.conf.ConfigurationManager;
//import com.glsk.connection.MysqlConn;
//import com.glsk.llm.impl.LLMToolFactory;
//import com.glsk.llm.impl.LLMTools;
//import com.glsk.llm.model.LLMRequest;
//import org.apache.poi.xssf.usermodel.XSSFCell;
//import org.apache.poi.xssf.usermodel.XSSFRow;
//import org.apache.poi.xssf.usermodel.XSSFSheet;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//
//import java.io.*;
//import java.sql.Connection;
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * 跑线上测试用例
// */
//public class RunRealTestMain {
//    public static void main(String[] args) throws IOException, NoApiKeyException, InputRequiredException {
//        long bgnTime = System.currentTimeMillis();
//
//        // 配置提示词版本及模型类型(1-智普 2-千问 3-豆包 4-百度)
//        String version = "(250513-qwen-plus)";
//        Integer modeType = 2;
////        String modeName = "qwen2.5-72b-instruct";
////        String modeName = "qwen2-72b-instruct";
//        String modeName = "qwen-plus";
////        String modeName = "qwen-plus-0806";
//
//        // data_new/data_tianwu
//        String dataType = "data_tianwu";
////        String dataType = "data_new";
//
//        Connection myConn = MysqlConn.getConnection(ConfigurationManager.getProperty("mysql.url"));
//        Map<String, String> nameMap = getNameMap();
//
//        // ruleCode -> rule
//        String query = "select rule_code,sub_rule,rule_score,main_rule_code from llm_rule where status = 1;";
//        ArrayList<ArrayList<String>> ery = MysqlConn.executeQuery(myConn, query, 4);
//
//        Map<String, Double> codeScoreMap = new HashMap<>();
//        Map<String, String> nameCodeMap = new HashMap<>();
//        Map<String, String> codeMainCodeMap = new HashMap<>();
//
//        Map<String, Double> mainCodeScoreMap = new HashMap<>();
//
//        for (ArrayList<String> e : ery) {
//            String[] codes = e.get(0).split("\\;");
//            for (String code : codes) {
//                codeScoreMap.put(code, Double.valueOf(e.get(2)));
//                codeMainCodeMap.put(code,e.get(3));
//            }
//
//            nameCodeMap.put(e.get(1), e.get(3));
//            mainCodeScoreMap.put(e.get(3), Double.valueOf(e.get(2)));
//        }
//
//
//        // 获取system prompt
////        String sql = "select system_prompt,user_prompt from llm_prompt where type_id = 5 and status = 1";
//        String sql = "select system_prompt,user_prompt from llm_prompt where type_id = 1 and status = 1";
//        ArrayList<ArrayList<String>> executeQuery = MysqlConn.executeQuery(myConn, sql, 2);
//
//        String systemPrompt = executeQuery.get(0).get(0);
////        String userPrompt = executeQuery.get(0).get(1).replaceAll("\r", "");
//        String userPrompt = executeQuery.get(0).get(1);
//
//        // 获取用例
//        List<String> contentList = new ArrayList<>();
////        InputStream in = new FileInputStream("F:\\AI技术研究\\话术质检模型测试用例.xlsx");
//        InputStream in = new FileInputStream("F:\\AI技术研究\\用例及提示词优化记录\\话术质检模型测试用例合集.xlsx");
//        XSSFWorkbook sheets = new XSSFWorkbook(in);
////        String sheetName = "实际话术样本预期结果汇总";
//        String sheetName = "实际话术样本预期结果汇总【0505规则版】";
//
//        XSSFSheet sheet = sheets.getSheet(sheetName);
//        int rows = sheet.getPhysicalNumberOfRows();
//
//        BufferedWriter totalWriter = new BufferedWriter(new FileWriter(new File("return_real" + File.separator + "总览" + version)));
//
//
//        BufferedWriter writer = new BufferedWriter(new FileWriter(new File("return_real" + File.separator + "用例分析" + version + ".csv")));
////        List<String> codeList = codeScoreMap.keySet().stream().sorted().collect(Collectors.toList());
//        List<String> codeList = codeMainCodeMap.values().stream().distinct().sorted().collect(Collectors.toList());
//
//        String header = "用例,";
//        for (String code : codeList) {
//            header += code+"_art," + code+"_llm," + code+"_diff,";
//        }
//        writer.write(header+"成交/战败情况\n");
//        writer.flush();
//
//        List<String> exampleDetails = new ArrayList<>();
////        List<Integer> idList = new ArrayList<>();
////        idList.add(11);
////        idList.add(15);
////        idList.add(19);
////        idList.add(20);
////        idList.add(22);
////        idList.add(23);
////        idList.add(30);
////        idList.add(31);
////        idList.add(32);
////        idList.add(33);
//
////        for (int i : idList) {
//        for (int i = 11; i < 41; i++) {
////        for (int i = 1; i < rows; i++) {
////            int i = 24;
//
//
//            XSSFRow row = sheet.getRow(i);
//
//            XSSFCell cell = row.getCell(0);
//            String caseNo  = cell.getStringCellValue();
//            System.out.println("开始用例" + caseNo + "....");
//
////            if (1  == 1) continue;
//
//
//            // dataNo多少
//            cell = row.getCell(1);
//            String dataNo = cell.getStringCellValue();
//
//            BufferedReader reader = new BufferedReader(new FileReader(new File(dataType+ File.separator + dataNo + "_" + caseNo + ".txt")));
////            BufferedReader reader = new BufferedReader(new FileReader(new File("data_new" + File.separator + dataNo + "_" + caseNo + ".txt")));
//            StringBuffer buffer = new StringBuffer();
//            String line = null;
//            while ((line = reader.readLine()) != null) {
//                buffer.append(line).append("\n");
//            }
//
//            // 成交或战败
//            cell = row.getCell(2);
//            String realTrade = cell.getStringCellValue();
//
//
//            cell = row.getCell(3);
//            double maxScore = cell.getNumericCellValue();
//
//            // 预期得分
//            TreeMap<String, Double> expectedMap = new TreeMap<>();
//            for (int k=5; k < 95; k++) {
////                System.out.println(row.getCell(k).getStringCellValue() + ":" + row.getCell(k+2).getNumericCellValue());
//                expectedMap.put(nameCodeMap.get(nameMap.get(row.getCell(k).getStringCellValue())), row.getCell(k+2).getNumericCellValue());
//                k += 3;
//            }
//
//            // 实际得分
//            HashMap<String, Double> actualMap = new HashMap<>();
//
//            LLMTools llmTool = LLMToolFactory.createLLMTool(modeType).init("");
//            LLMRequest req = new LLMRequest();
//            req.setSystem(systemPrompt);
//            req.setUser(userPrompt);
//            req.setContent(buffer.toString());
//            req.setModeName(modeName);
//            String retContent = llmTool.lauchLLM(req);
//
//            String detail = FormatUtil.extractJsonData(retContent);
//            detail = FormatUtil.formatStandard(detail);
//            System.out.println("格式化返回:\n" + detail);
//
//            JSONObject object = JSON.parseObject(detail);
//            JSONArray scores = object.getJSONArray("scores");
//            List<JSONObject> jsonObjects = JSON.parseArray(scores.toJSONString(), JSONObject.class);
//
//            JSONObject summary = object.getJSONObject("summary");
//            String trade = summary.getString("trade");
//
//
//            // 模型总分
//            double llmTotalScore = 0;
//            // 人工总分
//            double artTotalScore = 0;
//            HashMap<String, Boolean> ruleObjMap = new HashMap<>();
//            for (JSONObject obj : jsonObjects) {
//                if (obj.getBooleanValue("match") == false) continue;
//
//                String[] ruleCodes = obj.getString("rule").split("\\;");
//                for (String code : ruleCodes) {
//                    ruleObjMap.put(codeMainCodeMap.get(code), obj.getBooleanValue("match"));
//                    llmTotalScore += codeScoreMap.get(code);
//                }
//            }
//
//
//            // 不匹配选项
//            List<String> notMathList = new ArrayList<>();
//            // 不匹配分值
//            double difSumScore = 0;
//
//            StringBuffer exampleBuff = new StringBuffer();
//            Set<Map.Entry<String, Double>> entries = expectedMap.entrySet();
//            for (Map.Entry<String, Double> entry :entries) {
//                String ruleCode = entry.getKey();
//                Double artValue = entry.getValue();
//
//                // 人工评分
//                artTotalScore += artValue;
//
//                Boolean match = ruleObjMap.get(ruleCode);
//                if (((match == null || match.booleanValue() == false ) && artValue == 0)) {
////                    System.out.println(ruleCode + "匹配");
//                } else if (((match == null || match.booleanValue() == false ) && artValue > 0)) {
////                    System.out.println(ruleCode + "不匹配");
//                    notMathList.add(ruleCode);
//                    difSumScore += mainCodeScoreMap.get(ruleCode);
//                } else if (match.booleanValue() == true && artValue > 0) {
////                    System.out.println(ruleCode + "匹配");
//                } else {
////                    System.out.println(ruleCode + "不匹配");
//                    notMathList.add(ruleCode);
//                    difSumScore += mainCodeScoreMap.get(ruleCode);
//                }
//
//                // 结果对比输出
//                Boolean artResult = artValue>0 ? true:false;
//                Boolean llmResult = match == null ? false : match.booleanValue()==true?true:false;
//                exampleBuff.append(String.format("%s,%s,%s,", artResult, llmResult, artResult.booleanValue()==llmResult.booleanValue()?"匹配":"不匹配"));
//            }
//
//            if (trade.equals("战败"))
//                trade = "战败";
//            else if(trade.equals("未知"))
//                trade = "不确定";
//
//            String tradeStr = "";
//            if (realTrade.equals(trade)) {
//                tradeStr = "匹配";
//            } else {
//                tradeStr = "不匹配(人工:" + realTrade + ";质检:" + trade + ")";
//            }
//
//            exampleBuff.append(tradeStr);
//            exampleBuff.append("\n");
//            writer.write(dataNo + "," + exampleBuff.toString());
//            writer.flush();
//
//            System.out.println(String.format("%s不匹配选项:%s,不匹配分值累计:%f,成交(战败)情况:%s,模型总分:%f,人工总分:%f,总分差异:%f", caseNo, notMathList.toString(), difSumScore,tradeStr,llmTotalScore,artTotalScore,Math.abs(llmTotalScore-artTotalScore)));
//            totalWriter.write(String.format("%s不匹配选项:%s,不匹配分值累计:%f,成交(战败)情况:%s,模型总分:%f,人工总分:%f,总分差异:%f\n", caseNo, notMathList.toString(), difSumScore,tradeStr, llmTotalScore,artTotalScore,Math.abs(llmTotalScore-artTotalScore)));
//            totalWriter.flush();
//        }
//
//        totalWriter.close();
//        writer.close();
//
//        System.out.println("本次任务运行耗时:" + (System.currentTimeMillis()-bgnTime)/1000 + "秒");
//    }
//
//    private static Map<String, String> getNameMap() {
//        Map<String, String> nameMap = new HashMap<>();
//        nameMap.put("尊称敬语", "尊称");
//        nameMap.put("自我介绍_名字", "介绍姓名");
//        nameMap.put("自我介绍_职务", "职务介绍");
//        nameMap.put("讲解大纲", "讲解大纲");
//        nameMap.put("恭喜车主", "恭喜车主");
//        nameMap.put("新车使用注意事项-时速", "油门、车速、120、加速、电瓶");
//        nameMap.put("新车使用注意事项-空调", "空调");
//        nameMap.put("新车使用注意事项-充电", "充电");
//        nameMap.put("新车使用注意事项-方向盘", "方向盘");
//        nameMap.put("新车使用注意事项-暴力驾驶", "急加速、太快、暴力驾驶");
//        nameMap.put("保养事项-首保", "首保");
//        nameMap.put("保养事项-基础保养", "基础保养");
//        nameMap.put("保养事项-四年中保", "四年中保");
//        nameMap.put("保修质保-整车质保", "整车质保");
//        nameMap.put("保修质保-三电", "三电");
//        nameMap.put("保修质保-易损件", "易损、损耗");
//        nameMap.put("保修质保-保修", "保修、免费换新");
//        nameMap.put("延保升级_提及延保", "提及延保");
//        nameMap.put("延保介绍_延保年限和里程", "延保年限和里程");
//        nameMap.put("延保升级_延保的补贴政策", "延保的补贴政策");
//        nameMap.put("延保介绍_延保节省的费用", "延保节省的费用");
//        nameMap.put("延保结论", "延保结论");
//        nameMap.put("转介客户", "转介客户");
//
//
//        return nameMap;
//    }
//}