Spring AI文档切片

添加依赖:

bash 复制代码
   <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-tika-document-reader</artifactId>
        </dependency>

Java代码:

java 复制代码
package com.zkwm.springai.rag;

import org.springframework.ai.document.Document;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/docChunk")
public class DocumentChunkController {
    @Autowired
    private VectorStore vectorStore;

    @GetMapping("/processAndStore")
    public String processAndStore(String filePath) {

        TokenTextSplitter splitter = TokenTextSplitter.builder()
                .withChunkSize(500)    // 每块最大 Token 数
                .withMinChunkSizeChars(50)   // 最小字符数
                .withMaxNumChunks(10000)     // 最大分块数
                .withKeepSeparator(true)     // 保留分隔符
                .build();
        // 1. 读取文档(PDF/Word/TXT)
        TikaDocumentReader reader = new TikaDocumentReader(new FileSystemResource(filePath));
        List<Document> documents = reader.read();
        // 2. 文档切片(核心)
        List<Document> chunks = splitter.apply(documents);
        // 3. 向量化并存入向量库
        vectorStore.add(chunks);
        return "ok";
    }
}
相关推荐
苍煜12 小时前
SpringBoot Spring事务完整版详解:@Transactional注解实操 + 七大事务传播机制用法
spring boot·spring·oracle
小则又沐风a12 小时前
list模拟实现
java·服务器·list
卷卷说风控12 小时前
【卷卷观察】Redis 之父用 AI 写新数据类型:4个月,我干了以前一年才敢干的事
人工智能·redis·bootstrap
上弦月-编程12 小时前
C语言链表详解,新手也能看懂! ——从入门到精通的完整教程
java·c语言·c++
陈天伟教授12 小时前
假期细节-丁达尔效应-光影的折射
人工智能·科技·开源
网络工程小王12 小时前
[RAG 与文本向量化详解]RAG篇
数据库·人工智能·redis·机器学习
DogDaoDao12 小时前
【GitHub】Warp 终端深度解析:Rust + GPU 加速的 AI 原生终端开源架构
人工智能·程序员·rust·开源·github·ai编程·warp
sunneo12 小时前
专栏D-团队与组织-05-冲突与决策
前端·人工智能·产品运营·aigc·产品经理·ai-native
生成论实验室12 小时前
《事件关系阴阳博弈动力学:识势应势之道》第十篇:识势应势——从认知到行动的完整闭环
人工智能·算法·架构·创业创新·安全架构
Aision_12 小时前
为什么 CTI 场景需要知识图谱?
人工智能·python·安全·web安全·langchain·prompt·知识图谱