|
46 | 46 | import com.azure.core.util.logging.ClientLogger;
|
47 | 47 | import com.fasterxml.jackson.core.JsonProcessingException;
|
48 | 48 | import java.nio.ByteBuffer;
|
| 49 | +import java.util.concurrent.atomic.AtomicReference; |
49 | 50 | import reactor.core.publisher.Flux;
|
50 | 51 | import reactor.core.publisher.Mono;
|
51 | 52 |
|
@@ -556,6 +557,57 @@ public Mono<Response<ChatCompletions>> getChatCompletionsWithResponse(String dep
|
556 | 557 | .map(response -> new SimpleResponse<>(response, response.getValue().toObject(ChatCompletions.class)));
|
557 | 558 | }
|
558 | 559 |
|
| 560 | + /** |
| 561 | + * Gets chat completions for the provided chat messages. Chat completions support a wide variety of tasks and |
| 562 | + * generate text that continues from or "completes" provided prompt data. |
| 563 | + * |
| 564 | + * <p> |
| 565 | + * <strong>Code Samples</strong> |
| 566 | + * </p> |
| 567 | + * <!-- src_embed |
| 568 | + * com.azure.ai.openai.OpenAIAsyncClient.getChatCompletionsStream#String-ChatCompletionsOptionsMaxOverload --> |
| 569 | + * <pre> |
| 570 | + * openAIAsyncClient.getChatCompletionsStreamWithResponse(deploymentOrModelId, new ChatCompletionsOptions(chatMessages), |
| 571 | + * new RequestOptions().setHeader("my-header", "my-header-value")) |
| 572 | + * .subscribe( |
| 573 | + * response -> System.out.print(response.getValue().getId()), |
| 574 | + * error -> System.err.println("There was an error getting chat completions." + error), |
| 575 | + * () -> System.out.println("Completed called getChatCompletionsStreamWithResponse.")); |
| 576 | + * </pre> |
| 577 | + * <!-- end com.azure.ai.openai.OpenAIAsyncClient.getChatCompletionsStream#String-ChatCompletionsOptionsMaxOverload |
| 578 | + * --> |
| 579 | + * |
| 580 | + * @param deploymentOrModelName Specifies either the model deployment name (when using Azure OpenAI) or model name |
| 581 | + * (when using non-Azure OpenAI) to use for this request. |
| 582 | + * @param chatCompletionsOptions The configuration information for a chat completions request. Completions support a |
| 583 | + * wide variety of tasks and generate text that continues from or "completes" provided prompt data. |
| 584 | + * @param requestOptions The options to configure the HTTP request before HTTP client sends it. |
| 585 | + * @throws IllegalArgumentException thrown if parameters fail the validation. |
| 586 | + * @throws HttpResponseException thrown if the request is rejected by server. |
| 587 | + * @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401. |
| 588 | + * @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404. |
| 589 | + * @throws ResourceModifiedException thrown if the request is rejected by server on status code 409. |
| 590 | + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. |
| 591 | + * @return chat completions stream for the provided chat messages. Completions support a wide variety of tasks and |
| 592 | + * generate text that continues from or "completes" provided prompt data. |
| 593 | + */ |
| 594 | + @ServiceMethod(returns = ReturnType.COLLECTION) |
| 595 | + public Flux<Response<ChatCompletions>> getChatCompletionsStreamWithResponse(String deploymentOrModelName, |
| 596 | + ChatCompletionsOptions chatCompletionsOptions, RequestOptions requestOptions) { |
| 597 | + chatCompletionsOptions.setStream(true); |
| 598 | + Mono<Response<BinaryData>> chatCompletionsWithResponse = getChatCompletionsWithResponse(deploymentOrModelName, |
| 599 | + BinaryData.fromObject(chatCompletionsOptions), requestOptions); |
| 600 | + AtomicReference<Response<BinaryData>> responseCopy = new AtomicReference<>(); |
| 601 | + Flux<ByteBuffer> responseStream = chatCompletionsWithResponse.flatMapMany(response -> { |
| 602 | + responseCopy.set(response); |
| 603 | + return response.getValue().toFluxByteBuffer(); |
| 604 | + }); |
| 605 | + OpenAIServerSentEvents<ChatCompletions> chatCompletionsStream |
| 606 | + = new OpenAIServerSentEvents<>(responseStream, ChatCompletions.class); |
| 607 | + return chatCompletionsStream.getEvents() |
| 608 | + .map(chatCompletions -> new SimpleResponse<>(responseCopy.get(), chatCompletions)); |
| 609 | + } |
| 610 | + |
559 | 611 | /**
|
560 | 612 | * Return the embeddings for a given prompt.
|
561 | 613 | *
|
@@ -646,21 +698,10 @@ public Mono<Completions> getCompletions(String deploymentOrModelName, String pro
|
646 | 698 | * <pre>
|
647 | 699 | * openAIAsyncClient
|
648 | 700 | * .getChatCompletionsStream(deploymentOrModelId, new ChatCompletionsOptions(chatMessages))
|
649 |
| - * .toStream() |
650 |
| - * // Remove .skip(1) when using Non-Azure OpenAI API |
651 |
| - * // Note: the first chat completions can be ignored when using Azure OpenAI service which is a known service bug. |
652 |
| - * // TODO: remove .skip(1) after service fixes the issue. |
653 |
| - * .skip(1) |
654 |
| - * .forEach(chatCompletions -> { |
655 |
| - * ChatResponseMessage delta = chatCompletions.getChoices().get(0).getDelta(); |
656 |
| - * if (delta.getRole() != null) { |
657 |
| - * System.out.println("Role = " + delta.getRole()); |
658 |
| - * } |
659 |
| - * if (delta.getContent() != null) { |
660 |
| - * String content = delta.getContent(); |
661 |
| - * System.out.print(content); |
662 |
| - * } |
663 |
| - * }); |
| 701 | + * .subscribe( |
| 702 | + * chatCompletions -> System.out.print(chatCompletions.getId()), |
| 703 | + * error -> System.err.println("There was an error getting chat completions." + error), |
| 704 | + * () -> System.out.println("Completed called getChatCompletionsStream.")); |
664 | 705 | * </pre>
|
665 | 706 | * <!-- end com.azure.ai.openai.OpenAIAsyncClient.getChatCompletionsStream#String-ChatCompletionsOptions -->
|
666 | 707 | *
|
|
0 commit comments