|
| 1 | +;;; openai-engine.el --- Control moderations 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 | +;; [DEPRECATED] |
| 23 | +;; |
| 24 | +;; Given a input text, outputs if the model classifies it as violating OpenAI's |
| 25 | +;; content policy. |
| 26 | +;; |
| 27 | +;; See https://platform.openai.com/docs/api-reference/engines |
| 28 | +;; |
| 29 | + |
| 30 | +;;; Code: |
| 31 | + |
| 32 | +(require 'openai) |
| 33 | + |
| 34 | +;; |
| 35 | +;;; API |
| 36 | + |
| 37 | +(defun openai-engine-list (callback) |
| 38 | + "Lists the currently available (non-finetuned) models, and provides basic |
| 39 | +information about each one such as the owner and availability. |
| 40 | +
|
| 41 | +The argument CALLBACK is execuated after request is made." |
| 42 | + (openai-request "https://api.openai.com/v1/engines" |
| 43 | + :type "GET" |
| 44 | + :headers `(("Content-Type" . "application/json") |
| 45 | + ("Authorization" . ,(concat "Bearer " openai-key))) |
| 46 | + :parser 'json-read |
| 47 | + :success (cl-function |
| 48 | + (lambda (&key data &allow-other-keys) |
| 49 | + (funcall callback data))))) |
| 50 | + |
| 51 | +(defun openai-engine-retrieve (engine-id callback) |
| 52 | + "Retrieves a model instance, providing basic information about it such as the |
| 53 | +owner and availability. |
| 54 | +
|
| 55 | +The argument ENGINE-ID is the engine to use for this request. |
| 56 | +
|
| 57 | +The argument CALLBACK is execuated after request is made." |
| 58 | + (openai-request "https://api.openai.com/v1/engines/" |
| 59 | + :type "GET" |
| 60 | + :headers `(("Content-Type" . "application/json") |
| 61 | + ("Authorization" . ,(concat "Bearer " openai-key))) |
| 62 | + :data (json-encode |
| 63 | + `(("engine_id" . ,engine-id))) |
| 64 | + :parser 'json-read |
| 65 | + :success (cl-function |
| 66 | + (lambda (&key data &allow-other-keys) |
| 67 | + (funcall callback data))))) |
| 68 | + |
| 69 | +;; |
| 70 | +;;; Application |
| 71 | + |
| 72 | +(defvar openai-engine-entries nil |
| 73 | + "Async files entries.") |
| 74 | + |
| 75 | +(tblui-define |
| 76 | + openai-engine |
| 77 | + (lambda () openai-engine-entries) |
| 78 | + [("ID" 30 nil) |
| 79 | + ("Object" 8 nil) |
| 80 | + ("Owner" 20 nil) |
| 81 | + ("ready" 8 nil)] |
| 82 | + nil) |
| 83 | + |
| 84 | +;;;###autoload |
| 85 | +(defun openai-list-engines () |
| 86 | + "List currently available (non-finetuned) models." |
| 87 | + (interactive) |
| 88 | + (setq openai-engine-entries nil) ; reset |
| 89 | + (openai-engine-list (lambda (data) |
| 90 | + (let ((id 0)) |
| 91 | + (let-alist data |
| 92 | + (mapc (lambda (engine) |
| 93 | + (let-alist engine |
| 94 | + (push (list (number-to-string id) |
| 95 | + (vector .id |
| 96 | + .object |
| 97 | + .owner |
| 98 | + (if .ready "true" "false"))) |
| 99 | + openai-engine-entries)) |
| 100 | + (cl-incf id)) |
| 101 | + .data))) |
| 102 | + (openai-engine-goto-ui)))) |
| 103 | + |
| 104 | +(provide 'openai-engine) |
| 105 | +;;; openai-engine.el ends here |
0 commit comments