Skip to content

Authorization issue (html 500) #42

@Tom-De-Backer

Description

@Tom-De-Backer

I am trying to call OpenAi in a test website for future projects, but encountered a problem.

On my website I have a button and output field.
when clicking on the button the following code is executed via js:
` <script>
console.log("hello");
window.onload = function() {
document.getElementById("submit-request").addEventListener("click", function() {
var prompt = document.getElementById("prompt-input").value;
var xhr = new XMLHttpRequest();

        xhr.open("GET", "/wp-admin/admin-ajax.php?action=make_request, true);

        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var response = JSON.parse(xhr.responseText);
                document.getElementById("response-output").innerHTML = JSON.stringify(response);
            }
    };
    xhr.send();
});

}
</script>`

The PHP function that is behind the "make_request" is the following:
`
add_action( 'wp_ajax_make_request', 'make_request' );
add_action( 'wp_ajax_nopriv_make_request', 'make_request' );

function make_request() {

$client = OpenAI::client('sk-xxx');

$result = $client->completions()->create([
'model' => 'text-davinci-003',
'prompt' => 'PHP is',
'max_tokens' => 6
]);

echo $result['choices'][0]['text'];

wp_die();
}`

As you can see it is just the basic example from the Readme for the moment. The API key is removed for obvious reasons.
This is the error I got in the webbrowser console:
"GET https://xx.host.com/wp-admin/admin-ajax.php?action=make_request&prompt=kn 500"

What is the problem. Is there some extra authorisation I should do for Openai-php/client?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions