|
32 | 32 | import org.junit.jupiter.api.Test; |
33 | 33 | import org.slf4j.Logger; |
34 | 34 | import org.slf4j.LoggerFactory; |
| 35 | +import reactor.core.publisher.Flux; |
35 | 36 |
|
36 | 37 | import org.springframework.ai.chat.client.ChatClient; |
37 | 38 | import org.springframework.ai.chat.messages.AssistantMessage; |
@@ -409,6 +410,47 @@ void testMaxTokensForNonReasoningModels() { |
409 | 410 | } |
410 | 411 | } |
411 | 412 |
|
| 413 | + @Test |
| 414 | + void testModelInStreamingResponse() { |
| 415 | + String prompt = "List three colors of the rainbow."; |
| 416 | + |
| 417 | + // @formatter:off |
| 418 | + Flux<ChatResponse> responseFlux = ChatClient.create(this.chatModel).prompt() |
| 419 | + .options(AzureOpenAiChatOptions.builder() |
| 420 | + .deploymentName("gpt-4o") |
| 421 | + .build()) |
| 422 | + .user(prompt) |
| 423 | + .stream() |
| 424 | + .chatResponse(); |
| 425 | + // @formatter:on |
| 426 | + |
| 427 | + List<ChatResponse> responses = responseFlux.collectList().block(); |
| 428 | + |
| 429 | + assertThat(responses).isNotEmpty(); |
| 430 | + |
| 431 | + ChatResponse lastResponse = responses.get(responses.size() - 1); |
| 432 | + |
| 433 | + // Verify that the final merged response has model metadata |
| 434 | + assertThat(lastResponse.getMetadata()).as("Last response should have metadata").isNotNull(); |
| 435 | + assertThat(lastResponse.getMetadata().getModel()).as("Last response metadata should contain model").isNotNull(); |
| 436 | + |
| 437 | + String model = lastResponse.getMetadata().getModel(); |
| 438 | + logger.info("Final merged response model: {}", model); |
| 439 | + assertThat(model).isNotEmpty(); |
| 440 | + // Azure OpenAI models typically contain "gpt" in their name |
| 441 | + assertThat(model).containsIgnoringCase("gpt"); |
| 442 | + |
| 443 | + String content = responses.stream() |
| 444 | + .flatMap(r -> r.getResults().stream()) |
| 445 | + .map(Generation::getOutput) |
| 446 | + .map(AssistantMessage::getText) |
| 447 | + .filter(Objects::nonNull) |
| 448 | + .collect(Collectors.joining()); |
| 449 | + |
| 450 | + assertThat(content).isNotEmpty(); |
| 451 | + logger.info("Generated content: {}", content); |
| 452 | + } |
| 453 | + |
412 | 454 | record ActorsFilms(String actor, List<String> movies) { |
413 | 455 |
|
414 | 456 | } |
|
0 commit comments