#
Junjie
1 天以前 eb20a6b937d92cfa7b38bbbc0c901b7fb9373e26
#
2个文件已修改
142 ■■■■■ 已修改文件
src/main/java/com/zy/ai/service/LlmChatService.java 107 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/ai/timer/MakeMainProcessPseudocodeScheduler.java 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/ai/service/LlmChatService.java
@@ -46,14 +46,20 @@
        req.setMessages(messages);
        req.setTemperature(temperature != null ? temperature : 0.3);
        req.setMax_tokens(maxTokens != null ? maxTokens : 1024);
        req.setStream(false);
        ChatCompletionResponse response = llmWebClient.post()
                .uri("/chat/completions")
                .header(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_EVENT_STREAM)
                .bodyValue(req)
                .retrieve()
                .bodyToMono(ChatCompletionResponse.class)
                .exchangeToMono(resp -> resp.bodyToFlux(String.class)
                        .collectList()
                        .map(list -> {
                            String payload = String.join("\n\n", list);
                            return parseCompletion(payload);
                        }))
                .doOnError(ex -> log.error("调用 LLM 失败", ex))
                .onErrorResume(ex -> Mono.empty())
                .block();
@@ -61,9 +67,10 @@
        if (response == null ||
                response.getChoices() == null ||
                response.getChoices().isEmpty() ||
                response.getChoices().get(0).getMessage() == null) {
            return "AI 诊断失败:未获取到有效回复。";
                response.getChoices().get(0).getMessage() == null ||
                response.getChoices().get(0).getMessage().getContent() == null ||
                response.getChoices().get(0).getMessage().getContent().isEmpty()) {
            return null;
        }
        return response.getChoices().get(0).getMessage().getContent();
@@ -164,4 +171,94 @@
            }
        });
    }
    private ChatCompletionResponse mergeSseChunk(ChatCompletionResponse acc, String payload) {
        if (payload == null || payload.isEmpty()) return acc;
        String[] events = payload.split("\\r?\\n\\r?\\n");
        for (String part : events) {
            String s = part;
            if (s == null || s.isEmpty()) continue;
            if (s.startsWith("data:")) {
                s = s.substring(5);
                if (s.startsWith(" ")) s = s.substring(1);
            }
            if ("[DONE]".equals(s.trim())) {
                continue;
            }
            try {
                JSONObject obj = JSON.parseObject(s);
                if (obj == null) continue;
                JSONArray choices = obj.getJSONArray("choices");
                if (choices != null && !choices.isEmpty()) {
                    JSONObject c0 = choices.getJSONObject(0);
                    if (acc.getChoices() == null || acc.getChoices().isEmpty()) {
                        ChatCompletionResponse.Choice choice = new ChatCompletionResponse.Choice();
                        ChatCompletionRequest.Message msg = new ChatCompletionRequest.Message();
                        choice.setMessage(msg);
                        java.util.ArrayList<ChatCompletionResponse.Choice> list = new java.util.ArrayList<>();
                        list.add(choice);
                        acc.setChoices(list);
                    }
                    ChatCompletionResponse.Choice choice = acc.getChoices().get(0);
                    ChatCompletionRequest.Message msg = choice.getMessage();
                    if (msg.getRole() == null || msg.getRole().isEmpty()) {
                        msg.setRole("assistant");
                    }
                    JSONObject delta = c0.getJSONObject("delta");
                    if (delta != null) {
                        String c = delta.getString("content");
                        if (c != null) {
                            String prev = msg.getContent();
                            msg.setContent(prev == null ? c : prev + c);
                        }
                        String role = delta.getString("role");
                        if (role != null && !role.isEmpty()) msg.setRole(role);
                    }
                    JSONObject message = c0.getJSONObject("message");
                    if (message != null) {
                        String c = message.getString("content");
                        if (c != null) {
                            String prev = msg.getContent();
                            msg.setContent(prev == null ? c : prev + c);
                        }
                        String role = message.getString("role");
                        if (role != null && !role.isEmpty()) msg.setRole(role);
                    }
                    String fr = c0.getString("finish_reason");
                    if (fr != null && !fr.isEmpty()) choice.setFinishReason(fr);
                }
                String id = obj.getString("id");
                if (id != null && !id.isEmpty()) acc.setId(id);
                Long created = obj.getLong("created");
                if (created != null) acc.setCreated(created);
                String object = obj.getString("object");
                if (object != null && !object.isEmpty()) acc.setObjectName(object);
            } catch (Exception ignore) {}
        }
        return acc;
    }
    private ChatCompletionResponse parseCompletion(String payload) {
        if (payload == null) return null;
        try {
            ChatCompletionResponse r = JSON.parseObject(payload, ChatCompletionResponse.class);
            if (r != null && r.getChoices() != null && !r.getChoices().isEmpty() && r.getChoices().get(0).getMessage() != null) {
                return r;
            }
        } catch (Exception ignore) {}
        ChatCompletionResponse sse = mergeSseChunk(new ChatCompletionResponse(), payload);
        if (sse.getChoices() != null && !sse.getChoices().isEmpty() && sse.getChoices().get(0).getMessage() != null && sse.getChoices().get(0).getMessage().getContent() != null) {
            return sse;
        }
        ChatCompletionResponse r = new ChatCompletionResponse();
        ChatCompletionResponse.Choice choice = new ChatCompletionResponse.Choice();
        ChatCompletionRequest.Message msg = new ChatCompletionRequest.Message();
        msg.setRole("assistant");
        msg.setContent(payload);
        choice.setMessage(msg);
        java.util.ArrayList<ChatCompletionResponse.Choice> list = new java.util.ArrayList<>();
        list.add(choice);
        r.setChoices(list);
        return r;
    }
}
src/main/java/com/zy/ai/timer/MakeMainProcessPseudocodeScheduler.java
@@ -25,7 +25,7 @@
    @Autowired
    private RedisUtil redisUtil;
    @Scheduled(cron = "0/3 * * * * ? ")
    @Scheduled(cron = "0/10 * * * * ? ")
    public void refreshPseudocodeDaily() {
        try {
            initMainProcessPseudocode();
@@ -51,6 +51,29 @@
                code = new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
            }
        } catch (Exception ignore) {}
        String crnOperateProcessUtilsCode = null;
        try {
            String utilsClassName = "com.zy.core.utils.CrnOperateProcessUtils";
            String rel = utilsClassName.replace('.', '/') + ".java";
            java.nio.file.Path p = Paths.get(System.getProperty("user.dir"), "src", "main", "java", rel);
            if (Files.exists(p)) {
                crnOperateProcessUtilsCode = new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
                code += crnOperateProcessUtilsCode;
            }
        } catch (Exception ignore) {}
        String StationOperateProcessUtilsCode = null;
        try {
            String utilsClassName = "com.zy.core.utils.StationOperateProcessUtils";
            String rel = utilsClassName.replace('.', '/') + ".java";
            java.nio.file.Path p = Paths.get(System.getProperty("user.dir"), "src", "main", "java", rel);
            if (Files.exists(p)) {
                StationOperateProcessUtilsCode = new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
                code += StationOperateProcessUtilsCode;
            }
        } catch (Exception ignore) {}
        String result = null;
        if (code != null && !code.isEmpty()) {
            List<ChatCompletionRequest.Message> messages = new java.util.ArrayList<>();
@@ -203,8 +226,14 @@
                result = llmChatService.chat(messages, 0.2, 2048);
            } catch (Exception ignore) {}
        }
        redisUtil.set(RedisKeyType.MAIN_PROCESS_PSEUDOCODE.key, result, 60 * 60 * 24);
        News.info("主流程伪代码已刷新");
        if (result == null) {
            redisUtil.set(RedisKeyType.MAIN_PROCESS_PSEUDOCODE.key, "AI生成伪代码失败", 60 * 10);
            News.info("AI生成伪代码失败");
        }else {
            redisUtil.set(RedisKeyType.MAIN_PROCESS_PSEUDOCODE.key, result, 60 * 60 * 24);
            News.info("AI生成伪代码成功");
        }
    }
}