EP229:面向 RAG 智能体的 8 种分块策略
ByteByteGo 系统设计周刊介绍 8 种 RAG 分块策略,并简述 Google 翻译神经机器翻译流程与代码测试等话题。
中文处理结果
一站式智能云(赞助)
你的应用,几秒上线,免费使用。Railway 的按键式计算、存储与网络服务在注册后即可使用。
人类与智能体都运行在同一套垂直整合系统上,涵盖硬件、软件以及从提示到生产的体验。
通过下方链接注册,获取免费云额度,让你的智能体开工吧。
免费开始
本周系统设计速览:
- 如何测试你的代码(即使代码由 AI 编写)
- 面向 RAG 智能体的 8 种分块策略
- Google 翻译:神经翻译背后的理念
- 智能体技能(Agent Skills)清晰解释
- PostgreSQL 与 MySQL 对比
如何测试你的代码(即使代码由 AI 编写)
面向 RAG 智能体的 8 种分块策略
- 固定大小:每隔固定数量的 token 切分文本,并保留少量重叠。
- 递归:先按段落切分,只有当某片段仍然过大时,才回退到按行、再按词和字符切分。
- 基于文档:在文档本身已标记的断点处切分,例如标题、章节、HTML 标签,因此一个 Markdown 文件会自然按章节各成一块。
- 语义:对每个句子做嵌入,检查它与前一句的相似度,当相似度下降时开始新块。这样能找到真正的主题转换,而不是按长度猜测。
- 基于 LLM:让 LLM 把文本改写为独立命题,每行一个想法,再把每行变成一块;质量高,但在大规模语料上成本昂贵。
- 智能体式:由 AI 智能体阅读每份文档,判断其类型(代码、合同、表格),再为该文件选择分块方法,而不是对所有内容使用同一规则。
- 后置分块(Late Chunking):先把整份文档送入嵌入模型,再应用常规切分边界,并在每个边界内做均值池化,生成块嵌入。
- 层级式:把文档拆成层级、章节、段落,再到句子。
轮到你了:你在生产环境中使用哪种分块策略?
Google 翻译:神经翻译背后的理念
常见技术的简化示意,并非 Google 翻译当前系统的精确映射。
- 检测语言。一个小型分类器在毫秒级预测源语言。
- 分词。BPE 分词器把文本拆成子词 token,使模型能处理任何语言中的任何词。
- 添加目标 token。模型可处理所有语言对。在 token 序列前添加类似 <2es> 的特殊 token,引导输出偏向西班牙语。
- 编码器。token 经过 Transformer 层,输出为与每个输入 token 对应的向量序列。
- 解码器。随后 Transformer 的解码器部分可基于编码向量预测下一个 token 的概率分布。
- 束搜索。束搜索不是每次贪心地选一个 token,而是利用解码器在生成过程中保留若干候选译文,最终输出概率最高的那个。
原始正文摘录
EP229: 8 Chunking strategies for RAG-based agents
The all-in-one intelligent cloud (Sponsored)
Your app, live in seconds, for free. Railway’s push-button compute, storage, and networking is available immediately upon sign-up.
Humans and agents alike operate on one vertically integrated system including hardware, software, and a prompt-to-production experience.
Sign up with the link below for free cloud credits and let your agents cook.
Start free
This week’s system design refresher:
- How to Test Your Code (Even When AI Writes It)
- 8 Chunking strategies for RAG-based agents
- Google Translate: The Ideas Behind Neural Translation
- Agent Skills, Clearly Explained
- PostgreSQL versus MySQL
How to Test Your Code (Even When AI Writes It)
8 Chunking strategies for RAG-based agents
- Fixed-Size: cuts text every fixed number of tokens, with a small overlap.
- Recursive: splits on paragraphs first, falling back to lines then words and characters only when a piece is still too big.
- Document-Based: splits wherever the document already marks a break, a heading, a chapter, an HTML tag, so a markdown file becomes one chunk per section on its own.
- Semantic: embeds each sentence, checks its similarity to the one before it, and starts a new chunk when that similarity drops. This finds real topic shifts instead of guessing by length.
- LLM-Based: asks an LLM to rewrite the text as standalone propositions, one idea per line, and turns each into a chunk, high quality but expensive across a large corpus.
- Agentic: an AI agent reads each document, decides what it is (code, contract, table), and picks the chunking method for that file instead of one rule for everything.
- Late Chunking: runs the full document through an embedding model, then applies normal split boundaries and mean-pools within each boundary to create chunk embeddings.
- Hierarchical: breaks the document into layers, sections, paragraphs, then sentences
Over to you: which chunking strategy are you running in production?
Google Translate: The Ideas Behind Neural Translation
A simplified illustration of common techniques. Not an exact map of Google Translate’s current system.
- Detect language. A tiny classifier predicts the source language in milliseconds.
- Tokenization. A BPE tokenizer splits the text into subword tokens, so the model can handle any word in any language.
- Add target token. The model can handle every language pair. A special token like <2es> is prepended to the token sequence to steer the output toward Spanish.
- Encoder. The tokens pass through Transformer layers. The output is a sequence of vectors, corresponding to each input token.
- Decoder. Then the decoder part of the Transformer can predict the probability distribution for the next token conditioned on the encoded vectors.
- Beam search. Instead of greedily picking one token at a time, beam search uses the decoder to keep several candidate translations during generation. The final output is the one with the highest probability.
Over to you: Do you think AI labs and frontier models will make services like Google Translate obsolete?
Agent Skills, Clearly Explained
Why do we need Agent Skills? Long prompts hurt agent performance. Instead of one massive prompt, agents keep a small catalog of skills, reusable playbooks with clear instructions, loaded only when needed.
Here is what the Agent Skills workflow looks like:
- User Query: A user submits a request like “Analyze data & draft report”.
- Build Prompt + Skills Index: The agent runtime combines the query with Skills metadata, a lightweight list of available skills and their short descriptions.
- Reason & Select Skill: The LLM processes the prompt, thinks, and decides: “I want Skill X.”
- Load Skill into Context: The agent runtime receives the specific skill request from the LLM. Then, it loads SKILL. md and adds it into the LLM’s active context.
- Final Output: The LLM follows SKILL. md, runs scripts, and generates the final report.
By dynamically loading skills only when needed, Agent Skills keep context small and the LLM’s behavior consistent.
Over to you: What skills would you find most useful in agents?
PostgreSQL versus MySQL
Built using the C language, PostgreSQL uses a process-based architecture.
You can think of it like a factory with a manager (Postmaster) coordinating specialized workers. Each connection gets its own process and shares a common memory pool. Background workers handle tasks like writing data, vacuuming, and logging independently.
MySQL takes a thread-based approach. Imagine a single multi-tasking brain.
It uses a layered design with one server handling multiple connections through threads. The magic happens using pluggable storage engines (such as InnoDB, MyISAM) that you can swap based on your needs.
Over to you: Which database do you prefer?