How do I use chatgpt plugin in javascript?

  Kiến thức lập trình

I tried to import chatGPT into my js project to use it lately in html,
but the response is empty. I want to use it to generate random stories.
Do you know what might cause this?

const form = 0;
const mytextInput = 0;
const responseTextarea = 0;

function dok(){
    form = document.getElementById(`chat-form`);
    mytextInput = document.getElementById('mytext');
    responseTextarea = document.getElementById('response');
    document.getElementById("response").innerHTML = response;
}


const API_KEY = "sk-proj-d127W0pgdYpbW6uDRaUvT3BlbkFJ90Q1QFAuCi7UMkxxES8P";
const response = "";

async function generate()   {
    const mytext = mytextInput.value.trim();
    const mytext2 = "hello"
    if (mytext2) {
        try {
            response = await fetch('https://api.openai.com/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${API_KEY}`
                },
                body: JSON.stringify({
                    model: 'gpt-4o',
                    messages: [{ role: 'user', content: mytext }],
                    temperature: 1.0,
                    top_p: 0.7,
                    n: 1,
                    stream: false,
                    presence_penalty: 0,
                    frequency_penalty: 0,
                }),
            });

            if (response.ok) {
                const data = await response.json();
                responseTextarea.value = data.choices[0].message.content;
                
            } else {
                responseTextarea.value = 'Error: Unable to process your request.';
            }
        } catch (error) {
            console.error(error);
            responseTextarea.value = 'Error: Unable to process your request.';
        }
    }
};

I tried to completely rewrite it, use yt tutorials, but nothing worked and when I finally fixed all the errors in the code.

npm run dev

> [email protected] dev
> node skript1.js



New contributor

michal Orsah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT