慣性聚合 高效追讀感興趣之博客、新聞、科技資訊
閱原文 以慣性聚合開啟

推薦訂閱源

博客园 - 司徒正美
V
V2EX
T
Tailwind CSS Blog
有赞技术团队
有赞技术团队
aimingoo的专栏
aimingoo的专栏
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
月光博客
月光博客
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
V
Visual Studio Blog
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python)
大块分割之策于大语言模型应用:善用之,以臻更优之RAG系统
Vivek · 2026-05-24 · via DEV Community

学如何分块影响检索质量、嵌入性能及检索增强生成(RAG)系统之整体效能.

引言

造AI应用,用检索增强生成(RAG)时,开发者常专注择优LLM或嵌入模型。然一基始之步,往往未足为重。分段

分段

分段者,乃将宏文析为微段,俟生成嵌入,而存诸向量之库也.

分段不善,可致:

  • 取非所取之果
  • 妄生之答
  • 遗境之失
  • 推演之费增

善其分块者,则检索之精与应答之质,皆可骤增。

兹文之中,吾等将探赜索隐于众习之最。分块之术其权衡,及其所用之时。


何谓分块之要

大语言模型与嵌入模型,皆不能高效处理无限长之文书。

试思二百页之PDF文。

非将全文作一矢嵌入,乃分之为一小段:

Large Document
      ↓
 Chunking
      ↓
Embeddings
      ↓
Vector Database
      ↓
Semantic Retrieval
      ↓
LLM Response

入全屏模式 出全屏模式

无分段

一巨矢嵌入:

  • 失义理之精微
  • 得不相关之节段
  • 增符文之费

以分块之法

相关文段可寻可取。


明辨分块之权衡

块大小,影响检索之质。

甚微

Missing context

入全景模式 出全屏模式

甚巨

Noise + irrelevant information

入全景模式 退出全屏模式

理想之块平衡:

  • 语义之意义
  • 检索之精准
  • 符号之效率

1. 固定大小之块分

至简且最广用之法.

文牍依固定字符或符号之限而分.

例:

  • 五百符号
  • 千字文

其法如何

Document
──────────────────────────
Chunk 1 (500 tokens)
Chunk 2 (500 tokens)
Chunk 3 (500 tokens)

入全屏模式 出全屏模式

诸子之例

用LangChain:

from langchain.text_splitter import CharacterTextSplitter

splitter = CharacterTextSplitter(
    chunk_size=500,
    chunk_overlap=50
)

chunks = splitter.split_text(document)

入全屏模式 出全屏模式


其长

  • 易施之
  • 速其理
  • 可预知之块大小

缺点

  • 罔顾文书结构
  • 或斩句于中
  • 或减损义理

适于

  • 速制原型
  • 小量数据
  • 简易RAG系统

2. 递归分块

更智之版,固分寸之块也。

非盲分,乃存结构。

常序:

唯大节逾制,方再分。


工作流

Paragraph too large?
        ↓
Split into sentences
        ↓
Sentence too large?
        ↓
Split into words

入全屏模式 退出全屏模式


LangChain 递归分割器:

from langchain.text_splitter import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(
    chunk_size=500,
    chunk_overlap=50
)

chunks = splitter.split_text(document)

进入全屏模式 退出全屏模式


优点

  • 保存意义
  • 提升检索质量
  • 处理混合文档

缺点

  • 稍慢
  • 犹可忽其域之结构

最宜

众RAG系统.

此常为之常选.


3. 句本分段

此策使段与句界相合.

非任择其数:

Chunk = Complete Sentences

入全景模式 出全景模式


文:

AI systems rely on retrieval.
Chunking improves retrieval quality.
Poor chunking hurts accuracy.

入全景模式 出全景模式

或然之段:

Chunk 1:
AI systems rely on retrieval.

Chunk 2:
Chunking improves retrieval quality.

Chunk 3:
Poor chunking hurts accuracy.

入全景模式 出全景模式


Python之例

用NLTK:

import nltk
from nltk.tokenize import sent_tokenize

sentences = sent_tokenize(document)

入全景模式 出全景模式


优点

  • 自然语言边界
  • 嵌入更洁
  • 语义更完整

缺点

  • 片段大小不均
  • 长句或超限

适用场景

  • 对话数据
  • 文章
  • 问答系统

4. 按段分块

段落多含贯通之理.

是故适为分块之界.


Paragraph 1 → Chunk 1
Paragraph 2 → Chunk 2
Paragraph 3 → Chunk 3

入全屏模式 出全屏模式


  • 义理相贯
  • 人可读之段
  • 适于博客与文牍

缺点

  • 段落长短不一
  • 长段或可溢出

最佳适用

  • 博客
  • 文牍
  • 研究论文

5. 重叠分段

分段之一大弊:

境界失于边畔.

例:

块一:

The API authentication uses JWT...

入全屏模式 出全屏模式

块二:

...tokens for secure communication.

入全屏模式 出全屏模式

要义横跨两块.

重叠可解此困.


重叠之效如何

Chunk 1
──────────────
AAAA BBBB CCCC

Chunk 2
          CCCC DDDD EEEE

入全景模式 出全景模式

告:

CCCC

见于两段之中


代码示例

splitter = RecursiveCharacterTextSplitter(
    chunk_size=500,
    chunk_overlap=100
)

入全景模式 出全景模式


优点

  • 检索连续性更佳
  • 减少边界之患
  • 应答之准确度更高

  • 嵌入增多
  • 向量存储增广
  • 检索成本加增

适于

几乎所有生产RAG系统

典型重叠:

  • 10–20%

6. 语义分段

语义分段,取义不取量。

文牍之分,适于题旨之变。

此智识远胜。


概念

非若:

Every 500 tokens

入全屏模式 出全屏模式

乃分于:

Meaning shift

入全屏模式 出全屏模式


例证

文檔:

Section A → Databases
Section B → Kubernetes
Section C → Security

全屏模式開啟 全屏模式退出

語義分塊創造:

Chunk 1 → Database topic
Chunk 2 → Kubernetes topic
Chunk 3 → Security topic

全屏模式開啟 全屏模式退出


高階流程

Text
 ↓
Sentence embeddings
 ↓
Similarity comparison
 ↓
Topic boundary detection
 ↓
Chunks

全屏模式開啟 全屏模式退出


語言範例(概念性)

from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity

入全景模式 出全景模式

句相似度以定分断之位


优点

  • 检索质优
  • 识主题
  • 境合紧密

缺点

  • 算力耗费大
  • 施为需力多

适于

  • 企业搜索
  • 法律文书
  • 知识库

7. 结构感知分段

或有文书已具结构。

例:

  • HTML标题
  • Markdown段落
  • 带标题之PDF
  • 代码文件

不若漠视,吾用之。


例证

Markdown(马克道克)

# Authentication
JWT details...

# Rate Limiting
API throttling...

入全景模式 出全屏模式

段块:

Authentication section
Rate Limiting section

入全屏模式 出全屏模式


代码示例

Markdown標題分離器:

from langchain.text_splitter import MarkdownHeaderTextSplitter

headers = [
    ("#", "Header1"),
    ("##", "Header2")
]

入全景模式 退出全屏模式


优点

  • 语义高度一致
  • 善用作者意图
  • 宜于著述

缺点

  • 恃格式之洁
  • 于素文稍逊

最宜

  • 开发者之文
  • 类书之属
  • 技術手冊

8. 代码分块

源代码需特殊处理.

每500字符分割或可破逻辑.

宜:

宜:

  • 函数
  • 模块
  • 抽象语法树节点

不良分块

def login():
    ...

入全景模式 出全景模式

中途截断.


更优分段

Entire login() function

入全景模式 出全景模式


树状结构示例

import tree_sitter

入全景模式 出全景模式

基于抽象语法树的解析,语法得以保全。


优点

  • 逻辑结构之维系
  • 代码检索之优
  • 人工智能编程助手之强

  • 语言特定之工具

  • 最佳适用
  • 代码协作者
  • 仓库检索 __JHSNS_SEG_77cc3b1c_266__ 软件文档

比较分段之策

策略 质量 复杂度 最佳用途
固定大小 原型
递归 通用 RAG
句子 质询
段落 文章
重叠 制作 RAG
语义 极高 企业
结构感知 文档
代码分段 极高 代码人工智能

实用分段之策

多成功之RAG系统,用混合之法.

例:

Structure-aware
        +
Recursive splitting
        +
10–20% overlap

入全景模式 出全景模式

流程:

Document
   ↓
Heading Split
   ↓
Recursive Chunking
   ↓
Overlap
   ↓
Embeddings
   ↓
Vector DB

入全景模式 出全景模式

此常得权衡之至:

  • 切合
  • 费用
  • 简易

终章所思

分块非仅预演之事。

直接影响之:

  • 检索之精微
  • 嵌入之质
  • 幻听之率
  • 用戶體驗

无通策之至善。

善法者:

  • 始以递归 + 重叠
  • 迁于语义或结构感知分块 甚繁
  • 宜用知码分块以营工程之制

多事之中,善为分块,其得大焉,胜于易大之LLM