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
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- Android App Showing Two Toolbars Issue fix - Android
- Python: Merge two dictionaries into one dictionary - Python
- How to Access Terminal (Command Line) in Eclipse IDE - Eclipse
- import servlet API to eclipse project (javax.servlet cannot be resolved error) - Java
- List of common cURL Error Codes - cURL
- How to know if someone has read your WhatsApp message - WhatsApp
- Setting Up VS Code with Java JDK 21 - Java-JDK-21
- How to turn off Stage Manager - macOS Ventura - MacOS