We could not parse the JSON

Hello,
I’m no coder and asked chatGPT 4o to code a plugin for me that create posts on a forum. It gave me code for a wordpress plugin to test the integration and for debugging. Below are current errors and the test plugin:

Array
(
    [error] => Array
        (
            [message] => We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)
            [type] => invalid_request_error
            [param] => 
            [code] => 
        )

)

Raw Response:

{
    "error": {
        "message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

Plugin:

<?php
/*
Plugin Name: OpenAI Integration
Description: A plugin to integrate OpenAI API.
Version: 1.0
Author: Your Name
*/

class Openai {
    private function secret_key() {
        return 'Bearer ******YOUR-KEY-HERE********';
    }

    public function request($model, $prompt, $max_tokens) {
        $request_body = [
            "prompt" => $prompt,
            "max_tokens" => $max_tokens
        ];

        $postfields = json_encode($request_body);
        $headers = [
            'Content-Type' => 'application/json',
            'Authorization' => $this->secret_key()
        ];

        $response = wp_remote_post("https://api.openai.com/v1/engines/$model/completions", [
            'headers' => $headers,
            'body' => $postfields,
            'data_format' => 'body'
        ]);

        if (is_wp_error($response)) {
            return json_encode([
                "error" => $response->get_error_message(),
                "http_code" => wp_remote_retrieve_response_code($response)
            ]);
        } else {
            $body = wp_remote_retrieve_body($response);
            $http_code = wp_remote_retrieve_response_code($response);
            return json_encode([
                "response" => json_decode($body, true),
                "http_code" => $http_code,
                "raw_response" => $body,
                "request_body" => $postfields,
                "headers" => $headers
            ]);
        }
    }
}

function openai_shortcode() {
    $openai = new Openai();
    $model = 'text-davinci-003';
    $prompt = 'Say this is a test';
    $max_tokens = 7;

    $response = $openai->request($model, $prompt, $max_tokens);
    $response_data = json_decode($response, true);

    // Debugging: output the request body, response, headers, and HTTP code
    $output = '<p>Request Body: <pre>' . esc_html($response_data['request_body']) . '</pre></p>';
    $output .= '<p>Headers: <pre>' . esc_html(print_r($response_data['headers'], true)) . '</pre></p>';
    $output .= '<p>Response: <pre>' . esc_html(print_r($response_data['response'], true)) . '</pre></p>';
    $output .= '<p>Raw Response: <pre>' . esc_html($response_data['raw_response']) . '</pre></p>';
    $output .= '<p>HTTP Code: ' . esc_html($response_data['http_code']) . '</p>';
    return $output;
}

add_shortcode('openai_test', 'openai_shortcode');

Is this a question about creating WordPress plug-ins? If yes, I suggest you ask the WordPress community.