yandricr / gpti-js

This package simplifies your interaction with various GPT models, removing the need for tokens or other methods to access GPT
https://nexra.aryahcr.cc/
MIT License
42 stars 7 forks source link

some advices #7

Closed InfernalAzazel closed 5 months ago

InfernalAzazel commented 5 months ago

The code for this project should be written in a more standardized way. It is difficult to maintain a bunch of codes all piled together.

The features of this project are valuable and great.

yandricr commented 5 months ago

Thanks for the advice, I'll take it into account.

NEK-RA commented 5 months ago

Also wouldn't it be better to have all exported functions as async/await instead of sync functions with specifying callbacks? async/await code looks much more readable and comfortable to use, isn't it?

UPD: well, it's solvable by promisify from node:util, while using on nodejs.

InfernalAzazel commented 5 months ago

I don't understand. It's quite complicated to use?

2024-01-23 11-24-40屏幕截图

{"code":200,"status":true,"model":"gpt-4","gpt":"Of course, your name is Yandri. How can I assist you today, Yandri?","original":null}

e.g -> https://nexra.aryahcr.cc/documentation/chatgpt/en

fetch("https://nexra.aryahcr.cc/api/chat/gpt", {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
        messages: [
            {
                role: "assistant",
                content: "Hello! How are you today?"
            },
            {
                role: "user",
                content: "Hello, my name is Yandri."
            },
            {
                role: "assitant",
                content: "Hello, Yandri! How are you today?"
            }
        ],
        prompt: "Can you repeat my name?",
        model: "GPT-4",
        markdown: false
    })
}).then((response) => {
    response.text().then((result) => {
        let err = null;
        let res = null;

        let count = -1;
        for(let i = 0; i < result.length; i++){
            if(count <= -1){
                if(result[i] === "{"){
                    count = i;
                }
            } else {
                break;
            }
        }

        if(count <= -1){
            err = {
                "code": 500,
                "status": false,
                "error": "INTERNAL_SERVER_ERROR",
                "message": "general (unknown) error"
            };
            res = null;
        } else {
            try {
                let js = result.slice(count);
                js = JSON.parse(js);
                if(js != undefined && js != null && js.code != undefined && js.code != null && js.code === 200 && js.status != undefined && js.status != null && js.status === true){
                    res = js;
                    err = null;
                } else {
                    err = js;
                    res = null;
                }
            } catch(e){
                err = {
                    "code": 500,
                    "status": false,
                    "error": "INTERNAL_SERVER_ERROR",
                    "message": "general (unknown) error"
                };
                res = null;
            }
        }

        if(res === null && err != null){
            console.log(err);
        } else {
            console.log(res);
        }
    }).catch((err) => {
        console.log(err);
    });
}).catch((err) => {
    console.log(err);
});

We first determine its exception, and then determine whether its status is true, isn't it ok?

fetch('https://nexra.aryahcr.cc/api/chat/gpt', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        "messages": [
            {
                "role": "assistant",
                "content": "Hello! How are you today?"
            },
            {
                "role": "user",
                "content": "Hello, my name is Yandri."
            },
            {
                "role": "assitant",
                "content": "Hello, Yandri! How are you today?"
            }
        ],
        "prompt": "Can you repeat my name?",
        "model": "GPT-4",
        "markdown": false
    })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
yandricr commented 5 months ago

In this part:

if(res === null && err!= null){
     console.log(err);
} else {
     console.log(res);
}

This is how you get the response, if it gives an error it is "err" and if everything is correct it is "res", my server does not support more than 30 seconds of waiting, and I configure it like this, and sometimes it can take up to a minute with other models ​

yandricr commented 5 months ago

I'm sorry for doing it like this, but I had to avoid that sometimes or many times my api cannot respond correctly, due to that limited time of 30 seconds that my server has

yandricr commented 5 months ago

Also wouldn't it be better to have all exported functions as async/await instead of sync functions with specifying callbacks? async/await code looks much more readable and comfortable to use, isn't it?

UPD: well, it's solvable by promisify from node:util, while using on nodejs.

Yes, I know how to use async/await, but I coded it like this, in case I add more ias chats and also for streaming or also to generate images, and it didn't occur to me to use async/await either