|
19 | 19 | import java.util.ArrayList; |
20 | 20 | import java.util.List; |
21 | 21 | import java.util.Map; |
| 22 | +import java.util.Optional; |
22 | 23 | import java.util.stream.Collectors; |
23 | 24 |
|
24 | 25 | import org.slf4j.Logger; |
25 | 26 | import org.slf4j.LoggerFactory; |
| 27 | +import org.springframework.ai.chat.model.ChatResponse; |
| 28 | +import org.springframework.ai.chat.model.Generation; |
26 | 29 | import reactor.core.publisher.Flux; |
27 | 30 | import reactor.core.publisher.Mono; |
28 | 31 | import reactor.core.scheduler.Scheduler; |
@@ -142,17 +145,11 @@ public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChai |
142 | 145 | public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorChain advisorChain) { |
143 | 146 | List<Message> assistantMessages = new ArrayList<>(); |
144 | 147 | // Handle streaming case where we have a single result |
145 | | - if (chatClientResponse.chatResponse() != null && chatClientResponse.chatResponse().getResult() != null |
146 | | - && chatClientResponse.chatResponse().getResult().getOutput() != null) { |
147 | | - assistantMessages = List.of((Message) chatClientResponse.chatResponse().getResult().getOutput()); |
148 | | - } |
149 | | - else if (chatClientResponse.chatResponse() != null) { |
150 | | - assistantMessages = chatClientResponse.chatResponse() |
151 | | - .getResults() |
152 | | - .stream() |
153 | | - .map(g -> (Message) g.getOutput()) |
154 | | - .toList(); |
155 | | - } |
| 148 | + Optional.of(chatClientResponse) |
| 149 | + .map(ChatClientResponse::chatResponse) |
| 150 | + .map(ChatResponse::getResult) |
| 151 | + .map(Generation::getOutput) |
| 152 | + .ifPresent(assistantMessages::add); |
156 | 153 |
|
157 | 154 | if (!assistantMessages.isEmpty()) { |
158 | 155 | this.chatMemory.add(this.getConversationId(chatClientResponse.context(), this.defaultConversationId), |
|
0 commit comments