Search Tutorials


Spring Boot AI + Azure OpenAI Hello World Example | JavaInUse

Spring Boot AI + Azure OpenAI Hello World Example

In this tutorial, we will walk through the process of setting up Azure OpenAI and connecting it with Spring AI. Integrating artificial intelligence capabilities into your Spring Boot applications has become increasingly important in today's technology landscape. Azure OpenAI provides powerful AI models that can be seamlessly incorporated into your Java applications. This article explores how to connect a Spring Boot application to Azure OpenAI, allowing you to harness the power of advanced language models in your enterprise applications.
Spring Boot AI + Azure OpenAI Hello World Example

Video

This tutorial is explained in the below Youtube Video.

Step 1: Access the Azure Portal

Begin by logging into the Azure portal at portal.azure.com. This will give you access to all Azure services, including Azure OpenAI.


Azure OpenAI

Step 2: Navigate to Azure AI Services

From the Azure homepage, locate Azure OpenAI service by:

  • Looking under "Azure services" section
  • Clicking the "Azure OpenAI" icon with the star symbol

Azure services

Step 3: Create a New Azure OpenAI Resource

In the Azure AI services section:

  • Click the "Create" button to initiate a new Azure OpenAI instance setup
  • This will direct you to the resource creation form

New Azure OpenAI Resource

Step 4: Configure Your Azure OpenAI Resource

Fill in the following details in the creation form:

  • Subscription: Select your subscription
  • Resource group: "java"
  • Region: "East US" (crucial for model availability)
  • Name: "javainuse"
  • Pricing tier: "Standard S0"

Azure OpenAI Resource

Step 5: Access and Setup

After resource creation:

  • Verify your "javainuse" OpenAI resource is listed
  • Navigate to the resource overview page
  • Click "Go to Azure AI Foundry portal"

Azure OpenAI Access and Setup

Azure OpenAI Access and Setup

Azure OpenAI Access and Setup

Step 6: Model Deployment

In the Azure AI Foundry:

  • Click "+ Create new deployment"
  • Select "Deploy base model"
  • Choose "GPT-4o" from the model list
  • Name your deployment (e.g., "gpt-4o")
  • Configure deployment options

Azure OpenAI Deploy base model

Azure OpenAI Deploy base model




Step 7: Gather Credentials

Collect the following credentials:

  • API Key (from "Keys and Endpoint" section)
  • Endpoint URL (https://javainuse.openai.azure.com/)
  • Deployment ID (your chosen deployment name)

Azure OpenAI Deploy base model

Step 8: Spring Boot Configuration

Add these properties to your application.properties file:

azure.openai.api.key=your-copied-api-key
azure.openai.endpoint=https://javainuse.openai.azure.com/
azure.openai.deployment.model.id=gpt-4o
Next we will create the spring boot application for integrating with azure openai.
Azure OpenAI spring boot application
Set up your Maven project with the necessary dependencies:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.4.2</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainuse</groupId>
	<artifactId>spring-boot-ai</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-ai</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.azure</groupId>
			<artifactId>azure-ai-openai</artifactId>
			<version>1.0.0-beta.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
Create the Spring Boot bootstrap class
package com.azure.ai.openai.usage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AzureOpenAIApplication {
    public static void main(String[] args) {
        SpringApplication.run(AzureOpenAIApplication.class, args);
    }
}
Create a simple model class to handle incoming questions -
package com.azure.ai.openai.usage;

public class PromptQuestion {

	private String question;

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}
}
Implement the REST controller that handles AI completions.
  • OpenAIClientBuilder: A helper class that constructs an OpenAIClient object. It follows the builder design pattern, allowing you to chain configuration methods.
  • .endpoint(endpoint): Sets the API endpoint URL (stored in the variable endpoint). This is the address of the OpenAI service (e.g., an Azure OpenAI instance). The endpoint variable is assumed to be a string defined elsewhere in the code (e.g., "https://your-resource-name.openai.azure.com/").
  • .credential(new AzureKeyCredential(azureOpenaiKey)): Provides the authentication key (stored in the variable azureOpenaiKey) to access the API. The AzureKeyCredential class encapsulates the API key, which is likely a secret string provisioned for your Azure OpenAI resource.
  • .buildClient(): Finalizes the configuration and returns an OpenAIClient object, which will be used to send requests to the API.
  • List messages = new ArrayList<>: Initializes an empty list to hold ChatMessage objects. Each ChatMessage represents a single message in the conversation.
  • new ChatMessage(ChatRole.SYSTEM): Creates a message with the role SYSTEM. The system role typically defines the AI's behavior or persona.
    • .setContent("You are an AI assistant that helps people find information."): Sets the content of the system message, instructing the AI to act as a helpful assistant.
  • new ChatMessage(ChatRole.USER): Creates a message with the role USER, representing the human's input.
    • .setContent(getPrompt(promptQuestion)): Sets the content of the user message. The getPrompt(promptQuestion) method (assumed to be defined elsewhere) likely takes a promptQuestion variable and returns a formatted string. For example, promptQuestion might be "What is the weather like?" and getPrompt might return it unchanged or modify it.
  • ChatCompletionsOptions options = new ChatCompletionsOptions(messages): Creates an options object, passing in the messages list from the previous step. This tells the API what conversation to process.
  • .setTemperature(0.7): Sets the "temperature" parameter to 0.7. Temperature controls the randomness of the response:
    • Lower values (e.g., 0.0-0.5) make the output more focused and deterministic.
    • Higher values (e.g., 0.7-1.0) increase creativity and randomness.
    • 0.7 strikes a balance between coherence and variety.
  • .setTopP(0.95): Sets the "top-p" (nucleus sampling) parameter to 0.95. This controls diversity by only considering the smallest set of tokens whose cumulative probability exceeds 95%. It's an alternative to temperature for tuning randomness.
  • .setMaxTokens(800): Limits the response to 800 tokens (words or word pieces, depending on the model). This prevents overly long responses and manages cost/latency.
package com.azure.ai.openai.usage;

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.ChatChoice;
import com.azure.ai.openai.models.ChatCompletions;
import com.azure.ai.openai.models.ChatCompletionsOptions;
import com.azure.ai.openai.models.ChatMessage;
import com.azure.ai.openai.models.ChatRole;
import com.azure.core.credential.AzureKeyCredential;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class PromptController {

    @Value("${azure.openai.api.key}")
    private String azureOpenaiKey;

    @Value("${azure.openai.endpoint}")
    private String endpoint;

    @Value("${azure.openai.deployment.model.id}")
    private String deploymentOrModelId;

    @PostMapping("/answer")
    public List<String> getMethodName(@RequestBody PromptQuestion promptQuestion) {
    	
        List<String> responseList = new ArrayList<>();
        try {
            OpenAIClient client = new OpenAIClientBuilder()
                    .endpoint(endpoint)
                    .credential(new AzureKeyCredential(azureOpenaiKey))
                    .buildClient();

            List<ChatMessage> messages = new ArrayList<>();
            messages.add(new ChatMessage(ChatRole.SYSTEM).setContent("You are an AI assistant that helps people find information."));
            messages.add(new ChatMessage(ChatRole.USER).setContent(getPrompt(promptQuestion)));

            ChatCompletionsOptions options = new ChatCompletionsOptions(messages)
                    .setTemperature(0.7)
                    .setTopP(0.95)
                    .setMaxTokens(800);

            ChatCompletions completions = client.getChatCompletions(deploymentOrModelId, options);

            for (ChatChoice choice : completions.getChoices()) {
                responseList.add(choice.getMessage().getContent().trim());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            responseList.add("Exception Occurred");
        }
        return responseList;
    }

    private String getPrompt(PromptQuestion promptQuestion) {
        String input = promptQuestion.getQuestion().trim();        
        return input;
    }
}

Usage

To use the API, send a POST request to the /answer endpoint with a JSON body:

{
"question": "Your question here"
}
Note: The application is configured with these default parameters:
  • Temperature: 0.7 (controls response creativity)
  • Top P: 0.95 (nucleus sampling parameter)
  • Max Tokens: 800 (maximum response length)
We will be sending following request
{
	  "question":"How much does using azure openai cost"
}

Azure OpenAI spring boot application

Download Source Code

Download it -
Spring Boot AI + Azure OpenAI Hello World Example