|
| 1 | +;;; google-gemini-count-tokens.el --- Create count tokens request with Google Gemini API -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; This file is not part of GNU Emacs. |
| 4 | + |
| 5 | +;; This program is free software: you can redistribute it and/or modify |
| 6 | +;; it under the terms of the GNU General Public License as published by |
| 7 | +;; the Free Software Foundation, either version 3 of the License, or |
| 8 | +;; (at your option) any later version. |
| 9 | + |
| 10 | +;; This program is distributed in the hope that it will be useful, |
| 11 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +;; GNU General Public License for more details. |
| 14 | + |
| 15 | +;; You should have received a copy of the GNU General Public License |
| 16 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +;;; Commentary: |
| 19 | +;; |
| 20 | +;; Create count tokens request with Google Gemini API. |
| 21 | +;; |
| 22 | + |
| 23 | +;;; Code: |
| 24 | + |
| 25 | +(require 'google-gemini) |
| 26 | + |
| 27 | +;; |
| 28 | +;;; API |
| 29 | + |
| 30 | +;;;###autoload |
| 31 | +(cl-defun google-gemini-count-tokens ( text callback |
| 32 | + &key |
| 33 | + (content-type "application/json") |
| 34 | + (key google-gemini-key)) |
| 35 | + "Send count tokens request." |
| 36 | + (google-gemini-request (concat google-gemini-generativelanguage-url |
| 37 | + "v1beta/models/gemini-pro:countTokens?key=" |
| 38 | + key) |
| 39 | + :type "POST" |
| 40 | + :headers (google-gemini--headers content-type) |
| 41 | + :data (google-gemini--json-encode |
| 42 | + `(("contents" . [((parts . [((text . ,text))]))]))) |
| 43 | + :parser 'json-read |
| 44 | + :complete (cl-function |
| 45 | + (lambda (&key data &allow-other-keys) |
| 46 | + (funcall callback data))))) |
| 47 | + |
| 48 | +;; |
| 49 | +;;; Application |
| 50 | + |
| 51 | +;;;###autoload |
| 52 | +(defun google-gemini-count-tokens-prompt () |
| 53 | + "Send request to count tokens." |
| 54 | + (interactive) |
| 55 | + (if-let ((text (read-string "[Count Tokens] Content: "))) |
| 56 | + (google-gemini-count-tokens text |
| 57 | + (lambda (data) |
| 58 | + (let-alist data |
| 59 | + (message "`totalTokens` is %s" .totalTokens)))) |
| 60 | + (user-error "Abort, cancel generate content operation"))) |
| 61 | + |
| 62 | +(provide 'google-gemini-count-tokens) |
| 63 | +;;; google-gemini-count-tokens.el ends here |
0 commit comments