const OpenAI = require("openai")
const client = new OpenAI({
apiKey: "MOONSHOT_API_KEY", // Replace MOONSHOT_API_KEY with the API Key you obtained from the Kimi Open Platform
baseURL: "https://api.moonshot.ai/v1",
});
// We place the System Messages in a separate list because every request should carry the System Messages.
const systemMessages = [
{
role: "system",
content: "You are Kimi, an AI assistant provided by Moonshot AI. You are more proficient in conversing in Chinese and English. You provide users with safe, helpful, and accurate responses. You also reject any questions involving terrorism, racism, pornography, or violence. Moonshot AI is a proper noun and should not be translated into other languages.",
},
];
// We define a global variable messages to record the historical conversation messages between us and the Kimi large language model.
// The messages include both the questions we pose to the Kimi large language model (role=user) and the replies from the Kimi large language model (role=assistant).
// The messages are arranged in chronological order.
let messages = [];
async function makeMessages(input, n = 20) {
/**
* The makeMessages function controls the number of messages in each request to keep it within a reasonable range, such as the default value of 20. When building the message list, we first add the System Prompt because it is essential no matter how the messages are truncated. Then, we obtain the latest n messages from the historical records as the messages for the request. In most scenarios, this ensures that the number of Tokens occupied by the request messages does not exceed the model's context window.
*/
// First, we construct the user's latest question into a message (role=user) and add it to the end of the messages list.
messages.push({
role: "user",
content: input,
});
// newMessages is the list of messages we will use for the next request. Let's build it now.
let newMessages = [];
// Every request must carry the System Messages, so we need to add the systemMessages to the message list first.
// Note that even if the messages are truncated, the System Messages should still be in the messages list.
newMessages = systemMessages.concat(newMessages);
// Here, when the historical messages exceed n, we only keep the latest n messages.
if (messages.length > n) {
messages = messages.slice(-n);
}
newMessages = newMessages.concat(messages);
return newMessages;
}
async function chat(input) {
/**
* The chat function supports multi-turn conversations. Each time the chat function is called to converse with the Kimi large language model, the model can "see" the historical conversation messages that have already been generated. In other words, the Kimi large language model has memory.
*/
// We converse with the Kimi large language model carrying the messages.
const completion = await client.chat.completions.create({
model: "kimi-k2.5",
messages: await makeMessages(input)
});
// Through the API, we obtain the reply message from the Kimi large language model (role=assistant).
const assistantMessage = completion.choices[0].message;
// To ensure the Kimi large language model has a complete memory, we must add the message returned by the model to the messages list.
messages.push(assistantMessage);
return assistantMessage.content;
}
(async () => {
console.log(await chat("Hello, I am 27 years old this year."));
console.log(await chat("Do you know how old I am this year?")); // Here, based on the previous context, the Kimi large language model will know that you are 27 years old this year.
})();