OpenAI Service not return httpRequest

Henrique Eduardo Souza 0 Reputation points
2023-02-19T06:04:27.1566667+00:00

I try to test OpenAI Service, but it's no answer in correct response object.

  • JQuery not works
  • Feach not response object
  • XHR recive cors error
  • axios 🆗
function askToOpenAI(question) {
            debugger;
            var headers = new Headers();
            headers.append("Content-Type", "application/json");
            headers.append("api-key", "{{KEY}}");

            var raw = JSON.stringify({
                "prompt": question,
                "max_tokens": 4000,
                "temperature": 0.7,
                "frequency_penalty": 0,
                "presence_penalty": 0,
                "top_p": 1,
                "best_of": 1,
                "stop": null
            });

            var requestOptions = {
                method: 'POST',
                headers: headers,
                body: raw,
                redirect: 'follow'
            };

            fetch(urlGPT, requestOptions)
                .then(response => {
                    debugger;
                    console.log("Text: " + JSON.stringify(response.text()))
                })
                .then(result =>{
                        console.log(result)
                }).catch(error => console.log('error', error));
        }
Azure OpenAI in Foundry Models
0 comments No comments

2 answers

Sort by: Most helpful
  1. Henrique Eduardo Souza 0 Reputation points
    2023-02-20T15:50:44.06+00:00

    Thx for your attention !

    I tried to put header headers.append("Access-Control-Allow-Origin", "*");

    not works too, but when I consume by C#, Python or Java and JS with axios LIB Ok !

    If you could run same code was I posted, and check the result object result 200 OK but the object it's null

    Was this answer helpful?

    0 comments No comments

  2. Ramr-msft 17,836 Reputation points
    2023-02-20T15:27:03.55+00:00

    @Henrique Souza It seems like you are encountering a CORS error when trying to access the OpenAI API. This error occurs because the API is hosted on a different domain than your web application, and the browser is blocking the request due to security reasons.

    To resolve this issue, you can either use a proxy server to make the API request on behalf of your web application, or you can configure the API server to allow requests from your domain.

    In your code, you can add the following header to allow requests from your domain:

    headers.append("Access-Control-Allow-Origin", "*");
    

    This header will allow requests from any domain, but you can also specify a specific domain if you prefer.

    If you are using a proxy server, you can configure your fetch request to use the proxy server instead of directly accessing the API.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.