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

📝 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

typescript 复制代码
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 等等。

接着就可以调用模型:

typescript 复制代码
import { HumanMessage, SystemMessage } from "langchain/chat_models/messages";

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

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

typescript 复制代码
await chatModel.invoke(messages);

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

typescript 复制代码
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 仅支持使用回调函数的方式来实现,比如:

typescript 复制代码
const chat = new ChatOpenAI({
  streaming: true,
});

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

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

typescript 复制代码
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 模型的输出,比如:

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

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

typescript 复制代码
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 来定义函数调用的协议,比如定义一个提取字段的函数:

typescript 复制代码
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 这个库,写起来更方便:

typescript 复制代码
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."),
    })
  ),
};

调用函数:

typescript 复制代码
const model = new ChatOpenAI({
  modelName: "gpt-4",
}).bind({
  functions: [extractionFunctionSchema],
  function_call: { name: "extractor" },
});
typescript 复制代码
const result = await model.invoke([new HumanMessage("What a beautiful day!")]);
typescript 复制代码
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 官方出版,主要包括了六大策略,轻松学习提示词技巧。

相关推荐
向量引擎10 小时前
向量引擎接入 GPT Image 2 和 deepseek v4:一个 api key 把热门模型串起来,开发者终于不用深夜修接口了
人工智能·gpt·计算机视觉·aigc·api·ai编程·key
Hommy8816 小时前
【开源剪映小助手】字幕接口
开源·github·aigc·剪映小助手·视频剪辑自动化
绘梨衣54719 小时前
Agentic RAG、传统RAG、ReAct、Function Calling 核心关系
人工智能·chatgpt·tensorflow
AI趣实验19 小时前
Hermes Agent LLM Wiki + Obsidian Git 免费替代 Obsidian Sync:保姆级配置教程
aigc·agent
穷人小水滴19 小时前
(AI) 编写简单 AI 助手 (ds-agent)
aigc·agent·deepseek
常威正在打来福19 小时前
【技能篇】OpenClaw Skill 详解:给 AI 装上"专业外挂"
aigc·ai编程
qq56801807620 小时前
国内如何使用Gemini 3.1 Pro?
chatgpt·ai作画·ai编程·ai写作·agi
whyfail20 小时前
AI 平台订阅套餐 Coding Plan 、Token Plan对比指南(2026年4月)
人工智能·ai·chatgpt·订阅套餐·平台对比
小龙报20 小时前
【Coze-AI智能体平台】低代码省时高效:Coze 应用开发全流程指南
java·人工智能·python·深度学习·低代码·chatgpt·交互
大写的老王20 小时前
2026年AI工具终极对比:豆包、DeepSeek、元宝、ChatGPT、Cursor,谁才是你的最佳搭档?
人工智能·chatgpt