微信扫码
添加专属顾问
我要投稿
文本切分五个层级:
Level 1: Character Splitting - 简单的字符长度切分
Level 2: Recursive Character Text Splitting - 通过分隔符切分,然后递归合并
Level 3: Document Specific Splitting - 针对不同文档格式切分 (PDF, Python, Markdown)
Level 4: Semantic Splitting - 语义切分
Level 5: Agentic Splitting-使用代理实现自动切分
这个 切分器 的工作原理是确定何时分隔句子。这是通过查找任意两个句子之间的向量差异来完成的。当该差异超过某个阈值时,它们将被拆分。后面演示它是怎么实现的:
搭建语义切分流程
数据加载
# This is a long document we can split up.with open("state_of_the_union.txt") as f:state_of_the_union = f.read()创建拆分器
要实例化 SemanticChunker,我们必须指定一个嵌入模型。下面我们将使用 OpenAIEmbeddings,也可以使用自己的模型。
from langchain_experimental.text_splitter import SemanticChunkerfrom langchain_openai.embeddings import OpenAIEmbeddingstext_splitter = SemanticChunker(OpenAIEmbeddings())
拆分文本
docs = text_splitter.create_documents([state_of_the_union])print(docs[0].page_content)
这样我们就完成了基于向量的语义切分;下面介绍其参数控制:
切分的几种形式
text_splitter = SemanticChunker( OpenAIEmbeddings(), breakpoint_threshold_type="percentile")docs = text_splitter.create_documents([state_of_the_union])print(docs[0].page_content)
print(len(docs))
# 26
标准差
在此方法中,任何大于 X 个标准差的差值都将被拆分。
text_splitter = SemanticChunker( OpenAIEmbeddings(), breakpoint_threshold_type="standard_deviation")docs = text_splitter.create_documents([state_of_the_union])print(docs[0].page_content)
print(len(docs))
# 4
四分位距
在这种方法中,四分位数距离用于分割块。
text_splitter = SemanticChunker( OpenAIEmbeddings(), breakpoint_threshold_type="interquartile")docs = text_splitter.create_documents([state_of_the_union])print(docs[0].page_content)
print(len(docs))
# 25
梯度
在这种方法中,距离的梯度与百分位数方法一起用于分割块。当块彼此高度相关或特定于某个领域时,此方法非常有用。这个想法是在梯度数组上应用异常检测,使分布变得更宽,并且易于识别高度语义数据中的边界。
text_splitter = SemanticChunker( OpenAIEmbeddings(), breakpoint_threshold_type="gradient")docs = text_splitter.create_documents([state_of_the_union])print(docs[0].page_content)
print(len(docs))
26
以上介绍了langchain基于向量的语义切分实现,后续将介绍具体的算法实现和其它语义切分方式,敬请期待。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-04-06
全网爆火的大模型AI知识库,保姆级教程来了
2026-04-02
RAG进化了,深扒Claude Code源码中RAG高级技巧
2026-04-01
Claude Code 源码一夜流出:会看热闹的人很多,会读源码的人很少
2026-03-31
OpenDataLoader:PDF文档提取的一站式方案
2026-03-30
只用文件系统和 Bash,Vercel 做出了一套高效 RAG
2026-03-25
GraphRAG新范式 = LPG + 本体RDF
2026-03-25
基于 Ray 的蚂蚁数据构建引擎在搜推和 RAG 场景的实践
2026-03-23
知识基座:让“AI 越用越懂业务”的团队经验实践【天猫AI Coding实践系列】
2026-01-15
2026-02-13
2026-02-03
2026-02-03
2026-02-06
2026-02-02
2026-01-28
2026-02-05
2026-02-06
2026-02-06
2026-03-17
2026-03-11
2026-02-22
2026-02-15
2026-02-04
2026-02-03
2026-01-19
2026-01-12