-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
;;; google-gemini-count-tokens.el --- Create count tokens request with Google Gemini API -*- lexical-binding: t; -*- | ||
|
||
;; This file is not part of GNU Emacs. | ||
|
||
;; This program 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 of the License, or | ||
;; (at your option) any later version. | ||
|
||
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
;;; Commentary: | ||
;; | ||
;; Create count tokens request with Google Gemini API. | ||
;; | ||
|
||
;;; Code: | ||
|
||
(require 'google-gemini) | ||
|
||
;; | ||
;;; API | ||
|
||
;;;###autoload | ||
(cl-defun google-gemini-count-tokens ( text callback | ||
&key | ||
(content-type "application/json") | ||
(key google-gemini-key)) | ||
"Send count tokens request." | ||
(google-gemini-request (concat google-gemini-generativelanguage-url | ||
"v1beta/models/gemini-pro:countTokens?key=" | ||
key) | ||
:type "POST" | ||
:headers (google-gemini--headers content-type) | ||
:data (google-gemini--json-encode | ||
`(("contents" . [((parts . [((text . ,text))]))]))) | ||
:parser 'json-read | ||
:complete (cl-function | ||
(lambda (&key data &allow-other-keys) | ||
(funcall callback data))))) | ||
|
||
;; | ||
;;; Application | ||
|
||
;;;###autoload | ||
(defun google-gemini-count-tokens-prompt () | ||
"Send request to count tokens." | ||
(interactive) | ||
(if-let ((text (read-string "[Count Tokens] Content: "))) | ||
(google-gemini-count-tokens text | ||
(lambda (data) | ||
(let-alist data | ||
(message "`totalTokens` is %s" .totalTokens)))) | ||
(user-error "Abort, cancel generate content operation"))) | ||
|
||
(provide 'google-gemini-count-tokens) | ||
;;; google-gemini-count-tokens.el ends here |
This file contains 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