2023-03-25 13:09:44 -04:00
const { EmbedBuilder } = require ( 'discord.js' ) ;
var unirest = require ( 'unirest' ) ;
const jsonfile = require ( 'jsonfile' )
2023-04-02 20:06:58 -04:00
// 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-02 20:06:58 -04:00
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 ;
2023-04-02 20:06:58 -04:00
// End session defaults
// Set model list we have downloaded
2023-04-02 18:33:59 -04:00
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 ,
2023-04-02 18:20:06 -04:00
options : [ {
2023-04-02 18:33:59 -04:00
"name" : "model" ,
2023-04-07 20:43:12 -04:00
"description" : ` Choose from the following models: ${ modelList . join ( ", " ) } | Char case matters ` ,
2023-04-02 18:33:59 -04:00
"required" : false ,
2023-04-02 18:48:46 -04:00
"type" : 3
2023-04-02 18:33:59 -04:00
} ,
{
2023-04-02 18:20:06 -04:00
"name" : "init-prompt" ,
"description" : "A prompt you want to init the chat with, a default is used if not provided." ,
"required" : false ,
2023-04-02 20:06:58 -04:00
"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:06:58 -04:00
}
2023-04-02 20:28:20 -04:00
] ,
2023-04-02 20:06:58 -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
2023-04-02 20:06:58 -04:00
let options = interaction . options . _hoistedOptions ;
2023-04-02 20:50:13 -04:00
2023-04-02 20:06:58 -04:00
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 }
2023-04-02 20:06:58 -04:00
] ;
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 ;
}
}
}
2023-04-02 20:50:13 -04:00
2023-04-02 20:06:58 -04:00
// 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 ;
2023-04-02 20:06:58 -04:00
// Init Prompt Setting
if ( userInputInitPrompt === null ) {
console . log ( "-- No init-prompt provided, using default --" ) ;
2023-04-02 18:33:59 -04:00
} else {
2023-04-02 20:06:58 -04:00
initPrompt = userInputInitPrompt ;
2023-04-02 21:11:54 -04:00
console . log ( ` User set initPrompt to ${ initPrompt } ` )
2023-04-02 18:33:59 -04:00
}
2023-04-02 20:06:58 -04:00
// Modal Setting
if ( userInputModel === null ) {
2023-04-02 18:33:59 -04:00
console . log ( "-- No model provided, using default --" )
} else {
2023-04-02 20:06:58 -04:00
if ( modelList . includes ( userInputModel ) ) {
model = userInputModel ;
2023-04-02 21:11:54 -04:00
console . log ( ` User set initPrompt to ${ model } ` )
2023-04-02 18:33:59 -04:00
} else {
let modelListStr = modelList . join ( ", " ) ;
2023-04-02 20:06:58 -04:00
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 } ` )
2023-04-02 20:06:58 -04:00
} else {
// temperature is outside of range
return interaction . followUp ( ` Temperature must be between 0.1 and 2 ` ) ;
2023-04-02 18:33:59 -04:00
}
}
2023-04-02 18:20:06 -04:00
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 ` ) ;
}
}
2023-04-02 20:50:13 -04:00
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
} ;