GitHub
本示例介绍如何从 GitHub 仓库加载数据。
你可以将 GITHUB_ACCESS_TOKEN 环境变量设置为 GitHub 的访问令牌,以提高请求频率限制并访问私有仓库。
设置
GitHub 加载器需要 ignore npm 包 作为对等依赖。请按照如下方式安装:
- npm
- Yarn
- pnpm
npm install @langchain/community @langchain/core ignore
yarn add @langchain/community @langchain/core ignore
pnpm add @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, // Defaults to 2
}
);
const docs = await loader.load();
console.log({ docs });
};
API Reference:
- GithubRepoLoader from
@langchain/community/document_loaders/web/github
加载器将忽略二进制文件,如图片。
使用 .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 });
// Will not include any .md files
};
API Reference:
- GithubRepoLoader from
@langchain/community/document_loaders/web/github
使用不同的 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 });
};
API Reference:
- GithubRepoLoader from
@langchain/community/document_loaders/web/github
处理子模块
如果你的仓库包含子模块,你需要决定加载器是否处理这些子模块。你可以通过布尔类型的 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 });
};
API Reference:
- GithubRepoLoader from
@langchain/community/document_loaders/web/github
注意:加载器不会跟踪位于当前仓库所用 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, // Defaults to 2
}
);
const docs = [];
for await (const doc of loader.loadAsStream()) {
docs.push(doc);
}
console.log({ docs });
};
API Reference:
- GithubRepoLoader from
@langchain/community/document_loaders/web/github