-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathopenai-file.el
305 lines (262 loc) · 10.9 KB
/
openai-file.el
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
;;; openai-file.el --- Control files in OpenAI -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2025 Shen, Jen-Chieh
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Files are used to upload documents that can be used with features like
;; Fine-tuning.
;;
;; See https://beta.openai.com/docs/api-reference/files
;;
;;; Code:
(require 'openai)
;;
;;; API
(cl-defun openai-file-list ( callback
&key
(base-url openai-base-url)
(parameters openai-parameters)
(content-type "application/json")
(key openai-key)
org-id)
"Return a list of files that belong to the user's organization.
The argument CALLBACK is execuated after request is made.
Arguments BASE-URL, PARAMETERS, CONTENT-TYPE, KEY and ORG-ID are global
options; however, you can overwrite the value by passing it in."
(openai-request (concat base-url "/files")
:type "GET"
:params parameters
:headers (openai--headers content-type key org-id)
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
(cl-defun openai-file-upload ( file purpose callback
&key
(base-url openai-base-url)
(parameters openai-parameters)
(content-type "application/json")
(key openai-key)
org-id)
"Upload a file that contain document(s) to be used across various
endpoints/features.
The argument FILE is the JSON Lines file to be uploaded.
If the PURPOSE is set to \"fine-tune\", each line is a JSON record with
\"prompt\" and \"completion\" fields representing your training examples.
The argument PURPOSE is the intended purpose of the uploaded documents.
Use \"fine-tune\" for Fine-tuning. This allows us to validate the format of the
uploaded file.
Argument CALLBACK is function with data pass in.
Arguments BASE-URL, PARAMETERS, CONTENT-TYPE, KEY and ORG-ID are global
options; however, you can overwrite the value by passing it in."
(openai-request (concat base-url "/files")
:type "POST"
:params parameters
:headers (openai--headers content-type key org-id)
:data (openai--json-encode
`(("file" . ,file)
("purpose" . ,purpose)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
(cl-defun openai-file-delete ( file-id callback
&key
(base-url openai-base-url)
(parameters openai-parameters)
(content-type "application/json")
(key openai-key)
org-id)
"Delete a file.
The arument FILE-ID is id of the file to use for this request.
Argument CALLBACK is function with data pass in.
Arguments BASE-URL, PARAMETERS, CONTENT-TYPE, KEY and ORG-ID are global
options; however, you can overwrite the value by passing it in."
(openai-request (concat base-url "/files")
:type "DELETE"
:params parameters
:headers (openai--headers content-type key org-id)
:data (openai--json-encode
`(("file_id" . ,file-id)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
(cl-defun openai-file-retrieve ( file-id callback
&key
(base-url openai-base-url)
(parameters openai-parameters)
(content-type "application/json")
(key openai-key)
org-id)
"Return information about a specific file.
The arument FILE-ID is id of the file to use for this request.
The argument CALLBACK is execuated after request is made.
Arguments BASE-URL, PARAMETERS, CONTENT-TYPE, KEY and ORG-ID are global
options; however, you can overwrite the value by passing it in."
(openai-request (format "%s/files/%s" base-url file-id)
:type "GET"
:params parameters
:headers (openai--headers content-type key org-id)
:data (openai--json-encode
`(("file_id" . ,file-id)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
(cl-defun openai-file-retrieve-content ( file-id callback
&key
(base-url openai-base-url)
(parameters openai-parameters)
(content-type "application/json")
(key openai-key)
org-id)
"Return the contents of the specified file
The arument FILE-ID is id of the file to use for this request.
The argument CALLBACK is execuated after request is made.
Arguments BASE-URL, PARAMETERS, CONTENT-TYPE, KEY and ORG-ID are global
options; however, you can overwrite the value by passing it in."
(openai-request (format "%s/files/%s/content" base-url file-id)
:type "GET"
:params parameters
:headers (openai--headers content-type key org-id)
:data (openai--json-encode
`(("file_id" . ,file-id)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
;;
;;; Util
(defun openai--select-jsonl-files (candidate)
"Return t if CANDIDATE is either directory or a JSONL file."
(or (string-suffix-p ".jsonl" candidate t) ; only support png
(file-directory-p candidate))) ; allow navigation
(defun openai-file--select (callback)
"Select a file then execute CALLBACK."
(openai-file-list
(lambda (data)
(let ((options))
(let-alist data
(mapc (lambda (file)
(let-alist file
(push (cons .filename .id) options)))
.data))
(when (zerop (length options))
(user-error "No response, please try again"))
(let*
((offset (openai--completing-frame-offset options))
(file (if (= 1 (length options))
(car options)
(completing-read
"Select file: "
(lambda (string predicate action)
(if (eq action 'metadata)
`(metadata
(display-sort-function . ,#'identity)
(annotation-function
. ,(lambda (cand)
(concat (propertize " " 'display `((space :align-to (- right ,offset))))
(cdr (assoc cand options))))))
(complete-with-action action options string predicate)))
nil t)))
(file-id (cdr (assoc file options))))
(funcall callback options file file-id))))))
;;
;;; Application
(defvar openai-file-entries nil
"Async files entries.")
(tblui-define
openai-file
"OpenAI File" "Display file information from OpenAI."
(lambda () openai-file-entries)
[("Filename" 15 nil)
("ID" 30 nil)
("Bytes" 6 nil)
("Object" 10 nil)
("Created at" 10 nil)
("Purpose" 40 nil)]
nil)
;;;###autoload
(defun openai-list-files ()
"List files that belong to the user's organization."
(interactive)
(setq openai-file-entries nil) ; reset
(openai-file-list (lambda (data)
(let ((id 0))
(let-alist data
(mapc (lambda (file)
(let-alist file
(push (list (number-to-string id)
(vector .filename
.id
.bytes
.object
.created_at
.purpose))
openai-file-entries))
(cl-incf id))
.data)))
(openai-file-goto-ui))))
;;;###autoload
(defun openai-upload-file ()
"Prompt to upload the file to OpenAI server for file-tuning."
(interactive)
(when-let ((file (read-file-name "Select file: " nil nil t nil
#'openai--select-jsonl-files))
(purpose (read-string "Purpoe: ")))
(openai-file-upload file purpose
(lambda (data)
;; Nothing to do now, just print it out!
;;
;; XXX: Is there a way to improve the UX?
(message "%s" (pp-to-string data))))))
;;;###autoload
(defun openai-delete-file ()
"Prompt to select the file and delete it."
(interactive)
(openai-file--select
(lambda (_options file file-id)
(openai-file-delete
file-id
(lambda (data)
(let-alist data
(if .deleted
(message "Deleted file %s" file)
(message "Failed to delete file: %s %s" file file-id))))))))
;;;###autoload
(defun openai-retrieve-file ()
"Prompt to select the file and print its' information."
(interactive)
(openai-file--select
(lambda (_options _file file-id)
(openai-file-retrieve
file-id
(lambda (data)
;; Nothing to do now, just print it out!
;;
;; XXX: Is there a way to improve the UX?
(message "%s" (pp-to-string data)))))))
;;;###autoload
(defun openai-retrieve-file-content ()
"Prompt to select the file and print its' content."
(interactive)
(openai-file--select
(lambda (_options _file file-id)
(openai-file-retrieve-content
file-id
(lambda (data)
;; XXX: It seems like we should download it instead print it?
(message "%s" data))))))
(provide 'openai-file)
;;; openai-file.el ends here