Skip to content

Commit 17d5f3b

Browse files
committed
feat: discover Gemini models dynamically via API
1 parent be81dd7 commit 17d5f3b

File tree

1 file changed

+74
-24
lines changed

1 file changed

+74
-24
lines changed

chatgpt-shell-google.el

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
(require 'cl-lib))
3030
(require 'shell-maker)
3131
(require 'map)
32+
(require 'json)
3233

3334
(defvar chatgpt-shell-proxy)
3435

@@ -82,31 +83,80 @@ VALIDATE-COMMAND, and GROUNDING-SEARCH handler."
8283
(:key . chatgpt-shell-google-key)
8384
(:validate-command . chatgpt-shell-google--validate-command)))
8485

85-
(defun chatgpt-shell-google-models ()
86-
"Build a list of Google LLM models available."
87-
;; Context windows have been verified as of 11/26/2024. See
88-
;; https://ai.google.dev/gemini-api/docs/models/gemini.
89-
(list (chatgpt-shell-google-make-model :version "gemini-2.0-flash"
90-
:short-version "2.0-flash"
91-
:path "/v1beta/models/gemini-2.0-flash"
92-
:grounding-search t
93-
:token-width 4
94-
:context-window 1048576)
95-
(chatgpt-shell-google-make-model :version "gemini-1.5-pro-latest"
96-
:short-version "1.5-pro-latest"
97-
:path "/v1beta/models/gemini-1.5-pro-latest"
98-
:token-width 4
99-
:context-window 2097152)
100-
(chatgpt-shell-google-make-model :version "gemini-1.5-flash-latest"
101-
:short-version "1.5-flash-latest"
102-
:path "/v1beta/models/gemini-1.5-flash-latest"
103-
:token-width 4
104-
:context-window 1048576)
105-
(chatgpt-shell-google-make-model :version "gemini-2.0-flash-thinking-exp-01-21"
106-
:short-version "2.0-flash-thinking-exp"
107-
:path "/v1beta/models/gemini-2.0-flash-thinking-exp-01-21"
86+
87+
(defun chatgpt-shell--google-current-generative-model-p (model)
88+
"a predicate that looks at a model description returned from Google and
89+
returns non-nil if the model is current and supports \"generateContent\"."
90+
(let ((description (gethash "description" model))
91+
(supported-methods
92+
(gethash "supportedGenerationMethods" model)))
93+
(and
94+
(not (string-match-p (rx (or "discontinued" "deprecated")) description))
95+
(seq-contains-p supported-methods "generateContent"))))
96+
97+
(defun chatgpt-shell--google-get-generative-models ()
98+
"Retrieves the list of Generative models from
99+
generativelanguage.googleapis.com"
100+
(let ((url (concat chatgpt-shell-google-api-url-base "/v1beta/models?key=" (chatgpt-shell-google-key))))
101+
(with-current-buffer (url-retrieve-synchronously url)
102+
(goto-char (if (boundp 'url-http-end-of-headers)
103+
url-http-end-of-headers
104+
(error "`url-http-end-of-headers' marker is not defined")))
105+
;;(forward-line 2)
106+
(let ((json-object-type 'hash-table)
107+
(json-array-type 'list)
108+
(json-key-type 'string))
109+
(let ((parsed-response
110+
(json-read-from-string
111+
(buffer-substring-no-properties (point) (point-max)))))
112+
(seq-filter #'chatgpt-shell--google-current-generative-model-p
113+
(gethash "models" parsed-response)))))))
114+
115+
(defun chatgpt-shell--google-convert-model (model)
116+
"converts between the model returned by Gemini, and
117+
the model description needed by chatgpt-shell ."
118+
(let ((model-name (gethash "name" model))
119+
(model-cwindow (gethash "inputTokenLimit" model)))
120+
(let ((model-version (string-remove-prefix "models/" model-name)))
121+
(let ((model-shortversion (string-remove-prefix "gemini-" model-version))
122+
(model-urlpath (concat "/v1beta/" model-name)))
123+
(chatgpt-shell-google-make-model :version model-version
124+
:short-version model-shortversion
125+
:path model-urlpath
108126
:token-width 4
109-
:context-window 32767)))
127+
:context-window model-cwindow)))))
128+
129+
(defun chatgpt-shell-google-models ()
130+
"Dynamically build a list of Google LLM models available. See
131+
https://ai.google.dev/gemini-api/docs/models/gemini. "
132+
(mapcar #'chatgpt-shell--google-convert-model (chatgpt-shell--google-get-generative-models)))
133+
134+
135+
;; (defun chatgpt-shell-google-models ()
136+
;; "Build a list of Google LLM models available."
137+
;; ;; Context windows have been verified as of 11/26/2024. See
138+
;; ;; https://ai.google.dev/gemini-api/docs/models/gemini.
139+
;; (list (chatgpt-shell-google-make-model :version "gemini-2.0-flash"
140+
;; :short-version "2.0-flash"
141+
;; :path "/v1beta/models/gemini-2.0-flash"
142+
;; :grounding-search t
143+
;; :token-width 4
144+
;; :context-window 1048576)
145+
;; (chatgpt-shell-google-make-model :version "gemini-1.5-pro-latest"
146+
;; :short-version "1.5-pro-latest"
147+
;; :path "/v1beta/models/gemini-1.5-pro-latest"
148+
;; :token-width 4
149+
;; :context-window 2097152)
150+
;; (chatgpt-shell-google-make-model :version "gemini-1.5-flash-latest"
151+
;; :short-version "1.5-flash-latest"
152+
;; :path "/v1beta/models/gemini-1.5-flash-latest"
153+
;; :token-width 4
154+
;; :context-window 1048576)
155+
;; (chatgpt-shell-google-make-model :version "gemini-2.0-flash-thinking-exp-01-21"
156+
;; :short-version "2.0-flash-thinking-exp"
157+
;; :path "/v1beta/models/gemini-2.0-flash-thinking-exp-01-21"
158+
;; :token-width 4
159+
;; :context-window 32767)))
110160

111161
(defun chatgpt-shell-google--validate-command (_command _model _settings)
112162
"Return error string if command/setup isn't valid."

0 commit comments

Comments
 (0)