提问人:haz 提问时间:11/8/2023 最后编辑:ADysonhaz 更新时间:11/8/2023 访问量:40
使用 PHP 的 Azure GPT-4 API
Azure GPT-4 API using PHP
问:
使用 I 可以建立成功的连接,这将返回一个完成: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 终结点。
答:
2赞
haz
11/8/2023
#1
这奏效了:
- 删除引擎参数
$request_data = array(
//'engine' => 'azureGPT4',
'messages' => [
["role" => "system", "content" => "You are a helpful assistant."],
["role" => "user", "content" => "Hello"]
]
);
- 使用“api-key”而不是“Ocp-Apim-Subscription-Key”
'Content-Type: application/json',
'api-key: ' . $api_key,
评论