You are on page 1of 2

//FRONT END

import { gpt } from 'backend/socket'

let contexto = 'Internet'

$w.onReady(() => {

    gptRequest('Diga olá, e que vc é uma IA e atende em nome de Bad Drow')

    $w('#btnEnviar').onClick(() => {
        gptRequest($w('#tbEntradaGpt').value + ' me responda estritamente no contexto
: ' + contexto)
    })

});

export function gptRequest(texto) {

    $w('#txtResponse').hide()
    $w('#htmlLoading').show()

    gpt(texto)
        .then((responseAi) => {
            $w('#txtResponse').text = responseAi
            $w('#txtResponse').show()
            $w('#htmlLoading').hide()
            $w('#tbEntradaGpt').value = ''
        })

//BACKEND – socket.jsw
//Não esquecer de instalar o pacote NPM ‘openai’

export async function gpt(msg) {
    const { Configuration, OpenAIApi } = require("openai");

    const OPENAI_API_KEY = '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'

    const configuration = new Configuration({
        apiKey: OPENAI_API_KEY,
    });

    const openai = new OpenAIApi(configuration);

    try {
        const completion = await openai.createCompletion({
            model: "text-davinci-003",
            prompt: msg,
            max_tokens: 500,
            n: 1,
            temperature: 0.1,

        });
        console.log(completion.data.choices[0]);
        return completion.data.choices[0].text.replace(/\n/g, '')
    } catch (error) {
        if (error.response) {
            console.log(error.response.status);
            console.log(error.response.data);
        } else {
            console.log(error.message);
        }
    }
}

You might also like