Azure OpenAI

and Java

 

Azure OpenAI

Create Azure OpenAI

  • Click a button to create Azure OpenAI

Create an Azure OpenAI

  • Select your "Azure for Students" subscription.
  • Select the Resource group you previously created.
  • Set the Region to something in the United States. US East 2 has the most available OpenAI models.
  • Create a unique name.
  • Select "Standard S0" as the pricing tier. This stands for Standard On-Demand. You will pay-as-you-go for input and output tokens.

Go to the New Resource

  • On the Network tab, select to allow all networks to access this resource.
  • Do not enter anything on the Tags tab.
  • Review the settings and click the Create button.
  • Wait for the resource to deploy.
  • Click the "Go to resource" button.

Go to the New Resource

  • On the Overview tab, click the deployment name.
  • On the deployment page, click to view keys and endpoints.

Copy the Key and Endpoint

  • Copy Key 1. Save it as an environment variable called "AZURE_OPENAI_KEY".
  • Copy the endpoint. Save it as an environment variable called "AZURE_OPENAI_ENDPOINT".
  • Click the Overview button. Click the "Go to Azure AI Foundry portal".

Azure AI Foundry

Deploy New Model

  • Click a model name. 
  • Click the Deploy button.
  • If you get an "Insufficient quota" warning, that means this model is not available for your subscription or the region you selected.
  • Click "Models" to go back to the list.

Azure Student Deployment

  • These are the models I could select with my student account.

    • gpt-35-turbo

    • gpt-4

  • You may give the deployment a custom name.

  • Add the azure-ai-openai dependency to your pom.xml file.

Example Code

  • I got this code to work with "gpt-4" and "gpt-4o-mini" created from my personal Azure account, and "gpt-35-turbo" created from my student account.

  • It failed with "gpt-4" from my student account, saying "Access denied due to invalid subscription key or wrong API endpoint."

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.*;
import com.azure.core.credential.AzureKeyCredential;

import java.util.Arrays;
import java.util.List;

public class AzureOpenAIExample1 {

    public static void main(String[] args) {
        String systemMessage = "You are a helpful language translating assistant.";
        String userMessage = "Translate the following English text to French: 'Hello, how are you?'";
        String response = callAzureOpenAI(systemMessage, userMessage);
        System.out.println("Azure OpenAI Response: " + response);
    }

    public static String callAzureOpenAI(String systemMessage, String userMessage) {
        String azureOpenAIKey = "<your-api-key>"; // Use environment variable
        String azureOpenAIEndpoint = "<your-endpoint-uri>"; // Use environment variable
        String deploymentName = "gpt-35-turbo"; // "gpt-4", "gpt-4o-mini", or a custom name

        OpenAIClient client = new OpenAIClientBuilder()
                .credential(new AzureKeyCredential(azureOpenAIKey))
                .endpoint(azureOpenAIEndpoint)
                .buildClient();

        List<ChatRequestMessage> chatMessages = new ArrayList<>();
        chatMessages.add(new ChatRequestSystemMessage(systemMessage));
        chatMessages.add(new ChatRequestUserMessage(userMessage));
        
        ChatCompletionsOptions options = new ChatCompletionsOptions(chatMessages);
        String response = client.getChatCompletions(deploymentName, options).getChoices().get(0).getMessage().getContent();
        return response;
    }
}

Example Code 2

  • In this code sample, I overloaded the callAzureOpenAI method to include a flag variable to display additional data.

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.*;
import com.azure.core.credential.AzureKeyCredential;

import java.util.ArrayList;
import java.util.List;

public class AzureOpenAIExample1 {

    public static void main(String[] args) {
        String systemMessage = "You are a helpful language translating assistant.";
        String userMessage = "Translate the following English text to French: 'Hello, how are you?'";
        String response = callAzureOpenAI(systemMessage, userMessage, true);
        System.out.println("Azure OpenAI Response: " + response);
    }

    public static String callAzureOpenAI(String systemMessage, String userMessage) {
        return callAzureOpenAI(systemMessage, userMessage, false);
    }

    public static String callAzureOpenAI(String systemMessage, String userMessage, boolean displayData) {
        String azureOpenAIKey = "<your-api-key>"; // Use environment variable
        String azureOpenAIEndpoint = "<your-endpoint-uri>"; // Use environment variable
        String deploymentName = "gpt-35-turbo"; // "gpt-4", "gpt-4o-mini", or a custom name

        OpenAIClient client = new OpenAIClientBuilder()
                .credential(new AzureKeyCredential(azureOpenAIKey))
                .endpoint(azureOpenAIEndpoint)
                .buildClient();

        List<ChatRequestMessage> chatMessages = new ArrayList<>();
        chatMessages.add(new ChatRequestSystemMessage(systemMessage));
        chatMessages.add(new ChatRequestUserMessage(userMessage));

        ChatCompletions chatCompletions = client.getChatCompletions(deploymentName, new ChatCompletionsOptions(chatMessages));

        if(displayData) {
            System.out.printf("Model ID: %s\nCreated: %s%n", chatCompletions.getId(), chatCompletions.getCreatedAt());
            CompletionsUsage usage = chatCompletions.getUsage();
            System.out.printf("Usage\n- number of prompt tokens (request): %d, "
                            + "\n- number of completion tokens (response): %d, "
                            + "\n- number of total tokens: %d.%n",
                    usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
            System.out.println();
        }

        String response = chatCompletions.getChoices().get(0).getMessage().getContent();
        return response;
    }
}

Azure AI Foundry

  • After running the code a couple of times, go back to the Azure AI Foundry. Click the deployment name. Click the Metrics tab.

  • View the total requests and token count. Note the number of prompt and completion tokens.

  • Use the pricing page to estimate the cost.

Azure Personal Account

  • Note how I gave the "gpt-4o-mini" model a custom name.

  • I ran the code on the previous pages using the custom name "my-gpt-4o-mini", instead of the model name "gpt-4o-mini".

Azure Personal Account

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

public class AzureOpenAIExample2 {

    public static void main(String[] args) {
        String systemMessage = "You are a helpful language translating assistant.";
        String userMessage = "Translate the following English text to French: 'Hello, how are you?'";
        String response = callAzureOpenAI(systemMessage, userMessage);
        System.out.println("Azure OpenAI Response: " + response);
    }

    public static String callAzureOpenAI(String systemMessage, String userMessage) {
        String azureOpenAIKey = "<your-api-key>"; // Use environment variable
        String azureOpenAIEndpoint = "<your-endpoint-uri>"; // Use environment variable
        String deploymentName = "<your-custom-deployment-name>";
        String model = "gpt-4o-mini";
        String apiVersion = "2024-02-01";

        try {
            ObjectMapper objectMapper = new ObjectMapper();

            // Create JSON requestBody
            ObjectNode requestBody = objectMapper.createObjectNode();

            // Add the model name to the requestBody
            requestBody.put("model", model);
            // Add optional properties to the requestBody
            requestBody.put("temperature", 1.0);
            requestBody.put("top_p", 1.0);
            requestBody.put("max_tokens", 1000);

            // Create messages array (Chat API format)
//            "messages": [
//                {
//                    "role": "system",
//                    "content": "You are a helpful language translating assistant."
//                },
//                {
//                    "role": "user",
//                    "content": "Translate the following English text to French: 'Hello, how are you?'"
//                }
//            ],

            ArrayNode messages = objectMapper.createArrayNode();

            ObjectNode systemMessageNode = objectMapper.createObjectNode();
            systemMessageNode.put("role", "system");
            systemMessageNode.put("content", systemMessage);
            messages.add(systemMessageNode);

            ObjectNode userMessageNode = objectMapper.createObjectNode();
            userMessageNode.put("role", "user");
            userMessageNode.put("content", userMessage);
            messages.add(userMessageNode);

            // Add the messages to the requestBody
            requestBody.set("messages", messages);

            // Construct the HTTP request
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(azureOpenAIEndpoint + "/openai/deployments/" + deploymentName + "/chat/completions?api-version=" + apiVersion))
                    .header("Content-Type", "application/json")
                    .header("api-key", azureOpenAIKey)
                    .POST(HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(requestBody)))
                    .build();

            // Send request and get response
            HttpClient client = HttpClient.newHttpClient();
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            // View the JSON response
            // return response.body();

            // Parse and return the JSON response
            ObjectNode responseJson = objectMapper.readValue(response.body(), ObjectNode.class);
            return responseJson.get("choices").get(0).get("message").get("content").asText();

        } catch (Exception e) {
            return e.getMessage();
        }
    }
}

Java 3 - Azure OpenAI

By Marc Hauschildt

Java 3 - Azure OpenAI

  • 226