使用PHP的Azure GPT-4 API

0aydgbwb  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(147)

使用curl,我可以建立一个成功的连接,它返回一个完成:

curl "https://myazure.openai.azure.com/openai/deployments/azureGPT4/chat/completions?api-version=2023-07-01-preview" \
  -H "Content-Type: application/json" \
  -H "api-key: myapikey" \
  -d '{"messages":[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello"}]}'

字符串
但是,当尝试使用此PHP脚本执行相同操作时,

$api_url = "https://myazure.openai.azure.com/openai/deployments/azureGPT4/chat/completions?api-version=2023-07-01-preview";

$api_key = "myapikey";

$request_data = array(
    'engine' => 'azureGPT4',
    'messages' => [
        ["role" => "system", "content" => "You are a helpful assistant."],
        ["role" => "user", "content" => "Hello"]
    ]
);

$request_json = json_encode($request_data);

$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_STDERR, fopen('php://stderr', 'w')); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Ocp-Apim-Subscription-Key: ' . $api_key, 
));

$response = curl_exec($ch);


我得到这个错误:
错误代码:由于订阅密钥无效或API终结点错误,访问被拒绝。请确保为活动订阅提供有效密钥,并为资源使用正确的区域API终结点。

xxe27gdn

xxe27gdn1#

这个办法奏效了:
1.删除引擎参数

$request_data = array(
        //'engine' => 'azureGPT4',
        'messages' => [
            ["role" => "system", "content" => "You are a helpful assistant."],
            ["role" => "user", "content" => "Hello"]
        ]
    );

字符串
1.使用“api-key”而不是“ocp-Apim-Subscription-Key”

'Content-Type: application/json',
        'api-key: ' . $api_key,

相关问题