我正在使用php连接openai的whisper界面,但根据文档,我不断报告错误。
下面是我的代码:
<?php
define('OPENAI_API_KEY', 'sk-**********');
$type = empty($_GET['type'])? '':$_GET['type'];
$prompt = empty($_GET['prompt'])? '':$_GET['prompt'];
if(!empty($prompt)||$type=='audio'){
switch($type){
case 'text':
$url = 'https://api.openai.com/v1/completions';
$params['prompt'] = $prompt;
$params['max_tokens'] = 2048;
$params['model'] = 'text-davinci-003';
break;
case 'image':
$url = 'https://api.openai.com/v1/images/generations';
$params['prompt'] = $prompt;
//$params['model'] = 'image-alpha-001';
$params['n'] = 1;
$params['size'] = '512x512';
break;
case 'audio':
$url = 'https://api.openai.com/v1/audio/transcriptions';
// $params['prompt'] = $prompt;
// $params['max_tokens'] = 2048;
$params['model'] = 'whisper-1';
$params['file'] = curl_file_create(__DIR__ . '/audio/11.m4a');//fopen('./audio/11.wav','r+');//curl_file_create(__DIR__ . '/files/en-marvel-endgame.m4a');
//$params['response_format'] = 'text';
break;
default:
$url = 'https://api.openai.com/v1/completions';
$params['prompt'] = $prompt;
$params['max_tokens'] = 2048;
$params['model'] = 'text-davinci-003';
}
//var_dump($params['file']);exit;
$response = sendPostRequest($params,$url);
$response_data = json_decode($response,true);
var_dump($response_data);exit;
}
//echo $response;
function sendPostRequest($params,$url) {
$curl = curl_init();
//var_dump($params);exit;
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($params),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",//multipart/form-data
"Authorization: Bearer " . OPENAI_API_KEY
),
));
//var_dump($curl);
$response = curl_exec($curl);
//var_dump($response);exit;
curl_close($curl);
return $response;
}
图像和完成正确返回。耳语界面代码的问题出在哪里?
如果我在header中使用multipart/form-data,它会返回:
无法解析多部分表单
如果我删除头中的Content-Type,它将返回:
必须提供模型参数
1条答案
按热度按时间mlmc2os51#
我不使用URL,尝试
orhanerday/open-ai
在Github orhanerday/open-ai这些代码对我来说很有用: