Skip to content

Commit 44e9833

Browse files
committed
added (gemini::generate-with-search ...
1 parent 836aaa2 commit 44e9833

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/gemini/gemini.lisp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,42 @@ Returns the total token count as an integer."
104104
(concatenate 'string "User: " user-prompt "\n" "Gemini: " gemini-response
105105
"\n" *chat-history* "\n\n")))))))
106106

107-
;; (gemini::chat)
107+
;; (gemini::chat)
108+
109+
(defun generate-with-search (model-id prompt)
110+
(let* ((payload (make-hash-table :test 'equal)))
111+
(setf (gethash "contents" payload)
112+
(list (let ((contents (make-hash-table :test 'equal)))
113+
(setf (gethash "parts" contents)
114+
(list (let ((part (make-hash-table :test 'equal)))
115+
(setf (gethash "text" part) prompt)
116+
part)))
117+
contents)))
118+
(setf (gethash "tools" payload)
119+
(list (let ((tool (make-hash-table :test 'equal)))
120+
(setf (gethash "google_search" tool)
121+
(make-hash-table :test 'equal))
122+
tool)))
123+
(let* ((api-url (concatenate 'string *gemini-api-base-url* model-id ":generateContent"))
124+
(data (cl-json:encode-json-to-string payload))
125+
(response (dex:post api-url
126+
:headers `(("Content-Type" . "application/json")
127+
("x-goog-api-key" . ,*google-api-key*))
128+
:content data))
129+
(response-string (if (stringp response)
130+
response
131+
(flex:octets-to-string response :external-format :utf-8)))
132+
(decoded-response (cl-json:decode-json-from-string response-string))
133+
(candidates-pair (assoc :CANDIDATES decoded-response))
134+
(candidates (cdr candidates-pair))
135+
(candidate (first candidates))
136+
(content-pair (assoc :CONTENT candidate))
137+
(content (cdr content-pair))
138+
(parts-pair (assoc :PARTS content))
139+
(parts (cdr parts-pair))
140+
(first-part (first parts))
141+
(text-pair (assoc :TEXT first-part))
142+
(text (cdr text-pair)))
143+
text)))
144+
145+
;; (gemini::generate-with-search "gemini-2.5-flash" "Who won the euro 2024?")

0 commit comments

Comments
 (0)