【Spring AI】09. ETL 管道

文章目录

ETL Pipeline

提取转换加载 (ETL)框架是检索增强生成 (RAG)中数据处理的支柱。

ETL 管道编排了从原始数据源到结构化向量存储的流程,确保数据以最佳格式存储,以便 AI 模型检索。

RAG 用例是通过从数据体中检索相关信息来增强生成模型的能力,以提高生成输出的质量和相关性。

API 概述


ETL 管道的三个主要组件是

  • DocumentReader :实现Supplier<List>
  • DocumentTransformer :实现Function<List, List>
  • Consumer<List> :实现DocumentWriter
    Document类包含文本和元数据,使用DocumentReader可以基于 PDF、文本文件和其他文档类型创建 Document类。
    要构建一个简单的 ETL 管道,您可以将每种类型的实例链接在一起。

假如我们有这三种 ETL 类型的实例对象

  • PagePdfDocumentReade: DocumentReader的一个实现
  • TokenTextSplitter:DocumentTransformer的一个实现
  • VectorStore: DocumentWriter的一个实现
    使用以下代码,可以结合检索增强生成模式,把基本的数据加载到矢量数据库中。
java 复制代码
vectorStore.accept(tokenTextSplitter.apply(pdfReader.get()));

入门指南


要开始创建一个 Spring AI RAG 应用程序,请按照以下步骤进行:

  1. 下载最新的 Spring CLI Release,并按照 installation instructions 进行安装。

  2. 要创建一个简单的基于 OpenAI 的应用程序,请使用以下命令:

    shell 复制代码
    spring boot new --from ai-rag --name myrag
  3. 查看生成的README.md文件,了解如何获取 OpenAI API 密钥并运行您的第一个 AI RAG 应用程序。

ETL 接口和实现

ETL 管道由以下接口和实现组成。详细的 ETL 类图在下面的 ETL 类图部分 中显示。

DocumentReader

提供来自不同来源的文档资源。

java 复制代码
public interface DocumentReader extends Supplier<List<Document>> {

}
JsonReader

JsonReader解析 JSON 格式的文档。

例子:

java 复制代码
@Component
public class MyAiApp {

	@Value("classpath:bikes.json") // This is the json document to load
	private Resource resource;

	List<Document> loadJsonAsDocuments() {
		JsonReader jsonReader = new JsonReader(resource, "description");
		return jsonReader.get();
	}
}
TextReader

该TextReader处理纯文本文档。

例子:

java 复制代码
@Component
public class MyTextReader {

    @Value("classpath:text-source.txt") // This is the text document to load
	private Resource resource;

	List<Document> loadText() {
		TextReader textReader = new TextReader(resource);
		textReader.getCustomMetadata().put("filename", "text-source.txt");

		return textReader.get();
    }
}
PagePdfDocumentReader

该PagePdfDocumentReader使用 Apache PdfBox 库来解析 PDF 文档

例子:

java 复制代码
@Component
public class MyPagePdfDocumentReader {

	List<Document> getDocsFromPdf() {

		PagePdfDocumentReader pdfReader = new PagePdfDocumentReader("classpath:/sample1.pdf",
				PdfDocumentReaderConfig.builder()
					.withPageTopMargin(0)
					.withPageExtractedTextFormatter(ExtractedTextFormatter.builder()
						.withNumberOfTopTextLinesToDelete(0)
						.build())
					.withPagesPerDocument(1)
					.build());

		return pdfReader.get();
    }

}
ParagraphPdfDocumentReader

该ParagraphPdfDocumentReader使用 PDF 目录(例如 TOC)信息将输入的 PDF 拆分为文本段落,并为每个段落输出一个Document。注意:并非所有 PDF 文档都包含 PDF 目录。

例子:

java 复制代码
@Component
public class MyPagePdfDocumentReader {

	List<Document> getDocsFromPdfwithCatalog() {

        new ParagraphPdfDocumentReader("classpath:/sample1.pdf",
                PdfDocumentReaderConfig.builder()
                    .withPageTopMargin(0)
                    .withPageExtractedTextFormatter(ExtractedTextFormatter.builder()
                        .withNumberOfTopTextLinesToDelete(0)
                        .build())
                    .withPagesPerDocument(1)
                    .build());

		return pdfReader.get();
    }
}
TikaDocumentReader

TikaDocumentReader使用 Apache Tika 从各种文档格式中提取文本,如 PDF、DOC/DOCX、PPT/PPTX 和 HTML。有关支持的格式的详细列表,请参考 Tika documentation。

例子:

java 复制代码
@Component
public class MyTikaDocumentReader {

    @Value("classpath:/word-sample.docx") // This is the word document to load
	private Resource resource;

	List<Document> loadText() {
        TikaDocumentReader tikaDocumentReader = new TikaDocumentReader(resourceUri);
        return tikaDocumentReader.get();
    }
}

DocumentTransformer

作为处理工作流程的一部分,用于转换文档。

java 复制代码
public interface DocumentTransformer extends Function<List<Document>, List<Document>> {
TextSplitter

TextSplitter是一个抽象基类,帮助将文档分割以适应 AI 模型的上下文窗口。

TokenTextSplitter

在保持标记级完整性的同时拆分文档。

ContentFormatTransformer

确保所有文档中的内容格式统一。

KeywordMetadataEnricher

关键元数据增强文档。

SummaryMetadataEnricher

为增强检索而为文档添加摘要元数据。

DocumentWriter

管理 ETL 过程的最后阶段,将文档进行存储。

java 复制代码
public interface DocumentWriter extends Consumer<List<Document>> {

}
FileDocumentWriter

将文档持久化到文件中。

VectorStore

与各种向量存储进行集成。请参阅 05. 向量数据库 章节以获取完整列表。

ETL 类图

以下类图展示了 ETL 接口和实现。


相关推荐
turbolove4 分钟前
暑期编程预习指南
人工智能
阿_旭14 分钟前
超越YOLO! RT-DETR 实时目标检测技术介绍
人工智能·深度学习·yolo·目标检测·rt-detr
虫小宝18 分钟前
在Spring Boot中实现多线程任务调度
java·spring boot·spring
许思王21 分钟前
【Python】组合数据类型:序列,列表,元组,字典,集合
开发语言·人工智能·python
巴拉巴拉朵23 分钟前
大语言模型基础
人工智能·语言模型·自然语言处理
李加号pluuuus3 小时前
【扩散模型】LCM LoRA:一个通用的Stable Diffusion加速模块
人工智能·stable diffusion
Alkali!4 小时前
2-5 softmax 回归的简洁实现
人工智能·数据挖掘·回归
哥廷根数学学派5 小时前
基于Maximin的异常检测方法(MATLAB)
开发语言·人工智能·深度学习·机器学习
xrgs_shz5 小时前
人工智能、机器学习、神经网络、深度学习和卷积神经网络的概念和关系
人工智能·深度学习·神经网络·机器学习·卷积神经网络
zzyincsdn7 小时前
从FasterTransformer源码解读开始了解大模型(2.1)代码通读03
人工智能·llm·fastertransform