This commit is contained in:
luke 2025-06-23 16:22:03 +08:00
parent 73ebf1fb66
commit 0901ff78a5
4 changed files with 10 additions and 25 deletions

View File

@ -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<String, Object> 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));
llmServiceFactory.current().streamAnswer(llmToken, question, params, writer);
emitter.complete();
} catch (Exception e) {
try {
log.error("LLM调用异常", e);
emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}"));
} catch (IOException ignored) {
log.error("error", e);
}
emitter.completeWithError(e);
}
}).start();
return emitter;
}

View File

@ -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 格式
}
}
}

View File

@ -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));
}
}

View File

@ -4,7 +4,5 @@ import java.io.IOException;
public interface WriterAdapter {
void writeLine(String line) throws IOException;
void writeJsonLine(String line) throws IOException ;
}