跳到主要内容

LangChain v0.2

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

迁移

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

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

升级到新导入

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

迁移脚本有以下限制

  1. 它仅限于帮助用户从旧导入移动到新导入。它无助于解决其他弃用问题。
  2. 它无法处理涉及 as 的导入。
  3. 新导入始终放置在全局作用域中,即使被替换的旧导入位于某些局部作用域内(例如,函数体)。
  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. 有更好的替代方案可用且最终将被删除的代码,因此只有一种方法可以做事。(例如,ChatModels 中的 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.
});

此页面是否对您有帮助?


您也可以留下详细的反馈 在 GitHub 上.