Java: Support streaming on ChatCompletions (#2244)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
This commit is contained in:
John Oliver
2023-07-31 16:10:24 -04:00
committed by GitHub
parent 0583d43ffc
commit 1137b05533
6 changed files with 236 additions and 5 deletions
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;
import com.azure.ai.openai.OpenAIAsyncClient;
import com.microsoft.semantickernel.SamplesConfig;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.chatcompletion.ChatCompletion;
import com.microsoft.semantickernel.chatcompletion.ChatHistory;
import com.microsoft.semantickernel.exceptions.ConfigurationException;
import reactor.core.publisher.Mono;
import java.util.function.BiFunction;
import static com.microsoft.semantickernel.chatcompletion.ChatHistory.AuthorRoles.Assistant;
/**
* The following example shows how to use Semantic Kernel with Text Completion as streaming
*/
public class Example33_StreamingChat {
public static void main(String[] args) throws ConfigurationException {
StartStreamingChatAsync();
}
private static void StartStreamingChatAsync() throws ConfigurationException {
OpenAIAsyncClient client = SamplesConfig.getClient();
ChatCompletion chatCompletion = SKBuilders.chatCompletion().build(client, "gpt-35-turbo");
System.out.println("Chat content:");
System.out.println("------------------------");
var chatHistory = chatCompletion.createNewChat("You are a librarian, expert about books");
messageOutput(chatHistory);
// First user message
chatHistory.addMessage(ChatHistory.AuthorRoles.User, "Hi, I'm looking for book suggestions");
messageOutput(chatHistory);
// First bot assistant message
streamMessageOutputAsync(chatCompletion, chatHistory, Assistant);
// Second user message
chatHistory.addMessage(ChatHistory.AuthorRoles.User, "I love history and philosophy, I'd like to learn something new about Greece, any suggestion?");
messageOutput(chatHistory);
// Second bot assistant message
streamMessageOutputAsync(chatCompletion, chatHistory, Assistant);
}
private static void streamMessageOutputAsync(ChatCompletion chatGPT, ChatHistory chatHistory, ChatHistory.AuthorRoles authorRole) {
System.out.println(authorRole.name() + ": ");
BiFunction<String, String, String> agg = (o, o2) -> o + o2;
Mono<String> mono = chatGPT
.generateMessageStream(chatHistory, null)
.reduce("", agg);
String fullMessage = mono.block();
System.out.println(fullMessage);
System.out.println("\n------------------------");
chatHistory.addMessage(authorRole, fullMessage);
}
private static void messageOutput(ChatHistory chatHistory) {
var message = chatHistory.getMessages().get(chatHistory.getMessages().size() - 1);
System.out.println(message.getAuthorRoles().name() + " " + message.getContent());
System.out.println("------------------------");
}
}