From 58dc0727a11481c127fc6111b73fa309b03505b5 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期四, 11 十二月 2025 18:54:36 +0800
Subject: [PATCH] #AI

---
 src/main/java/com/zy/ai/service/LlmChatService.java |  116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 116 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/ai/service/LlmChatService.java b/src/main/java/com/zy/ai/service/LlmChatService.java
new file mode 100644
index 0000000..e8c7a77
--- /dev/null
+++ b/src/main/java/com/zy/ai/service/LlmChatService.java
@@ -0,0 +1,116 @@
+package com.zy.ai.service;
+
+import com.zy.ai.entity.ChatCompletionRequest;
+import com.zy.ai.entity.ChatCompletionResponse;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.stereotype.Service;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Flux;
+
+import java.util.List;
+import java.util.function.Consumer;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+
+@Slf4j
+@Service
+@RequiredArgsConstructor
+public class LlmChatService {
+
+    private final WebClient llmWebClient;
+
+    @Value("${llm.api-key}")
+    private String apiKey;
+
+    @Value("${llm.model}")
+    private String model;
+
+    /**
+     * 閫氱敤瀵硅瘽鏂规硶锛氫紶鍏� messages锛岃繑鍥炲ぇ妯″瀷鏂囨湰鍥炲
+     */
+    public String chat(List<ChatCompletionRequest.Message> messages,
+                       Double temperature,
+                       Integer maxTokens) {
+
+        ChatCompletionRequest req = new ChatCompletionRequest();
+        req.setModel(model);
+        req.setMessages(messages);
+        req.setTemperature(temperature != null ? temperature : 0.3);
+        req.setMax_tokens(maxTokens != null ? maxTokens : 1024);
+
+        ChatCompletionResponse response = llmWebClient.post()
+                .uri("/chat/completions")
+                .header(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
+                .contentType(MediaType.APPLICATION_JSON)
+                .bodyValue(req)   // 2.5.14 宸叉敮鎸� bodyValue
+                .retrieve()
+                .bodyToMono(ChatCompletionResponse.class)
+                .doOnError(ex -> log.error("璋冪敤 LLM 澶辫触", ex))
+                .onErrorResume(ex -> Mono.empty())
+                .block();
+
+        if (response == null ||
+                response.getChoices() == null ||
+                response.getChoices().isEmpty() ||
+                response.getChoices().get(0).getMessage() == null) {
+
+            return "AI 璇婃柇澶辫触锛氭湭鑾峰彇鍒版湁鏁堝洖澶嶃��";
+        }
+
+        return response.getChoices().get(0).getMessage().getContent();
+    }
+
+    public void chatStream(List<ChatCompletionRequest.Message> messages,
+                           Double temperature,
+                           Integer maxTokens,
+                           Consumer<String> onChunk,
+                           Runnable onComplete,
+                           Consumer<Throwable> onError) {
+
+        ChatCompletionRequest req = new ChatCompletionRequest();
+        req.setModel(model);
+        req.setMessages(messages);
+        req.setTemperature(temperature != null ? temperature : 0.3);
+        req.setMax_tokens(maxTokens != null ? maxTokens : 1024);
+        req.setStream(true);
+
+        Flux<String> flux = llmWebClient.post()
+                .uri("/chat/completions")
+                .header(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
+                .contentType(MediaType.APPLICATION_JSON)
+                .accept(MediaType.TEXT_EVENT_STREAM)
+                .bodyValue(req)
+                .retrieve()
+                .bodyToFlux(String.class)
+                .doOnError(ex -> log.error("璋冪敤 LLM 娴佸紡澶辫触", ex));
+
+        flux.subscribe(payload -> {
+            String s = payload == null ? null : payload.trim();
+            if (s == null || s.isEmpty()) return;
+            if (s.startsWith("data:")) s = s.substring(5).trim();
+            if ("[DONE]".equals(s)) return;
+            try {
+                JSONObject obj = JSON.parseObject(s);
+                JSONArray choices = obj.getJSONArray("choices");
+                if (choices != null && !choices.isEmpty()) {
+                    JSONObject c0 = choices.getJSONObject(0);
+                    JSONObject delta = c0.getJSONObject("delta");
+                    if (delta != null) {
+                        String content = delta.getString("content");
+                        if (content != null) onChunk.accept(content);
+                    }
+                }
+            } catch (Exception ignore) {}
+        }, err -> {
+            if (onError != null) onError.accept(err);
+        }, () -> {
+            if (onComplete != null) onComplete.run();
+        });
+    }
+}

--
Gitblit v1.9.1