OpenAI ChatGPT (GPT-3.5) API error in WordPress PHP code: "Invalid URL (POST /v4/engines/davinci-codex/completions)"

huangapple go评论87阅读模式
英文:

OpenAI ChatGPT (GPT-3.5) API error in WordPress PHP code: "Invalid URL (POST /v4/engines/davinci-codex/completions)"

问题

以下是翻译好的部分:

我已要求ChatGPT生成一段代码,以将其合并到WordPress的function.php中,以自动生成所有新创建或更新的帖子的标签。

以下是代码:

function add_tags_with_gpt($post_ID) {
    // 检查文章内容是否实际更改
    $post = get_post($post_ID);
    $old_content = get_post_meta($post_ID, '_old_content', true);
    $new_content = $post->post_content;
    if ($old_content === $new_content) {
        return; // 内容未更改,因此不执行任何操作
    }
    update_post_meta($post_ID, '_old_content', $new_content);

    // 使用OpenAI API生成标签
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v4/engines/davinci-codex/completions');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
        'prompt' => 'Generate tags for a blog post with the following content: '.$new_content,
        'max_tokens' => 60
    )));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: Bearer your_openai_api_key', // 用您的实际API密钥替换'your_openai_api_key'
        'Content-Type: application/json'
    ));
    $response = curl_exec($ch);
    if (!$response) {
        // 将错误记录到WordPress的调试日志文件中
        error_log('生成标签时出错: ' . curl_error($ch));
        return;
    }
    curl_close($ch);

    // 将API响应记录到WordPress的调试日志文件中
    error_log('OpenAI API响应: ' . $response);

    $response_data = json_decode($response, true);
    if(!isset($response_data['choices'][0]['text'])) {
        error_log('错误: API响应不包含标签');
        return;
    }
    $tags = explode(',', $response_data['choices'][0]['text']);
    $tags = array_slice($tags, 0, 8);

    // 验证和清理标签
    $tags = array_map('sanitize_text_field', $tags);
    $tags = array_map('wp_strip_all_tags', $tags);
    $tags = array_filter($tags, function($tag) {
        return strlen($tag) > 2 && strlen($tag) <= 20; // 排除长度小于3和大于20的标签
    });

    // 将标签添加到文章
    wp_set_post_tags($post_ID, $tags, true);
}
add_action('save_post', 'add_tags_with_gpt');

请注意,这段代码有一个错误,它尝试使用OpenAI API生成标签,但似乎出现了无效的URL错误。你可能需要检查API的URL是否正确,并确保你的API密钥有效。另外,PHP已弃用str_replace()函数中传递null参数的用法,你可以尝试修复这个问题。

英文:

I've asked ChatGPT to generate a code to be incorporated in WordPress' function.php to generate tags automatically for all my newly created or updated post.

Here is the code:

function add_tags_with_gpt($post_ID) {
// V&#233;rifier si le contenu de l&#39;article a r&#233;ellement chang&#233;
$post = get_post($post_ID);
$old_content = get_post_meta($post_ID, &#39;_old_content&#39;, true);
$new_content = $post-&gt;post_content;
if ($old_content === $new_content) {
return; // Le contenu n&#39;a pas chang&#233;, donc on ne fait rien
}
update_post_meta($post_ID, &#39;_old_content&#39;, $new_content);
// Utiliser l&#39;API OpenAI pour g&#233;n&#233;rer des tags
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, &#39;https://api.openai.com/v4/engines/davinci-codex/completions&#39;);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
&#39;prompt&#39; =&gt; &#39;Generate tags for a blog post with the following content: &#39;.$new_content,
&#39;max_tokens&#39; =&gt; 60
)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
&#39;Authorization: Bearer your_openai_api_key&#39;, // Remplacez &#39;your_openai_api_key&#39; par votre v&#233;ritable cl&#233; API
&#39;Content-Type: application/json&#39;
));
$response = curl_exec($ch);
if (!$response) {
// Enregistrer l&#39;erreur dans le fichier de log de d&#233;bogage de WordPress
error_log(&#39;Erreur lors de la g&#233;n&#233;ration de tags: &#39; . curl_error($ch));
return;
}
curl_close($ch);
// Enregistrer la r&#233;ponse de l&#39;API dans le fichier de log de d&#233;bogage de WordPress
error_log(&#39;R&#233;ponse de l\&#39;API OpenAI : &#39; . $response);
$response_data = json_decode($response, true);
if(!isset($response_data[&#39;choices&#39;][0][&#39;text&#39;])) {
error_log(&#39;Erreur: La r&#233;ponse de l\&#39;API ne contient pas de tags&#39;);
return;
}
$tags = explode(&#39;,&#39;, $response_data[&#39;choices&#39;][0][&#39;text&#39;]);
$tags = array_slice($tags, 0, 8);
// Valider et nettoyer les tags
$tags = array_map(&#39;sanitize_text_field&#39;, $tags);
$tags = array_map(&#39;wp_strip_all_tags&#39;, $tags);
$tags = array_filter($tags, function($tag) {
return strlen($tag) &gt; 2 &amp;&amp; strlen($tag) &lt;= 20; // Exclure les tags de moins de 3 caract&#232;res et de plus de 20 caract&#232;res
});
// Ajouter les tags &#224; l&#39;article
wp_set_post_tags($post_ID, $tags, true);
}
add_action(&#39;save_post&#39;, &#39;add_tags_with_gpt&#39;);

The problem is that the code doesn't work and returns this error message in debug.log

[23-Jul-2023 15:16:27 UTC] R&#233;ponse de l&#39;API OpenAI : {
&quot;error&quot;: {
&quot;message&quot;: &quot;Invalid URL (POST /v4/engines/davinci-codex/completions)&quot;,
&quot;type&quot;: &quot;invalid_request_error&quot;,
&quot;param&quot;: null,
&quot;code&quot;: null
}
}
[23-Jul-2023 15:16:27 UTC] Erreur: La r&#233;ponse de l&#39;API ne contient pas de tags
[23-Jul-2023 15:16:34 UTC] PHP Deprecated:  str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/***/webapps/***/wp-includes/formatting.php on line 4303

Any idea what the issue is and how to fix it? The problem doesn't come from the API key that I copy/pasted from my OpenAI account into the code.

Many thanks,

答案1

得分: 1

所有引擎 API 端点已被弃用。

OpenAI ChatGPT (GPT-3.5) API error in WordPress PHP code: "Invalid URL (POST /v4/engines/davinci-codex/completions)"

将 URL 从这个...

https://api.openai.com/v4/engines/davinci-codex/completions

...更改为这个。

https://api.openai.com/v1/chat/completions

聊天完成 API需要 4 个必填参数:

  • model
  • messages
    • role
    • content

查看我的先前答案,其中使用gpt-3.5-turbo模型提供了 PHP 中的工作示例。

英文:

All Engines API endpoints are deprecated.

OpenAI ChatGPT (GPT-3.5) API error in WordPress PHP code: "Invalid URL (POST /v4/engines/davinci-codex/completions)"

Change the URL from this...

https://api.openai.com/v4/engines/davinci-codex/completions

...to this.

https://api.openai.com/v1/chat/completions

There are 4 required parameters for the Chat Completions API:

  • model
  • messages
    • role
    • content

See my past answer for a working example in PHP using the gpt-3.5-turbo model.

huangapple
  • 本文由 发表于 2023年7月23日 23:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定