【RAG入门教程04】Langchian的文档切分

在 Langchain 中,文档转换器是一种在将文档提供给其他 Langchain 组件之前对其进行处理的工具。通过清理、处理和转换文档,这些工具可确保 LLM 和其他 Langchain 组件以优化其性能的格式接收数据。

上一章我们了解了文档加载器,加载完文档之后还需要对文档进行转换。

  • 文本分割器
  • 集成

Text Splitters

文本分割器专门用于将文本文档分割成更小、更易于管理的单元。

理想情况下,这些块应该是句子或段落,以便理解文本中的上下文和关系。

分割器考虑了 LLM 处理能力的局限性。通过创建更小的块,LLM 可以在其上下文窗口内更有效地分析信息。

  • CharacterTextSplitter
  • RecursiveCharacterTextSplitter
  • Split by tokens
  • Semantic Chunking
  • HTMLHeaderTextSplitter
  • MarkdownHeaderTextSplitter
  • RecursiveJsonSplitter
  • Split Cod

CharacterTextSplitter

from langchain_text_splitters import CharacterTextSplittertext_splitter = CharacterTextSplitter(separator="\n\n",chunk_size=1000,chunk_overlap=200,length_function=len,is_separator_regex=False,
)
  • separator:这是用于标识文本中自然断点的分隔符。在本例中,它被设置为“\n\n”,这意味着分割器将寻找双换行符作为潜在的分割点。
  • chunk_size:此参数指定每个文本块的目标大小,以字符数表示。在这里,它被设置为 1000,这意味着分割器将旨在创建大约 1000 个字符长的文本块。
  • chunk_overlap:此参数允许连续块之间重叠字符。它被设置为 200,这意味着每个块将包含前一个块末尾的 200 个字符。这种重叠可以帮助确保在块之间的边界上不会丢失任何重要信息。
  • length_function:这是一个用于测量文本块长度的函数。在本例中,它被设置为内置的 len 函数,该函数计算字符串中的字符数。
  • is_separator_regex:此参数指定分隔符是否为正则表达式。它被设置为 False,表示分隔符是一个纯字符串,而不是正则表达式模式。

CharacterTextSplitter根据指定的分隔符拆分文本,默认情况下分隔符设置为 ‘\n\n’。chunk_size参数确定每个块的最大大小,并且只有在可行的情况下才会进行拆分。如果字符串以 n 个字符开头,后跟一个分隔符,然后在下一个分隔符之前有 m 个字符,则如果 chunk_size 小于 n + m + len(separator),则第一个块的大小将为 n。

from langchain_community.document_loaders import PyPDFLoaderloader = PyPDFLoader("book.pdf")
pages = loader.load_and_split()from langchain_text_splitters import CharacterTextSplittertext_splitter = CharacterTextSplitter(separator="\n",chunk_size=1000,chunk_overlap=200,length_function=len,is_separator_regex=False,
)texts = text_splitter.split_text(pages[0].page_content)
print(len(texts))# 4texts[0]"""
'Our goal with this book is to provide the guidance and framework for you,the reader, to grow on \nthe path to being a truly excellent database 
reliability engineer (DBRE). When naming the book we \nchose to use thewords reliability engineer , rather than administrator.  \nBen Treynor, 
VP of Engineering at Google, says the following about reliability engi‐ 
neering:  \nfundamentally doing work that has historically been done by an 
operations team, but using engineers with software \nexpertise, and bankingon the fact that these engineers are inherently both predisposed to, and 
have the ability to, \nsubstitute automation for  human labor.  \nToday’s 
database professionals must be engineers, not administrators. 
We build things. We create \nthings. As engineers practicing devops, 
we are all in this together, and nothing is someone else’s \nproblem.As engineers, we apply repeatable processes, establ ished knowledge, 
and expert judgment'
"""texts[1]"""
'things. As engineers practicing devops, we are all in this together, and nothing is someone else’s \nproblem. As engineers, we apply repeatable processes, establ ished knowledge, and expert judgment \nto design, build, and operate production data stores and the data structures within. As database \nreliability engineers, we must take the operational principles and the depth of database expertise \nthat we possess one ste p further.  \nIf you look at the non -storage components of today’s infrastructures, you will see sys‐ tems that are \neasily built, run, and destroyed via programmatic and often automatic means. The lifetimes of these \ncomponents can be measured in days, and sometimes even  hours or minutes. When one goes away, \nthere is any number of others to step in and keep the quality of service at expected levels.  \nOur next goal is that you gain a framework of principles and practices for the design, building, and'
"""

RecursiveCharacterTextSplitter

关键区别在于,如果结果块仍然大于所需的 chunk_size,它将继续分割结果块,以确保所有最终块都在指定的大小限制内。它由字符列表参数化。

from langchain_text_splitters import RecursiveCharacterTextSplittertext_splitter = RecursiveCharacterTextSplitter(# Set a really small chunk size, just to show.separators=["\n\n", "\n", " ", ""],chunk_size=50,chunk_overlap=40,length_function=len,is_separator_regex=False,
)
texts = text_splitter.split_text(pages[0].page_content)
print(len(texts))texts[2]"""
'book is to provide the guidance and framework for'
"""texts[3]"""
'provide the guidance and framework for you, the'
"""

在文本拆分的上下文中,“递归”意味着拆分器将重复将其拆分逻辑应用于生成的块,直到它们满足某些标准,例如小于指定的最大长度。这在处理需要分解成更小、更易于管理的片段(可能在不同的粒度级别)的非常长的文本时特别有用。

Split By Tokens

原文:“The quick brown fox jumps over the lazy dog。”

标记:[“The”、“quick”、“brown”、“fox”、“jumps”、“over”、“the”、“lazy”、“dog”]

在此示例中,文本根据空格和标点符号拆分为标记。每个单词都成为单独的标记。在实践中,标记化可能更复杂,尤其是对于具有不同书写系统的语言或处理特殊情况(例如,“don’t”可能拆分为“do”和“n’t”)。

有各种标记器。

TokenTextSplitter 来自 tiktoken 库。

from langchain_text_splitters import TokenTextSplittertext_splitter = TokenTextSplitter(chunk_size=10, chunk_overlap=1)texts = text_splitter.split_text(pages[0].page_content)texts[0]"""
'Our goal with this book is to provide the guidance'
"""texts[1]"""
' guidance and framework for you, the reader, to'
"""

SpacyTextSplitter 来自spacy库。

from langchain_text_splitters import SpacyTextSplittertext_splitter = SpacyTextSplitter(chunk_size=1000)texts = text_splitter.split_text(pages[0].page_content)

NLTKTextSplitter来自nltk库。

from langchain_text_splitters import NLTKTextSplittertext_splitter = NLTKTextSplitter(chunk_size=1000)texts = text_splitter.split_text(pages[0].page_content)

我们甚至可以利用 Hugging Face 标记器。

from transformers import GPT2TokenizerFasttokenizer = GPT2TokenizerFast.from_pretrained("gpt2")text_splitter = CharacterTextSplitter.from_huggingface_tokenizer(tokenizer, chunk_size=100, chunk_overlap=10
)
texts = text_splitter.split_text(pages[0].page_content)

HTMLHeaderTextSplitter

HTMLHeaderTextSplitter是一个网页代码分块器,它根据 HTML 元素拆分文本,并将相关元数据分配给分块内的每个标头。它可以返回单个分块或将具有相同元数据的元素组合在一起,以保持语义分组并保留文档的结构上下文。此拆分器可与分块管道中的其他文本拆分器结合使用。

from langchain_text_splitters import HTMLHeaderTextSplitterhtml_string = """
<!DOCTYPE html>
<html>
<body><div><h1>Foo</h1><p>Some intro text about Foo.</p><div><h2>Bar main section</h2><p>Some intro text about Bar.</p><h3>Bar subsection 1</h3><p>Some text about the first subtopic of Bar.</p><h3>Bar subsection 2</h3><p>Some text about the second subtopic of Bar.</p></div><div><h2>Baz</h2><p>Some text about Baz</p></div><br><p>Some concluding text about Foo</p></div>
</body>
</html>
"""headers_to_split_on = [("h1", "Header 1"),("h2", "Header 2"),("h3", "Header 3"),
]html_splitter = HTMLHeaderTextSplitter(headers_to_split_on=headers_to_split_on)
html_header_splits = html_splitter.split_text(html_string)
html_header_splits"""
[Document(page_content='Foo'),Document(page_content='Some intro text about Foo.  \nBar main section Bar subsection 1 Bar subsection 2', metadata={'Header 1': 'Foo'}),Document(page_content='Some intro text about Bar.', metadata={'Header 1': 'Foo', 'Header 2': 'Bar main section'}),Document(page_content='Some text about the first subtopic of Bar.', metadata={'Header 1': 'Foo', 'Header 2': 'Bar main section', 'Header 3': 'Bar subsection 1'}),Document(page_content='Some text about the second subtopic of Bar.', metadata={'Header 1': 'Foo', 'Header 2': 'Bar main section', 'Header 3': 'Bar subsection 2'}),Document(page_content='Baz', metadata={'Header 1': 'Foo'}),Document(page_content='Some text about Baz', metadata={'Header 1': 'Foo', 'Header 2': 'Baz'}),Document(page_content='Some concluding text about Foo', metadata={'Header 1': 'Foo'})]
"""

MarkdownHeaderTextSplitter

类似于 HTMLHeaderTextSplitter ,专用于 markdown 文件。

from langchain_text_splitters import MarkdownHeaderTextSplittermarkdown_document = "# Foo\n\n    ## Bar\n\nHi this is Jim\n\nHi this is Joe\n\n ### Boo \n\n Hi this is Lance \n\n ## Baz\n\n Hi this is Molly"headers_to_split_on = [("#", "Header 1"),("##", "Header 2"),("###", "Header 3"),
]markdown_splitter = MarkdownHeaderTextSplitter(headers_to_split_on=headers_to_split_on)
md_header_splits = markdown_splitter.split_text(markdown_document)
md_header_splits"""
[Document(page_content='Hi this is Jim  \nHi this is Joe', metadata={'Header 1': 'Foo', 'Header 2': 'Bar'}),Document(page_content='Hi this is Lance', metadata={'Header 1': 'Foo', 'Header 2': 'Bar', 'Header 3': 'Boo'}),Document(page_content='Hi this is Molly', metadata={'Header 1': 'Foo', 'Header 2': 'Baz'})]
"""

RecursiveJsonSplitter

import requests# This is a large nested json object and will be loaded as a python dict
json_data = requests.get("https://api.smith.langchain.com/openapi.json").json()from langchain_text_splitters import RecursiveJsonSplittersplitter = RecursiveJsonSplitter(max_chunk_size=300)# Recursively split json data - If you need to access/manipulate the smaller json chunks
json_chunks = splitter.split_json(json_data=json_data)json_chunks
"""
{'openapi': '3.0.2','info': {'title': 'LangSmith', 'version': '0.1.0'},'paths': {'/api/v1/sessions/{session_id}': {'get': {'tags': ['tracer-sessions'],'summary': 'Read Tracer Session','description': 'Get a specific session.'}}}},{'paths': {'/api/v1/sessions/{session_id}': {'get': {'operationId': 'read_tracer_session_api_v1_sessions__session_id__get'}}}},{'paths': {'/api/v1/sessions/{session_id}': {'get': {'parameters': [{'required': True,'schema': {'title': 'Session Id', 'type': 'string', 'format': 'uuid'},'name': 'session_id','in': 'path'},{'required': False,'schema': {'title': 'Include Stats','type': 'boolean','default': False},'name': 'include_stats','in': 'query'},{'required': False,'schema': {'title': 'Accept', 'type': 'string'},'name': 'accept','in': 'header'}]}}}},{'paths': {'/api/v1/sessions/{session_id}': {'get': {'responses': {'200': {'description': 'Successful Response','content': {'application/json': {'schema': {'$ref': '#/components/schemas/TracerSession'}}}}}}}}},{'paths': {'/api/v1/sessions/{session_id}': {'get': {'responses': {'422': {'description': 'Validation Error','content': {'application/json': {'schema': {'$ref': '#/components/schemas/HTTPValidationError'}}}}},'security': [{'API Key': []}, {'Tenant ID': []}, {'Bearer Auth': []}]}}}},
...{'components': {'securitySchemes': {'API Key': {'type': 'apiKey','in': 'header','name': 'X-API-Key'},'Tenant ID': {'type': 'apiKey', 'in': 'header', 'name': 'X-Tenant-Id'},'Bearer Auth': {'type': 'http', 'scheme': 'bearer'}}}}]
"""

Split Code

Langchain 中的“Split Code”概念是指将代码划分为更小、更易于管理的段或块的过程。

from langchain_text_splitters import Language[e.value for e in Language]"""
['cpp','go','java','kotlin','js','ts','php','proto','python','rst','ruby','rust','scala','swift','markdown','latex','html','sol','csharp','cobol','c','lua','perl']
"""
from langchain_text_splitters import (Language,RecursiveCharacterTextSplitter,
)PYTHON_CODE = """
def hello_world():print("Hello, World!")# Call the function
hello_world()
"""
python_splitter = RecursiveCharacterTextSplitter.from_language(language=Language.PYTHON, chunk_size=50, chunk_overlap=0
)
python_docs = python_splitter.create_documents([PYTHON_CODE])
python_docs"""
[Document(page_content='def hello_world():\n    print("Hello, World!")'),Document(page_content='# Call the function\nhello_world()')]
"""
JS_CODE = """
function helloWorld() {console.log("Hello, World!");
}// Call the function
helloWorld();
"""

js_splitter = RecursiveCharacterTextSplitter.from_language(language=Language.JS, chunk_size=60, chunk_overlap=0
)
js_docs = js_splitter.create_documents([JS_CODE])
js_docs"""
[Document(page_content='function helloWorld() {\n  console.log("Hello, World!");\n}'),Document(page_content='// Call the function\nhelloWorld();')]
"""

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/25359.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

运维实用小脚本,登录即自动显示系统信息

今天给大家安利一个超级实用的Linux小技巧&#xff0c;让你每次登录终端时都能感受到满满的科技感和效率爆棚&#xff01; 你是否厌倦了每次手动检查系统状态&#xff0c;像内存使用、CPU负载这些繁琐操作&#xff1f;别担心&#xff0c;一个小调整&#xff0c;让这一切自动化…

技术与业务的完美融合:大数据BI如何真正提升业务价值

数据分析有一点经典案例 沃尔玛的啤酒和尿布案例 开始做BI的时候&#xff0c;大家肯定都看过书&#xff0c;那么一定也看过一个经典的案例&#xff0c;就是沃尔玛的啤酒和尿布的案例。这个案例确实很经典&#xff0c;但其实是一个失败的案例。为什么这么说呢&#xff1f;很明显…

Android.mk文件生成的so工程文件并Debug调试native code

1.这里主要展示一下从最原始先新建一个工程 2.将hello的子工程文件放入上面新建好的工程里面&#xff0c;直接拷贝放置这里 3.修改根目录下的settings.gradle 加入hello 4.app工程下的build.gradle加入依赖&#xff0c;这样就可以识别hello中的java包文件 5.MainActivity 中来&…

Leetcode 3040. 相同分数的最大操作数目 II(记忆化搜索)

Leetcode 3040. 相同分数的最大操作数目 II 每次操作给出三种选择&#xff0c;根据三种选择分别进行dfs 超时 class Solution {int dfs(int[] nums, int l, int r, int sum){if(l > r)return 0;int res 0;// 删最前面两个if(sum -1 || nums[l] nums[l 1] sum){res M…

RabbitMQ-topic exchange使用方法

RabbitMQ-默认读、写方式介绍 RabbitMQ-发布/订阅模式 RabbitMQ-直连交换机(direct)使用方法 目录 1、概述 2、topic交换机使用方法 2.1 适用场景 2.2 解决方案 3、代码实现 3.1 源代码实现 3.2 运行记录 4、小结 1、概述 topic 交换机是比直连交换机功能更加强大的…

信息学奥赛初赛天天练-24-二叉树、N叉树遍历技巧与前缀表达式、中缀表达式、后缀表达式应用实战演练

PDF文档公众号回复关键字:20240609 单项选择题(共15题,每题2分,共计30分:每题有且仅有一个正确选项) 5 根节点的高度为1,一根拥有2023个节点的三叉树高度至少为( )。 A 6 B 7 C 8 D 9 8 后缀表达式 6 2 3 + - 3 8 2 / + * 2 ^ 3 + 对应的中缀表达式是( ) A ((…

「动态规划」打家劫舍的变形题,你会做吗?

213. 打家劫舍 IIhttps://leetcode.cn/problems/house-robber-ii/description/ 你是一个专业的小偷&#xff0c;计划偷窃沿街的房屋&#xff0c;每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈&#xff0c;这意味着第一个房屋和最后一个房屋是紧挨着的。同时&#x…

Spring RestClient报错:400 Bad Request : [no body]

我项目采用微服务架构&#xff0c;所以各服务之间通过Spring RestClient远程调用&#xff0c;本来一直工作得好好的&#xff0c;昨天突然发现远程调用一直报错&#xff0c;错误详情如下&#xff1a; org.springframework.web.client.HttpClientErrorException$BadRequest: 400…

使用 Vue 官方脚手架初始化 Vue3 项目

Vite 官网&#xff1a;https://cn.vitejs.dev/ Vue 官网&#xff1a;https://vuejs.org/ Vue 官方文档&#xff1a;https://cn.vuejs.org/guide/introduction.html Element Plus 官网&#xff1a;https://element-plus.org/ Tailwind CSS 官网&#xff1a;https://tailwindcss.…

LocalDate和Date有什么区别?两者如何转换?

LocalDate与Date 在Java中&#xff0c;LocalDate和Date是用来处理日期的两种不同的类。 区别&#xff1a; Date是Java早期的日期类&#xff0c;它包含了日期和时间的信息。但是在Java 8之后&#xff0c;Date类被标记为过时的&#xff0c;推荐使用新的日期时间API&#xff0c;…

Codeforces Round 951 (Div. 2)C. Earning on Bets

Problem - C - Codeforces 合理的答案&#xff1a; 求出 k1 ~ kn 的最小公倍数lcm&#xff0c;如果 lcm/k1 lcm/k2 ... lcm/kn < lcm 即符合题意。 左边之和为我们付的总钱数&#xff0c;右边才是每次选择得到的钱数(都为lcm)。 直接拿1e9检查是否可以分即可&#xff…

基于Django+MySQL的智慧校园系统

此项目基于Django MySQL HTML CSS JS jQuery bootstrap实现的功能有 学生管理部门管理代办清单管理校园论坛校园医疗服务校园看点校园生活助手常用功能入口 1. 一些注意点 1. 页面body会自动有一些边界距&#xff0c;处理方法&#xff1a; <head><style>b…

你还在纠结U盘怎么选吗?小白带你来看

前言 2024年的618活动已经开始了&#xff0c;这个活动买电子产品着实是比其他时间要便宜很多。 前几天小白的一个好朋友问我&#xff1a;U盘该怎么选&#xff1f; 呃&#xff0c;本来是想写“老朋友”的&#xff0c;结果她愣是要我改成“好朋友”。 行吧&#xff0c;那就好朋…

kmp算法c++

kmp算法通过next数组使查找失败时减少跳转后比较的次数来优化字符串查找&#xff0c;next数组存储了前缀和后缀相同的位置信息&#xff0c;类似动规&#xff0c;可以存储查找数组的信息来防止重复查找&#xff0c;最终复杂度可以达到O&#xff08;nm&#xff09;。 以t“abcab…

车载电子电气架构 --- 车载信息安全

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不对。非必要不费力证明自己,无利益不试图说服别人,是精神上的节…

Vite - 开发初体验,以及按需导入配置

目录 开始 创建一个 Vite 项目 项目结构 /src/main.js index.html package.json vite.config.js Vite 项目中使用 vue-router Vite 组件的“按需引入” 传统的方式引入一个组件 传统方式引入带来的问题 解决办法&#xff08;配置 按需引入 插件&#xff09; 示例&…

嵌入式学习——Linux高级编程复习(线程)——day40

1. 线程 1.1 定义 线程是一个轻量级的进程 是一个任务被创建、调度、消亡的过程 1.2 线程和进程的区别与联系 1. 线程是CPU任务调度的最小单元 2. 进程是操作系统资源分配的最小单元 3. 线程&#xff08;Thread&#xff09;是操作系统能够进行运算调度的最小单位…

OpenFeign远程接口调用使用公共模块出现的错误

今天在使用openfeign和sentinel实现fallback服务降级时遇到找不到类型的异常 检查代码发现没有错误&#xff0c;EnableFeignClients也在启动类上标注了 错误信息&#xff1a;A component required a bean of type com.zxc.cloud.apis.PayFeignSentinelApi that could not be f…

《精通ChatGPT:从入门到大师的Prompt指南》第9章:实战练习

第9章&#xff1a;实战练习 9.1 Prompt练习题 在本节中&#xff0c;我们将提供一系列练习题&#xff0c;旨在帮助读者通过实际操作提升使用ChatGPT的能力。这些练习题涵盖了从基础到高级的不同难度级别&#xff0c;并针对各种应用场景设计&#xff0c;确保读者能够在实际使用…

基于Springboot + vue实现的火锅店管理系统

作者主页&#xff1a;Java码库 主营内容&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app等设计与开发。 收藏点赞不迷路 关注作者有好处 文末获取源码 技术选型 【后端】&#xff1a;Java 【框架】&#xff1a;spring…