Skip to content

Commit 081fb24

Browse files
authored
Merge pull request #303 from ccqpein/grounding-search
Add grounding search feature to gemini
2 parents 2d10caf + 61ab16f commit 081fb24

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

chatgpt-shell-google.el

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ If you use Gemini through a proxy service, change the URL base."
5050

5151
;; https://ai.google.dev/gemini-api/docs/tokens
5252
;; A token is equivalent to _about_ 4 characters.
53-
(cl-defun chatgpt-shell-google-make-model (&key version short-version path token-width context-window)
53+
(cl-defun chatgpt-shell-google-make-model (&key version short-version path token-width context-window grounding-search)
5454
"Create a Google model.
5555
5656
Set VERSION, SHORT-VERSION, PATH, TOKEN-WIDTH, CONTEXT-WINDOW and
@@ -72,6 +72,7 @@ VALIDATE-COMMAND handler."
7272
(:path . ,path)
7373
(:token-width . ,token-width)
7474
(:context-window . ,context-window)
75+
(:grounding-search . ,grounding-search)
7576
(:url-base . chatgpt-shell-google-api-url-base)
7677
(:handler . chatgpt-shell-google--handle-gemini-command)
7778
(:filter . chatgpt-shell-google--extract-gemini-response)
@@ -88,6 +89,7 @@ VALIDATE-COMMAND handler."
8889
(list (chatgpt-shell-google-make-model :version "gemini-2.0-flash"
8990
:short-version "2.0-flash"
9091
:path "/v1beta/models/gemini-2.0-flash"
92+
:grounding-search t
9193
:token-width 4
9294
:context-window 1048576)
9395
(chatgpt-shell-google-make-model :version "gemini-1.5-pro-latest"
@@ -150,12 +152,12 @@ or
150152
"Create the API headers."
151153
(list "Content-Type: application/json; charset=utf-8"))
152154

153-
(cl-defun chatgpt-shell-google--make-payload (&key _model context settings)
155+
(cl-defun chatgpt-shell-google--make-payload (&key model context settings)
154156
"Create the API payload using MODEL CONTEXT and SETTINGS."
155157
(chatgpt-shell-google--make-gemini-payload
156158
:context context
157-
:temperature (map-elt settings :temperature)
158-
:system-prompt (map-elt settings :system-prompt)))
159+
:model model
160+
:settings settings))
159161

160162
(cl-defun chatgpt-shell-google--handle-gemini-command (&key model command context shell settings)
161163
"Handle Gemini COMMAND (prompt) using MODEL, CONTEXT, SHELL, and SETTINGS."
@@ -167,25 +169,27 @@ or
167169
:data (chatgpt-shell-google--make-gemini-payload
168170
:prompt command
169171
:context context
170-
:temperature (map-elt settings :temperature)
171-
:system-prompt (map-elt settings :system-prompt))
172+
:model model
173+
:settings settings)
172174
:headers (list "Content-Type: application/json; charset=utf-8")
173175
:filter #'chatgpt-shell-google--extract-gemini-response
174176
:shell shell))
175177

176-
(cl-defun chatgpt-shell-google--make-gemini-payload (&key prompt context system-prompt temperature)
178+
(cl-defun chatgpt-shell-google--make-gemini-payload (&key prompt context settings model)
177179
"Create the request payload.
178180
179181
Compose using PROMPT, CONTEXT, SYSTEM-PROMPT and TEMPERATURE."
180182
(append
181-
(when system-prompt
182-
`((system_instruction . ((parts . ((text . ,system-prompt)))))))
183+
(when (map-elt settings :system-prompt)
184+
`((system_instruction . ((parts . ((text . ,(map-elt settings :system-prompt))))))))
183185
`((contents . ,(vconcat
184186
(chatgpt-shell-google--gemini-user-model-messages
185187
(append context
186188
(when prompt
187-
(list (cons prompt nil)))))))
188-
(generation_config . ((temperature . ,(or temperature 1))
189+
(list (cons prompt nil))))))))
190+
(when (map-elt model :grounding-search)
191+
'((tools . ((google_search . ())))))
192+
`((generation_config . ((temperature . ,(or (map-elt settings :temperature) 1))
189193
;; 1 is most diverse output.
190194
(topP . 1))))))
191195

0 commit comments

Comments
 (0)