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";
    }
}
相关推荐
IT枫斗者几秒前
浏览器跨域预检 OPTIONS:什么时候触发,为什么被接口拒绝
java·开发语言·数据库·mysql·fastapi
愚公搬代码4 分钟前
【愚公系列】《微信小程序项目实战(AI编程+视频图解)》045-项目实战:使用 AI 编程工具辅助实现我的订单页面
人工智能·微信小程序·ai编程
xyphf_和派孔明12 分钟前
第九章: Agent Skill 研发中的常见误区及总结
人工智能
SimonKing13 分钟前
可以自动部署项目的SSH客户端,部署时间可以摸鱼了
java·后端·程序员
硅谷秋水14 分钟前
InstAgent:一种用于异构机器人协作的RSI智能体操作系统
人工智能·机器学习·语言模型·机器人
小oo呆15 分钟前
【自然语言处理与大模型】LangChainV1.0入门指南:短期记忆与长期记忆的区别
人工智能
水巷石子21 分钟前
从请求到逐字返回:用 Spring WebFlux + SSE 打通 Java 大模型的打字机流式输出
java·后端·spring
水巷石子23 分钟前
从一行调用到四种入口:Spring AI 1.x ChatClient 实战与 LangChain4j 选型对比
java·人工智能·spring·langchain
核数聚35 分钟前
具身数据采集的合规工程:授权链、脱敏流水线与数据溯源
人工智能·机器人·核数聚
LucianaiB36 分钟前
【华为云码道|Agent / 自定义 Skills 】把架构红线写成 AI 照做的 Skill:多模块隐蔽故障排错实测
java·架构·华为云·atomgit·华为云码道·ai写码你来写故事