Skip to content

Commit

Permalink
Add count tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Feb 12, 2024
1 parent 2d82df4 commit 00d26ee
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
10 changes: 6 additions & 4 deletions google-gemini-content.el
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
top-p
top-k)
"Send generate content request."
(google-gemini-request (format "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=%s" key)
(google-gemini-request (concat google-gemini-generativelanguage-url
"v1beta/models/gemini-pro:generateContent?key="
key)
:type "POST"
:headers (google-gemini--headers content-type)
:data (google-gemini--json-encode
Expand All @@ -65,16 +67,16 @@

;;;###autoload
(defun google-gemini-content-prompt ()
"Start making a conversation to Google Gemini."
"Ask to generate contents from Google Gemini."
(interactive)
(if-let ((text (read-string "Content: ")))
(if-let ((text (read-string "[Generate] Content: ")))
(google-gemini-content-generate text
(lambda (data)
(let-alist data
(let-alist (elt .candidates 0)
(let-alist .content
(let-alist (elt .parts 0)
(message "%s" .text)))))))
(message "Response: %s" .text)))))))
(user-error "Abort, cancel generate content operation")))

(provide 'google-gemini-content)
Expand Down
63 changes: 63 additions & 0 deletions google-gemini-count-tokens.el
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
7 changes: 7 additions & 0 deletions google-gemini.el
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,12 @@ The URL is the url for `request' function; then BODY is the arguments for rest."
(google-gemini--handle-error response)))
,@body)))

;;
;;; Constants

(defconst google-gemini-generativelanguage-url
"https://generativelanguage.googleapis.com/"
"Base Url for generativelanguage services.")

(provide 'google-gemini)
;;; google-gemini.el ends here

0 comments on commit 00d26ee

Please sign in to comment.