From 7fef7f8c7007eee9a54c7f6255391f7e46885825 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期五, 12 十二月 2025 08:28:00 +0800
Subject: [PATCH] #AI
---
src/main/java/com/zy/ai/service/LlmChatService.java | 63 +++++++++++++++++++++++++++++++
1 files changed, 62 insertions(+), 1 deletions(-)
diff --git a/src/main/java/com/zy/ai/service/LlmChatService.java b/src/main/java/com/zy/ai/service/LlmChatService.java
index b001d0e..5e709af 100644
--- a/src/main/java/com/zy/ai/service/LlmChatService.java
+++ b/src/main/java/com/zy/ai/service/LlmChatService.java
@@ -10,8 +10,13 @@
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
@@ -60,4 +65,60 @@
return response.getChoices().get(0).getMessage().getContent();
}
-}
\ No newline at end of file
+
+ 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;
+ if (s == null || s.isEmpty()) return;
+ if (s.startsWith("data:")) {
+ s = s.substring(5);
+ if (s.startsWith(" ")) s = s.substring(1);
+ }
+ // 淇濈暀妯″瀷杈撳嚭涓殑鎹㈣锛屽彧鍦ㄥ垽鏂粨鏉熸爣璁版椂蹇界暐绌虹櫧
+ if ("[DONE]".equals(s.trim())) 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");
+// log.info("chunk = [{}] len = {}", content, content.length());
+// for (char ch : content.toCharArray()) {
+// log.info("char: {} ({})", (int) ch, ch == '\n' ? "\\n" : ch);
+// }
+ 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