diff --git a/google-gemini-content.el b/google-gemini-content.el index 05ba2c1..dba6e50 100644 --- a/google-gemini-content.el +++ b/google-gemini-content.el @@ -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 @@ -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) diff --git a/google-gemini-count-tokens.el b/google-gemini-count-tokens.el new file mode 100644 index 0000000..6e93155 --- /dev/null +++ b/google-gemini-count-tokens.el @@ -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 . + +;;; 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 diff --git a/google-gemini.el b/google-gemini.el index 98fc91b..7c22ab4 100644 --- a/google-gemini.el +++ b/google-gemini.el @@ -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