|
6 | 6 | ;; Maintainer: JenChieh <jcs090218@gmail.com> |
7 | 7 | ;; URL: https://github.com/emacs-openai/google-gemini |
8 | 8 | ;; Version: 0.1.0 |
9 | | -;; Package-Requires: ((emacs "26.1")) |
| 9 | +;; Package-Requires: ((emacs "26.1") (request "0.3.0")) |
10 | 10 | ;; Keywords: comm google gemini |
11 | 11 |
|
12 | 12 | ;; This file is not part of GNU Emacs. |
|
31 | 31 |
|
32 | 32 | ;;; Code: |
33 | 33 |
|
34 | | -;; Happy coding! ;) |
| 34 | +(require 'auth-source) |
| 35 | +(require 'cl-lib) |
| 36 | +(require 'let-alist) |
| 37 | +(require 'pcase) |
| 38 | +(require 'pp) |
| 39 | +(require 'json) |
| 40 | + |
| 41 | +(require 'request) |
| 42 | + |
| 43 | +(defgroup google-gemini nil |
| 44 | + "Elisp library for the Google Gemini API." |
| 45 | + :prefix "google-gemini-" |
| 46 | + :group 'comm |
| 47 | + :link '(url-link :tag "Repository" "https://github.com/emacs-openai/google-gemini")) |
| 48 | + |
| 49 | +;; |
| 50 | +;;; Logger |
| 51 | + |
| 52 | +(defvar google-gemini--show-log nil |
| 53 | + "Get more information from the program.") |
| 54 | + |
| 55 | +(defun google-gemini--log (fmt &rest args) |
| 56 | + "Debug message like function `message' with same argument FMT and ARGS." |
| 57 | + (when google-gemini--show-log |
| 58 | + (apply 'message fmt args))) |
| 59 | + |
| 60 | +;; |
| 61 | +;;; Request |
| 62 | + |
| 63 | +(defvar google-gemini-key "" |
| 64 | + "Variable storing the gemini key or a function name to retrieve it. |
| 65 | +
|
| 66 | +The function should take no arguments and return a string containing the key. |
| 67 | +
|
| 68 | +A function, `google-gemini-key-auth-source', that retrieves the key from |
| 69 | +auth-source is provided for convenience.") |
| 70 | + |
| 71 | +(defcustom google-gemini-base-url "googleapis.com" |
| 72 | + "The base URL for Google Gemini API requests." |
| 73 | + :type 'string |
| 74 | + :group 'google-gemini) |
| 75 | + |
| 76 | +;;;###autoload |
| 77 | +(defun google-gemini-key-auth-source (&optional base-url) |
| 78 | + "Retrieve the Google Gemini API key from auth-source given a BASE-URL. |
| 79 | +If BASE-URL is not specified, it defaults to `google-gemini-base-url'." |
| 80 | + (if-let ((auth-info |
| 81 | + (auth-source-search :max 1 |
| 82 | + :host (or (url-host (url-generic-parse-url (or base-url google-gemini-base-url))) |
| 83 | + google-gemini-base-url) |
| 84 | + :require '(:user :secret)))) |
| 85 | + (funcall (plist-get (car auth-info) :secret)) |
| 86 | + (error "Google Gemini API key not found in auth-source"))) |
| 87 | + |
| 88 | +(defun google-gemini--alist-omit-null (alist) |
| 89 | + "Omit null value or empty string in ALIST." |
| 90 | + (cl-remove-if (lambda (pair) |
| 91 | + (let ((value (cdr pair))) |
| 92 | + (or (null value) ; ignore null |
| 93 | + (and (stringp value) ; ignore empty string |
| 94 | + (string-empty-p value))))) |
| 95 | + alist)) |
| 96 | + |
| 97 | +(defun google-gemini--headers (content-type) |
| 98 | + "Construct request headers. |
| 99 | +
|
| 100 | +Arguments CONTENT-TYPE are common request headers." |
| 101 | + (google-gemini--alist-omit-null |
| 102 | + `(("Content-Type" . ,content-type) |
| 103 | + ,(if (or (null key) |
| 104 | + (string-empty-p key)) |
| 105 | + "" |
| 106 | + (pcase openai-key-type |
| 107 | + (:bearer `("Authorization" . ,(concat "Bearer " key))) |
| 108 | + (:azure-api `("api-key" . ,key)) |
| 109 | + (_ (user-error "Invalid key type: %s" |
| 110 | + openai-key-type)))) |
| 111 | + ))) |
| 112 | + |
| 113 | +(defun google-gemini--json-encode (object) |
| 114 | + "Wrapper for function `json-encode' but it remove nil value before |
| 115 | +constructing JSON data. |
| 116 | +
|
| 117 | +The argument OBJECT is an alist that can be construct to JSON data; see function |
| 118 | +`json-encode' for the detials." |
| 119 | + (let* ((object (google-gemini--alist-omit-null object)) |
| 120 | + (encoded (json-encode object))) |
| 121 | + (google-gemini--log "[ENCODED]: %s" encoded) |
| 122 | + encoded)) |
35 | 123 |
|
36 | 124 | (provide 'google-gemini) |
37 | 125 | ;;; google-gemini.el ends here |
0 commit comments