OpenAI Chat in Java
This page was automatically generated by AI; not yet reviewed for accuracy...
The content and code samples on this page were generated by using the ai
CLI with customized prompts in this repository.
It's cool, but, it's experimental. 😁
Please review the content and code before using it in your application.
This sample demonstrates how to use the OpenAI Chat API in a Java console application.
Main.java
OpenAIChatCompletionsClass.java
How to generate this sample
AI - Azure AI CLI, Version 1.0.0
Copyright (c) 2024 Microsoft Corporation. All Rights Reserved.
This PUBLIC PREVIEW version may change at any time.
See: https://aka.ms/azure-ai-cli-public-preview
Generating 'openai-chat' in 'openai-chat-java' (3 files)...
pom.xml
src/Main.java
src/OpenAIChatCompletionsClass.java
Generating 'openai-chat' in 'openai-chat-java' (3 files)... DONE!
Main.java
STEP 1: Read the configuration settings from environment variables.
String openAIAPIKey = (System.getenv("AZURE_OPENAI_API_KEY") != null) ? System.getenv("AZURE_OPENAI_API_KEY") : "<insert your OpenAI API key here>";
String openAIEndpoint = (System.getenv("AZURE_OPENAI_ENDPOINT") != null) ? System.getenv("AZURE_OPENAI_ENDPOINT") : "<insert your OpenAI endpoint here>";
String openAIChatDeployment = (System.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT") != null) ? System.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT") : "<insert your OpenAI chat deployment name here>";
String openAISystemPrompt = (System.getenv("AZURE_OPENAI_SYSTEM_PROMPT") != null) ? System.getenv("AZURE_OPENAI_SYSTEM_PROMPT") : "You are a helpful AI assistant.";
STEP 2: Validate the environment variables.
if (openAIAPIKey.contains("<insert") || openAIEndpoint.contains("<insert") || openAIChatDeployment.contains("<insert")) {
System.err.println("Please set the environment variables properly.");
System.exit(1);
}
STEP 3: Initialize the helper class with the configuration settings.
OpenAIChatCompletionsClass chat = new OpenAIChatCompletionsClass(openAIAPIKey, openAIEndpoint, openAIChatDeployment, openAISystemPrompt);
STEP 4: Create the OpenAI client.
OpenAIClient client = new OpenAIClientBuilder()
.endpoint(openAIEndpoint)
.credential(new AzureKeyCredential(openAIAPIKey))
.buildClient();
STEP 5: Obtain user input, use the helper class to get the assistant's response, and display responses as they are received.
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("User: ");
if (!scanner.hasNextLine()) break;
String userPrompt = scanner.nextLine();
if (userPrompt.isEmpty() || "exit".equals(userPrompt)) break;
String response = chat.getChatCompletion(userPrompt);
System.out.println("\nAssistant: " + response + "\n");
}
scanner.close();
OpenAIChatCompletionsClass.java
STEP 1: Create the client and initialize chat message history with a system message.
public OpenAIChatCompletionsClass (String openAIAPIKey, String openAIEndpoint, String openAIChatDeployment, String openAISystemPrompt) {
this.openAIChatDeployment = openAIChatDeployment;
this.openAISystemPrompt = openAISystemPrompt;
client = new OpenAIClientBuilder()
.endpoint(openAIEndpoint)
.credential(new AzureKeyCredential(openAIAPIKey))
.buildClient();
List<ChatRequestMessage> chatMessages = new ArrayList<>();
options = new ChatCompletionsOptions(chatMessages);
ClearConversation();
}
STEP 2: Clear previous conversation and set the initial system message.
public void ClearConversation(){
List<ChatRequestMessage> chatMessages = options.getMessages();
chatMessages.clear();
chatMessages.add(new ChatRequestSystemMessage(this.openAISystemPrompt));
}
STEP 3: When the user provides input, add the user message to the chat message history.
public String getChatCompletion(String userPrompt) {
options.getMessages().add(new ChatRequestUserMessage(userPrompt));
STEP 4: Send the chat message history to the OpenAI Chat API and process the response.
ChatCompletions chatCompletions = client.getChatCompletions(this.openAIChatDeployment, options);
String responseContent = chatCompletions.getChoices().get(0).getMessage().getContent();
}
STEP 5: Add the assistant's response to the chat message history and return the response.