Skip to main content

LangChain v0.2 版本

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

迁移

本文档将帮助您将代码升级到 LangChain0.2.x..要准备迁移,我们首先建议您按照以下步骤进行作:

  1. 安装 的 0.2.x 版本@langchain/core、langchain 并升级到您可能正在使用的其他软件包的最新版本(例如@langchain/langgraph,@langchain/community,@langchain/openai等)
  2. 验证您的代码是否使用新包正确运行(例如,单元测试通过)
  3. 安装最新版本的langchain-cli,然后使用该工具将代码使用的旧导入替换为新导入。(请参阅下面的说明。
  4. 手动解决任何剩余的弃用警告
  5. 重新运行单元测试

升级到新的进口

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

迁移脚本具有以下限制:

  1. 它仅限于帮助用户从旧导入转移到新导入。它无助于解决其他弃用问题。
  2. 它无法处理涉及as.
  3. 新导入始终放置在全局范围内,即使被替换的旧导入位于某个本地范围内(例如。g,函数体)。
  4. 它可能会错过一些已弃用的导入。

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

从包装打包已弃用的导入新导入
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"

弃用时间表

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

  1. langchain导入到另一个包中(例如@langchain/community)

如果您尝试从langchain,它将失败,因为入口点已被删除。

  1. 具有更好的替代方案并最终将被删除的代码,因此只有一种方法可以执行作。(例如,predictMessages方法已被弃用,取而代之的是invoke).

其中许多在 0.2 中被标记为删除。我们已将删除率提高到 0.3。

安装

注意

0.2.X 迁移脚本仅在0.0.14-rc.1或之后。

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

用法

鉴于迁移脚本并不完美,您应该确保首先备份您的代码(例如,使用git).

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

调用迁移脚本会将此导入替换为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,
});

其他选项

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.
});