跳到主要内容
本指南将引导您完成使用 Notion API 从 Notion 页面和数据库加载文档所需的步骤。

概览

Notion 是一个多功能的生产力平台,将笔记记录、任务管理和数据组织工具整合到一个界面中。 此文档加载器能够获取完整的 Notion 页面和数据库,并将其转换为 LangChain 文档,以便集成到您的项目中。

设置

  1. 您首先需要安装官方 Notion 客户端和 notion-to-md 包作为对等依赖项
npm
npm install @langchain/community @langchain/core @notionhq/client notion-to-md
  1. 创建 Notion 集成 并安全记录内部集成密钥(也称为 NOTION_INTEGRATION_TOKEN)。
  2. 在您的页面或数据库上添加与新集成的连接。为此,请打开您的 Notion 页面,转到右上角的设置点,向下滚动到 Add connections 并选择您的新集成。
  3. 获取您要加载的页面或数据库的 PAGE_IDDATABASE_ID
URL 路径中的 32 个字符的十六进制表示 ID。例如
PAGE_ID:https://www.notion.so/skarard/LangChain-Notion-API-b34ca03f219c4420a6046fc4bdfdf7b4
DATABASE_ID:https://www.notion.so/skarard/c393f19c3903440da0d34bf9c6c12ff2?v=9c70a0f4e174498aa0f9021e0a9d52de
正则表达式:/(?<!=)[0-9a-f]{32}/

使用示例

import { NotionAPILoader } from "@langchain/community/document_loaders/web/notionapi";
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";

// Loading a page (including child pages all as separate documents)
const pageLoader = new NotionAPILoader({
  clientOptions: {
    auth: "<NOTION_INTEGRATION_TOKEN>",
  },
  id: "<PAGE_ID>",
  type: "page",
});

const splitter = new RecursiveCharacterTextSplitter();

// Load the documents
const pageDocs = await pageLoader.load();
// Split the documents using the text splitter
const splitDocs = await splitter.splitDocuments(pageDocs);

console.log({ splitDocs });

// Loading a database (each row is a separate document with all properties as metadata)
const dbLoader = new NotionAPILoader({
  clientOptions: {
    auth: "<NOTION_INTEGRATION_TOKEN>",
  },
  id: "<DATABASE_ID>",
  type: "database",
  onDocumentLoaded: (current, total, currentTitle) => {
    console.log(`Loaded Page: ${currentTitle} (${current}/${total})`);
  },
  callerOptions: {
    maxConcurrency: 64, // Default value
  },
  propertiesAsHeader: true, // Prepends a front matter header of the page properties to the page contents
});

// A database row contents is likely to be less than 1000 characters so it's not split into multiple documents
const dbDocs = await dbLoader.load();

console.log({ dbDocs });

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.