Skip to main content
LangChain.js 支持 Moonshot AI 系列模型。 https://platform.moonshot.cn/docs/intro

安装

您需要注册一个 Moonshot API 密钥,并将其设置为名为 MOONSHOT_API_KEY 的环境变量。 https://platform.moonshot.cn/console 您还需要安装以下依赖项:
有关安装 LangChain 包的通用说明,请参阅此部分
npm
npm install @langchain/community @langchain/core

用法

示例如下:
import { ChatMoonshot } from "@langchain/community/chat_models/moonshot";
import { HumanMessage } from "@langchain/core/messages";

// 默认模型是 moonshot-v1-8k
const moonshotV18K = new ChatMoonshot({
  apiKey: "YOUR-API-KEY", // 在 Node.js 中默认为 process.env.MOONSHOT_API_KEY
});

// 使用 moonshot-v1-128k
const moonshotV1128k = new ChatMoonshot({
  apiKey: "YOUR-API-KEY", // 在 Node.js 中默认为 process.env.MOONSHOT_API_KEY
  model: "moonshot-v1-128k", // 可用模型: moonshot-v1-8k, moonshot-v1-32k, moonshot-v1-128k
  temperature: 0.3,
});

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

const res = await moonshotV18K.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 moonshotV1128k.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.",
}
*/

相关