package admin.service.impl; import admin.listener.DeepSeekAiListener; import admin.model.deepseek.req.ChatMessage; import admin.model.deepseek.req.ChatReqMsg; import admin.model.deepseek.req.ChatStreamOption; import admin.model.deepseek.resp.starnd.Message; import admin.model.deepseek.resp.stream.ChatStreamResponse; import admin.model.deepseek.resp.stream.StreamChoice; import admin.service.DeepSeekOpenApiService; import com.google.gson.Gson; import lombok.RequiredArgsConstructor; import okhttp3.*; import okhttp3.sse.EventSources; import okio.BufferedSource; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.concurrent.TimeUnit; /** * @author lj * @date 2025/2/23 13:42 */ @Service @RequiredArgsConstructor public class DeepSeekOpenApiServiceImpl implements DeepSeekOpenApiService { @Override public void chatCompletions(String content) { OkHttpClient client = new OkHttpClient().newBuilder().readTimeout(1, TimeUnit.MINUTES) .build(); MediaType mediaType = MediaType.parse("application/json"); ChatReqMsg req = createChatReqMsg(content); Gson gson = new Gson(); RequestBody body = RequestBody.create(mediaType, gson.toJson(req)); Request request = new Request.Builder() .url("http://192.168.0.122:6006/v1/chat/completions") .method("POST", body) .addHeader("Content-Type", "application/json") .addHeader("Accept", "application/json") .addHeader("Authorization", "Bearer zkhjdpQ8") .build(); DeepSeekAiListener deepSeekAiListener = new DeepSeekAiListener(null,null); // OkHttpClient okHttpClient = new OkHttpClient.Builder() // .connectTimeout(1, TimeUnit.MINUTES) // .readTimeout(1, TimeUnit.MINUTES) // .build(); EventSources.createFactory(client).newEventSource(request,deepSeekAiListener); // factory.newEventSource(request, eventSourceListener); // // client.newCall(request).enqueue(new Callback() { // @Override // public void onFailure(@NotNull Call call, @NotNull IOException e) { // // } // // @Override // public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { // if (!response.isSuccessful()) { // System.err.println("请求失败: " + response.code()); // return; // } // // try (ResponseBody body = response.body()) { // if (body != null) { // processStream(body.source()); // } // } // } // }); } public static void processStream(BufferedSource source) throws IOException { StringBuilder fullContent = new StringBuilder(); Gson gson = new Gson(); while (!source.exhausted()) { String line = source.readUtf8Line(); if (line == null) break; // 过滤心跳包和非数据行 if (line.startsWith("data: ")) { String jsonData = line.substring(6).trim(); // 去掉 "data: " if ("[DONE]".equals(jsonData)) { System.out.println("\n[流式传输结束]"); break; } // 解析JSON块 ChatStreamResponse chunk = gson.fromJson(jsonData, ChatStreamResponse.class); if (chunk != null && chunk.getChoices() != null) { for (StreamChoice choice : chunk.getChoices()) { Message deltaContent = choice.getDelta(); if (deltaContent != null && deltaContent.getContent()!=null) { fullContent.append(deltaContent.getContent()); System.out.print(deltaContent.getContent()); // 实时输出 } } } } } System.out.println("\n完整内容:\n" + fullContent); } private ChatReqMsg createChatReqMsg(String content) { ChatReqMsg chatReqMsg = new ChatReqMsg(); ChatMessage chatMessage = new ChatMessage(); chatMessage.setContent(content); chatMessage.setRole("user"); chatReqMsg.setMessages(new ArrayList<>(Collections.singleton(chatMessage))); chatReqMsg.setModel("DeepSeek-R1-Distill-Qwen-32B-GGUF"); chatReqMsg.setStream(true); ChatStreamOption chatStreamOption = new ChatStreamOption(); chatStreamOption.setIncludeUsage(true); chatStreamOption.setContinuousUsageStats(false); chatReqMsg.setStreamOptions(chatStreamOption); // ChatRespType chatRespType = new ChatRespType(); // chatRespType.setType("text"); // chatReqMsg.setResponse_format(chatRespType); return chatReqMsg; } }