RAG文本切分LV3:轻松定制Markdown切分

人工智能
分块通常旨在将具有共同上下文的文本放在一起。考虑到这一点,我们可能希望特别尊重文档本身的结构。

基本概念和环境

分块通常旨在将具有共同上下文的文本放在一起。考虑到这一点,我们可能希望特别尊重文档本身的结构。例如,markdown 文件按标题组织。在特定标题组中创建块是一种直观的想法。为了解决这一挑战,我们可以使用MarkdownHeaderTextSplitter。这将按指定的一组标题拆分 markdown 文件。

本文用到的安装包如下:

pip install langchain-text-splitters

切分实现

我们可以指定要拆分的标题headers_to_split_on,切分之后内容按标题分组 :
markdown_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)
md_header_splits = markdown_splitter.split_text(
  markdown_document)
print(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'})]

默认情况下,MarkdownHeaderTextSplitter从输出块的内容中剥离被分割的标头。可以通过设置strip_headers = False来禁用此功能。

markdown_splitter = MarkdownHeaderTextSplitter(
    headers_to_split_on, 
    strip_headers=False)
md_header_splits = markdown_splitter.split_text(
  markdown_document)
print(md_header_splits)

可以看到,标题添加到内容中了。

[Document(page_content='# Foo  \n## Bar  \nHi this is Jim  \nHi this is Joe', metadata={'Header 1': 'Foo', 'Header 2': 'Bar'}),
 Document(page_content='### Boo  \nHi this is Lance', metadata={'Header 1': 'Foo', 'Header 2': 'Bar', 'Header 3': 'Boo'}),
 Document(page_content='## Baz  \nHi this is Molly', metadata={'Header 1': 'Foo', 'Header 2': 'Baz'})]

如何将 Markdown 行返回为单独的文档

默认情况下,MarkdownHeaderTextSplitter根据headers_to_split_on中指定的标题聚合行。我们可以通过指定return_each_line来禁用此功能,使得一行就是一条内容:

markdown_splitter = MarkdownHeaderTextSplitter(
    headers_to_split_on,
    return_each_line=True,
)
md_header_splits = markdown_splitter.split_text(markdown_document)
print(md_header_splits)
[Document(page_content='Hi this is Jim', metadata={'Header 1': 'Foo', 'Header 2': 'Bar'}),
 Document(page_content='Hi 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'})]

如何限制块大小:

然后,我们可以在每个 markdown 组中应用任何我们想要的文本分割器,例如RecursiveCharacterTextSplitter,它允许进一步控制块大小。

markdown_document = "# Intro \n\n    ## History \n\n Markdown[9] is a lightweight markup language for creating formatted text using a plain-text editor. John Gruber created Markdown in 2004 as a markup language that is appealing to human readers in its source code form.[9] \n\n Markdown is widely used in blogging, instant messaging, online forums, collaborative software, documentation pages, and readme files. \n\n ## Rise and divergence \n\n As Markdown popularity grew rapidly, many Markdown implementations appeared, driven mostly by the need for \n\n additional features such as tables, footnotes, definition lists,[note 1] and Markdown inside HTML blocks. \n\n #### Standardization \n\n From 2012, a group of people, including Jeff Atwood and John MacFarlane, launched what Atwood characterised as a standardisation effort. \n\n ## Implementations \n\n Implementations of Markdown are available for over a dozen programming languages."


headers_to_split_on = [
    ("#", "Header 1"),
    ("##", "Header 2"),
]


# MD splits
markdown_splitter = MarkdownHeaderTextSplitter(
    headers_to_split_on=headers_to_split_on, strip_headers=False
)
md_header_splits = markdown_splitter.split_text(markdown_document)


# Char-level splits
from langchain_text_splitters import RecursiveCharacterTextSplitter


chunk_size = 250
chunk_overlap = 30
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=chunk_size, chunk_overlap=chunk_overlap
)


# Split
splits = text_splitter.split_documents(md_header_splits)
splits
责任编辑:武晓燕 来源: 哎呀AIYA
相关推荐

2022-01-07 14:00:35

分库分表业务量

2019-11-25 10:12:59

Python技巧工具

2011-08-18 16:03:48

数据切分MySQL

2021-03-17 16:15:55

数据MySQL 架构

2017-06-19 16:45:41

数据库水平切分用户中心

2017-07-17 14:45:43

数据库DB分库切分策略

2017-12-08 10:42:49

HBase切分细节

2023-10-10 14:03:47

swap排序解法

2017-08-28 16:40:07

Region切分触发策略

2024-04-11 13:51:47

markdown前端

2020-10-14 11:37:07

MAXHUB

2024-07-09 11:48:47

2017-08-11 13:55:13

数据库水平切分架构

2011-08-11 18:54:01

数据库分页查询

2017-07-11 16:44:04

数据库水平切分架构

2022-08-21 17:35:31

原子多线程

2016-01-25 14:38:49

金蝶企业定制

2018-01-24 09:35:12

高并发数据库设计水平切分

2018-01-29 09:50:16

数据库设计水平切分

2024-04-12 12:19:08

语言模型AI
点赞
收藏

51CTO技术栈公众号