1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| private Set<String> getRecommendWordV1(GptRecommendReqVO gptRecommendReqVO, AuthInfo authInfo, String prompt, String cacheKeyPreFix) { String cacheValue = ""; try { if (StringUtils.isBlank(gptRecommendReqVO.getValue())) { return Collections.emptySet(); } String formatValue = FormatUtil.escapeFormat(gptRecommendReqVO.getValue()); if(StringUtils.isBlank(formatValue) || formatValue.length()<=1){ log.info("GptGrpcWrapper value:{},formatValue:{}",gptRecommendReqVO.getValue(),formatValue); return Collections.emptySet(); } if(gptRecommendReqVO.getLanguage()==null){ gptRecommendReqVO.setLanguage("英语"); }
String language = gptRecommendReqVO.getLanguage(); String cacheKey = cacheKeyPreFix + "v6" + ":" + gptRecommendReqVO.getLanguage()+":"+gptRecommendReqVO.getValue(); cacheValue = recommendWordCache.get(cacheKey); if(StringUtils.isNotBlank(cacheValue)){ log.info("GptGrpcWrapper call success,param:{},result:{}",JSON.toJSONString(gptRecommendReqVO),cacheValue); if(cacheValue.contains("[") && cacheValue.contains("]")){ cacheValue = cacheValue.substring(cacheValue.indexOf("["), cacheValue.lastIndexOf("]") + 1); } Set<String> synonyms = getLimitSize(Lists.newArrayList(JSON.parseArray(cacheValue, String.class)), gptRecommendReqVO.getSize()); return synonyms; } log.info("GptGrpcWrapper call param:{},prompt:{}",JSON.toJSONString(gptRecommendReqVO),prompt); CompletableFuture<String> future = CompletableFuture.supplyAsync( () -> gptRequest(authInfo.getOrgId(),authInfo.getAccId(),authInfo.getEmail(),prompt, GPTModelVersionEnum.GPT_4O_MINI.getVersion()), GPT_REQUEST_EXECUTOR); String res = (String)FutureResultUtil.getResult("gpt-recommard-future",future,120, TimeUnit.SECONDS); if (StringUtils.isBlank(res)) { log.info("getRecommend failed.name:{},language:{}",formatValue, language); return Collections.emptySet(); } cacheValue = res; recommendWordCache.set(cacheKey,cacheValue,TimeUnit.DAYS.toMillis(100)); log.info("GptGrpcWrapper call success,param:{},result:{}",JSON.toJSONString(gptRecommendReqVO),cacheValue); if(cacheValue.contains("[") && cacheValue.contains("]")){ cacheValue = cacheValue.substring(cacheValue.indexOf("["), cacheValue.lastIndexOf("]") + 1); } Set<String> synonyms = getLimitSize(Lists.newArrayList(JSON.parseArray(cacheValue, String.class)), gptRecommendReqVO.getSize()); return synonyms; }catch (Exception e) { log.error("getGptRecommend param:{}",JSON.toJSONString(gptRecommendReqVO), e); return Collections.emptySet(); } }
|