Skip to main content
LangChain.js 支持智谱 AI 系列模型。 https://open.bigmodel.cn/dev/howuse/model

设置

你需要注册获取智谱 API 密钥,并将其设置为名为 ZHIPUAI_API_KEY 的环境变量。 https://open.bigmodel.cn 你还需要安装以下依赖项:
通用安装 LangChain 包的说明请参阅此章节
npm
npm install @langchain/community @langchain/core jsonwebtoken

使用示例

以下是一个示例:
import { ChatZhipuAI } from "@langchain/community/chat_models/zhipuai";
import { HumanMessage } from "@langchain/core/messages";

// 默认模型为 glm-3-turbo
const glm3turbo = new ChatZhipuAI({
  zhipuAIApiKey: "YOUR-API-KEY", // 在 Node.js 中默认为 process.env.ZHIPUAI_API_KEY
});

// 使用 glm-4
const glm4 = new ChatZhipuAI({
  model: "glm-4", // 可用模型:
  temperature: 1,
  zhipuAIApiKey: "YOUR-API-KEY", // 在 Node.js 中默认为 process.env.ZHIPUAI_API_KEY
});

const messages = [new HumanMessage("Hello")];

const res = await glm3turbo.invoke(messages);
/*
AIMessage {
  content: "Hello! How can I help you today? Is there something you would like to talk about or ask about? I'm here to assist you with any questions you may have.",
}
*/

const res2 = await glm4.invoke(messages);
/*
AIMessage {
  text: "Hello! How can I help you today? Is there something you would like to talk about or ask about? I'm here to assist you with any questions you may have.",
}
*/

相关链接