Understanding and Creating Prompts with Spring AI : Spring AI Prompt Integration

In the realm of AI applications, prompts play a crucial role in guiding AI models to generate specific responses based on user inputs.

This article will explain what a Spring AI Prompt is, how to create and use prompts within the Spring AI framework, and provide you with code examples to get started.

What is a Spring AI Prompt?

A Spring AI Prompt is a way to interact with AI models by providing them with specific instructions or queries that guide their responses.

In AI applications, prompts are used to elicit desired outputs from models by framing the input in a way that the model can understand and generate relevant results.

Prompts can vary in complexity, from simple questions or commands to detailed and nuanced instructions.

The effectiveness of a prompt depends on how well it is crafted to match the AI model’s capabilities and the context of the task at hand.

Creating Prompts Using Spring AI

To integrate and use prompts with Spring AI, you need to follow a series of steps to set up your project and configure the prompts correctly. Here’s how you can do it:

We have already discussed about, how to create spring ai project. You can go through that post for initial project setup.

I hope you are good with setup. We will directly jump to service implementation.

Service Class : Create a service class to interact with the AI model using the defined prompt. We will guide ai model to return top 10 famous food for requested city or place. 

				
					package com.example.demo.service;

import java.util.List;
import java.util.Map;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SpringAIService {

	@Autowired
	ChatClient chatClient;

	@Autowired
	OpenAiChatModel aiChatModel;

	public Map<String, String> getdummyChatResponse(String message) {
		return Map.of("completion", chatClient.prompt().user(message).call().content());
	}

	public String getPromptResponse(String request) {
		String userText = """
				Tell me top 10 famous food in {request}.
				""";
		String systemText = """
				Please don't send response if {request} is outside India.
				If user is asking response for outside India, then return, "Please google it."
				And response should be in json format.
				""";

		PromptTemplate promptTemplate = new PromptTemplate(userText);
		Message message = promptTemplate.createMessage(Map.of("request", request));

		SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);
		Message systemMessage = systemPromptTemplate.createMessage(Map.of("request", request));
		Prompt prompt = new Prompt(List.of(message, systemMessage));
		return aiChatModel.call(prompt).getResult().getOutput().getContent();
	}

}

				
			

Controller Class: Create a controller class to expose an endpoint for generating responses based on the prompt.

				
					package com.example.demo.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.service.SpringAIService;

@RestController
public class SpringAIController {

	@Autowired
	SpringAIService aiService;

	@GetMapping("/ai")
	ResponseEntity<Map<String, String>> completion(
			@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {

		return new ResponseEntity<Map<String, String>>(aiService.getdummyChatResponse(message), HttpStatus.OK);
	}

	@GetMapping("/prompts")
	ResponseEntity<String> getPromptsResponse(@RequestParam(value = "message") String message) {

		return new ResponseEntity<String>(aiService.getPromptResponse(message), HttpStatus.OK);
	}

}

				
			

Testing the Prompt

Start your Spring Boot application and test the prompt using Postman or any API testing tool.

  • Test the Prompt:
    • Open Postman (or your preferred API testing tool).
    • Create a new GET request with the following URL:
				
					http://localhost:8080/prompts?message=kolkata
				
			
    • Replace Your text here with the text you want to summarize.
    • Click Send to execute the request and view the generated summary.

Conclusion

Using prompts with Spring AI is a powerful way to make your AI applications more effective and user-friendly. By setting up and customizing prompts, you can guide the AI to provide specific and useful responses based on your needs. This guide has shown you how to create and use prompts in Spring AI, helping you get started with practical examples.

Now that you have the basics, you can experiment with different prompts and see how they improve your AI interactions. Keep refining your prompts to get the best results and make your applications smarter and more responsive.

Happy coding!

Leave a Comment