🔥🔥🔥一文搞懂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"分隔符连接。接下来我们介绍文本拆分器时,这是字符拆分器的默认拆分字符。

相关推荐
cat2bug4 小时前
介绍一下如何在Cat2Bug-Platform中通过OpenAI来创建测试用例
功能测试·测试工具·ai·测试用例·bug·openai
百***78756 小时前
Sora Video2 API国内接入避坑与场景落地:开发者实战笔记
人工智能·笔记·gpt
2301_772204287 小时前
ARM——定时器(EPIT GPT)
gpt
小程故事多_809 小时前
打破传统桎梏,LLM 让智能运维实现从 “自动化” 到 “自进化”
运维·人工智能·自动化·aigc
松涛和鸣11 小时前
60、嵌入式定时器深度解析:EPIT与GPT
c语言·arm开发·单片机·嵌入式硬件·gpt·fpga开发
edisao12 小时前
【开源】轻量级 LLM 文本质检工具:精准识别核心概念缺失,支持动态别名 + 反馈闭环
大数据·开发语言·人工智能·经验分享·gpt·架构·开源
java1234_小锋13 小时前
基于GPT-2通用文本模型全量微调训练
gpt
不大姐姐AI智能体1 天前
搭了个小红书笔记自动生产线,一句话生成图文,一键发布,支持手机端、电脑端发布
人工智能·经验分享·笔记·矩阵·aigc
GISer_Jing1 天前
AI驱动营销:业务技术栈实战(From AIGC,待总结)
前端·人工智能·aigc·reactjs
GISer_Jing1 天前
2026年前端开发目标(From豆包)
前端·学习·aigc