Reputation: 19
I am using php to connect to the whisper interface of openai, but according to the document, I keep reporting errors.
Here is my code:
<?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;
}
Images and Completions returns correctly.Where is the problem with the whisper interface code?
If I use multipart/form-data in the header, it returns:
Could not parse multipart form
If I remove the Content-Type in the header, it returns:
you must provide a model parameter
Upvotes: 1
Views: 3051
Reputation: 1
Just past post fields as an array for whisper model, remove json_encode
from this CURLOPT_POSTFIELDS => json_encode($params)
Upvotes: 0
Reputation: 11
I am not using URL, try orhanerday/open-ai
on Github orhanerday/open-ai
These code works for me:
// remove this line if you use a PHP Framework.
require_once __DIR__ . '/vendor/autoload.php';
// Import the OpenAI class from the SDK
use Orhanerday\OpenAi\OpenAi;
// Set the path to the audio file to transcribe
$audioFile = 'path/to/audio.mp3';
// Create a CURL file object for the audio file
$cFile = curl_file_create($audioFile, 'audio.mp3');
// Use transcription
$response_text = $open_ai->transcribe([
"model" => "whisper-1",
"file" => $cFile,
]);
var_dump($response_text);
// Use translation
$reply_vtt = $open_ai->translate([
"model" => "whisper-1",
"file" => $cFile,
"response_format" => 'vtt',
]);
var_dump($reply_vtt);
Upvotes: 1