42 lines
1.3 KiB
Java
42 lines
1.3 KiB
Java
package com.knowledge.base.application.service;
|
|
|
|
import com.knowledge.base.infrastructure.south.llm.LLMServiceFactory;
|
|
import com.knowledge.base.infrastructure.util.http.SseOutputAdapter;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
@Slf4j
|
|
public class LLMAppServiceImpl implements LLMAppService {
|
|
|
|
private final LLMServiceFactory llmServiceFactory;
|
|
|
|
@Override
|
|
public String getToken(String password) throws Exception {
|
|
return llmServiceFactory.current().fetchToken(password);
|
|
}
|
|
|
|
@Override
|
|
public SseEmitter ask(String llmToken, String question, Map<String, Object> params) throws Exception {
|
|
SseEmitter emitter = new SseEmitter(0L); // 不超时
|
|
SseOutputAdapter writer = new SseOutputAdapter(emitter);
|
|
|
|
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;
|
|
}
|
|
}
|
|
|