function wordsmith_ai_rewrite_title( $title ) {
$api_key = get_option( ‘wordsmith_ai_api_key’ );
$url = ‘https://api.openai.com/v1/engines/davinci-codex/completions’;
$headers = array(
‘Content-Type: application/json’,
‘Authorization: Bearer ‘ . $api_key
);
$data = array(
‘prompt’ => ‘Rewrite the following title for SEO: “‘ . $title . ‘”.’,
‘max_tokens’ => 50,
‘temperature’ => 0.5,
‘n’ => 1,
‘stop’ => ‘.’
);
$options = array(
‘http’ => array(
‘header’ => implode( “\r\n”, $headers ),
‘method’ => ‘POST’,
‘content’ => json_encode( $data ),
‘timeout’ => 30
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result, true );
var_dump($title); // Add this line to check the value of $title
var_dump($response); // Add this line to check the value of $response
return $response[‘choices’][0][‘text’];
}