Skip to main content
本示例演示如何从 GitHub 仓库加载数据。 您可以设置 GITHUB_ACCESS_TOKEN 环境变量为 GitHub 访问令牌,以提高速率限制并访问私有仓库。

安装设置

GitHub 加载器需要 ignore npm 包 作为对等依赖项。请按如下方式安装:
npm
npm install @langchain/community @langchain/core ignore

使用方式

import { GithubRepoLoader } from "@langchain/community/document_loaders/web/github";

export const run = async () => {
  const loader = new GithubRepoLoader(
    "https://github.com/langchain-ai/langchainjs",
    {
      branch: "main",
      recursive: false,
      unknown: "warn",
      maxConcurrency: 5, // 默认为 2
    }
  );
  const docs = await loader.load();
  console.log({ docs });
};
加载器将忽略二进制文件(如图像)。

使用 .gitignore 语法

要忽略特定文件,您可以在构造函数中传入 ignorePaths 数组:
import { GithubRepoLoader } from "@langchain/community/document_loaders/web/github";

export const run = async () => {
  const loader = new GithubRepoLoader(
    "https://github.com/langchain-ai/langchainjs",
    { branch: "main", recursive: false, unknown: "warn", ignorePaths: ["*.md"] }
  );
  const docs = await loader.load();
  console.log({ docs });
  // 将不包含任何 .md 文件
};

使用不同的 GitHub 实例

您可能希望指向 github.com 以外的其他 GitHub 实例,例如,如果您公司有 GitHub Enterprise 实例。 为此,您需要两个额外参数:
  • baseUrl - 您的 GitHub 实例的基础 URL,因此 githubUrl 匹配 <baseUrl>/<owner>/<repo>/...
  • apiUrl - 您的 GitHub 实例的 API 端点 URL
import { GithubRepoLoader } from "@langchain/community/document_loaders/web/github";

export const run = async () => {
  const loader = new GithubRepoLoader(
    "https://github.your.company/org/repo-name",
    {
      baseUrl: "https://github.your.company",
      apiUrl: "https://github.your.company/api/v3",
      accessToken: "ghp_A1B2C3D4E5F6a7b8c9d0",
      branch: "main",
      recursive: true,
      unknown: "warn",
    }
  );
  const docs = await loader.load();
  console.log({ docs });
};

处理子模块

如果您的仓库包含子模块,您需要决定加载器是否应跟随它们。您可以通过布尔参数 processSubmodules 来控制此行为。默认情况下,子模块不会被处理。 请注意,处理子模块仅在将 recursive 参数设置为 true 时有效。
import { GithubRepoLoader } from "@langchain/community/document_loaders/web/github";

export const run = async () => {
  const loader = new GithubRepoLoader(
    "https://github.com/langchain-ai/langchainjs",
    {
      branch: "main",
      recursive: true,
      processSubmodules: true,
      unknown: "warn",
    }
  );
  const docs = await loader.load();
  console.log({ docs });
};
请注意,加载器不会跟随位于与当前仓库不同的 GitHub 实例上的子模块。

流式处理大型仓库

对于需要以内存高效的方式处理大型仓库的情况,您可以使用 loadAsStream 方法从整个 GitHub 仓库异步流式传输文档。
import { GithubRepoLoader } from "@langchain/community/document_loaders/web/github";

export const run = async () => {
  const loader = new GithubRepoLoader(
    "https://github.com/langchain-ai/langchainjs",
    {
      branch: "main",
      recursive: false,
      unknown: "warn",
      maxConcurrency: 3, // 默认为 2
    }
  );

  const docs = [];
  for await (const doc of loader.loadAsStream()) {
    docs.push(doc);
  }

  console.log({ docs });
};