Spring AI: OutputParsers

Introduction to OutputParsers

In Spring AI, OutputParsers are powerful tools that allow you to process and interpret the responses from large language models (LLMs). They enable you to convert raw model outputs into structured data formats that are easier to work with in your applications. This tutorial will guide you through setting up a Spring Boot application and demonstrate how to use OutputParsers to handle AI-generated content effectively.

1. Setting Up the Project

Step 1: Create a New Spring Boot Project

You can create a new Spring Boot project using Spring Initializr or your preferred IDE. Ensure you include the necessary 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, Spring AI
  • Generate the project and unzip it.

Step 2: Add spring-ai-openai-spring-boot-starter Dependency

In your project's pom.xml, add the following dependency:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

2. Configuring the Spring Boot Starter

Step 1: Add API Key to Configuration

Create a application.properties or application.yml file in your src/main/resources directory and add your OpenAI API key.

For application.properties:

openai.api.key=your_openai_api_key

For application.yml:

openai:
  api:
    key: your_openai_api_key

Step 2: Create a Configuration Class

Create a new configuration class to set up the OpenAI client and the ChatClient abstraction.

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiClient;

@Configuration
public class OpenAiConfig {

    @Bean
    public OpenAiClient openAiClient() {
        return new OpenAiClient();
    }

    @Bean
    public ChatClient chatClient(OpenAiClient openAiClient) {
        return new OpenAiChatClient(openAiClient);
    }
}

3. Implementing the OutputParsers

Step 1: Create a Service for Parsing Output

Create a service class that will handle interactions with the ChatClient abstraction and use OutputParsers to process the AI-generated responses.

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.ai.openai.parser.OutputParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OutputParserService {

    @Autowired
    private ChatClient chatClient;

    @Autowired
    private OutputParser outputParser;

    public List<String> parseAiResponse(String userInput) {
        // Create the chat request
        ChatRequest request = new ChatRequest();
        request.setMessage(userInput);

        // Send the chat request and get the response
        ChatResponse response = chatClient.sendMessage(request);

        // Parse the response using the output parser
        return outputParser.parse(response.getReply());
    }
}

Step 2: Create a Controller for the Service

Create a controller to expose an endpoint for parsing AI-generated responses.

package com.example.demo.controller;

import com.example.demo.service.OutputParserService;
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.List;

@RestController
public class OutputParserController {

    @Autowired
    private OutputParserService outputParserService;

    @GetMapping("/parseResponse")
    public List<String> parseResponse(@RequestParam String userInput) {
        return outputParserService.parseAiResponse(userInput);
    }
}

4. Creating an OutputParser Implementation

Step 1: Create a Custom Output Parser

Create a custom implementation of the OutputParser interface to handle specific parsing logic.

package com.example.demo.parser;

import org.springframework.ai.openai.parser.OutputParser;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class CustomOutputParser implements OutputParser {

    @Override
    public List<String> parse(String output) {
        // Example parsing logic: split the output into sentences
        return Arrays.asList(output.split("\\. "));
    }
}

Step 2: Register the Custom Output Parser

Ensure your custom OutputParser is registered as a Spring bean so it can be injected into the service.

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.demo.parser.CustomOutputParser;
import org.springframework.ai.openai.OpenAiClient;
import org.springframework.ai.openai.ChatClient;
import org.springframework.ai.openai.OpenAiChatClient;

@Configuration
public class OpenAiConfig {

    @Bean
    public OpenAiClient openAiClient() {
        return new OpenAiClient();
    }

    @Bean
    public ChatClient chatClient(OpenAiClient openAiClient) {
        return new OpenAiChatClient(openAiClient);
    }

    @Bean
    public OutputParser outputParser() {
        return new CustomOutputParser();
    }
}

5. Testing the Integration

Step 1: Run the Application

Run your Spring Boot application. Ensure the application starts without errors.

Step 2: Access the Endpoint

Use Postman, curl, or your browser to test the endpoint. For example:

http://localhost:8080/parseResponse?userInput=Tell me a story.

You should receive a response with the parsed AI-generated content.

Conclusion

In this tutorial, you learned how to set up and use the OutputParsers feature in a Spring Boot application with Spring AI. You created a service to handle AI responses, a controller to expose an endpoint, and a custom OutputParser implementation to process the AI-generated content. This setup allows you to transform raw AI outputs into structured and usable data formats, making your AI integrations more powerful and flexible. 

Explore further customization and enhancements to leverage the full potential of OutputParsers in your applications.


Comments