File size: 9,756 Bytes
5f32fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php

namespace App\Http\Controllers;

use App\Helpers\Classes\Helper;
use App\Models\Setting;
use App\Models\SettingTwo;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Orhanerday\OpenAi\OpenAi;

class AIFineTuneController extends Controller
{
    public function addFineTune(Request $request)
    {

        if (Helper::appIsDemo()) {
            return response()->json(__('This feature is disabled in Demo version.'), 419);
        }

        $title = ! empty($_POST['title']) ? $_POST['title'] : 'model-' . Str::random(7);
        $purpose = ! empty($_POST['purpose']) ? $_POST['purpose'] : 'fine-tune';
        $model = ! empty($_POST['model']) ? $_POST['model'] : 'gpt-3.5-turbo-1106';
        $file = ! empty($_FILES['file']) ? $_FILES['file'] : [];

        if (empty($title)) {
            return response()->json(__('Title filed is empty!'), 419);
        }

        if (empty($file['name'])) {
            return response()->json(__('Select a JSONL File!'), 419);
        } else {
            $file_name = basename($file['name']);
            $tmp_file = $file['tmp_name'];
            $file_type = $file['type'];
            $c_file = curl_file_create($tmp_file, $file_type, $file_name);
        }

        // Fetch the Site Settings object with openai_api_secret
        $settings = Setting::first();
        if ($settings?->user_api_option) {
            $apiKeys = explode(',', auth()->user()?->api_keys);
        } else {
            $apiKeys = explode(',', $settings?->openai_api_secret);
        }
        $fine_tune_list = json_decode(SettingTwo::first()->fine_tune_list, true);
        $apiKey = $apiKeys[0];
        $open_ai = new OpenAi($apiKey);
        $html = '';

        // upload file
        $uploadFile = $open_ai->uploadFile([
            'purpose' => $purpose,
            'file'    => $c_file,
        ]);
        $uploadFile_array = json_decode($uploadFile, true);
        $uploadFile = json_decode($uploadFile);

        if (isset($uploadFile->error)) {
            return response()->json($uploadFile->error->message, 419);
        }

        // create fine-tune
        $createFineTune = $open_ai->createFineTune([
            'model'         => $model,
            'training_file' => $uploadFile->id,
        ]);
        $createFineTune = json_decode($createFineTune);

        if (isset($createFineTune->error)) {
            return response()->json($createFineTune->error, 419);
        }

        // update data
        $fine_tune_list[$uploadFile->id] = [
            'title' => $title,
            'file'  => $uploadFile_array,
        ];

        $save_settings = SettingTwo::first();
        $save_settings->fine_tune_list = json_encode($fine_tune_list);
        $save_settings->save();

        return response()->json([
            'output' => sprintf(
                '<tr>
                    <td>%1$s</td>
                    <td>%2$s</td>
                    <td>%3$s</td>
                    <td>%4$s</td>
                    <td>%5$s</td>
                    <td>%6$s</td>
                    <td><button type="button"
                        class="delete-fine-tune btn relative z-10 p-0 border w-[36px] shrink-0 h-[36px] hover:bg-red-600 hover:text-white"
                        title="%7$s">
                        <svg width="10" height="10" viewBox="0 0 10 10"
                            fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                            <path
                                d="M9.08789 1.74609L5.80664 5L9.08789 8.25391L8.26758 9.07422L4.98633 5.82031L1.73242 9.07422L0.912109 8.25391L4.16602 5L0.912109 1.74609L1.73242 0.925781L4.98633 4.17969L8.26758 0.925781L9.08789 1.74609Z" />
                        </svg>
                    </a></td>
                </tr>',
                $title,
                $uploadFile->id,
                $uploadFile->bytes,
                $model,
                $fine_tune->fine_tuned_model ?? '-',
                $createFineTune->status,
                __('Delete')
            ),
        ], 200);

    }

    public function deleteFineTune(Request $request)
    {

        if (Helper::appIsDemo()) {
            return response()->json(__('This feature is disabled in Demo version.'), 419);
        }

        $file_id = ! empty($_POST['file_id']) ? $_POST['file_id'] : '';
        $model = ! empty($_POST['model']) ? $_POST['model'] : '';

        if (empty($file_id) && empty($model)) {
            return response()->json(__('Fine-tune not found!'), 419);
        }

        // Fetch the Site Settings object with openai_api_secret
        $settings = Setting::first();
        if ($settings?->user_api_option) {
            $apiKeys = explode(',', auth()->user()?->api_keys);
        } else {
            $apiKeys = explode(',', $settings?->openai_api_secret);
        }
        $fine_tune_list = json_decode(SettingTwo::first()->fine_tune_list, true);
        $apiKey = $apiKeys[0];
        $open_ai = new OpenAi($apiKey);

        if ($model) {
            $deleteFineTune = $open_ai->deleteFineTune($model);
            $deleteFineTune = json_decode($deleteFineTune);

            if (isset($deleteFineTune->error)) {
                return response()->json($deleteFineTune->error->message, 419);
            }
        }

        $deleteFile = $open_ai->deleteFile($file_id);
        $deleteFile = json_decode($deleteFile);

        if (isset($deleteFile->error)) {
            return response()->json($deleteFile->error->message, 419);
        }

        if (isset($fine_tune_list[$file_id])) {
            unset($fine_tune_list[$file_id]);
            $save_settings = SettingTwo::first();
            $save_settings->fine_tune_list = json_encode($fine_tune_list);
            $save_settings->save();
        }

        return response()->json(__('Fine-tune deleted!'), 200);

    }

    public static function getFineTuneTableRow()
    {

        // Fetch the Site Settings object with openai_api_secret
        $settings = Setting::first();
        if ($settings?->user_api_option) {
            $apiKeys = explode(',', auth()->user()?->api_keys);
        } else {
            $apiKeys = explode(',', $settings?->openai_api_secret);
        }
        $fine_tune_list = json_decode(SettingTwo::first()->fine_tune_list, true);
        $apiKey = $apiKeys[0];
        $open_ai = new OpenAi($apiKey);
        $html = '';

        $listFiles = json_decode($open_ai->listFiles());
        $listFineTunes = json_decode($open_ai->listFineTunes());

        if (empty($fine_tune_list)) {
            return printf('<tr class="info"><td colspan="5">%s</td></tr>', __('There is no fine-tune data!'));
        }

        foreach (array_reverse($fine_tune_list) as $file_id => $values) {
            foreach ($listFineTunes?->data ?? [] as $fine_tune) {
                if ($fine_tune->training_file == $file_id) {
                    $html .= sprintf(
                        '<tr>
                            <td>%1$s</td>
                            <td>%2$s</td>
                            <td>%3$s</td>
                            <td>%4$s</td>
                            <td>%5$s</td>
                            <td>%6$s %7$s</td>
                            <td><button type="button" data-file="%8$s" data-model="%9$s"
                                class="delete-fine-tune btn relative z-10 p-0 border w-[36px] shrink-0 h-[36px] hover:bg-red-600 hover:text-white"
                                title="%10$s">
                                <svg width="10" height="10" viewBox="0 0 10 10"
                                    fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                                    <path
                                        d="M9.08789 1.74609L5.80664 5L9.08789 8.25391L8.26758 9.07422L4.98633 5.82031L1.73242 9.07422L0.912109 8.25391L4.16602 5L0.912109 1.74609L1.73242 0.925781L4.98633 4.17969L8.26758 0.925781L9.08789 1.74609Z" />
                                </svg>
                            </a></td>
                        </tr>',
                        $values['title'],
                        $file_id,
                        $values['file']['bytes'],
                        $fine_tune->model,
                        $fine_tune->fine_tuned_model ?? '-',
                        $fine_tune->error->message ?? '',
                        $fine_tune->status,
                        $file_id,
                        $fine_tune->fine_tuned_model,
                        __('Delete')
                    );
                }
            }
        }

        echo $html;

    }

    public static function getFineModelOption($selected)
    {

        // Fetch the Site Settings object with openai_api_secret
        $settings = Setting::first();
        if ($settings?->user_api_option) {
            $apiKeys = explode(',', auth()->user()?->api_keys);
        } else {
            $apiKeys = explode(',', $settings?->openai_api_secret);
        }
        $fine_tune_list = json_decode(SettingTwo::first()->fine_tune_list, true);
        $apiKey = $apiKeys[0];
        $open_ai = new OpenAi($apiKey);
        $html = '';

        $listFineTunes = json_decode($open_ai->listFineTunes());

        foreach ($listFineTunes?->data ?? [] as $fine_tune) {
            if ($fine_tune->status == 'succeeded' && isset($fine_tune_list[$fine_tune->training_file])) {

                $html .= sprintf(
                    '<option value="%1$s" %3$s>%2$s (%1$s)</option>',
                    $fine_tune->fine_tuned_model,
                    $fine_tune_list[$fine_tune->training_file]['title'],
                    $selected == $fine_tune->fine_tuned_model ? 'selected' : ''
                );
            }
        }

        echo $html;

    }
}