diff --git a/src/main/java/com/knowledge/base/application/service/LLMAppServiceImpl.java b/src/main/java/com/knowledge/base/application/service/LLMAppServiceImpl.java index fd950cf..10bdb0d 100644 --- a/src/main/java/com/knowledge/base/application/service/LLMAppServiceImpl.java +++ b/src/main/java/com/knowledge/base/application/service/LLMAppServiceImpl.java @@ -7,7 +7,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; -import java.io.*; import java.util.Map; @Service @@ -25,20 +24,16 @@ public class LLMAppServiceImpl implements LLMAppService { @Override public SseEmitter ask(String llmToken, String question, Map params) throws Exception { SseEmitter emitter = new SseEmitter(0L); // 不超时 + SseOutputAdapter writer = new SseOutputAdapter(emitter); - new Thread(() -> { - try { - llmServiceFactory.current().streamAnswer(llmToken, question, params, new SseOutputAdapter(emitter)); - emitter.complete(); - } catch (Exception e) { - try { - emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}")); - } catch (IOException ignored) { - log.error("error", e); - } - emitter.completeWithError(e); - } - }).start(); + try { + llmServiceFactory.current().streamAnswer(llmToken, question, params, writer); + emitter.complete(); + } catch (Exception e) { + log.error("LLM调用异常", e); + emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}")); + emitter.completeWithError(e); + } return emitter; } diff --git a/src/main/java/com/knowledge/base/infrastructure/south/llm/AnythingLLMServiceImpl.java b/src/main/java/com/knowledge/base/infrastructure/south/llm/AnythingLLMServiceImpl.java index a53da9d..055f9fc 100644 --- a/src/main/java/com/knowledge/base/infrastructure/south/llm/AnythingLLMServiceImpl.java +++ b/src/main/java/com/knowledge/base/infrastructure/south/llm/AnythingLLMServiceImpl.java @@ -64,10 +64,7 @@ public class AnythingLLMServiceImpl implements LLMService { String line; while ((line = reader.readLine()) != null) { - if (line.startsWith("data:")) { - line = line.substring(5).trim(); - } - writer.writeJsonLine(line); + writer.writeLine(line); // 透传 data: xxx\n\n 格式 } } } diff --git a/src/main/java/com/knowledge/base/infrastructure/util/http/SseOutputAdapter.java b/src/main/java/com/knowledge/base/infrastructure/util/http/SseOutputAdapter.java index ec89fcd..7f91c9d 100644 --- a/src/main/java/com/knowledge/base/infrastructure/util/http/SseOutputAdapter.java +++ b/src/main/java/com/knowledge/base/infrastructure/util/http/SseOutputAdapter.java @@ -19,9 +19,4 @@ public class SseOutputAdapter implements WriterAdapter { emitter.send(SseEmitter.event().data(line)); } } - - @Override - public void writeJsonLine(String json) throws IOException { - emitter.send(SseEmitter.event().data(json)); - } } diff --git a/src/main/java/com/knowledge/base/infrastructure/util/http/WriterAdapter.java b/src/main/java/com/knowledge/base/infrastructure/util/http/WriterAdapter.java index e53101d..92a73d0 100644 --- a/src/main/java/com/knowledge/base/infrastructure/util/http/WriterAdapter.java +++ b/src/main/java/com/knowledge/base/infrastructure/util/http/WriterAdapter.java @@ -4,7 +4,5 @@ import java.io.IOException; public interface WriterAdapter { void writeLine(String line) throws IOException; - - void writeJsonLine(String line) throws IOException ; }