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.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, new SseOutputAdapter(emitter));
|
llmServiceFactory.current().streamAnswer(llmToken, question, params, writer);
|
||||||
emitter.complete();
|
emitter.complete();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
try {
|
log.error("LLM调用异常", e);
|
||||||
emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}"));
|
emitter.send(SseEmitter.event().data("{\"error\": \"LLM异常\"}"));
|
||||||
} catch (IOException ignored) {
|
|
||||||
log.error("error", e);
|
|
||||||
}
|
|
||||||
emitter.completeWithError(e);
|
emitter.completeWithError(e);
|
||||||
}
|
}
|
||||||
}).start();
|
|
||||||
|
|
||||||
return emitter;
|
return emitter;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user