HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/laravel/public/gpt-dev.php
<?php

function downloadUrlToFile($url, $outFileName)
{   
    
    if(is_file($url)) {
        copy($url, $outFileName); 
    } else {
        $options = array(
          CURLOPT_FILE    => fopen('/tmp/'.$outFileName, 'w'),
          CURLOPT_TIMEOUT =>  28800, // set this to 8 hours so we dont timeout on big files
          CURLOPT_URL     => $url
        );

        $ch = curl_init();
        curl_setopt_array($ch, $options);
        curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $httpcode;
    }
}

$params = json_decode(file_get_contents('php://input'), true);
header('Content-Type: application/json');
$input = '';

$apiKey = $params['key'];
$status = 'success';
$message = '';


$data = [];

$messages = [];

if (!empty(trim($params['prompt']))) {
    $messages = [['role' => 'system', 'content' => $params['prompt']]];
}

if (!empty($params['messages'])) { 
    foreach ($params['messages'] as $message) {
        $messages[] = ['role' => 'user', 'content' => $message['user']];
        $messages[] = ['role' => 'assistant', 'content' => $message['assistant']];
    }
}

if (!empty($params['request'])) {
    $input = $params['request'];
    $messages[] = ['role' => 'user', 'content' => $input];
} elseif (!empty($params['voice'])) { 
    $url = 'https://api.openai.com/v1/audio/transcriptions';
    $headers = [
        'Content-Type: multipart/form-data',
        'Authorization: Bearer ' . $apiKey,
    ];

    $file = $params['voice'];
    $fileName = basename($file);
    downloadUrlToFile($file, $fileName);
    
    $data = array_merge($data, [
        'model' => 'whisper-1',
        'file' => curl_file_create('/tmp/'.$fileName, $fileName),
    ]);
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        $result = curl_error($ch);
    } 
    curl_close($ch);
    
    if (is_string($result)) {
        $result = json_decode($result, true);
    }
   
    if (!empty($result['text'])) {
        $input = $result['text'];
        $messages[] = ['role' => 'user', 'content' => $input];
    }
    $data = [];
}

if (!empty($fileName) && is_file('/tmp/'.$fileName)) {
    unlink('/tmp/'.$fileName);
}

$headers = [
    'Content-Type: application/json',
    'Authorization: Bearer ' . $apiKey,
];
$temperature = 0.6;
if (!empty($params['temperature'])) { 
    $temperature = $params['temperature'];
}
$max_tokens = 500;
if (!empty($params['max_tokens'])) { 
    $max_tokens = $params['max_tokens'];
}
    
$url = 'https://api.openai.com/v1/chat/completions';  
$data = array_merge($data, [
    'model' => $params['model'],
    'messages' => $messages,
    'temperature' => $temperature,

]);

if (substr_count($params['model'], 'o1') ||  substr_count($params['model'], 'o3')) {
    $data = array_merge($data, [
        'max_completion_tokens' => $max_tokens,
    ]);
} else {
    $data = array_merge($data, [
        'max_tokens' => $max_tokens,
    ]);
}

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    $result = curl_error($ch);
} 
curl_close($ch);

$result = json_decode($result);

$finalResult = ['input' => $input, 'result' => $result];
$finalResult = json_encode($finalResult);
echo $finalResult;