Hello T_ray,
Sorry I forgot about the “new CURLFile” part, glad you solved it by yourself.
Anyway, since I did not find this anywhere, I will leave my full code here for: file uploading, retreving fine tune id, fine-tuning and making a completion with your trained model. Hope it helps the few id!ots like me who still need to use PHP for this:
Step 1: Uploading a fine-tune file (given that you already have a formated jsonl file):
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
$headers[] = "Authorization: Bearer **YOUR_KEY**";
$c_file = new CurlFile("file.jsonl", 'application/json', "file.jsonl"); // file must be in the same directory
$post_fields = array('purpose' => 'fine-tune', 'file' => $c_file);
$ch = curl_init();
$curl_info = [ // some of this options are probably not needed
CURLOPT_URL => "https://api.openai.com/v1/files",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $post_fields,
CURLOPT_HTTPHEADER => $headers,
];
curl_setopt_array($ch, $curl_info);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result; // you will find the file id here, which should look like this: file-g4mXvDisehjEKLSNKiUSDnBQAy
Step 2: Fine-tune a model with that file
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/fine-tunes");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{
"model": "davinci",
"training_file": "file-g4mXvDisehjEKLSNKiUSDnBQAy"
}');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer **YOUR_KEY**";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result; // you will find the fine-tune id here, which should look like this: ft-OQeSiun18gzOSKE5sRVtYA7T
Next steps in the following reply…
Step 3: Retrieve the fine-tuned model id (you need to wait until it is uploaded, it could take up to 30 minutes, or maybe more?)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/fine-tunes/ft-OQeSiun18gzOSKE5sRVtYA7T");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer **YOUR_KEY**";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result; // you will find the model id here, which should look like this: davinci:ft-personal-2023-02-07-05-20-40
As I said, you need to wait until the fine-tune is uploaded, and you can find out by checking the [events] array in this request. There should be an event which gives you the uploaded model name. If you can’t find it that means you still need to wait. You can check the available events to check the progress.
Step 4: Make a completion with your fine-tuned model
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/completions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{
"model": "davinci:ft-personal-2023-02-07-05-20-40",
"prompt": "**YOUR_PROMPT**",
"max_tokens": 100,
"temperature": 0,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer **YOUR_KEY**";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result;
Hope it helps.