🔥🔥🔥一文搞懂Langchain Document Loader(二)

Transform Loaders:将数据从特定格式加载到文档格式

转换加载器(Transform Loaders:)就像上文提到的的TextLoader一样 - 它们将输入格式转换为我们的文档格式。LangChain中有越来越多的转换加载器,包括但不限于以下几种:

  • CSV
  • Email
  • HTML
  • Markdown
  • Microsoft Word/PowerPoint
  • Notion (raw files or through API integration)
  • Reddit
  • PDF

许多这些加载器的基础是Unstructured Python库。这个库非常擅长将各种文件类型转换为我们文档所需的文本数据。

无结构分区(Unstructured Partitions)

Unstructured库的核心概念是将文档划分为元素。当传递一个文件时,库将读取源文档,将其分割为多个部分,对这些部分进行分类,然后提取每个部分的文本。在划分之后,返回一个文档元素列表。

以下是直接使用库时的例子:

python 复制代码
from unstructured.partition.auto import partition
elements = partition(filename="dashboard.html")

该库在底层使用了一些工具来自动检测文件类型,并根据文件类型正确地进行划分。

例子:加载Microsoft Word文档

让我们看一下加载Microsoft Word文档的过程是什么样的。

这是我们的样例Word文档:

现在我们可以使用LangChain的UnstructuredWordDocumentLoader来划分这个文档。

python 复制代码
from langchain.document_loaders import UnstructuredWordDocumentLoader

# use mode="elements" to return each Element as a Document
# otherwise it defaults the "single" option which returns a single document
loader = UnstructuredWordDocumentLoader(file_path="test_doc.docx", mode="elements")

data = loader.load()

print(data)

当使用mode="elements"时的结果,它将为源文档中的每个元素返回一个文档。

python 复制代码
[
    Document(page_content = 'Title Text', metadata = {
        'source': 'test_doc.docx',
        'filename': 'test_doc.docx',
        'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'page_number': 1,
        'category': 'Title'
    }),
    Document(page_content = 'Heading 1', metadata = {
        'source': 'test_doc.docx',
        'filename': 'test_doc.docx',
        'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'page_number': 1,
        'category': 'Title'
    }),
    Document(page_content = 'This is paragraph 1', metadata = {
        'source': 'test_doc.docx',
        'filename': 'test_doc.docx',
        'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'page_number': 1,
        'category': 'NarrativeText'
    }),
    Document(page_content = 'Heading 2', metadata = {
        'source': 'test_doc.docx',
        'filename': 'test_doc.docx',
        'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'page_number': 1,
        'category': 'Title'
    }),
    Document(page_content = 'This is paragraph 2', metadata = {
        'source': 'test_doc.docx',
        'filename': 'test_doc.docx',
        'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'page_number': 1,
        'category': 'NarrativeText'
    })
]

使用默认的mode="single"时的结果,它将为源文档中的所有文本返回一个单一的文档。

python 复制代码
[
	Document(
		page_content='Title Text\n\nHeading 1\n\nThis is paragraph 1\n\nHeading 2\n\nThis is paragraph 2', 
		metadata={'source': 'test_doc.docx'}
	)
]

总结下,在"single"模式下,元素之间使用"\n\n"分隔符连接。接下来我们介绍文本拆分器时,这是字符拆分器的默认拆分字符。

相关推荐
新智元29 分钟前
世界首富换人!81 岁硅谷狂人 4000 亿身价碾压马斯克,33 岁华裔才女逆袭
人工智能·openai
机器之心44 分钟前
交互扩展时代来临:创智复旦字节重磅发布AgentGym-RL,昇腾加持,开创智能体训练新范式
人工智能·openai
墨风如雪2 小时前
AI赛场新黑马:阿联酋K2-Think,小身板藏着大智慧!
aigc
新智元3 小时前
刚刚,英伟达祭出下一代 GPU!狂飙百万 token 巨兽,投 1 亿爆赚 50 亿
人工智能·openai
不大姐姐AI智能体3 小时前
公众号AI 明星漫画怎么制作?Coze智能体一键生成,保姆级教程
aigc
算家计算3 小时前
一张图+一段音频=电影级视频!阿里Wan2.2-S2V-14B本地部署教程:实现丝滑口型同步
人工智能·开源·aigc
阿杜杜不是阿木木4 小时前
开始 ComfyUI 的 AI 绘图之旅-Flux.1文生图(全网首发,官网都没有更新)(七)
人工智能·ai·ai作画·aigc·图生图
杀生丸学AI5 小时前
【三维重建】3R-GS:优化相机位姿的3DGS最佳实践
人工智能·3d·aigc·三维重建·视觉大模型·高斯泼溅
iThinkAi智能体5 小时前
Coze智能体工作流一分钟生成10w+爆款魔性灵魂画手视频,无需剪辑
aigc
一泽Eze6 小时前
AI 不懂「疯狂星期四」
aigc·openai