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
Example 2: Custom Prompt:
# curl localhost:8080/ai/simple?message=What is 2+5

Note: If you do not add the API key you will get an exception:
java.lang.IllegalArgumentException: OpenAI API key must be set
Reference:
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!