包阅导读总结
1. `LangChain4J`、`Spring Boot`、`JSON 输出`、`语言模型`、`结构化信息`
2. 本文介绍了如何在 Spring Boot 应用中使用 LangChain4J 从文本中提取结构化信息并以 JSON 格式输出。涵盖了应用设置、数据结构定义、AI 服务接口创建、语言模型配置等步骤,并提供了完整的示例代码。
3. – 应用设置
– 介绍了在 `pom.xml` 文件中添加所需依赖,包括 `langchain4j` 相关和 `Spring Boot` 的 `web` 依赖。
– 数据结构定义
– 定义了用于存储提取的书籍数据的 `record` 或 `class` 。
– AI 服务接口创建
– 创建接口,使用 `@UserMessage` 注解指定提取书籍数据的提示。
– 语言模型配置
– 使用 `OpenAI API` 密钥和指定模型名称配置语言模型,启用请求和响应的日志记录。
– 运行应用
– 提供了完整的示例代码,运行时将输入文本发送到 `OpenAI API` 进行处理并输出结构化的 `Book` 对象。
– 结论
– 总结了使用 `LangChain4J` 实现结构化 JSON 输出的过程。
思维导图:
文章地址:https://www.javacodegeeks.com/using-langchain4j-for-structured-json-output.html
文章来源:javacodegeeks.com
作者:Omozegie Aziegbe
发布时间:2024/7/24 13:53
语言:英文
总字数:924字
预计阅读时间:4分钟
评分:87分
标签:LangChain4J,Spring Boot,JSON Output,OpenAI,Java
以下为原文内容
本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com
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;@SpringBootApplicationpublic 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:
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.