gpt4 book ai didi

php - OpenAI GPT-3 API 错误 : "You didn' t provide an API key. 您需要使用 Bearer auth 在授权 header 中提供您的 API key ”

转载 作者:行者123 更新时间:2023-12-02 22:45:58 26 4
gpt4 key购买 nike

我收到以下 PHP 代码的错误:

$curl = curl_init("https://api.openai.com/v1/engines/davinci/completions");

$data = array(
'prompt' => 'how many sundays in 2023',
'max_tokens' => 256,
'temperature' => 0.7,
'model' => 'text-davinci-003'
);

curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer sk-MY-API-KEY']);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

$result = curl_exec($curl);
curl_close($curl);

$result = json_decode($result);
print $result->choices[0]->text;

我正确提供了 API key ,但出现此错误:

Error message: You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY)

最佳答案

全部Engines endpoints已弃用。

Deprecated

这是正确的 Completions endpoint :

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

工作示例

如果您运行 test.php,OpenAI API 将返回以下完成信息:

string(23) "

This is indeed a test"

test.php

<?php
$ch = curl_init();

$url = 'https://api.openai.com/v1/completions';

$api_key = 'sk-xxxxxxxxxxxxxxxxxxxx';

$post_fields = '{
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}';

$header = [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
];

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);

$response = json_decode($result);
var_dump($response->choices[0]->text);
?>

关于php - OpenAI GPT-3 API 错误 : "You didn' t provide an API key. 您需要使用 Bearer auth 在授权 header 中提供您的 API key ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75246412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com