Skip to content

Commit 00d26ee

Browse files
committed
Add count tokens
1 parent 2d82df4 commit 00d26ee

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

google-gemini-content.el

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
top-p
4343
top-k)
4444
"Send generate content request."
45-
(google-gemini-request (format "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=%s" key)
45+
(google-gemini-request (concat google-gemini-generativelanguage-url
46+
"v1beta/models/gemini-pro:generateContent?key="
47+
key)
4648
:type "POST"
4749
:headers (google-gemini--headers content-type)
4850
:data (google-gemini--json-encode
@@ -65,16 +67,16 @@
6567

6668
;;;###autoload
6769
(defun google-gemini-content-prompt ()
68-
"Start making a conversation to Google Gemini."
70+
"Ask to generate contents from Google Gemini."
6971
(interactive)
70-
(if-let ((text (read-string "Content: ")))
72+
(if-let ((text (read-string "[Generate] Content: ")))
7173
(google-gemini-content-generate text
7274
(lambda (data)
7375
(let-alist data
7476
(let-alist (elt .candidates 0)
7577
(let-alist .content
7678
(let-alist (elt .parts 0)
77-
(message "%s" .text)))))))
79+
(message "Response: %s" .text)))))))
7880
(user-error "Abort, cancel generate content operation")))
7981

8082
(provide 'google-gemini-content)

google-gemini-count-tokens.el

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

google-gemini.el

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,12 @@ The URL is the url for `request' function; then BODY is the arguments for rest."
140140
(google-gemini--handle-error response)))
141141
,@body)))
142142

143+
;;
144+
;;; Constants
145+
146+
(defconst google-gemini-generativelanguage-url
147+
"https://generativelanguage.googleapis.com/"
148+
"Base Url for generativelanguage services.")
149+
143150
(provide 'google-gemini)
144151
;;; google-gemini.el ends here

0 commit comments

Comments
 (0)