Added new bot-chat module
This commit is contained in:
parent
7d735f5785
commit
8861c0f343
|
|
@ -1,156 +0,0 @@
|
|||
const Discord = require("discord.js");
|
||||
const fetch = require("node-fetch");
|
||||
const fs = require('fs');
|
||||
|
||||
const server = "192.168.1.70";
|
||||
|
||||
class Fourchat {
|
||||
|
||||
|
||||
async random_prompt() {
|
||||
const response = await fetch(`http://${server}:7860/run/textgen`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
data: [
|
||||
`-----
|
||||
--- 865467536
|
||||
`,
|
||||
250, //max tokens
|
||||
true, //do_sample
|
||||
0.63, //temperature
|
||||
0.98, //top_p
|
||||
1, //typical_p
|
||||
1.05, //rep penalty
|
||||
0, //top_k
|
||||
0, //min_length
|
||||
0, //no_repeat_ngram_size
|
||||
1, //num_beams
|
||||
0, //penalty_alpha
|
||||
1, //length_penalty
|
||||
false, //early_stopping
|
||||
]
|
||||
})
|
||||
});
|
||||
|
||||
const response_json = await response.json();
|
||||
return response_json;
|
||||
}
|
||||
|
||||
async prompt(input_text) {
|
||||
const response = await fetch(`http://${server}:7860/run/textgen`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
data: [
|
||||
`-----
|
||||
--- 865467536
|
||||
${input_text}
|
||||
--- 865467537
|
||||
>>865467536
|
||||
`,
|
||||
250, //max tokens
|
||||
true, //do_sample
|
||||
0.63, //temperature
|
||||
0.98, //top_p
|
||||
1, //typical_p
|
||||
1.05, //rep penalty
|
||||
0, //top_k
|
||||
0, //min_length
|
||||
0, //no_repeat_ngram_size
|
||||
1, //num_beams
|
||||
0, //penalty_alpha
|
||||
1, //length_penalty
|
||||
false, //early_stopping
|
||||
]
|
||||
})
|
||||
});
|
||||
|
||||
const response_json = await response.json();
|
||||
return response_json;
|
||||
}
|
||||
|
||||
async prompt_reply(input_text1,input_text2) {
|
||||
const response = await fetch(`http://${server}:7860/run/textgen`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
data: [
|
||||
`-----
|
||||
--- 865467535
|
||||
${input_text1}
|
||||
--- 865467536
|
||||
>>865467535
|
||||
${input_text2}
|
||||
--- 865467537
|
||||
>>865467536
|
||||
`,
|
||||
250, //max tokens
|
||||
true, //do_sample
|
||||
0.63, //temperature
|
||||
0.98, //top_p
|
||||
1, //typical_p
|
||||
1.05, //rep penalty
|
||||
0, //top_k
|
||||
0, //min_length
|
||||
0, //no_repeat_ngram_size
|
||||
1, //num_beams
|
||||
0, //penalty_alpha
|
||||
1, //length_penalty
|
||||
false, //early_stopping
|
||||
]
|
||||
})
|
||||
});
|
||||
|
||||
const response_json = await response.json();
|
||||
return response_json;
|
||||
}
|
||||
|
||||
find_response(res) {
|
||||
const startIndex = res.indexOf("865467537");
|
||||
const output_only = res.slice(startIndex+9);
|
||||
const responseIndex = output_only.indexOf(">>865467536");
|
||||
const s = output_only.slice(responseIndex+12).split("---")[0];
|
||||
return s.trim();
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.matches = [ "4chat", "botmention", "botrandom" ];
|
||||
}
|
||||
|
||||
async action(msg, botrandom) {
|
||||
let chan = msg.channel;
|
||||
try {
|
||||
const input_text = msg.content.replace("!4chat","").replace(/<@.+?>/,"anon").trim();
|
||||
console.log(`Input Text: ${input_text}`);
|
||||
await chan.sendTyping();
|
||||
if (botrandom == "botrandom" || msg.content.startsWith('!botrandom')) {
|
||||
const output_json = await this.random_prompt();
|
||||
const output_text = (output_json.data[0]);
|
||||
const startIndex = output_text.indexOf("865467536");
|
||||
const output_only = output_text.slice(startIndex+9).split("---")[0].trim();
|
||||
console.log(`Generating random nonsense: ${output_only}`);
|
||||
chan.send(output_only);
|
||||
} else if (msg.reference) {
|
||||
const repliedTo = await msg.channel.messages.fetch(msg.reference.messageId);
|
||||
const oldReply = repliedTo.content;
|
||||
const output_json = await this.prompt_reply(oldReply,input_text);
|
||||
const output_text = (output_json.data[0]);
|
||||
const trimmed_text = this.find_response(output_text);
|
||||
console.log(`Generated Reply: ${trimmed_text}`);
|
||||
chan.send(trimmed_text);
|
||||
} else {
|
||||
const output_json = await this.prompt(input_text);
|
||||
const output_text = (output_json.data[0]);
|
||||
const trimmed_text = this.find_response(output_text);
|
||||
console.log(`Generated Message: ${trimmed_text}`);
|
||||
chan.send(trimmed_text);
|
||||
}
|
||||
} catch (e) {
|
||||
chan.send(`Error: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Fourchat;
|
||||
Loading…
Reference in New Issue