🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
ChatClient.Introduction
Prompts are essential for directing AI models to generate specific outputs. In this tutorial, we will create and use prompts directly without relying on the PromptTemplate class, giving you more control over the prompt creation process. We will use Spring AI to interact with OpenAI's GPT models through the ChatClient abstraction.
You can use the PromptTemplate class to dynamically generate prompts for AI model interactions via ChatClient.
1. Setting Up the Project
Step 1: Create a New Spring Boot Project
Use Spring Initializr to create a new Spring Boot project. Include dependencies for Spring Web and Spring AI.
Using Spring Initializr:
- Go to start.spring.io
- Select:
- Project: Maven Project
- Language: Java
- Spring Boot: 3.0.0 (or latest)
- Dependencies: Spring Web
- Generate the project and unzip it.
Step 2: Add Dependencies
In your project's pom.xml, add the necessary dependencies for Spring AI.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
2. Configuring Spring AI
Step 1: Add API Key to Configuration
Add your OpenAI API key to application.properties or application.yml.
For application.properties:
openai.api.key=your_openai_api_key
For application.yml:
openai:
api:
key: your_openai_api_key
Step 2: Configure Spring Beans
Create a configuration class to set up all necessary Spring beans, including the OpenAiClient and ChatClient.
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ai.openai.OpenAiClient;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.ChatClient;
@Configuration
public class AppConfig {
@Bean
public OpenAiClient openAiClient() {
return new OpenAiClient();
}
@Bean
public ChatClient chatClient(OpenAiClient openAiClient) {
return new OpenAiChatClient(openAiClient);
}
}
3. Creating and Using Prompts
Step 1: Create a Service to Use Prompts
Create a service to manually create prompts and interact with the AI model through ChatClient.
package com.example.demo.service;
import org.springframework.ai.openai.ChatClient;
import org.springframework.ai.openai.model.ChatRequest;
import org.springframework.ai.openai.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PromptService {
private final ChatClient chatClient;
@Autowired
public PromptService(ChatClient chatClient) {
this.chatClient = chatClient;
}
public String generateJoke(String adjective, String topic) {
// Manually create the prompt
String prompt = "Tell me a " + adjective + " joke about " + topic;
// Create a ChatRequest with the generated prompt
ChatRequest request = new ChatRequest();
request.setMessage(prompt);
// Get the response from the ChatClient
ChatResponse response = chatClient.sendMessage(request);
return response.getReply();
}
}
Step 2: Create a Controller to Expose the Service
Create a controller to expose an endpoint for generating jokes.
package com.example.demo.controller;
import com.example.demo.service.PromptService;
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;
@RestController
public class PromptController {
private final PromptService promptService;
@Autowired
public PromptController(PromptService promptService) {
this.promptService = promptService;
}
@GetMapping("/joke")
public String getJoke(@RequestParam String adjective, @RequestParam String topic) {
return promptService.generateJoke(adjective, topic);
}
}
4. Testing the Integration
Step 1: Run the Application
Run your Spring Boot application. Ensure the application starts without errors.
Step 2: Access the Joke Endpoint
Use Postman, curl, or your browser to test the endpoint. For example:
http://localhost:8080/joke?adjective=funny&topic=technology
You should receive a joke response generated by the AI model based on the manually created prompt.
Conclusion
This tutorial demonstrated how to set up and use prompts directly in a Spring Boot application with Spring AI, without using the PromptTemplate class. You learned how to manually create prompts, use them in a service, and expose an endpoint to interact with the AI model.
You can use the PromptTemplate class to dynamically generate prompts for AI model interactions via ChatClient.
For more detailed information, refer to the Spring AI documentation.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment