|
| 1 | +;;; openai-fine-tune.el --- Control fine-tunes with OpenAI API -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2023 Shen, Jen-Chieh |
| 4 | + |
| 5 | +;; This file is not part of GNU Emacs. |
| 6 | + |
| 7 | +;; This program is free software: you can redistribute it and/or modify |
| 8 | +;; it under the terms of the GNU General Public License as published by |
| 9 | +;; the Free Software Foundation, either version 3 of the License, or |
| 10 | +;; (at your option) any later version. |
| 11 | + |
| 12 | +;; This program is distributed in the hope that it will be useful, |
| 13 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +;; GNU General Public License for more details. |
| 16 | + |
| 17 | +;; You should have received a copy of the GNU General Public License |
| 18 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +;;; Commentary: |
| 21 | +;; |
| 22 | +;; Manage fine-tuning jobs to tailor a model to your specific training data. |
| 23 | +;; |
| 24 | +;; See https://platform.openai.com/docs/api-reference/fine-tunes |
| 25 | +;; |
| 26 | + |
| 27 | +;;; Code: |
| 28 | + |
| 29 | +(require 'openai) |
| 30 | + |
| 31 | +(defcustom openai-fine-tune-model "curie" |
| 32 | + "The name of the base model to fine-tune. |
| 33 | +
|
| 34 | +You can select one of \"ada\", \"babbage\", \"curie\", \"davinci\", or a |
| 35 | +fine-tuned model created after 2022-04-21. To learn more about these models, |
| 36 | +see the Models documentation." |
| 37 | + :type 'string |
| 38 | + :group 'openai) |
| 39 | + |
| 40 | +(defcustom openai-fine-tune-validation-file "" |
| 41 | + "The ID of an uploaded file that contains validation data." |
| 42 | + :type 'string |
| 43 | + :group 'openai) |
| 44 | + |
| 45 | +(defcustom openai-fine-tune-n-epochs 4 |
| 46 | + "The number of epochs to train the model for. |
| 47 | +
|
| 48 | +An epoch refers to one full cycle through the training dataset." |
| 49 | + :type 'integer |
| 50 | + :group 'openai) |
| 51 | + |
| 52 | +(defcustom openai-fine-tune-batch-size nil |
| 53 | + "The batch size to use for training. |
| 54 | +
|
| 55 | +The batch size is the number of training examples used to train a single forward |
| 56 | +and backward pass. |
| 57 | +
|
| 58 | +By default, the batch size will be dynamically configured to be ~0.2% of the |
| 59 | +number of examples in the training set, capped at 256 - in general, we've found |
| 60 | +that larger batch sizes tend to work better for larger datasets." |
| 61 | + :type 'integer |
| 62 | + :group 'openai) |
| 63 | + |
| 64 | +(defcustom openai-fine-tune-learning-rate-multiplier nil |
| 65 | + "The learning rate multiplier to use for training. |
| 66 | +
|
| 67 | +The fine-tuning learning rate is the original learning rate used for pretraining |
| 68 | +multiplied by this value. |
| 69 | +
|
| 70 | +By default, the learning rate multiplier is the 0.05, 0.1, or 0.2 depending on |
| 71 | +final `batch_size' (larger learning rates tend to perform better with larger |
| 72 | +batch sizes). We recommend experimenting with values in the range 0.02 to 0.2 to |
| 73 | +see what produces the best results." |
| 74 | + :type 'integer |
| 75 | + :group 'openai) |
| 76 | + |
| 77 | +(defcustom openai-fine-tune-prompt-loss-weight 0.01 |
| 78 | + "The weight to use for loss on the prompt tokens. This controls how much the |
| 79 | +model tries to learn to generate the prompt (as compared to the completion which |
| 80 | +always has a weight of 1.0), and can add a stabilizing effect to training when |
| 81 | +completions are short. |
| 82 | +
|
| 83 | +If prompts are extremely long (relative to completions), it may make sense to |
| 84 | +reduce this weight so as to avoid over-prioritizing learning the prompt." |
| 85 | + :type 'number |
| 86 | + :group 'openai) |
| 87 | + |
| 88 | +(defcustom openai-fine-tune-compute-classification-metrics 0.01 |
| 89 | + "If set, we calculate classification-specific metrics such as accuracy and F-1 |
| 90 | +score using the validation set at the end of every epoch. |
| 91 | +
|
| 92 | +In order to compute classification metrics, you must provide a `validation_file'. |
| 93 | +Additionally, you must specify `classification_n_classes' for multiclass |
| 94 | +classification or `classification_positive_class' for binary classification." |
| 95 | + :type 'boolean |
| 96 | + :group 'openai) |
| 97 | + |
| 98 | +(defcustom openai-fine-tune-classification-n-classes nil |
| 99 | + "The number of classes in a classification task. |
| 100 | +
|
| 101 | +This parameter is required for multiclass classification." |
| 102 | + :type 'integer |
| 103 | + :group 'openai) |
| 104 | + |
| 105 | +(defcustom openai-fine-tune-classification-positive-class nil |
| 106 | + "The positive class in binary classification. |
| 107 | +
|
| 108 | +This parameter is needed to generate precision, recall, and F1 metrics when |
| 109 | +doing binary classification." |
| 110 | + :type 'string |
| 111 | + :group 'openai) |
| 112 | + |
| 113 | +(defcustom openai-fine-tune-classification-betas nil |
| 114 | + "If this is provided, we calculate F-beta scores at the specified beta values. |
| 115 | +
|
| 116 | +The F-beta score is a generalization of F-1 score. This is only used for binary |
| 117 | +classification. |
| 118 | +
|
| 119 | +With a beta of 1 (i.e. the F-1 score), precision and recall are given the same |
| 120 | +weight. A larger beta score puts more weight on recall and less on precision. |
| 121 | +A smaller beta score puts more weight on precision and less on recall." |
| 122 | + :type 'list |
| 123 | + :group 'openai) |
| 124 | + |
| 125 | +(defcustom openai-fine-tune-suffix nil |
| 126 | + "A string of up to 40 characters that will be added to your fine-tuned model |
| 127 | +name. |
| 128 | +
|
| 129 | +For example, a suffix of \"custom-model-name\" would produce a model name like |
| 130 | +`ada:ft-your-org:custom-model-name-2022-02-15-04-21-04'." |
| 131 | + :type 'string |
| 132 | + :group 'openai) |
| 133 | + |
| 134 | +;; |
| 135 | +;;; API |
| 136 | + |
| 137 | +(defun openai-fine-tune-create (training-file callback) |
| 138 | + "Creates a job that fine-tunes a specified model from a given dataset. |
| 139 | +
|
| 140 | +Response includes details of the enqueued job including job status and the name |
| 141 | +of the fine-tuned models once complete. |
| 142 | +
|
| 143 | +The argument TRAINING-FILE is the ID of an uploaded file that contains training |
| 144 | +data. |
| 145 | +
|
| 146 | +The argument CALLBACK is execuated after request is made." |
| 147 | + (openai-request "https://api.openai.com/v1/fine-tunes" |
| 148 | + :type "POST" |
| 149 | + :headers `(("Content-Type" . "application/json") |
| 150 | + ("Authorization" . ,(concat "Bearer " openai-key))) |
| 151 | + :data (json-encode |
| 152 | + `(("model" . ,openai-fine-tune-model) |
| 153 | + ("training_file" . ,training-file) |
| 154 | + ("validation_file" . ,openai-fine-tune-validation-file) |
| 155 | + ("n_epochs" . ,openai-fine-tune-n-epochs) |
| 156 | + ("batch_size" . ,openai-fine-tune-batch-size) |
| 157 | + ("learning_rate_multiplier" . ,openai-fine-tune-learning-rate-multiplier) |
| 158 | + ("prompt_loss_weight" . ,openai-fine-tune-prompt-loss-weight) |
| 159 | + ("compute_classification_metrics" . ,openai-fine-tune-compute-classification-metrics) |
| 160 | + ("classification_n_classes" . ,openai-fine-tune-classification-n-classes) |
| 161 | + ("classification_positive_class" . ,openai-fine-tune-classification-positive-class) |
| 162 | + ("classification_betas" . ,openai-fine-tune-classification-betas) |
| 163 | + ("suffix" . ,openai-fine-tune-suffix))) |
| 164 | + :parser 'json-read |
| 165 | + :success (cl-function |
| 166 | + (lambda (&key data &allow-other-keys) |
| 167 | + (funcall callback data))))) |
| 168 | + |
| 169 | +(defun openai-fine-tune-list (callback) |
| 170 | + "List your organization's fine-tuning jobs. |
| 171 | +
|
| 172 | +The argument CALLBACK is execuated after request is made." |
| 173 | + (openai-request "https://api.openai.com/v1/fine-tunes" |
| 174 | + :type "GET" |
| 175 | + :headers `(("Content-Type" . "application/json") |
| 176 | + ("Authorization" . ,(concat "Bearer " openai-key))) |
| 177 | + :parser 'json-read |
| 178 | + :success (cl-function |
| 179 | + (lambda (&key data &allow-other-keys) |
| 180 | + (funcall callback data))))) |
| 181 | + |
| 182 | +(defun openai-fine-tune-retrieve (fine-tune-id callback) |
| 183 | + "Gets info about the fine-tune job. |
| 184 | +
|
| 185 | +The FINE-TUNE-ID of the fine-tune job. |
| 186 | +
|
| 187 | +The argument CALLBACK is execuated after request is made." |
| 188 | + (openai-request (format "https://api.openai.com/v1/fine-tunes/%s" fine-tune-id) |
| 189 | + :type "GET" |
| 190 | + :headers `(("Content-Type" . "application/json") |
| 191 | + ("Authorization" . ,(concat "Bearer " openai-key))) |
| 192 | + :parser 'json-read |
| 193 | + :success (cl-function |
| 194 | + (lambda (&key data &allow-other-keys) |
| 195 | + (funcall callback data))))) |
| 196 | + |
| 197 | +(defun openai-fine-tune-cancel (fine-tune-id callback) |
| 198 | + "Immediately cancel a fine-tune job. |
| 199 | +
|
| 200 | +The FINE-TUNE-ID of the fine-tune job to cancel. |
| 201 | +
|
| 202 | +The argument CALLBACK is execuated after request is made." |
| 203 | + (openai-request (format "https://api.openai.com/v1/fine-tunes/%s/cancel" fine-tune-id) |
| 204 | + :type "POST" |
| 205 | + :headers `(("Content-Type" . "application/json") |
| 206 | + ("Authorization" . ,(concat "Bearer " openai-key))) |
| 207 | + :parser 'json-read |
| 208 | + :success (cl-function |
| 209 | + (lambda (&key data &allow-other-keys) |
| 210 | + (funcall callback data))))) |
| 211 | + |
| 212 | +(defun openai-fine-tune-list-events (fine-tune-id callback) |
| 213 | + "Get fine-grained status updates for a fine-tune job. |
| 214 | +
|
| 215 | +The FINE-TUNE-ID of the fine-tune job to get events for. |
| 216 | +
|
| 217 | +The argument CALLBACK is execuated after request is made." |
| 218 | + (openai-request (format "https://api.openai.com/v1/fine-tunes/%s/events" fine-tune-id) |
| 219 | + :type "GET" |
| 220 | + :headers `(("Content-Type" . "application/json") |
| 221 | + ("Authorization" . ,(concat "Bearer " openai-key))) |
| 222 | + :parser 'json-read |
| 223 | + :success (cl-function |
| 224 | + (lambda (&key data &allow-other-keys) |
| 225 | + (funcall callback data))))) |
| 226 | + |
| 227 | +(defun openai-fine-tune-list-events (model callback) |
| 228 | + "Delete a fine-tuned model. You must have the Owner role in your organization. |
| 229 | +
|
| 230 | +The MODEL to delete. |
| 231 | +
|
| 232 | +The argument CALLBACK is execuated after request is made." |
| 233 | + (openai-request (format "https://api.openai.com/v1/models/%s" model) |
| 234 | + :type "DELETE" |
| 235 | + :headers `(("Content-Type" . "application/json") |
| 236 | + ("Authorization" . ,(concat "Bearer " openai-key))) |
| 237 | + :parser 'json-read |
| 238 | + :success (cl-function |
| 239 | + (lambda (&key data &allow-other-keys) |
| 240 | + (funcall callback data))))) |
| 241 | + |
| 242 | +;; |
| 243 | +;;; Application |
| 244 | + |
| 245 | +(defvar openai-fine-tune-entries nil |
| 246 | + "Async files entries.") |
| 247 | + |
| 248 | +(tblui-define |
| 249 | + openai-fine-tune |
| 250 | + (lambda () openai-fine-tune-entries) |
| 251 | + [("ID" 15 nil) |
| 252 | + ("Object" 30 nil) |
| 253 | + ("Model" 6 nil) |
| 254 | + ("Created at" 10 nil) |
| 255 | + ("fine_tuned_model" 10 nil) |
| 256 | + ("hyperparams" 40 nil) |
| 257 | + ("organization_id" 40 nil) |
| 258 | + ("status" 10 nil) |
| 259 | + ("validation_files" 10 nil) |
| 260 | + ("training_files" 10 nil) |
| 261 | + ("Updated at" 10 nil)] |
| 262 | + nil) |
| 263 | + |
| 264 | +;;;###autoload |
| 265 | +(defun openai-list-fine-tunes () |
| 266 | + "List fine-tuning jobs." |
| 267 | + (interactive) |
| 268 | + (setq openai-fine-tune-entries nil) ; reset |
| 269 | + (openai-file-list (lambda (data) |
| 270 | + (let ((id 0)) |
| 271 | + (let-alist data |
| 272 | + (mapc (lambda (fine-tune) |
| 273 | + (let-alist fine-tune |
| 274 | + (push (list (number-to-string id) |
| 275 | + (vector .id |
| 276 | + .object |
| 277 | + .model |
| 278 | + .created_at |
| 279 | + .fine_tuned_model |
| 280 | + .hyperparams |
| 281 | + .organization_id |
| 282 | + .result_files |
| 283 | + .status |
| 284 | + .validation_files |
| 285 | + .training_files |
| 286 | + .updated_at)) |
| 287 | + openai-fine-tune-entries)) |
| 288 | + (cl-incf id)) |
| 289 | + .data))) |
| 290 | + (openai-fine-tune-goto-ui)))) |
| 291 | + |
| 292 | +(provide 'openai-fine-tune) |
| 293 | +;;; openai-fine-tune.el ends here |
0 commit comments