chat-application-322 / GPT4All.js
GPT4All.js
Raw
const gpt4all = require('gpt4all')

function GPT4All(){
    this.model = null;

    this.loadModel = async function(modelName, options){
        this.model = await gpt4all.loadModel(modelName, options);
    }

    this.checkIfMessageInappropriate = async function(message){

        const messages = [
            { 
                role: "system", 
                content: "You are acting as a content moderator for a chat application. Find offensive, sexual, or hateful content such as curse words, slurs, racism, sexism and homophobia towards other users, but try to avoid flagging coloquial writting or sentiments. Determine whether the message is inappropriate and answer yes or no, if yes also include a justification for it"
            },
            {
                role: "user",
                content: "Is this message inappropriate: Sup? I am going to f you up!"
            },
            {
                role: "assistant",
                content: "Yes. The message contains a threat, making it inappropriate."
            },
            {
                role: "user",
                content: "Is this message inappropriate: I hate it here"
            },
            {
                role: "assistant",
                content: "No."
            },
            {
              role: "user",
              content: "Is this message inappropriate: I hate this bitch "
            },
            {
                role: "assistant",
                content: "Yes. The message is inappropriate as it uses offensive language."
            },
            {
                role: "user",
                content: "Is this message inappropriate: Hi, how are you doing? "
            },
            {
                role: "assistant",
                content: "No."
            },
            {
                role: "user",
                content: `Is this message inappropriate: ${message}`
            },
       
        ];
        const result = await gpt4all.createCompletion(this.model, messages);
        const returnMessage = result.choices[0].message.content.split('.');
        var regex = /yes/i;
        const inappropriate = regex.test(returnMessage[0]);
        const returnObj = {
            isInappropriate: inappropriate,
        }
        if (inappropriate){
            returnObj.justification = returnMessage[1];

        }
        console.log("result: ",regex.test(result.choices[0].message.content));
        console.log("AI message: ",result.choices[0].message.content);
        return returnObj;
    }

    this.loadModel('mistral-7b-openorca.gguf2.Q4_0.gguf', {verbose: true});
}

module.exports = GPT4All;