-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrobby-models.el
45 lines (36 loc) · 1.53 KB
/
robby-models.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
;;; robby-models.el --- Update available models from OpenAI -*- lexical-binding:t -*-
;;; Commentary:
;; Defines the `robby--get-models` function to fetch the list of available models from OpenAI.
;;; Code:
(require 'robby-api-key)
(require 'robby-request)
(require 'robby-customization)
(defvar robby-models nil)
(defun robby--get-models ()
"Get the list of available models from OpenAI.
Make request to OpenAI API to get the list of available models."
(if robby-models
robby-models
(let* ((inhibit-message t)
(message-log-max nil)
(url "https://api.openai.com/v1/models")
(url-request-method "GET")
(url-request-extra-headers
`(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " (robby--get-api-key)))))
(inhibit-message t)
(message-log-max nil))
(with-current-buffer (url-retrieve-synchronously url)
(goto-char (point-min))
(re-search-forward "^{")
(backward-char 1)
(let* ((json-object-type 'alist)
(resp (json-read))
(err (robby--request-parse-error-data resp)))
(if err
(error "Error fetching models: %S" err)
(let* ((all-models (seq-map (lambda (obj) (cdr (assoc 'id obj))) (cdr (assoc 'data resp))))
(gpt-models (seq-filter (lambda (name) (string-prefix-p "gpt" name)) all-models)))
(setq robby-models gpt-models))))))))
(provide 'robby-models)
;;; robby-models.el ends here