As AI-driven applications evolve, the need to generate and manage structured data becomes increasingly important. Structured outputs in spring ai allow AI models to deliver results in an organized, predictable format, making it easier to process and integrate the output into other systems.
In Spring AI, this is achieved through the use of a Structured Output Converter.
This article will explain what a Structured Output Converter is, why it’s essential, and how you can implement it in your Spring AI projects with practical code examples.
What is a Structured Output Converter?
A Structured Output Converter in Spring AI is a component that transforms the raw output of an AI model into a structured format, such as JSON, XML, or a custom data structure.
This conversion ensures that the output is easily consumable by other parts of your application, facilitating seamless data handling, storage, and further processing.
For example, rather than receiving a plain text response from an AI model, a structured output converter can organize the response into key-value pairs, lists, or objects that align with your application’s data model.
This approach enhances the robustness and scalability of AI-powered applications by ensuring that outputs are standardized and easy to work with.
Implementing Structured Output Conversion in Spring AI
To implement a Structured Output Converter in Spring AI, you’ll need to set up your project and create the necessary components to handle structured data. Here’s how you can do it:
Set Up Your Spring AI Project
We have already discussed about, how to create spring ai project. You can go through that post for initial project setup.
Integrate the Structured Output Converter in Your Service
Now that you have your converter, integrate it into your service layer to process the AI model’s output and return the structured data.
Service Class:
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.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
record HotelDTO(Integer id, String name, String address) {
}
@Service
public class SpringAIService {
@Autowired
ChatClient chatClient;
@Autowired
OpenAiChatModel aiChatModel;
public HotelDTO returnStructuredResponse(String hotel) {
BeanOutputConverter beanOutputConverter = new BeanOutputConverter<>(HotelDTO.class);
String format = beanOutputConverter.getFormat();
String template = """
return hotel details {hotel}.
{format}
""";
Generation generation = aiChatModel
.call(new Prompt(
new PromptTemplate(template, Map.of("hotel", hotel, "format", format)).createMessage()))
.getResult();
return beanOutputConverter.convert(generation.getOutput().getContent());
}
public List returnListStructuredResponse(String hotel) {
BeanOutputConverter> beanOutputConverter = new BeanOutputConverter<>(
new ParameterizedTypeReference>() {
});
String format = beanOutputConverter.getFormat();
String template = """
return all hotel details {hotel}.
{format}
""";
Generation generation = aiChatModel
.call(new Prompt(
new PromptTemplate(template, Map.of("hotel", hotel, "format", format)).createMessage()))
.getResult();
return beanOutputConverter.convert(generation.getOutput().getContent());
}
}
Expose an API Endpoint for Structured Output
Finally, create a REST controller to expose an endpoint that users can call to get structured output from the AI model.
Controller Class:
package com.example.demo.controller;
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("/structured-output")
ResponseEntity
Test Your Structured Output
Start your Spring Boot application and test the structured output by calling the API endpoint using Postman or any other API testing tool.
- Test the API:
- Open Postman and create a new GET request with the URL:
http://localhost:8080/structured-output?hotel=ibis
- To get list of hotel details, try this url:
http://localhost:8080/structured-output-list?hotel=ibis
- Click Send to execute the request and view the structured hotel details response.
Conclusion
Implementing structured outputs in Spring AI allows you to transform raw AI model outputs into organized and easily consumable data.
By using a Structured Output Converter, you ensure that the information generated by your AI models is in a format that can be seamlessly integrated into your application’s workflows.
The steps and code examples provided in this guide give you the tools to start implementing structured outputs in your own Spring AI projects.
As you explore further, you’ll find even more ways to leverage structured data to build robust, scalable AI-driven applications.