fix
This commit is contained in:
parent
73ebf1fb66
commit
0901ff78a5
@ -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));
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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 格式
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,5 @@ import java.io.IOException;
|
||||
|
||||
public interface WriterAdapter {
|
||||
void writeLine(String line) throws IOException;
|
||||
|
||||
void writeJsonLine(String line) throws IOException ;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user