-
Notifications
You must be signed in to change notification settings - Fork 79
/
chatgpt-shell-google.el
277 lines (242 loc) · 11.8 KB
/
chatgpt-shell-google.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
;;; chatgpt-shell-google.el --- Google-specific logic -*- lexical-binding: t -*-
;; Copyright (C) 2023 Alvaro Ramirez
;; Author: Alvaro Ramirez https://xenodium.com
;; URL: https://github.com/xenodium/chatgpt-shell
;; Package-Requires: ((emacs "28.1") (shell-maker "0.72.1"))
;; This package 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, or (at your option)
;; any later version.
;; This package 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Adds Google specifics for `chatgpt-shell'.
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'shell-maker)
(require 'map)
(defvar chatgpt-shell-proxy)
(defcustom chatgpt-shell-google-key nil
"Google API key as a string or a function that loads and returns it."
:type '(choice (function :tag "Function")
(string :tag "String"))
:group 'chatgpt-shell)
(defcustom chatgpt-shell-google-api-url-base "https://generativelanguage.googleapis.com"
"Google API's base URL.
API url = base + path.
If you use Gemini through a proxy service, change the URL base."
:type 'string
:safe #'stringp
:group 'chatgpt-shell)
;; https://ai.google.dev/gemini-api/docs/tokens
;; A token is equivalent to _about_ 4 characters.
(cl-defun chatgpt-shell-google-make-model (&key version short-version path token-width context-window)
"Create a Google model.
Set VERSION, SHORT-VERSION, PATH, TOKEN-WIDTH, CONTEXT-WINDOW and
VALIDATE-COMMAND handler."
(unless version
(error "Missing mandatory :version param"))
(unless short-version
(error "Missing mandatory :short-version param"))
(unless path
(error "Missing mandatory :path param"))
(unless token-width
(error "Missing mandatory :token-width param for %s" version))
(unless context-window
(error "Missing mandatory :context-window param for %s" version))
`((:version . ,version)
(:short-version . ,short-version)
(:label . "Gemini")
(:provider . "Google")
(:path . ,path)
(:token-width . ,token-width)
(:context-window . ,context-window)
(:url-base . chatgpt-shell-google-api-url-base)
(:handler . chatgpt-shell-google--handle-gemini-command)
(:filter . chatgpt-shell-google--extract-gemini-response)
(:payload . chatgpt-shell-google--make-payload)
(:url . chatgpt-shell-google--make-url)
(:headers . chatgpt-shell-google--make-headers)
(:key . chatgpt-shell-google-key)
(:validate-command . chatgpt-shell-google--validate-command)))
(defun chatgpt-shell-google-models ()
"Build a list of Google LLM models available."
;; Context windows have been verified as of 11/26/2024. See
;; https://ai.google.dev/gemini-api/docs/models/gemini.
(list (chatgpt-shell-google-make-model :version "gemini-1.5-pro-latest"
:short-version "1.5-pro-latest"
:path "/v1beta/models/gemini-1.5-pro-latest"
:token-width 4
:context-window 2097152)
(chatgpt-shell-google-make-model :version "gemini-1.5-flash"
:short-version "1.5-flash"
:path "/v1beta/models/gemini-1.5-flash"
:token-width 4
:context-window 1048576)))
(defun chatgpt-shell-google--validate-command (_command _model _settings)
"Return error string if command/setup isn't valid."
(unless chatgpt-shell-google-key
"Variable `chatgpt-shell-google-key' needs to be set to your key.
Try M-x set-variable chatgpt-shell-google-key
or
(setq chatgpt-shell-google-key \"my-key\")"))
(defun chatgpt-shell-google-key ()
"Get the Google API key."
(cond ((stringp chatgpt-shell-google-key)
chatgpt-shell-google-key)
((functionp chatgpt-shell-google-key)
(condition-case _err
(funcall chatgpt-shell-google-key)
(error
"KEY-NOT-FOUND")))
(t
nil)))
(cl-defun chatgpt-shell-google--make-url (&key _command model settings)
"Create the API URL using MODEL and SETTINGS."
(unless model
(error "Missing mandatory :model param"))
(unless settings
(error "Missing mandatory :settings param"))
(concat chatgpt-shell-google-api-url-base
(or (map-elt model :path)
(error "Provider :path not found"))
(if (map-elt settings :streaming)
":streamGenerateContent"
":generateContent")
"?key="
(or (chatgpt-shell-google-key)
(error "Your chatgpt-shell-google-key is missing"))
"&alt=sse")) ;; Needed or streaming doesn't work.
(cl-defun chatgpt-shell-google--make-headers (&key _model _settings)
"Create the API headers."
(list "Content-Type: application/json; charset=utf-8"))
(cl-defun chatgpt-shell-google--make-payload (&key _model context settings)
"Create the API payload using MODEL CONTEXT and SETTINGS."
(chatgpt-shell-google--make-gemini-payload
:context context
:temperature (map-elt settings :temperature)
:system-prompt (map-elt settings :system-prompt)))
(cl-defun chatgpt-shell-google--handle-gemini-command (&key model command context shell settings)
"Handle Gemini COMMAND (prompt) using MODEL, CONTEXT, SHELL, and SETTINGS."
(shell-maker-make-http-request
:async t
:url (chatgpt-shell-google--make-url :model model
:settings settings)
:proxy chatgpt-shell-proxy
:data (chatgpt-shell-google--make-gemini-payload
:prompt command
:context context
:temperature (map-elt settings :temperature)
:system-prompt (map-elt settings :system-prompt))
:headers (list "Content-Type: application/json; charset=utf-8")
:filter #'chatgpt-shell-google--extract-gemini-response
:shell shell))
(cl-defun chatgpt-shell-google--make-gemini-payload (&key prompt context system-prompt temperature)
"Create the request payload.
Compose using PROMPT, CONTEXT, SYSTEM-PROMPT and TEMPERATURE."
(append
(when system-prompt
`((system_instruction . ((parts . ((text . ,system-prompt)))))))
`((contents . ,(vconcat
(chatgpt-shell-google--gemini-user-model-messages
(append context
(when prompt
(list (cons prompt nil)))))))
(generation_config . ((temperature . ,(or temperature 1))
;; 1 is most diverse output.
(topP . 1))))))
(defun chatgpt-shell-google--gemini-user-model-messages (context)
"Convert CONTEXT to gemini messages.
Sequence must be a vector for json serialization.
For example:
[
((role . \"user\") (parts . ((text . \"hello\"))))
((role . \"model\") (parts . ((text . \"world\"))))
]"
(let ((result))
(mapc
(lambda (item)
(when (car item)
(push (list (cons 'role "user")
(cons 'parts (vconcat ;; Vector for json
(list (list (cons 'text (car item))))))) result))
(when (cdr item)
(push (list (cons 'role "model")
(cons 'parts (vconcat ;; Vector for json
(list (list (cons 'text (cdr item))))))) result)))
context)
(nreverse result)))
(defun chatgpt-shell-google--extract-gemini-response (raw-response)
"Extract Gemini response from RAW-RESPONSE."
(if-let* ((whole (shell-maker--json-parse-string raw-response))
(response (or (let-alist whole
.error.message)
(let-alist whole
(mapconcat (lambda (choice)
(let-alist choice
(or .delta.content
.message.content)))
.choices)))))
response
(if-let ((chunks (shell-maker--split-text raw-response)))
(let ((response)
(pending)
(result))
(mapc (lambda (chunk)
;; Response chunks come in the form:
;; data: {...}
;; data: {...}
(if-let* ((is-data (equal (map-elt chunk :key) "data:"))
(obj (shell-maker--json-parse-string (map-elt chunk :value)))
(text (let-alist obj
(or (let-alist (seq-first .candidates)
(cond ((seq-first .content.parts)
(let-alist (seq-first .content.parts)
.text))
((equal .finishReason "RECITATION")
"")
((equal .finishReason "STOP")
"")
((equal .finishReason "CANCELLED")
"Error: Request cancellled.")
((equal .finishReason "CRASHED")
"Error: An error occurred. Try again.")
((equal .finishReason "END_OF_PROMPT")
"Error: Couldn't generate a response. Try rephrasing.")
((equal .finishReason "LENGTH")
"Error: Response is too big. Try rephrasing.")
((equal .finishReason "TIME")
"Error: Timed out.")
((equal .finishReason "SAFETY")
"Error: Flagged for safety.")
((equal .finishReason "LANGUAGE")
"Error: Flagged for language.")
((equal .finishReason "BLOCKLIST")
"Error: Flagged for forbidden terms.")
((equal .finishReason "PROHIBITED_CONTENT")
"Error: Flagged for prohibited content.")
((equal .finishReason "SPII")
"Error: Flagged for sensitive personally identifiable information.")
(.finishReason
(format "\n\nError: Something's up (%s)" .finishReason))))
.error.message))))
(unless (string-empty-p text)
(setq response (concat response text)))
(setq pending (concat pending
(or (map-elt chunk :key) "")
(map-elt chunk :value)))))
chunks)
(setq result
(list (cons :filtered (unless (string-empty-p response)
response))
(cons :pending pending)))
result)
(list (cons :filtered nil)
(cons :pending raw-response)))))
(provide 'chatgpt-shell-google)
;;; chatgpt-shell-google.el ends here