#Trying to get response from GPT 3.5 using XHR XMLHttpRequest();

2 messages · Page 1 of 1 (latest)

hushed fjord
#
const chatForm = document.getElementById("chat-form");
const chatInput = document.getElementById("chat-input");
const chatOutput = document.getElementById("chat-output");
const url = "https://api.openai.com/v1/chat/completions";
const apiKey = "******************************************";

chatForm.addEventListener("submit", async (e) => {
    let xhr = new XMLHttpRequest();
    const data =  {
        "model": "gpt-3.5-turbo",
        "messages": [
            {"role": "system", "content": "Sen insanlara Viora adlı mutfak, yemek, temizlik ve temizlik ürünleri sitesi için mutfak, yemek, temizlik veya temizlik ürünleri hakkında bilgi veren Viora adında bir botsun"},
            {"role": "user", "content": chatInput}
        ]
    }

    xhr.open('POST',url, true);
    xhr.setRequestHeader("Content-Type application/json");
    xhr.setRequestHeader("Authorization: Bearer "+ apiKey);
    xhr.send(data);

    console.log()
});

Code above is what I have so far, I just need to get the response from it to be finished. I can't use node.js, so I have to use XMLHttpRequest() to send the HTTP requests to OpenAI API.

short thunder
#

thats lowkey sus, but you'll need to register event listeners against your xhr object

const req = new XMLHttpRequest();

req.addEventListener("progress", updateProgress);
req.addEventListener("load", transferComplete);
req.addEventListener("error", transferFailed);
req.addEventListener("abort", transferCanceled);

So it looks like you need to pass in a fn to xhr.addEventListener("load", (response) => {...})