> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# GitHub 集成

> 使用 LangChain JavaScript 与 GitHub 文档加载器集成。

本示例介绍如何从 GitHub 仓库加载数据。
你可以将 `GITHUB_ACCESS_TOKEN` 环境变量设置为 GitHub 访问令牌，以提高速率限制并访问私有仓库。

## 设置

GitHub 加载器需要 [ignore npm 包](https://www.npmjs.com/package/ignore) 作为对等依赖。安装方法如下：

```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
npm install @langchain/community @langchain/core ignore
```

## 用法

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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` 数组：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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 企业版实例。
为此，你需要两个额外的参数：

* `baseUrl` - 你的 GitHub 实例的基础 URL，因此 githubUrl 匹配 `<baseUrl>/<owner>/<repo>/...`
* `apiUrl` - 你的 GitHub 实例的 API 端点 URL

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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 时有效。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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 仓库的文档。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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 });
};
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/document_loaders/web_loaders/github.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
