File: /var/www/html/laravel/public/gpt-tuning.php
<?php
function downloadFileByUrl($url, $outFileName)
{
shell_exec('cd /var/www/ai-notam/laravel/storage/app/public/uploads/ && wget ' . $url);
}
function deployFile($file, $apiKey) {
$url = 'https://api.openai.com/v1/files';
$headers = [
'Content-Type: multipart/form-data',
'Authorization: Bearer ' . $apiKey,
];
$fileName = basename($file);
downloadFileByUrl($file, $fileName);
$data = [
'purpose' => 'fine-tune',
'file' => curl_file_create('/var/www/ai-notam/laravel/storage/app/public/uploads/'.$fileName, 'application/jsonl', $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);
return json_decode($result);
}
try {
$params = json_decode(file_get_contents('php://input'), true);
header('Content-Type: application/json');
$input = '';
$status = 'success';
$data = [];
$apiKey = $params['key'];
$path = $params['path'];
$headers = [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
];
if (!empty($params['file'])) {
$uploadedFile = deployFile($params['file'], $apiKey);
if (property_exists($uploadedFile, 'id')) {
$data = [
'training_file' => $uploadedFile->id,
'model' => $params['model']
];
}
}
$url = 'https://api.openai.com/v1/' . $path;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($params['method'] !== 'GET') {
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);
echo $result;
} catch (\Exception $e) {
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}