This commit is contained in:
luke 2025-06-23 19:20:32 +08:00
parent 87198e42e4
commit eb3a858681
2 changed files with 18 additions and 4 deletions

View File

@ -2,7 +2,6 @@ package com.knowledge.base.application.service;
import com.knowledge.base.infrastructure.south.llm.LLMServiceFactory; import com.knowledge.base.infrastructure.south.llm.LLMServiceFactory;
import com.knowledge.base.infrastructure.util.http.FilteredSseOutputAdapter; import com.knowledge.base.infrastructure.util.http.FilteredSseOutputAdapter;
import com.knowledge.base.infrastructure.util.http.SseOutputAdapter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -28,10 +27,23 @@ public class LLMAppServiceImpl implements LLMAppService {
try { try {
FilteredSseOutputAdapter adapter = new FilteredSseOutputAdapter(emitter); FilteredSseOutputAdapter adapter = new FilteredSseOutputAdapter(emitter);
// 异步执行避免阻塞返回
new Thread(() -> {
try {
llmServiceFactory.current().streamAnswer(llmToken, question, params, adapter); llmServiceFactory.current().streamAnswer(llmToken, question, params, adapter);
emitter.complete();
} catch (Exception e) { } catch (Exception e) {
log.error("LLM调用异常", e); log.error("LLM流式调用异常", e);
try {
emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}"));
} catch (Exception ignored) {
log.error("error", ignored);
}
emitter.completeWithError(e);
}
}).start();
} catch (Exception e) {
log.error("LLM初始化异常", e);
emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}")); emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}"));
emitter.completeWithError(e); emitter.completeWithError(e);
} }

View File

@ -58,6 +58,8 @@ public class FilteredSseOutputAdapter implements WriterAdapter {
lastChunk.clear(); // 清空缓存 lastChunk.clear(); // 清空缓存
String payload = mapper.writeValueAsString(filtered); String payload = mapper.writeValueAsString(filtered);
emitter.send(SseEmitter.event().data(payload)); emitter.send(SseEmitter.event().data(payload));
// 增加主动关闭连接逻辑
emitter.complete();
} else if (!filtered.isEmpty()) { } else if (!filtered.isEmpty()) {
// 普通中间片段 // 普通中间片段
String payload = mapper.writeValueAsString(filtered); String payload = mapper.writeValueAsString(filtered);