Spring Boot AI + LLM + Java Code Example


Let us take a look at a simple example of how to make use of Spring Boot AI with LLM AI models such as ChatGPT

Step 1: Create a new project using Spring CLI

# spring boot new --from ai --name myai-app

Getting project from https://github.com/rd-1-2022/ai-openai-helloworld
Created project in directory 'myai-app'

Step 2: Add the API key details in the application.properties file

spring.ai.openai.api-key=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
spring.ai.openai.chat.options.model=gpt-3.5-turbo
spring.ai.openai.chat.options.temperature=0.6

Step 3: Run the application and access endpoint /ai/simple

You can notice that we already get a hello-world project setup with a SimpleAIController created for us with an API end-point /ai/simple with a default prompt as "Tell me a joke"

package org.springframework.ai.openai.samples.helloworld.simple;

import org.springframework.ai.chat.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class SimpleAiController {

	private final ChatClient chatClient;

	@Autowired
	public SimpleAiController(ChatClient chatClient) {
		this.chatClient = chatClient;
	}

	@GetMapping("/ai/simple")
	public Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
		return Map.of("generation", chatClient.call(message));
	}
}
Example 1: Default Prompt:
# curl localhost:8080/ai/simple

Why do Java developers wear glasses?

Because they don't see sharp!

Example 2: Custom Prompt:
# curl localhost:8080/ai/simple?message=What is 2+5

2 + 5 equals 7.

Spring AI support for all major Model providers such as OpenAI, Microsoft, Amazon, Google, and Huggingface.

Spring Boot AI example with ChatGPT

Note: If you do not add the API key you will get an exception:

java.lang.IllegalArgumentException: OpenAI API key must be set


Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap