fix
This commit is contained in:
parent
87198e42e4
commit
eb3a858681
@ -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);
|
||||||
llmServiceFactory.current().streamAnswer(llmToken, question, params, adapter);
|
// 异步执行,避免阻塞返回
|
||||||
emitter.complete();
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
llmServiceFactory.current().streamAnswer(llmToken, question, params, adapter);
|
||||||
|
} catch (Exception 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) {
|
} catch (Exception e) {
|
||||||
log.error("LLM调用异常", 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);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user