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

相关推荐
boring_1111 小时前
KubeFlow
gpt
智算菩萨2 小时前
2025年通用大语言模型前沿进展评测:GPT-5.1、Claude 4.5、文心一言5.0 等全面解析
gpt·语言模型·文心一言
前端小东9 小时前
Langchain Template 全面指南
langchain·openai
墨风如雪12 小时前
阿里AI的“船票之战”:千问APP剑指C端,能否重塑格局?
aigc
程序员X小鹿13 小时前
阿里又放大招!上线了一款国民级AI助手千问APP,挑战ChatGPT!
aigc
CS创新实验室13 小时前
练习项目:基于 LangGraph 和 MCP 服务器的本地语音助手
运维·服务器·ai·aigc·tts·mcp
EdisonZhou15 小时前
MAF快速入门(1)化繁为简的Agent创建范式
llm·aigc·agent·.net core
小鱼小鱼.oO15 小时前
Claude Code 功能+技巧
github·aigc·claude code
机器之心16 小时前
VinciCoder:多模态统一代码生成框架和视觉反馈强化学习,数据代码模型权重已开源
人工智能·openai
倔强的石头_16 小时前
https://blog.csdn.net/2302_78391795/article/details/154952565
openai·ai编程