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.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.*;
import java.util.Map; import java.util.Map;
@Service @Service
@ -25,20 +24,16 @@ public class LLMAppServiceImpl implements LLMAppService {
@Override @Override
public SseEmitter ask(String llmToken, String question, Map<String, Object> params) throws Exception { public SseEmitter ask(String llmToken, String question, Map<String, Object> params) throws Exception {
SseEmitter emitter = new SseEmitter(0L); // 不超时 SseEmitter emitter = new SseEmitter(0L); // 不超时
SseOutputAdapter writer = new SseOutputAdapter(emitter);
new Thread(() -> { try {
try { llmServiceFactory.current().streamAnswer(llmToken, question, params, writer);
llmServiceFactory.current().streamAnswer(llmToken, question, params, new SseOutputAdapter(emitter)); emitter.complete();
emitter.complete(); } catch (Exception e) {
} catch (Exception e) { log.error("LLM调用异常", e);
try { emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}"));
emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}")); emitter.completeWithError(e);
} catch (IOException ignored) { }
log.error("error", e);
}
emitter.completeWithError(e);
}
}).start();
return emitter; return emitter;
} }

View File

@ -64,10 +64,7 @@ public class AnythingLLMServiceImpl implements LLMService {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (line.startsWith("data:")) { writer.writeLine(line); // 透传 data: xxx\n\n 格式
line = line.substring(5).trim();
}
writer.writeJsonLine(line);
} }
} }
} }

View File

@ -19,9 +19,4 @@ public class SseOutputAdapter implements WriterAdapter {
emitter.send(SseEmitter.event().data(line)); 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 { public interface WriterAdapter {
void writeLine(String line) throws IOException; void writeLine(String line) throws IOException;
void writeJsonLine(String line) throws IOException ;
} }