Last active
November 20, 2025 09:41
-
-
Save CoderPrans/2a674fdeb58f6f3d391ce5d9baf2ada2 to your computer and use it in GitHub Desktop.
All you need for a one off Query, call it a poor man's gpt.el
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (eval-when-compile | |
| (require 'url) | |
| (require 'json) | |
| (require 'dash) | |
| ) | |
| (defconst ask-gemini--api-key GEMINI_API_KEY | |
| "Your Gemini API key.") | |
| (defconst ask-gemini--api-url "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent" | |
| "The base URL for the fast Gemini 2.5 Flash-Lite model.") | |
| (defun ask-gemini (prompt) | |
| "Queries the Gemini 2.5 Flash Lite API synchronously with the given PROMPT. Displayes response in an Org buffer." | |
| (interactive "sPrompt: ") | |
| ;; kill any existing *Gemini Response* buffer | |
| (when (get-buffer "*Gemini Response*") | |
| (delete-windows-on "*Gemini Response*" t) | |
| (kill-buffer "*Gemini Response*")) | |
| (let* ((request-url (concat ask-gemini--api-url "?key=" ask-gemini--api-key)) | |
| (content-header '(("Content-Type" . "application/json"))) | |
| (request-body (json-encode `((contents . (((parts . (((text . ,prompt)))))))))) | |
| ;; dynamic variables required for url-retreive-synchronously | |
| (url-request-method "POST") | |
| (url-request-data request-body) | |
| (url-request-extra-headers content-header) | |
| ;; the API request | |
| (response-buffer (url-retrieve-synchronously request-url nil nil 15)) | |
| (json-response (with-current-buffer response-buffer | |
| (goto-char (point-min)) | |
| (when (re-search-forward "^$" nil t) | |
| (buffer-substring (point) (point-max))) | |
| )) | |
| (text-response (->> json-response | |
| (json-read-from-string) | |
| (alist-get 'candidates) | |
| ((lambda (v) (aref v 0))) | |
| (alist-get 'content) | |
| (alist-get 'parts) | |
| ((lambda (v) (aref v 0))) | |
| (alist-get 'text))) | |
| (output-buffer (get-buffer-create "*Gemini Response*"))) | |
| (with-current-buffer output-buffer | |
| (erase-buffer) | |
| (insert (decode-coding-string text-response 'utf-8)) | |
| (goto-char (point-min)) | |
| (visual-line-mode 1) | |
| (setq-local buffer-read-only t) | |
| (local-set-key (kbd "SPC") 'scroll-up-command) | |
| (local-set-key (kbd "<backspace>") 'scroll-down-command) | |
| (local-set-key (kbd "q") (lambda () | |
| (interactive) | |
| (if (one-window-p) | |
| (bury-buffer) | |
| (delete-window))))) | |
| (split-window-below) | |
| (other-window 1) | |
| (switch-to-buffer output-buffer) | |
| (message "Gemini Response received!")) | |
| ) | |
| (provide 'ask-gemini) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment