Back

Step by Step Guide to Building an Azure OpenAI Service API Integration in Java

Aug 7, 20247 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your Java app with some AI goodness? Let's dive into integrating Azure OpenAI Service using the nifty azure-ai-openai package. This guide assumes you're already familiar with Java and Azure, so we'll keep things snappy and focus on the good stuff.

Prerequisites

Before we jump in, make sure you've got:

  • A Java development environment (you know the drill)
  • An Azure account with an active subscription
  • An Azure OpenAI Service resource up and running

Got all that? Great! Let's get coding.

Setup

First things first, let's add the azure-ai-openai dependency to your project. If you're using Maven, toss this into your pom.xml:

<dependency> <groupId>com.azure</groupId> <artifactId>azure-ai-openai</artifactId> <version>1.0.0-beta.1</version> </dependency>

Now, grab your API key and endpoint from the Azure portal. You'll need these to authenticate your requests.

Initializing the Client

Time to get that client up and running. Import the necessary classes and let's build our OpenAIClient:

import com.azure.ai.openai.OpenAIClient; import com.azure.ai.openai.OpenAIClientBuilder; import com.azure.core.credential.AzureKeyCredential; OpenAIClient client = new OpenAIClientBuilder() .endpoint("https://your-resource-name.openai.azure.com/") .credential(new AzureKeyCredential("your-api-key")) .buildClient();

Easy peasy, right? Now we're ready to make some API calls!

Making API Calls

Let's explore a few common operations:

Text Completion

String prompt = "Once upon a time"; CompletionsOptions options = new CompletionsOptions("your-model-deployment-name"); options.setMaxTokens(50); CompletionsResponse response = client.getCompletions(prompt, options); System.out.println(response.getChoices().get(0).getText());

Chat Completion

List<ChatMessage> messages = Arrays.asList( new ChatMessage(ChatRole.SYSTEM, "You are a helpful assistant."), new ChatMessage(ChatRole.USER, "What's the capital of France?") ); ChatCompletionsOptions options = new ChatCompletionsOptions("your-model-deployment-name"); ChatCompletionsResponse response = client.getChatCompletions(messages, options); System.out.println(response.getChoices().get(0).getMessage().getContent());

Embeddings

List<String> texts = Arrays.asList("Hello, world!", "OpenAI is awesome!"); EmbeddingsOptions options = new EmbeddingsOptions("your-model-deployment-name"); EmbeddingsResponse response = client.getEmbeddings(texts, options); System.out.println(response.getData().get(0).getEmbedding());

Handling Responses

The azure-ai-openai package does a lot of the heavy lifting for you, returning nicely structured objects. But don't forget to handle those pesky exceptions:

try { // Your API call here } catch (HttpResponseException e) { System.err.println("Error code: " + e.getResponse().getStatusCode()); System.err.println("Error message: " + e.getMessage()); }

Best Practices

  • Mind your rate limits! Azure OpenAI Service has quotas, so be a good citizen.
  • Cache responses when it makes sense to reduce API calls.
  • Keep your API key secret. Use Azure Key Vault in production environments.

Advanced Topics

Want to level up? Look into async operations for non-blocking calls and streaming responses for real-time output. The azure-ai-openai package supports both!

Conclusion

And there you have it! You're now equipped to integrate Azure OpenAI Service into your Java applications. Remember, with great power comes great responsibility – use AI wisely and ethically.

Want to dive deeper? Check out the official Azure OpenAI Service documentation and the azure-ai-openai GitHub repo.

Now go forth and build something awesome! 🚀

Full Working Sample

Here's a little taste of what you can do by putting it all together:

import com.azure.ai.openai.OpenAIClient; import com.azure.ai.openai.OpenAIClientBuilder; import com.azure.core.credential.AzureKeyCredential; public class AzureOpenAIDemo { public static void main(String[] args) { OpenAIClient client = new OpenAIClientBuilder() .endpoint("https://your-resource-name.openai.azure.com/") .credential(new AzureKeyCredential("your-api-key")) .buildClient(); // Text completion CompletionsOptions completionsOptions = new CompletionsOptions("your-model-deployment-name"); completionsOptions.setMaxTokens(50); CompletionsResponse completionsResponse = client.getCompletions("Once upon a time", completionsOptions); System.out.println("Completion: " + completionsResponse.getChoices().get(0).getText()); // Chat completion List<ChatMessage> messages = Arrays.asList( new ChatMessage(ChatRole.SYSTEM, "You are a helpful assistant."), new ChatMessage(ChatRole.USER, "What's the capital of France?") ); ChatCompletionsOptions chatOptions = new ChatCompletionsOptions("your-model-deployment-name"); ChatCompletionsResponse chatResponse = client.getChatCompletions(messages, chatOptions); System.out.println("Chat response: " + chatResponse.getChoices().get(0).getMessage().getContent()); // Embeddings List<String> texts = Arrays.asList("Hello, world!", "OpenAI is awesome!"); EmbeddingsOptions embeddingsOptions = new EmbeddingsOptions("your-model-deployment-name"); EmbeddingsResponse embeddingsResponse = client.getEmbeddings(texts, embeddingsOptions); System.out.println("Embedding: " + embeddingsResponse.getData().get(0).getEmbedding()); } }

This sample demonstrates text completion, chat completion, and embeddings all in one go. Happy coding!