← [ BACK_TO_NOTES ]

Document Ingestion

Document Ingestion 是从各种数据源(PDF、Word、网页、数据库)加载文档并转换为标准格式的过程。它是 RAG 管线的第一步。

#concept #evergreen #ai

[!info] related notes

Document Ingestion

一句话定义

Document Ingestion 是从各种数据源加载文档并转换为标准格式的过程。PDF、Word、网页、数据库、API——各种格式的数据都需要统一成 Document 对象。

核心原理

数据源类型

数据源加载方式挑战
PDFPyPDF, pdfplumber表格、图片、多栏
Wordpython-docx格式保留
网页BeautifulSoup, Trafilatura去噪、提取正文
数据库SQL 查询结构化转文本
APIHTTP 请求认证、分页
Markdown直接读取格式解析

Document 对象

@dataclass
class Document:
    content: str              # 文本内容
    metadata: dict            # 元数据
    source: str               # 来源(文件路径、URL)
    doc_type: str             # 文档类型

Python 实现

class DocumentIngestionPipeline:
    def __init__(self):
        self.loaders = {
            "pdf": PDFLoader(),
            "docx": DocxLoader(),
            "web": WebLoader(),
            "markdown": MarkdownLoader(),
        }

    async def ingest(self, source: str, doc_type: str) -> list[Document]:
        loader = self.loaders.get(doc_type)
        if not loader:
            raise ValueError(f"Unsupported doc type: {doc_type}")

        documents = await loader.load(source)
        return [self.normalize(doc) for doc in documents]

    def normalize(self, doc: Document) -> Document:
        # 清理文本
        doc.content = self.clean_text(doc.content)
        # 添加元数据
        doc.metadata["ingested_at"] = datetime.now().isoformat()
        return doc

常见坑

  1. 不做文本清理: PDF 提取的文本包含乱码、页眉页脚
  2. 不保留元数据: 丢失了来源、页码等信息
  3. 不做格式检测: PDF 是扫描件还是文本层 PDF

参考资料

创建于 2026/6/30 更新于 2026/7/15
Digital Biome
Access

键盘导航

j / k / ↑ / ↓
选择对象
Enter / o
打开选择
/ 或 Ctrl+K
全站搜索
i / f / 1–4
筛选当前列表
g → h / l / p / t / a
首页 / 笔记 / 项目 / 工具 / About
r → 1–9
打开所选对象的关联
u
返回上一页
Esc
关闭浮层 / 取消命令;输入框内清空文本