|
| 1 | +package com.apolloconfig.apollo.ai.qabot.controller; |
| 2 | + |
| 3 | +import com.apolloconfig.apollo.ai.qabot.entity.Answer; |
| 4 | +import com.apolloconfig.apollo.ai.qabot.openai.OpenAiResponseService; |
| 5 | +import com.google.common.base.Strings; |
| 6 | +import com.openai.models.responses.ResponseCompletedEvent; |
| 7 | +import com.openai.models.responses.ResponseStreamEvent; |
| 8 | +import com.openai.models.responses.ResponseTextDeltaEvent; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.regex.Pattern; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | +import org.springframework.http.MediaType; |
| 17 | +import org.springframework.web.bind.annotation.GetMapping; |
| 18 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 19 | +import org.springframework.web.bind.annotation.RequestParam; |
| 20 | +import org.springframework.web.bind.annotation.RestController; |
| 21 | +import reactor.core.publisher.Flux; |
| 22 | + |
| 23 | +@RestController |
| 24 | +@RequestMapping("/qa") |
| 25 | +public class QAWithResponseController { |
| 26 | + |
| 27 | + private static final Logger LOGGER = LoggerFactory.getLogger(QAWithResponseController.class); |
| 28 | + private static final String END_SYMBOL = "$END$"; |
| 29 | + |
| 30 | + private final OpenAiResponseService aiService; |
| 31 | + |
| 32 | + public QAWithResponseController(OpenAiResponseService aiService) { |
| 33 | + this.aiService = aiService; |
| 34 | + } |
| 35 | + |
| 36 | + @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
| 37 | + public Flux<Answer> qa(@RequestParam String question, |
| 38 | + @RequestParam(required = false, defaultValue = "") String threadId) { |
| 39 | + question = question.trim(); |
| 40 | + if (Strings.isNullOrEmpty(question)) { |
| 41 | + return Flux.just(Answer.EMPTY); |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + return doQA(threadId, question); |
| 46 | + } catch (Throwable exception) { |
| 47 | + LOGGER.error("Error while calling Assistants API", exception); |
| 48 | + return Flux.just(Answer.ERROR); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private Flux<Answer> doQA(String threadId, String question) { |
| 53 | + if (LOGGER.isDebugEnabled()) { |
| 54 | + LOGGER.debug("\nPrompt message: {}", question); |
| 55 | + } |
| 56 | + |
| 57 | + Flux<ResponseStreamEvent> result = aiService.getResponseMessage(threadId, question); |
| 58 | + |
| 59 | + return result.filter( |
| 60 | + responseStreamEvent -> responseStreamEvent.isOutputTextDelta() |
| 61 | + || responseStreamEvent.isCompleted()) |
| 62 | + .map(responseStreamEvent -> { |
| 63 | + if (LOGGER.isDebugEnabled()) { |
| 64 | + LOGGER.debug("responseStreamEvent: {}", responseStreamEvent); |
| 65 | + } |
| 66 | + if (responseStreamEvent.isOutputTextDelta() && responseStreamEvent.outputTextDelta() |
| 67 | + .isPresent()) { |
| 68 | + return getAnswerFromOutputTextDelta(responseStreamEvent.outputTextDelta().get()); |
| 69 | + } else if (responseStreamEvent.isCompleted() && responseStreamEvent.completed() |
| 70 | + .isPresent()) { |
| 71 | + return getAnswerFromCompleted(responseStreamEvent.completed().get()); |
| 72 | + } |
| 73 | + return Answer.EMPTY; |
| 74 | + }).onErrorReturn(Answer.ERROR); |
| 75 | + } |
| 76 | + |
| 77 | + private @NotNull Answer getAnswerFromCompleted(ResponseCompletedEvent responseCompletedEvent) { |
| 78 | + Set<String> relatedFiles = responseCompletedEvent.response().output().stream() |
| 79 | + .filter(responseOutputItem -> responseOutputItem.isMessage() |
| 80 | + && responseOutputItem.message().isPresent()) |
| 81 | + .flatMap(responseOutputItem -> responseOutputItem.message().stream()) |
| 82 | + .flatMap(message -> message.content().stream()) |
| 83 | + .filter(content -> content.isOutputText() && content.outputText().isPresent()) |
| 84 | + .flatMap(content -> content.outputText().stream()) |
| 85 | + .flatMap(outputText -> outputText.annotations().stream()) |
| 86 | + .filter(annotation -> |
| 87 | + annotation.isFileCitation() && annotation.fileCitation().isPresent()) |
| 88 | + .map(annotation -> { |
| 89 | + String fileName = annotation.fileCitation().get().filename(); |
| 90 | + if (fileName.endsWith(".md")) { |
| 91 | + fileName = fileName.substring(0, fileName.length() - 3); |
| 92 | + } |
| 93 | + return fileName; |
| 94 | + }) |
| 95 | + .collect(Collectors.toSet()); |
| 96 | + return new Answer(END_SYMBOL, responseCompletedEvent.response().id(), relatedFiles); |
| 97 | + } |
| 98 | + |
| 99 | + private @NotNull Answer getAnswerFromOutputTextDelta( |
| 100 | + ResponseTextDeltaEvent responseTextDeltaEvent) { |
| 101 | + String text = responseTextDeltaEvent.delta(); |
| 102 | + return new Answer(text, "", Collections.emptySet()); |
| 103 | + } |
| 104 | +} |
0 commit comments