优秀的编程知识分享平台

网站首页 > 技术文章 正文

LangChain手册(JS/TS版)01:设置和安装

nanyue 2024-08-22 17:28:15 技术文章 7 ℃

Supported Environments 支持的环境

LangChain is written in TypeScript and can be used in:
LangChain 是用 TypeScript 编写的,可用于:

  • Node.js (ESM and CommonJS) - 18.x, 19.x, 20.x
    Node.js (ESM 和 CommonJS) - 18.x, 19.x, 20.x
  • Cloudflare Workers Cloudflare Workers
  • Vercel / Next.js (Browser, Serverless and Edge functions)
    Vercel / Next.js(浏览器、无服务器和边缘功能)
  • Supabase Edge Functions 超基边缘函数
  • Browser
  • Deno

Quickstart? 快速入门

If you want to get started quickly on using LangChain in Node.js, clone this repository and follow the README instructions for a boilerplate project with those dependencies set up.
如果您想快速开始使用 Node.js中的 LangChain,请克隆此存储库并按照设置了这些依赖项的样板项目的自述文件说明进行操作。

If you prefer to set things up yourself, or you want to run LangChain in other environments, read on for instructions.
如果您希望自己设置,或者想在其他环境中运行 LangChain,请继续阅读以获取说明。

Installation? 安装

To get started, install LangChain with the following command:
要开始使用,请使用以下命令安装 LangChain:

  • npm

npm install -S langchain

  • Yarn

yarn add langchain

  • pnpm
pnpm add langchain

TypeScript?

LangChain is written in TypeScript and provides type definitions for all of its public APIs.
LangChain 是用 TypeScript 编写的,并为其所有公共 API 提供类型定义。

Loading the library? 加载库

ESM? 无害环境管理

LangChain provides an ESM build targeting Node.js environments. You can import it using the following syntax:
LangChain提供了一个针对Node.js环境的ESM构建。您可以使用以下语法导入它:

import { OpenAI } from "langchain/llms/openai";

If you are using TypeScript in an ESM project we suggest updating your tsconfig.json to include the following:
如果您在 ESM 项目中使用 TypeScript,我们建议您更新 tsconfig.json 以包含以下内容:

tsconfig.json

{
  "compilerOptions": {
    ...
    "target": "ES2020", // or higher
    "module": "nodenext",
  }
}

CommonJS? 通用JS

LangChain provides a CommonJS build targeting Node.js environments. You can import it using the following syntax:
LangChain提供了一个针对Node.js环境的CommonJS构建。您可以使用以下语法导入它:

const { OpenAI } = require("langchain/llms/openai");

Cloudflare Workers? Cloudflare Workers ?

LangChain can be used in Cloudflare Workers. You can import it using the following syntax:
LangChain 可用于 Cloudflare Workers。您可以使用以下语法导入它:

import { OpenAI } from "langchain/llms/openai";

Vercel / Next.js? 韦塞尔 / 下一页.js

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:
LangChain 可以在 Vercel / Next.js 中使用。我们支持在前端组件、无服务器函数和边缘函数中使用 LangChain。您可以使用以下语法导入它:

import { OpenAI } from "langchain/llms/openai";

Deno / Supabase Edge Functions?Deno / Supabase 边缘函数

LangChain can be used in Deno / Supabase Edge Functions. You can import it using the following syntax:
LangChain 可用于 Deno / Supabase Edge Functions。您可以使用以下语法导入它:

import { OpenAI } from "https://esm.sh/langchain/llms/openai";

We recommend looking at our Supabase Template for an example of how to use LangChain in Supabase Edge Functions.
我们建议查看我们的Supabase模板,以获取如何在Supabase Edge Functions中使用LangChain的示例。

Browser? 浏览器

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:
LangChain可以在浏览器中使用。在我们的CI中,我们测试了将LangChain与Webpack和Vite捆绑在一起,但其他捆绑器也应该可以工作。您可以使用以下语法导入它:

import { OpenAI } from "langchain/llms/openai";

Updating from <0.0.52? 从 <0.0.52 更新

If you are updating from a version of LangChain prior to 0.0.52, you will need to update your imports to use the new path structure.
如果要从 0.0.52 之前的 LangChain 版本进行更新,则需要更新导入以使用新的路径结构。

For example, if you were previously doing
例如,如果您以前在做

import { OpenAI } from "langchain/llms";

you will now need to do
你现在需要做

import { OpenAI } from "langchain/llms/openai";

This applies to all imports from the following 6 modules, which have been split into submodules for each integration. The combined modules are deprecated, do not work outside of Node.js, and will be removed in a future version.
这适用于从以下 6 个模块导入的所有模块,这些模块已为每个集成拆分为子模块。组合模块已弃用,不能在 Node.js 之外工作,并将在未来的版本中删除。

  • If you were using langchain/llms, see LLMs for updated import paths.
    如果使用 langchain/llms ,请参阅 LLM 以获取更新的导入路径。
  • If you were using langchain/chat_models, see Chat Models for updated import paths.
    如果使用 langchain/chat_models ,请参阅聊天模型以获取更新的导入路径。
  • If you were using langchain/embeddings, see Embeddings for updated import paths.
    如果使用 langchain/embeddings ,请参阅嵌入以获取更新的导入路径。
  • If you were using langchain/vectorstores, see Vector Stores for updated import paths.
    如果使用 langchain/vectorstores ,请参阅矢量存储以获取更新的导入路径。
  • If you were using langchain/document_loaders, see Document Loaders for updated import paths.
    如果使用 langchain/document_loaders ,请参阅文档加载程序以获取更新的导入路径。
  • If you were using langchain/retrievers, see Retrievers for updated import paths.
    如果使用 langchain/retrievers ,请参阅检索器以获取更新的导入路径。

Other modules are not affected by this change, and you can continue to import them from the same path.
其他模块不受此更改的影响,您可以继续从同一路径导入它们。

Additionally, there are some breaking changes that were needed to support new environments:
此外,还需要一些重大更改来支持新环境:

  • import { Calculator } from "langchain/tools"; now moved to import { Calculator } from "langchain/tools"; 现在移至import { Calculator } from "langchain/tools/calculator";
  • import { loadLLM } from "langchain/llms"; now moved to import { loadLLM } from "langchain/llms"; 现在移至import { loadLLM } from "langchain/llms/load";
  • import { loadAgent } from "langchain/agents"; now moved to import { loadAgent } from "langchain/agents"; 现在移至import { loadAgent } from "langchain/agents/load";
  • import { loadPrompt } from "langchain/prompts"; now moved to import { loadPrompt } from "langchain/prompts"; 现在移至import { loadPrompt } from "langchain/prompts/load";
  • import { loadChain } from "langchain/chains"; now moved to import { loadChain } from "langchain/chains"; 现在移至import { loadChain } from "langchain/chains/load";

Unsupported: Node.js 16? 不支持:节点.js 16

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.
我们不支持 Node.js 16,但如果您仍然想在 Node.js 16 上运行 LangChain,则需要按照本节中的说明进行操作。我们不保证这些说明将来会继续有效。

You will have to make fetch available globally, either:
您必须使 fetch 全局可用,其中之一:

  • run your application with NODE_OPTIONS='--experimental-fetch' node ..., or
    以 NODE_OPTIONS='--experimental-fetch' node ... 运行应用程序,或
  • install node-fetch and follow the instructions here
    安装 node-fetch 并按照此处的说明进行操作

Additionally you'll have to polyfill unstructuredClone, eg. by installing core-js and following the instructions here.
此外,您必须填充 unstructuredClone ,例如。通过安装 core-js 并按照此处的说明进行操作。

If you are running this on Node.js 18+, you do not need to do anything.
如果你在 Node.js 18+ 上运行它,你不需要做任何事情。

最近发表
标签列表