Core Java

Using LangChain4J for Structured JSON Output

LangChain4J is a powerful framework that enables the integration of language models into Java applications. In this article, we will demonstrate how to use LangChain4J in a Spring Boot application to extract structured information from text and output it as JSON.

1. Setting Up the Application

Here is an example of a pom.xml file that includes the necessary dependency for a Spring Boot application using LangChain4J.

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
            <version>0.32.0</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-spring-boot-starter</artifactId>            
            <version>0.32.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2. Defining the Data Structure

Next, we define a simple record to hold the extracted book data.

record Book(String title, String author, int year, String genre) {}

The Book record contains fields for the title, author, year of publication, and genre of the book we want to extract from the text.

We can also use a Book class instead of a record.

public class Book {

    private String title;
    private String author;
    private int year;
    private String genre;

    public Book() {
    }

    public Book(String title, String author, int year, String genre) {
        this.title = title;
        this.author = author;
        this.year = year;
        this.genre = genre;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    @Override
    public String toString() {
        return "Book{" + "title=" + title + ", author=" + author + ", year=" + year + ", genre=" + genre + '}';
    }

}

3. Creating the AI Service Interface

We create an interface to define the method for extracting the book data from the input text. The @UserMessage annotation specifies the prompt that will be sent to the language model.

import dev.langchain4j.service.UserMessage;

interface BookExtractor {

    @UserMessage("""
            Extract the title, author, year of publication, and genre of the book described below.
            Return only JSON, without any markdown markup surrounding it.
            Here is the document describing the book:
            ---
            {{it}}
            """)
    Book extract(String text);

}

This interface method extract takes a String input and returns a Book object. The prompt guides the language model to extract and return the required data in JSON format.

4. Configuring the Language Model

We then configure the language model using the OpenAI API key and specify the model name (GPT_3_5_TURBO). We also enable logging for requests and responses to facilitate debugging and monitoring.

@Bean("bookExtractionApplicationRunner")
ApplicationRunner applicationRunner() {
  return args -> {
    ChatLanguageModel model = OpenAiChatModel.builder()
        .apiKey(OPENAI_API_KEY)
        .modelName(OpenAiChatModelName.GPT_3_5_TURBO)
        .logRequests(true)
        .logResponses(true)
        .build();

    BookExtractor bookExtractor = AiServices.create(BookExtractor.class, model);

In this configuration, we create an instance of ChatLanguageModel using the OpenAiChatModel builder, providing the necessary API key and model name. We enable logging to capture the details of requests and responses for easier troubleshooting. Using AiServices.create, we create an instance of BookExtractor which will handle the text extraction based on the prompt we defined earlier.

Finally, we provide the input text and call the extract method to extract the structured information. The extracted Book object is then printed to the console.

String inputText = """
                    "The Age of Reason" is a work by the English and American political activist Thomas Paine, 
                    arguing for the philosophical position of deism. Originally published in 1794, it critiques 
                    institutionalized religion and challenges the legitimacy of the Bible. The book is classified 
                    under the genre of philosophy and has been influential in the development of secularism.
                    """;

            Book book = bookExtractor.extract(inputText);

            System.out.println(book);
        };
    }

In this example, the input text describes a book titled “The Age of Reason” by Thomas Paine. The language model processes this text, extracts the relevant information, and returns it in a structured format as a Book object.

5. Running the Application

Here is the full example code used for this article:

import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiChatModelName;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class StructuredResponseAppApplication {

    @Value("${OPENAI_API_KEY}")
    private String OPENAI_API_KEY;

    public static void main(String[] args) {
        SpringApplication.run(StructuredResponseAppApplication.class, args);
    }

    @Bean("bookExtractionApplicationRunner")
    ApplicationRunner applicationRunner() {
        return args -> {
            ChatLanguageModel model = OpenAiChatModel.builder()
                    .apiKey(OPENAI_API_KEY)
                    .modelName(OpenAiChatModelName.GPT_3_5_TURBO)
                    .logRequests(true)
                    .logResponses(true)
                    .build();

            BookExtractor bookExtractor = AiServices.create(BookExtractor.class, model);

            String inputText = """
                    "The Age of Reason" is a work by the English and American political activist Thomas Paine, 
                    arguing for the philosophical position of deism. Originally published in 1794, it critiques 
                    institutionalized religion and challenges the legitimacy of the Bible. The book is classified 
                    under the genre of philosophy and has been influential in the development of secularism.
                    """;

            Book book = bookExtractor.extract(inputText);

            System.out.println(book);
        };
    }

}

When you run the application, The request sent to OpenAI API contains the input text and the prompt defined in the @UserMessage annotation. The prompt guides the language model to extract the book’s title, author, year of publication, and genre, and return this information in JSON format.

The request logged in the console would look similar to this:

structured json output

6. Conclusion

In this article, we explored how to use LangChain4J to extract structured information from text and output it as JSON in a Spring Boot application. We covered the definition of the data structure, the creation of an AI service interface, the configuration of the language model, and the extraction of structured information from input text.

7. Download the Source Code

This article covered using LangChain4J to produce structured JSON output.

Download
You can download the full source code of this example here: structured json output

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button