Building AI Applications with Spring AI(1): Your First Prompt

Welcome to my learning notes on developing AI applications with Spring AI. If the realm of AI has ever piqued your curiosity but you’ve felt daunted about where to begin, these notes are for you. So, let’s embark on this exploration together into the fascinating world of AI development. Feel free to share your thoughts and engage in discussions as we learn and grow together.

Kickstart Your Spring Boot Project

First and foremost, let’s get your development environment ready. We’ll be leveraging Spring Initializr, an incredibly user-friendly tool that streamlines the setup process.

  • Begin by visiting Spring Initializr.
  • Configure your project with these specifics:
    • Project: Gradle Project (for straightforward dependency management)
    • Language: Java (widely appreciated and used)
    • Spring Boot: Opt for version 3.2.4 or the most recent stable version
    • Project Metadata: Fill in the details as they fit your project. For this tutorial:
      • Group: io.github.deno
      • Artifact: ai
      • Name: ai
      • Description: A demo project for Spring Boot
      • Package Name: io.github.demo.ai
      • Packaging: Jar (simplifies deployment)
      • Java Version: 17 (to take advantage of the latest features)
  • In the Dependencies section, include Spring Web, Spring Data JPA, PostgreSQL Driver, Spring Security, and notably, the crucial Spring AI related dependencies.
  • Click Generate to download your project setup.

To make things even easier, you can directly use this link to pre-fill the setup with the necessary configurations: Spring Initializr Pre-filled Setup

Configure Your Application

After downloading and extracting your project, the next step is to configure it to communicate with OpenAI services.

  • Open src/main/resources/application.yml in your favorite IDE or text editor.
  • Add the following configuration, ensuring you replace <Your-OpenAI-API-Key> with your actual OpenAI API key:
1
2
3
4
5
6
7
8
9
spring:
application:
name: ai
ai:
openai:
api-key: <Your-OpenAI-API-Key>
chat:
options:
model: gpt-4-0125-preview

This configures your application’s name and integrates it with OpenAI services, specifying the AI model you intend to use.

Creating Your First Prompt

Let’s dive into the exciting part—interacting with AI.

  • Create a file named ApplicationStartup.java in your main package.
  • Write a component to send a prompt to OpenAI’s ChatGPT and print the response:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Component
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class ApplicationStartup {

final OpenAiChatClient openAiChatClient;

@EventListener(ApplicationReadyEvent.class)
public void afterStartup() throws Exception {
Prompt prompt = new Prompt("tell a joke",
OpenAiChatOptions.builder()
.withTemperature(0.8f)
.build());
ChatResponse response = openAiChatClient.call(prompt);
System.out.println("Output:");
System.out.println(response.getResult().getOutput().getContent());
}
}

Here, we’re sending a simple request to ChatGPT to “tell a joke”. The response will be printed out for you to enjoy.

Output:

1
2
3
Why don't skeletons fight each other? 

They don't have the guts!

Handling JSON Responses

Often, you’ll want your AI’s wisdom in a structured format. Here’s how:

  • Modify the afterStartup method to request jokes in JSON format, thereby easing parsing and use in your application.
1
2
3
4
5
6
7
8
9
10
public void afterStartup() throws Exception {
Prompt prompt = new Prompt("List 8 jokes. Use JSON response",
OpenAiChatOptions.builder()
.withTemperature(0.8f)
.withResponseFormat(new ChatCompletionRequest.ResponseFormat("json_object"))
.build());
ChatResponse response = openAiChatClient.call(prompt);
System.out.println("Output:");
System.out.println(response.getResult().getOutput().getContent());
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{
"jokes": [
{
"id": 1,
"setup": "What do you call fake spaghetti?",
"punchline": "An impasta!"
},
{
"id": 2,
"setup": "Why don’t skeletons fight each other?",
"punchline": "They don’t have the guts."
},
{
"id": 3,
"setup": "Why couldn't the bicycle stand up by itself?",
"punchline": "It was two-tired."
},
{
"id": 4,
"setup": "What do you call a dinosaur with an extensive vocabulary?",
"punchline": "A thesaurus."
},
{
"id": 5,
"setup": "Why did the coffee file a police report?",
"punchline": "It got mugged."
},
{
"id": 6,
"setup": "What's orange and sounds like a parrot?",
"punchline": "A carrot."
},
{
"id": 7,
"setup": "Why don't eggs tell jokes?",
"punchline": "They'd crack each other up."
},
{
"id": 8,
"setup": "How does a penguin build its house?",
"punchline": "Igloos it together."
}
]
}

Understanding Output Parsing

Parsing outputs, especially JSON, can be nuanced. Remember, the AI’s response is a string formatted as JSON, not a direct JSON object. You’ll need to parse this string into a data structure that your application can work with.

The challenge of precise output formatting led to the development of “OpenAI Functions”, which allow for specifying the desired output format directly.

And there you have it—a simple yet comprehensive guide to starting your journey into AI development with Spring Boot and OpenAI. The world of AI is vast and filled with possibilities. Keep experimenting, and who knows? You might just create something that will astonish even the experts.

Stay tuned for more tutorials in this series, where we’ll delve deeper into the exciting possibilities of AI application development with Spring AI. Happy coding!

References