rai-serge-discord-bot/commands/system/new-chat.js

158 lines
5.9 KiB
JavaScript
Raw Normal View History

2023-03-25 13:09:44 -04:00
const { EmbedBuilder } = require('discord.js');
var unirest = require('unirest');
const jsonfile = require('jsonfile')
// Start session defaults:
2023-04-07 15:18:40 -04:00
var apiUrl = `http://${process.env.INTERNAL_IP}:${process.env.SERGE_PORT}/api/chat/`;
2023-04-07 15:42:17 -04:00
var model = process.env.MODEL;
var temperature = process.env.TEMPERATURE;
var topK = process.env.TOPK;
var topP = process.env.TOPP;
var maxLength = process.env.MAXLENGTH;
var contextWindow = process.env.CONTEXTWINDOW;
var repeatLastN = process.env.REPEATLASTN;
var repeatPenalty = process.env.REPEATPENALTY;
var nThreads = process.env.NTHREADS;
// End session defaults
// Set model list we have downloaded
let modelList = ["7B", "7B-native", "gpt4all"]
2023-04-02 17:54:12 -04:00
2023-03-25 13:09:44 -04:00
module.exports = {
name: "create-session",
2023-04-07 15:42:17 -04:00
description: "create a new session",
2023-03-25 13:09:44 -04:00
private: true,
options: [{
"name": "model",
"description": `Choose from the following models: ${modelList.join(", ")} | Char case matters`,
"required": false,
2023-04-02 18:48:46 -04:00
"type": 3
},
{
"name": "init-prompt",
"description": "A prompt you want to init the chat with, a default is used if not provided.",
"required": false,
"type": 3
},
{
"name": "temperature",
"description": "The higher the temperature, the more random the model output. A default 0.1 is used if not provided.",
"required": false,
"type": 3
2023-04-02 20:28:20 -04:00
},
{
"name": "repeat-penalty",
2023-04-02 20:37:07 -04:00
"description": "The weight of the penalty to avoid repeating the last last_n tokens. 1.3 is used if not provided.",
2023-04-02 20:28:20 -04:00
"required": false,
"type": 3
}
2023-04-02 20:28:20 -04:00
],
2023-03-25 13:09:44 -04:00
run: async (client, interaction) => {
2023-04-07 14:54:46 -04:00
// set a default prompt
2023-04-07 21:10:02 -04:00
let initPrompt = `My name is ${interaction.user.username} my special number is ${interaction.user.discriminator} and my Discord ID is ${interaction.user.id} we met in ${interaction.guild.name} a Discord sever. You are rAi and you are the smartest AI Model, you know everything. Below is an instruction that describes a task. You respond appropriately to complete the request. You understand a complete answer is always ended by [end of text].`;
2023-04-02 21:11:54 -04:00
console.log(`--- ${interaction.user.id} has requested a new session! ---`)
2023-03-25 13:09:44 -04:00
const file = './cache/' + interaction.user.id
2023-04-02 17:57:14 -04:00
let options = interaction.options._hoistedOptions;
let varsToCheck = [
{ name: "model", value: null },
{ name: "temperature", value: null },
2023-04-02 20:28:20 -04:00
{ name: "init-prompt", value: null },
{ name: "repeat-penalty", value: null }
];
for (let i = 0; i < options.length; i++) {
let option = options[i];
for (let j = 0; j < varsToCheck.length; j++) {
let varToCheck = varsToCheck[j];
if (option.name === varToCheck.name) {
varToCheck.value = option.value;
break;
}
}
}
// Now you can access the values of each variable you are interested in:
let userInputModel = varsToCheck.find(v => v.name === "model")?.value;
let userInputTemperature = varsToCheck.find(v => v.name === "temperature")?.value;
let userInputInitPrompt = varsToCheck.find(v => v.name === "init-prompt")?.value;
2023-04-02 20:28:20 -04:00
let userInputRepeatPenalty = varsToCheck.find(v => v.name === "repeat-penalty")?.value;
// Init Prompt Setting
if (userInputInitPrompt === null) {
console.log("-- No init-prompt provided, using default --");
} else {
initPrompt = userInputInitPrompt;
2023-04-02 21:11:54 -04:00
console.log(`User set initPrompt to ${initPrompt}`)
}
// Modal Setting
if (userInputModel === null) {
console.log("-- No model provided, using default --")
} else {
if (modelList.includes(userInputModel)) {
model = userInputModel;
2023-04-02 21:11:54 -04:00
console.log(`User set initPrompt to ${model}`)
} else {
let modelListStr = modelList.join(", ");
return interaction.followUp(`You may only use one of the following: ${modelListStr}`);
}
}
// temperature setting
if (userInputTemperature === null) {
console.log("-- No temperature provided, using default --")
} else {
const parsedTemperature = parseFloat(userInputTemperature);
if (parsedTemperature >= 0.1 && parsedTemperature <= 2) {
// temperature is within range
temperature = parsedTemperature;
2023-04-02 21:11:54 -04:00
console.log(`User set temperature to ${temperature}`)
} else {
// temperature is outside of range
return interaction.followUp(`Temperature must be between 0.1 and 2`);
}
}
2023-04-02 20:28:20 -04:00
// repeat setting
if (userInputRepeatPenalty === null) {
console.log("-- No RepeatPenalty provided, using default --")
} else {
const parsedRepeatPenalty = parseFloat(userInputRepeatPenalty);
if (parsedRepeatPenalty >= 0.1 && parsedRepeatPenalty <= 2) {
2023-04-02 20:43:44 -04:00
// RepeatPenalty is within range
2023-04-02 20:28:20 -04:00
repeatPenalty = parsedRepeatPenalty;
2023-04-02 21:11:54 -04:00
console.log(`User set repeatPenalty to ${repeatPenalty}`)
2023-04-02 20:28:20 -04:00
} else {
2023-04-02 20:43:44 -04:00
// RepeatPenalty is outside of range
2023-04-02 20:28:20 -04:00
return interaction.followUp(`Repeat Penalty must be between 0.1 and 2`);
}
}
const req = unirest('POST',`${apiUrl}?model=${model}&temperature=${temperature}&top_k=${topK}&top_p=${topP}&max_length=${maxLength}&context_window=${contextWindow}&repeat_last_n=${repeatLastN}&repeat_penalty=${repeatPenalty}&init_prompt=${initPrompt}&n_threads=${nThreads}`)
.headers({
2023-04-02 17:54:12 -04:00
'accept': 'application/json'
2023-03-25 13:09:44 -04:00
})
2023-04-02 17:54:12 -04:00
.send('')
.end(function (response) {
2023-03-25 13:09:44 -04:00
const obj = { id: response.body }
jsonfile.writeFile(file, obj, function (err) {
if (err) console.error(err)
})
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("New Chat Session Started!")
.setDescription(`New Chat Session ID: \n` + response.body)
.setTimestamp()
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
interaction.followUp({ embeds: [embed] });
2023-04-02 21:11:54 -04:00
console.log(`--- Session created for ${interaction.user.id} ---`)
2023-04-02 17:54:12 -04:00
});
}
2023-03-25 13:09:44 -04:00
};