Skip to main content

LangChain v0.2

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

迁移

这份文档将帮助您将代码升级到 LangChain 0.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. 新的导入语句始终放置在全局作用域中,即使被替换的旧导入语句位于某个局部作用域内(例如,函数体内部)。
  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 方法在 ChatModels 中已被弃用,改为使用 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.
});