Skip to main content

LangChain v0.2

LangChain v0.2 于 2024 年 5 月发布。此版本包含一些重大更改和弃用。本文档包含升级到 0.2.x 的指南,以及弃用和重大更改列表。

¥LangChain v0.2 was released in May 2024. This release includes a number of breaking changes and deprecations. This document contains a guide on upgrading to 0.2.x, as well as a list of deprecations and breaking changes.

迁移

¥Migration

本文档将帮助你将代码升级到 LangChain 0.2.x.。为了准备迁移,我们首先建议你执行以下步骤:

¥This documentation will help you upgrade your code to LangChain 0.2.x.. To prepare for migration, we first recommend you take the following steps:

  1. 安装 @langchain/core 和 langchain 的 0.2.x 版本,并升级到你可能正在使用的其他软件包的最新版本(例如 @langchain/langgraph@langchain/community@langchain/openai 等)。

    ¥install the 0.2.x versions of @langchain/core, langchain and upgrade to recent versions of other packages that you may be using (e.g. @langchain/langgraph, @langchain/community, @langchain/openai, etc.)

  2. 使用新软件包验证你的代码是否正常运行(例如,单元测试通过)

    ¥Verify that your code runs properly with the new packages (e.g., unit tests pass)

  3. 安装最新版本的 langchain-cli,并使用该工具将代码中使用的旧导入替换为新导入。(请参阅说明) (见下文。)

    ¥Install a recent version of langchain-cli , and use the tool to replace old imports used by your code with the new imports. (See instructions below.)

  4. 手动解决任何剩余的弃用警告

    ¥Manually resolve any remaining deprecation warnings

  5. 重新运行单元测试

    ¥Re-run unit tests

升级到新导入

¥Upgrade to new imports

我们创建了一个工具来帮助迁移你的代码。此工具仍处于测试阶段,可能无法涵盖所有情况,但我们希望它能帮助你更快地迁移代码。

¥We created a tool to help migrate your code. This tool is still in beta and may not cover all cases, but we hope that it will help you migrate your code more quickly.

迁移脚本有以下限制:

¥The migration script has the following limitations:

  1. 它仅限于帮助用户从旧的导入迁移到新的导入。它无助于解决其他弃用问题。

    ¥It's limited to helping users move from old imports to new imports. It doesn't help address other deprecations.

  2. 它无法处理涉及 as 的导入。

    ¥It can't handle imports that involve as .

  3. 新的导入始终位于全局作用域中,即使被替换的旧导入位于某个本地作用域内(例如,函数体)。

    ¥New imports are always placed in global scope, even if the old import that was replaced was located inside some local scope (e..g, function body).

  4. 它可能会遗漏一些已弃用的导入。

    ¥It will likely miss some deprecated imports.

下面是一个迁移脚本可以自动应用的导入更改示例:

¥Here is an example of the import changes that the migration script can help apply automatically:

From PackageTo PackageDeprecated ImportNew Import
langchain@langchain/communityimport { UpstashVectorStore } from "langchain/vectorstores/upstash"import { UpstashVectorStore } from "@langchain/community/vectorstores/upstash"
@langchain/community@langchain/openaiimport { ChatOpenAI } from "@langchain/community/chat_models/openai"import { ChatOpenAI } from "@langchain/openai"
langchain@langchain/coreimport { Document } from "langchain/schema/document"import { Document } from "@langchain/core/documents"
langchain@langchain/textsplittersimport { RecursiveCharacterTextSplitter } from "langchain/text_splitter"import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters"

弃用时间表

¥Deprecation timeline

我们主要有两种弃用类型:

¥We have two main types of deprecations:

  1. langchain 移至其他包(例如 @langchain/community)的代码

    ¥Code that was moved from langchain into another package (e.g, @langchain/community)

如果你尝试从 langchain 导入它,则会失败,因为入口点已被移除。

¥If you try to import it from langchain, it will fail since the entrypoint has been removed.

  1. 有更好的替代方案的代码最终会被移除,因此只有一种处理方式。(例如,ChatModels 中的 predictMessages 方法已弃用,取而代之的是 invoke)。

    ¥Code that has better alternatives available and will eventually be removed, so there's only a single way to do things. (e.g., predictMessages method in ChatModels has been deprecated in favor of invoke).

其中许多在 0.2 版本中被标记为删除。我们已将移除内容提升至 0.3。

¥Many of these were marked for removal in 0.2. We have bumped the removal to 0.3.

安装

¥Installation

note

0.2.X 迁移脚本仅适用于 0.0.14-rc.1 或更高版本。

¥The 0.2.X migration script is only available in version 0.0.14-rc.1 or later.

npm i @langchain/scripts@0.0.14-rc.1

用法

¥Usage

由于迁移脚本并不完善,你应该首先确保已备份代码(例如,使用 git 之类的版本控制)。

¥Given that the migration script is not perfect, you should make sure you have a backup of your code first (e.g., using version control like git).

例如,假设你的代码仍然使用 import ChatOpenAI from "@langchain/community/chat_models/openai";

¥For example, say your code still uses import ChatOpenAI from "@langchain/community/chat_models/openai";:

调用迁移脚本将用 import ChatOpenAI from "@langchain/openai"; 替换此导入。

¥Invoking the migration script will replace this import with import ChatOpenAI from "@langchain/openai";.

import { updateEntrypointsFrom0_x_xTo0_2_x } from "@langchain/scripts/migrations";

const pathToMyProject = "..."; // This path is used in the following glob pattern: `${projectPath}/**/*.{ts,tsx,js,jsx}`.

updateEntrypointsFrom0_x_xTo0_2_x({
projectPath: pathToMyProject,
shouldLog: true,
});

其他选项

¥Other options

updateEntrypointsFrom0_x_xTo0_2_x({
projectPath: pathToMyProject,
tsConfigPath: "tsconfig.json", // Path to the tsConfig file. This will be used to load all the project files into the script.
testRun: true, // If true, the script will not save any changes, but will log the changes that would be made.
files: ["..."], // A list of .ts file paths to check. If this is provided, the script will only check these files.
});