安装
¥Installation
支持的环境
¥Supported Environments
LangChain 使用 TypeScript 编写,可用于:
¥LangChain is written in TypeScript and can be used in:
Node.js(ESM 和 CommonJS) - 18.x, 19.x, 20.x
¥Node.js (ESM and CommonJS) - 18.x, 19.x, 20.x
Cloudflare Workers
Vercel / Next.js(浏览器、无服务器和 Edge 函数)
¥Vercel / Next.js (Browser, Serverless and Edge functions)
Supabase 边缘函数
¥Supabase Edge Functions
浏览器
¥Browser
Deno
Bun
但是,请注意,并非所有环境都支持单个集成。
¥However, note that individual integrations may not be supported in all environments.
安装
¥Installation
要安装主 langchain
包,请运行:
¥To install the main langchain
package, run:
- npm
- Yarn
- pnpm
npm install langchain @langchain/core
yarn add langchain @langchain/core
pnpm add langchain @langchain/core
虽然此软件包是使用 LangChain 的合理起点,但 LangChain 的大部分价值在于将其与各种模型提供程序、数据存储等集成。默认情况下,执行此操作所需的依赖未安装。你需要为特定的集成单独安装依赖。我们将在本指南的后续部分中演示如何操作。
¥While this package acts as a sane starting point to using LangChain, much of the value of LangChain comes when integrating it with various model providers, datastores, etc. By default, the dependencies needed to do that are NOT installed. You will need to install the dependencies for specific integrations separately. We'll show how to do that in the next sections of this guide.
安装 LangChain 软件包时,另请参阅 安装集成包 部分,了解一些特殊注意事项。
¥Please also see the section on installing integration packages for some special considerations when installing LangChain packages.
生态系统软件包
¥Ecosystem packages
除了 langsmith
SDK 之外,LangChain 生态系统中的所有软件包都依赖于 @langchain/core
,它包含其他软件包使用的基类和抽象。下方的依赖图显示了不同软件包之间的关系。有向箭头表示源包依赖于目标包:
¥With the exception of the langsmith
SDK, all packages in the LangChain ecosystem depend on @langchain/core
, which contains base
classes and abstractions that other packages use. The dependency graph below shows how the difference packages are related.
A directed arrow indicates that the source package depends on the target package:
注意:你的应用仅使用一个版本的 @langchain/core
非常重要。常见的包管理器在解析直接依赖时可能会引入其他版本,即使你并非有意为之。有关更多信息和解决方法,请参阅 此部分介绍如何安装集成包。
¥Note: It is important that your app only uses one version of @langchain/core
. Common package managers may introduce additional versions
when resolving direct dependencies, even if you don't intend this. See this section on installing integration packages
for more information and ways to remedy this.
@langchain/community
@langchain/community 软件包包含一系列第三方集成。安装方式:
¥The @langchain/community package contains a range of third-party integrations. Install with:
- npm
- Yarn
- pnpm
npm install @langchain/community @langchain/core
yarn add @langchain/community @langchain/core
pnpm add @langchain/community @langchain/core
还有一些更精细的软件包,其中包含针对各个提供商的 LangChain 集成。
¥There are also more granular packages containing LangChain integrations for individual providers.
@langchain/core
@langchain/core 软件包包含 LangChain 生态系统其余部分使用的基本抽象以及 LangChain 表达式语言。它应该单独安装:
¥The @langchain/core package contains base abstractions that the rest of the LangChain ecosystem uses, along with the LangChain Expression Language. It should be installed separately:
- npm
- Yarn
- pnpm
npm install @langchain/core
yarn add @langchain/core
pnpm add @langchain/core
LangGraph
LangGraph.js 是一个使用 LLM 构建有状态、多参与者应用的库。它可以与 LangChain 无缝集成,但也可以单独使用。
¥LangGraph.js is a library for building stateful, multi-actor applications with LLMs. It integrates smoothly with LangChain, but can be used without it.
安装方式:
¥Install with:
- npm
- Yarn
- pnpm
npm install @langchain/langgraph @langchain/core
yarn add @langchain/langgraph @langchain/core
pnpm add @langchain/langgraph @langchain/core
LangSmith SDK
LangSmith SDK 由 LangChain 自动安装。如果你未将其与 LangChain 一起使用,请使用以下方式安装:
¥The LangSmith SDK is automatically installed by LangChain. If you're not using it with LangChain, install with:
- npm
- Yarn
- pnpm
npm install langsmith
yarn add langsmith
pnpm add langsmith
安装集成包
¥Installing integration packages
LangChain 支持包含与各个第三方提供商的模块集成的软件包。它们可以像 @langchain/anthropic
一样具体,包含仅适用于人思模型的集成,或像 @langchain/community
一样广泛的集成,后者包含更广泛的社区贡献集成。
¥LangChain supports packages that contain module integrations with individual third-party providers.
They can be as specific as @langchain/anthropic
, which contains integrations just for Anthropic models,
or as broad as @langchain/community
, which contains broader variety of community contributed integrations.
这些软件包以及主要的 LangChain 软件包都将 @langchain/core
作为对等依赖,以避免软件包管理器安装同一软件包的多个版本。它包含这些集成包扩展的基本抽象。
¥These packages, as well as the main LangChain package, all have @langchain/core
as a peer dependency
to avoid package managers installing multiple versions of the same package. It contains the base abstractions that these integration packages extend.
为了确保所有集成及其类型能够正常交互,务必使用相同版本的 @langchain/core
。如果你在基类周围遇到类型错误,你可能需要确保你的包管理器解析的是单个版本的 @langchain/core
。为此,你可以在项目的 package.json
中添加如下所示的 "resolutions"
或 "overrides"
字段。名称取决于你的包管理器:
¥To ensure that all integrations and their types interact with each other properly, it is important that they all use the same version of @langchain/core
.
If you encounter type errors around base classes, you may need to guarantee that your package manager is resolving a single version of @langchain/core
. To do so,
you can add a "resolutions"
or "overrides"
field like the following in your project's package.json
. The name will depend on your package manager:
yarn
或 pnpm
的 resolutions
或 pnpm.overrides
字段必须在根 package.json
文件中设置。
¥The resolutions
or pnpm.overrides
fields for yarn
or pnpm
must be set in the root package.json
file.
如果你使用 yarn
:
¥If you are using yarn
:
{
"name": "your-project",
"version": "0.0.0",
"private": true,
"engines": {
"node": ">=18"
},
"dependencies": {
"@langchain/anthropic": "^0.0.2",
"@langchain/core": "^0.3.0",
"langchain": "0.0.207"
},
"resolutions": {
"@langchain/core": "0.3.0"
}
}
如果你使用的是 yarn
版本 2 或更高版本,也可以尝试运行 yarn dedupe
命令。
¥You can also try running the yarn dedupe
command if you are on yarn
version 2 or higher.
或者对于 npm
:
¥Or for npm
:
{
"name": "your-project",
"version": "0.0.0",
"private": true,
"engines": {
"node": ">=18"
},
"dependencies": {
"@langchain/anthropic": "^0.0.2",
"@langchain/core": "^0.3.0",
"langchain": "0.0.207"
},
"overrides": {
"@langchain/core": "0.3.0"
}
}
你也可以尝试 npm dedupe
命令。
¥You can also try the npm dedupe
command.
或者对于 pnpm
:
¥Or for pnpm
:
{
"name": "your-project",
"version": "0.0.0",
"private": true,
"engines": {
"node": ">=18"
},
"dependencies": {
"@langchain/anthropic": "^0.0.2",
"@langchain/core": "^0.3.0",
"langchain": "0.0.207"
},
"pnpm": {
"overrides": {
"@langchain/core": "0.3.0"
}
}
}
你也可以尝试 pnpm dedupe
命令。
¥You can also try the pnpm dedupe
command.
加载库
¥Loading the library
TypeScript
LangChain 使用 TypeScript 编写,并为其所有公共 API 提供类型定义。
¥LangChain is written in TypeScript and provides type definitions for all of its public APIs.
ESM
LangChain 提供了一个针对 Node.js 环境的 ESM 构建。你可以使用以下语法导入数据:
¥LangChain provides an ESM build targeting Node.js environments. You can import it using the following syntax:
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/core
yarn add @langchain/openai @langchain/core
pnpm add @langchain/openai @langchain/core
import { ChatOpenAI } from "@langchain/openai";
如果你在 ESM 项目中使用 TypeScript,我们建议你更新你的 tsconfig.json
以包含以下内容:
¥If you are using TypeScript in an ESM project we suggest updating your tsconfig.json
to include the following:
{
"compilerOptions": {
...
"target": "ES2020", // or higher
"module": "nodenext",
}
}
CommonJS
LangChain 提供了一个针对 Node.js 环境的 CommonJS 构建版本。你可以使用以下语法导入数据:
¥LangChain provides a CommonJS build targeting Node.js environments. You can import it using the following syntax:
const { ChatOpenAI } = require("@langchain/openai");
Cloudflare Workers
LangChain 可在 Cloudflare Workers 中使用。你可以使用以下语法导入数据:
¥LangChain can be used in Cloudflare Workers. You can import it using the following syntax:
import { ChatOpenAI } from "@langchain/openai";
Vercel / Next.js
LangChain 可以在 Vercel / Next.js 中使用。我们支持在前端组件、无服务器函数和边缘函数中使用 LangChain。你可以使用以下语法导入数据:
¥LangChain can be used in Vercel / Next.js. We support using LangChain in frontend components, in Serverless functions and in Edge functions. You can import it using the following syntax:
import { ChatOpenAI } from "@langchain/openai";
Deno / Supabase 边缘函数
¥Deno / Supabase Edge Functions
LangChain 可以在 Deno / Supabase Edge Functions 中使用。你可以使用以下语法导入数据:
¥LangChain can be used in Deno / Supabase Edge Functions. You can import it using the following syntax:
import { ChatOpenAI } from "https://esm.sh/@langchain/openai";
or
import { ChatOpenAI } from "npm:@langchain/openai";
浏览器
¥Browser
LangChain 可以在浏览器中使用。在我们的持续集成 (CI) 中,我们测试了使用 Webpack 和 Vite 打包 LangChain,但其他打包工具也应该可以正常工作。你可以使用以下语法导入数据:
¥LangChain can be used in the browser. In our CI we test bundling LangChain with Webpack and Vite, but other bundlers should work too. You can import it using the following syntax:
import { ChatOpenAI } from "@langchain/openai";
不支持:Node.js 16
¥Unsupported: Node.js 16
我们不支持 Node.js 16,但如果你仍想在 Node.js 16 上运行 LangChain,则需要遵循本节中的说明。我们不保证这些说明在未来仍然有效。
¥We do not support Node.js 16, but if you still want to run LangChain on Node.js 16, you will need to follow the instructions in this section. We do not guarantee that these instructions will continue to work in the future.
你必须使 fetch
在全球范围内可用,方式如下:
¥You will have to make fetch
available globally, either:
使用
NODE_OPTIONS='--experimental-fetch' node ...
运行你的应用,或者¥run your application with
NODE_OPTIONS='--experimental-fetch' node ...
, or安装
node-fetch
并按照 此处 的说明操作¥install
node-fetch
and follow the instructions here
你还需要通过安装以下内容来安装 polyfill ReadableStream
:
¥You'll also need to polyfill ReadableStream
by installing:
- npm
- Yarn
- pnpm
npm i web-streams-polyfill@4
yarn add web-streams-polyfill@4
pnpm add web-streams-polyfill@4
然后将其添加到主入口点的全局命名空间中:
¥And then adding it to the global namespace in your main entrypoint:
import "web-streams-polyfill/polyfill";
此外,你还需要对 structuredClone
进行 polyfill,例如,安装 core-js
并按照 此处 的说明进行操作。
¥Additionally you'll have to polyfill structuredClone
, eg. by installing core-js
and following the instructions here.
如果你运行的是 Node.js 18+ 版本,则无需执行任何操作。
¥If you are running Node.js 18+, you do not need to do anything.