Skip to content

Commit 1577f07

Browse files
committed
complete api:
1 parent 47e8150 commit 1577f07

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed

openai-embedding.el

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
;;; openai-embedding.el --- Create embeddings 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+
;; Get a vector representation of a given input that can be easily consumed by
23+
;; machine learning models and algorithms.
24+
;;
25+
;; See https://platform.openai.com/docs/api-reference/embeddings
26+
;;
27+
28+
;;; Code:
29+
30+
(require 'openai)
31+
32+
(defcustom openai-embedding-model "text-embedding-ada-002"
33+
"ID of the model to use."
34+
:type 'string
35+
:group 'openai)
36+
37+
;;
38+
;;; API
39+
40+
(defun openai-embedding-create (input callback)
41+
"Creates an embedding vector representing the input text.
42+
43+
INPUT text to get embeddings for, encoded as a string or array of tokens.
44+
To get embeddings for multiple inputs in a single request, pass an array of
45+
strings or array of token arrays. Each input must not exceed 8192 tokens in
46+
length.
47+
48+
The argument CALLBACK is execuated after request is made."
49+
(openai-request "https://api.openai.com/v1/embeddings"
50+
:type "POST"
51+
:headers `(("Content-Type" . "application/json")
52+
("Authorization" . ,(concat "Bearer " openai-key)))
53+
:data (json-encode
54+
`(("model" . ,openai-embedding-model)
55+
("input" . ,input)
56+
("user" . ,openai-user)))
57+
:parser 'json-read
58+
:success (cl-function
59+
(lambda (&key data &allow-other-keys)
60+
(funcall callback data)))))
61+
62+
(provide 'openai-embedding)
63+
;;; openai-embedding.el ends here

openai-engine.el

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

openai-file.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ The argument CALLBACK is execuated after request is made."
183183
(defun openai-list-files ()
184184
"List files that belong to the user's organization."
185185
(interactive)
186+
(setq openai-file-entries nil) ; reset
186187
(openai-file-list (lambda (data)
187188
(let ((id 0))
188189
(let-alist data

openai-moderation.el

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
;;; openai-moderation.el --- Classifies if text violates OpenAI's Content Policy -*- 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+
;; Given a input text, outputs if the model classifies it as violating OpenAI's
23+
;; content policy.
24+
;;
25+
;; See https://platform.openai.com/docs/api-reference/embeddings
26+
;;
27+
28+
;;; Code:
29+
30+
(require 'openai)
31+
32+
(defcustom openai-moderation-model "text-moderation-latest"
33+
"Two content moderations models are available: `text-moderation-stable' and
34+
`text-moderation-latest'.
35+
36+
The default is text-moderation-latest which will be automatically upgraded over
37+
time. This ensures you are always using our most accurate model. If you use
38+
`text-moderation-stable', we will provide advanced notice before updating the
39+
model. Accuracy of `text-moderation-stable' may be slightly lower than for
40+
`text-moderation-latest'."
41+
:type 'string
42+
:group 'openai)
43+
44+
;;
45+
;;; API
46+
47+
(defun openai-moderation-create (input callback)
48+
"Classifies if text violates OpenAI's Content Policy.
49+
50+
Argument INPUT is the text to classify.
51+
52+
The argument CALLBACK is execuated after request is made."
53+
(openai-request "https://api.openai.com/v1/embeddings"
54+
:type "POST"
55+
:headers `(("Content-Type" . "application/json")
56+
("Authorization" . ,(concat "Bearer " openai-key)))
57+
:data (json-encode
58+
`(("model" . ,openai-moderation-model)
59+
("input" . ,input)))
60+
:parser 'json-read
61+
:success (cl-function
62+
(lambda (&key data &allow-other-keys)
63+
(funcall callback data)))))
64+
65+
(provide 'openai-moderation)
66+
;;; openai-moderation.el ends here

0 commit comments

Comments
 (0)