当前位置: 首页 > news >正文

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

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 官方出版,主要包括了六大策略,轻松学习提示词技巧。

http://www.lryc.cn/news/270303.html

相关文章:

  • pyCharm 打印控制台中文乱码解决办法
  • 计算机基础--Linux详解
  • 基于OpenAI的Whisper构建的高效语音识别模型:faster-whisper
  • cfa一级考生复习经验分享系列(十六)
  • 数模学习day05-插值算法
  • hive中struct相关函数总结
  • macos下转换.dmg文件为 .iso .cdr文件的简单方法
  • ALSA学习(5)——设备中的alsa
  • uniapp中组件库的丰富NumberBox 步进器的用法
  • 【Matlab】基于遗传算法优化BP神经网络 (GA-BP)的数据时序预测
  • 计算机毕业设计 基于HTML5+CSS3的在线英语阅读分级平台的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 云原生|kubernetes|kubernetes资源备份和集群迁移神器velero的部署和使用
  • 【26.4K⭐】ShareX:一款开源免费、功能强大且丰富的截屏录屏软件
  • 什么是ajax,为什么使用ajax?
  • AI面板识别 - 华为OD统一考试
  • Linux之磁盘分区,挂载
  • 2核2G3M服务器上传速度多少?以阿里云和腾讯云为例
  • Cisco模拟器-OSPF路由协议
  • SpEL 的使用
  • 数据采集实战:电商详情页数据埋点
  • 计算机网络——计算大题(七)
  • 子网掩码与IP段计算
  • 【译文】IEEE白皮书 6G 太赫兹技术的基本原理 2023版
  • AUTOSAR从入门到精通-网络通信(UDPNm)(三)
  • ubuntu 使用openssl制作一个自签名证书
  • WPF+Halcon 培训项目实战(1-5):Halcon安装,图像处理,Halcon简单模板匹配
  • 虚函数的讲解
  • Java强软弱虚引用
  • QCharView使用
  • 华为hcia之ipv6实验手册