如何在PHP中使用OpenAI Whisper

5vf7fwbs  于 2023-04-28  发布在  PHP
关注(0)|答案(1)|浏览(298)

我正在使用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,它将返回:

必须提供模型参数

mlmc2os5

mlmc2os51#

我不使用URL,尝试orhanerday/open-ai在Github orhanerday/open-ai
这些代码对我来说很有用:

// 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);

相关问题