mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Java: Add example 17 and 33 (#4597)
### Motivation and Context Add ChatGPT and ChatGPT streaming examples. <!-- 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:
+4
@@ -48,6 +48,10 @@ public class OpenAIChatCompletion implements ChatCompletionService {
|
||||
attributes.put(MODEL_ID_KEY, ContextVariable.of(modelId));
|
||||
}
|
||||
|
||||
public static OpenAIChatCompletion.Builder builder() {
|
||||
return new OpenAIChatCompletion.Builder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, ContextVariable<?>> getAttributes() {
|
||||
return attributes;
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.microsoft.semantickernel.samples.syntaxexamples;
|
||||
|
||||
import com.azure.ai.openai.OpenAIAsyncClient;
|
||||
import com.azure.ai.openai.OpenAIClientBuilder;
|
||||
import com.azure.core.credential.AzureKeyCredential;
|
||||
import com.azure.core.credential.KeyCredential;
|
||||
import com.microsoft.semantickernel.aiservices.openai.chatcompletion.OpenAIChatCompletion;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatCompletionService;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatHistory;
|
||||
|
||||
public class Example17_ChatGPT {
|
||||
|
||||
private static final boolean USE_AZURE_CLIENT = Boolean.parseBoolean(
|
||||
System.getenv("USE_AZURE_CLIENT"));
|
||||
private static final String CLIENT_KEY = System.getenv("CLIENT_KEY");
|
||||
|
||||
// Only required if USE_AZURE_CLIENT is true
|
||||
private static final String CLIENT_ENDPOINT = System.getenv("CLIENT_ENDPOINT");
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("======== Open AI - ChatGPT ========");
|
||||
|
||||
OpenAIAsyncClient client;
|
||||
|
||||
if (USE_AZURE_CLIENT) {
|
||||
client = new OpenAIClientBuilder()
|
||||
.credential(new AzureKeyCredential(CLIENT_KEY))
|
||||
.endpoint(CLIENT_ENDPOINT)
|
||||
.buildAsyncClient();
|
||||
} else {
|
||||
client = new OpenAIClientBuilder()
|
||||
.credential(new KeyCredential(CLIENT_KEY))
|
||||
.buildAsyncClient();
|
||||
}
|
||||
|
||||
ChatCompletionService chatGPT = OpenAIChatCompletion.builder()
|
||||
.withModelId("gpt-35-turbo")
|
||||
.withOpenAIAsyncClient(client)
|
||||
.build();
|
||||
|
||||
System.out.println("Chat content:");
|
||||
System.out.println("------------------------");
|
||||
|
||||
ChatHistory chatHistory = new ChatHistory("You are a librarian, expert about books");
|
||||
|
||||
// First user message
|
||||
chatHistory.addUserMessage("Hi, I'm looking for book suggestions");
|
||||
messageOutput(chatHistory);
|
||||
|
||||
GPTReply(chatGPT, chatHistory);
|
||||
messageOutput(chatHistory);
|
||||
|
||||
chatHistory.addUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion");
|
||||
messageOutput(chatHistory);
|
||||
|
||||
GPTReply(chatGPT, chatHistory);
|
||||
messageOutput(chatHistory);
|
||||
}
|
||||
|
||||
private static void messageOutput(ChatHistory chatHistory) {
|
||||
var message = chatHistory.getLastMessage().get();
|
||||
System.out.println(message.getAuthorRole() + ": " + message.getContent());
|
||||
System.out.println("------------------------");
|
||||
}
|
||||
|
||||
private static void GPTReply(ChatCompletionService chatGPT, ChatHistory chatHistory) {
|
||||
var reply = chatGPT.getChatMessageContentsAsync(chatHistory, null, null)
|
||||
.block();
|
||||
|
||||
StringBuilder message = new StringBuilder();
|
||||
reply.forEach(chatMessageContent -> message.append(chatMessageContent.getContent()));
|
||||
chatHistory.addAssistantMessage(message.toString());
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package com.microsoft.semantickernel.samples.syntaxexamples;
|
||||
|
||||
import com.azure.ai.openai.OpenAIAsyncClient;
|
||||
import com.azure.ai.openai.OpenAIClientBuilder;
|
||||
import com.azure.core.credential.AzureKeyCredential;
|
||||
import com.azure.core.credential.KeyCredential;
|
||||
import com.microsoft.semantickernel.aiservices.openai.chatcompletion.OpenAIChatCompletion;
|
||||
import com.microsoft.semantickernel.chatcompletion.AuthorRole;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatCompletionService;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatHistory;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatMessageContent;
|
||||
import com.microsoft.semantickernel.chatcompletion.StreamingChatMessageContent;
|
||||
|
||||
public class Example33_StreamingChat {
|
||||
|
||||
private static final boolean USE_AZURE_CLIENT = Boolean.parseBoolean(
|
||||
System.getenv("USE_AZURE_CLIENT"));
|
||||
private static final String CLIENT_KEY = System.getenv("CLIENT_KEY");
|
||||
|
||||
// Only required if USE_AZURE_CLIENT is true
|
||||
private static final String CLIENT_ENDPOINT = System.getenv("CLIENT_ENDPOINT");
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("======== Open AI - ChatGPT Streaming ========");
|
||||
|
||||
OpenAIAsyncClient client;
|
||||
|
||||
if (USE_AZURE_CLIENT) {
|
||||
client = new OpenAIClientBuilder()
|
||||
.credential(new AzureKeyCredential(CLIENT_KEY))
|
||||
.endpoint(CLIENT_ENDPOINT)
|
||||
.buildAsyncClient();
|
||||
} else {
|
||||
client = new OpenAIClientBuilder()
|
||||
.credential(new KeyCredential(CLIENT_KEY))
|
||||
.buildAsyncClient();
|
||||
}
|
||||
|
||||
ChatCompletionService chatGPT = OpenAIChatCompletion.builder()
|
||||
.withModelId("gpt-35-turbo")
|
||||
.withOpenAIAsyncClient(client)
|
||||
.build();
|
||||
|
||||
System.out.println("Chat content:");
|
||||
System.out.println("------------------------");
|
||||
|
||||
ChatHistory chatHistory = new ChatHistory("You are a librarian, expert about books");
|
||||
|
||||
// First user message
|
||||
chatHistory.addUserMessage("Hi, I'm looking for book suggestions");
|
||||
messageOutput(chatHistory);
|
||||
|
||||
GPTReply(chatGPT, chatHistory);
|
||||
|
||||
chatHistory.addUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion");
|
||||
messageOutput(chatHistory);
|
||||
|
||||
GPTReply(chatGPT, chatHistory);
|
||||
}
|
||||
|
||||
private static void messageOutput(ChatHistory chatHistory) {
|
||||
var message = chatHistory.getLastMessage().get();
|
||||
System.out.println(message.getAuthorRole() + ": " + message.getContent());
|
||||
System.out.println("------------------------");
|
||||
}
|
||||
|
||||
private static void GPTReply(ChatCompletionService chatGPT, ChatHistory chatHistory) {
|
||||
var reply = chatGPT.getStreamingChatMessageContentsAsync(chatHistory, null, null);
|
||||
System.out.print(AuthorRole.ASSISTANT + ": ");
|
||||
|
||||
String message = reply
|
||||
.doOnNext(streamingChatMessage -> {
|
||||
String content = streamingChatMessage.getContent();
|
||||
System.out.print(content);
|
||||
})
|
||||
.map(StreamingChatMessageContent::getContent)
|
||||
.collect(StringBuilder::new, StringBuilder::append)
|
||||
.map(StringBuilder::toString)
|
||||
.block();
|
||||
|
||||
System.out.println("\n------------------------");
|
||||
chatHistory.addAssistantMessage(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user