LangChain.js 实战系列:入门介绍

本文围绕LangChain.js展开,阐述了该框架在AI应用开发中的优势,包括模型集成、API管理、调用方式、流式处理和高级功能如JSONMode和函数调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

📝 LangChain.js 是一个快速开发大模型应用的框架,它提供了一系列强大的功能和工具,使得开发者能够更加高效地构建复杂的应用程序。LangChain.js 实战系列文章将介绍在实际项目中使用 LangChain.js 时的一些方法和技巧。

LangChain.js 是一个快速构建 AI 应用的库,它提供了一系列的工具,可以帮助你快速构建一个 AI 应用。

LangChain.js 目前还在快速迭代中,这是由于 AI 技术自身也正在快速迭代中,所以很多功能可能很快就被废弃掉,比如 generate() 方法。

使用 LangChain.js 的好处有挺多,比如:

  1. 封装了大量的模型,比如 OpenAI、Azure OpenAI、Claude、文心一言等等,填入响应的 API Key 等参数即可调用
  2. 提供了大量方便的方法,比如链式调用、对话管理、回钩子等等
  3. 和 LangSmith 结合,对 AI 应用可以很好地进行调试开发

LangChain.js 的基本使用

调用模型

LangChain.js 新改版区分了两种调用方式,一种是LLM,一种是ChatModel,不过这两种调用方式本质都一样,最终都是调用模型,一般我们使用后者。

实例化 ChatModel

import { ChatOpenAI } from "langchain/chat_models/openai";

const chatModel = new ChatOpenAI({
  openAIApiKey: "...",
});

这里 openAIApiKey 可以在实例化的时候传入,也可以放置在环境变量 OPENAI_API_KEY 中,这样就不用每次都传入了,LangChain 会自动从 process.env 读取。如果是 Azure OpenAI,那对应的就是 AZURE_OPENAI_API_KEYAZURE_OPENAI_API_INSTANCE_NAMEAZURE_OPENAI_API_DEPLOYMENT_NAME 等等。

接着就可以调用模型:

import { HumanMessage, SystemMessage } from "langchain/chat_models/messages";

const messages = [
  new SystemMessage("你是一位语言模型专家"),
  new HumanMessage("模型正则化的目的是什么?"),
];

这里的 SystemMessage 和 HumanMessage 都是 LangChain.js 提供的消息类,分别表示系统消息和用户消息。用户消息好理解,系统消息的话可以看作是针对 AI 模型的一个高级指令(instruction),比如 SystemMessage("你是一位语言模型专家") 就是告诉 AI 模型,你是一位语言模型专家,这样 AI 模型就会以这个身份来回答你的问题,SystemMessage 是可选的。

await chatModel.invoke(messages);

这里的 invoke() 方法就是调用模型,它会返回一个 Promise,这个 Promise 的结果就是 AI 模型的回复,比如:

AIMessage { content: 'The purpose of model regularization is to prevent overfitting in machine learning models. Overfitting occurs when a model becomes too complex and starts to fit the noise in the training data, leading to poor generalization on unseen data. Regularization techniques introduce additional constraints or penalties to the model's objective function, discouraging it from becoming overly complex and promoting simpler and more generalizable models. Regularization helps to strike a balance between fitting the training data well and avoiding overfitting, leading to better performance on new, unseen data.' }

流式传输

流式传输是一个基本功能了,一开始 LangChain 仅支持使用回调函数的方式来实现,比如:

const chat = new ChatOpenAI({
  streaming: true,
});

const response = await chat.call([new HumanMessage("讲个笑话")], {
  callbacks: [
    {
      handleLLMNewToken(token: string) {
        console.log({ token });
      },
    },
  ],
});

这样每当模型返回的时候,都会触发 handleLLMNewToken 回调函数,新版 LangChain.js 更加灵活,使用 .stream() 方法可以实现同样的功能:

const stream = await chat.stream([new HumanMessage("讲个笑话")]);

for await (const chunk of stream) {
  console.log(chunk);
}

这里的 stream 是一个 AsyncIterableIterator,可以使用 for await 来遍历,每当模型返回的时候,就会触发 for await 中的代码。

JSON Mode

JSON Mode 是 OpenAI 新版的能力,它可以让你更好地控制 AI 模型的输出,比如:

const jsonModeModel = new ChatOpenAI({
  modelName: "gpt-4-1106-preview",
}).bind({
  response_format: {
    type: "json_object",
  },
});

注意,目前仅 gpt-4-1106-preview 模型支持 JSON Mode,另外还有一个强制性的要求,就是 SystemMessage 必须包含 JSON 字眼:

const res = await jsonModeModel.invoke([
  ["system", "Only return JSON"],
  ["human", "Hi there!"],
]);

后续 GPT 迭代 JSON Mode 应该就会变成通用能力,之语 SystemMessage 的规则,不知道后续会不会改变。

函数调用

函数调用(Function Calling)是 OpenAI 的一个重点能力,也就是目前 AI 应用和程序的一个重要交互协议。函数调用其实很简单,就是先让 AI 去选择调用哪个函数,然后在程序中调用真正的函数。

最常见的场景就是联网回答,你提供了「联网搜索」的函数,当用户提问「今天的重点新闻是什么」的时候,AI 会先调用「联网搜索」函数,然后根据函数执行得到的信息,最终再回答用户的问题。

OpenAI 使用 JSON Schema 来定义函数调用的协议,比如定义一个提取字段的函数:

const extractionFunctionSchema = {
  // 定义函数的名字
  name: "extractor",
  // 定义函数的描述
  description: "Extracts fields from the input.",
  // 定义函数的入参有哪些
  parameters: {
    type: "object",
    properties: {
      tone: {
        type: "string",
        enum: ["positive", "negative"],
        description: "The overall tone of the input",
      },
      word_count: {
        type: "number",
        description: "The number of words in the input",
      },
      chat_response: {
        type: "string",
        description: "A response to the human's input",
      },
    },
    required: ["tone", "word_count", "chat_response"],
  },
};

也可以使用 zod 这个库,写起来更方便:

import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";

const extractionFunctionSchema = {
  name: "extractor",
  description: "Extracts fields from the input.",
  parameters: zodToJsonSchema(
    z.object({
      tone: z
        .enum(["positive", "negative"])
        .describe("The overall tone of the input"),
      entity: z.string().describe("The entity mentioned in the input"),
      word_count: z.number().describe("The number of words in the input"),
      chat_response: z.string().describe("A response to the human's input"),
      final_punctuation: z
        .optional(z.string())
        .describe("The final punctuation mark in the input, if any."),
    })
  ),
};

调用函数:

const model = new ChatOpenAI({
  modelName: "gpt-4",
}).bind({
  functions: [extractionFunctionSchema],
  function_call: { name: "extractor" },
});
const result = await model.invoke([new HumanMessage("What a beautiful day!")]);
console.log(result);
/*
AIMessage {
  //...
  additional_kwargs: {
    function_call: {
      name: 'extractor',
      arguments: '{\n' +
        '"tone": "positive",\n' +
        '"entity": "day",\n' +
        '"word_count": 4,\n' +
        `"chat_response": "I'm glad you're enjoying the day!",\n` +
        '"final_punctuation": "!"\n' +
        '}'
    }
  }
}
*/

最后

推荐一些好用的资源

👉 StarFlow.tech ,一个集聊天、工作流和知识库的 AI 平台。在这里,你可以免费使用 ChatGPT3.5 和 3.5 16K,还有 GPT-4 Vision、DELL·E3、Midjourney 等多种模型可供选择。这个平台就像一个小型工作室,助力个人效率 Max!

👉 OpenAI 官方提示词指南 ,专门面向中文的提示词工程指南,该教程是 OpenAI 官方出版,主要包括了六大策略,轻松学习提示词技巧。

### 解决 Maven 中未解析依赖问题 当遇到 `dev.langchain4j:langchain4j-chroma:jar:0.35.0` 的未解析依赖问题时,可能的原因包括以下几个方面: #### 1. **检查本地仓库缓存** 如果本地 Maven 缓存损坏或者缺失该依赖项,则可能导致无法下载。可以尝试清理本地仓库并重新安装依赖。 运行以下命令来清除本地缓存: ```bash mvn dependency:purge-local-repository ``` 之后执行标准的构建流程以重新拉取依赖: ```bash mvn clean install ``` #### 2. **确认中央仓库是否存在目标版本** 有时特定版本并未发布到官方 Maven Central Repository 或其他默认远程仓库中。可以通过访问 [Maven Central](https://search.maven.org/) 并搜索 `dev.langchain4j:langchain4j-chroma` 来验证是否有对应版本号 `0.35.0` 存在[^1]。 如果没有找到匹配的结果,则说明此依赖尚未被上传至公共存储库,需联系开发者团队获取更多信息或寻找替代方案。 #### 3. **添加自定义镜像源地址** 部分开源项目会托管其构件于第三方私有仓库而非公开渠道上。对于 LangChain4J 而言,建议查阅文档了解是否提供了额外的 repository URL 地址。如果有指定位置,则应在 `pom.xml` 文件内的 `<repositories>` 部分加入相应条目以便正确加载资源。 例如,在某些情况下可增加如下片段: ```xml <repositories> <repository> <id>langchain4j</id> <url>https://repo.example.com/langchain4j</url> </repository> </repositories> ``` 注意替换实际可用链接作为上述示例中的占位符内容。 #### 4. **手动引入 JAR 包** 假如以上方法均不可行而仍然迫切需要使用这个组件的话,还可以考虑将其原始 jar 文件直接导入工程之中作为一种临时解决方案。具体操作步骤如下所示: - 下载所需版本的二进制包; - 使用 mvn 命令将它显式注册进入全局范围: ```bash mvn install:install-file \ -Dfile=/path/to/downloaded.jar \ -DgroupId=dev.langchain4j \ -DartifactId=langchain4j-chroma \ -Dversion=0.35.0 \ -Dpackaging=jar ``` 完成这些调整后再次尝试编译应该能够解决问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值