Skip to content

Commit 120a21d

Browse files
committed
Add embedding module
1 parent 27c09ea commit 120a21d

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

google-gemini-content.el

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
&key
3535
(content-type "application/json")
3636
(key google-gemini-key)
37+
(model "gemini-pro")
3738
(category "HARM_CATEGORY_DANGEROUS_CONTENT")
3839
(threshold "BLOCK_ONLY_HIGH")
3940
stop-sequences
@@ -43,12 +44,12 @@
4344
top-k)
4445
"Send generate content request."
4546
(google-gemini-request (concat google-gemini-generativelanguage-url
46-
"v1beta/models/gemini-pro:generateContent?key="
47+
"v1beta/models/" model ":generateContent?key="
4748
key)
4849
:type "POST"
4950
:headers (google-gemini--headers content-type)
5051
:data (google-gemini--json-encode
51-
`(("contents" . [((parts . [((text . ,text))]))])
52+
`(("contents" . [(("parts" . [(("text" . ,text))]))])
5253
("safetySettings" . [(("category" . ,category)
5354
("threshold" . ,threshold))])
5455
("generationConfig" .

google-gemini-count-tokens.el

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
(cl-defun google-gemini-count-tokens ( text callback
3232
&key
3333
(content-type "application/json")
34+
(model "gemini-pro")
3435
(key google-gemini-key))
3536
"Send count tokens request."
3637
(google-gemini-request (concat google-gemini-generativelanguage-url
37-
"v1beta/models/gemini-pro:countTokens?key="
38+
"v1beta/models/" model ":countTokens?key="
3839
key)
3940
:type "POST"
4041
:headers (google-gemini--headers content-type)

google-gemini-embedding.el

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
;;; google-gemini-embedding.el --- Embedding module -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2024 Shen, Jen-Chieh
4+
5+
;; This file is not part of GNU Emacs.
6+
7+
;; This program is free software: you can redistribute it and/or modify
8+
;; it under the terms of the GNU General Public License as published by
9+
;; the Free Software Foundation, either version 3 of the License, or
10+
;; (at your option) any later version.
11+
12+
;; This program is distributed in the hope that it will be useful,
13+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
;; GNU General Public License for more details.
16+
17+
;; You should have received a copy of the GNU General Public License
18+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
;;; Commentary:
21+
;;
22+
;; Embedding module.
23+
;;
24+
25+
;;; Code:
26+
27+
(require 'google-gemini)
28+
29+
;;
30+
;;; API
31+
32+
;;;###autoload
33+
(cl-defun google-gemini-embedding ( text callback
34+
&key
35+
(content-type "application/json")
36+
(key google-gemini-key)
37+
(model "embedding-001")
38+
(category "HARM_CATEGORY_DANGEROUS_CONTENT")
39+
(threshold "BLOCK_ONLY_HIGH")
40+
stop-sequences
41+
temperature
42+
max-output-tokens
43+
top-p
44+
top-k)
45+
"Send generate content request."
46+
(google-gemini-request (concat google-gemini-generativelanguage-url
47+
"v1beta/models/" model ":embedContent?key="
48+
key)
49+
:type "POST"
50+
:headers (google-gemini--headers content-type)
51+
:data (google-gemini--json-encode
52+
`(("model" . ,(concat "models/" model))
53+
("content" . (("parts" . [(("text" . ,text))])))))
54+
:parser 'json-read
55+
:complete (cl-function
56+
(lambda (&key data &allow-other-keys)
57+
(funcall callback data)))))
58+
59+
;;
60+
;;; Application
61+
62+
;;;###autoload
63+
(defun google-gemini-embedding-prompt ()
64+
"Ask to embedding from Google Gemini."
65+
(interactive)
66+
(if-let ((text (read-string "[Embedding] Content: ")))
67+
(google-gemini-embedding text (lambda (data) (message "%s" data)))
68+
(user-error "Abort, cancel embedding operation")))
69+
70+
(provide 'google-gemini-embedding)
71+
;;; google-gemini-embedding.el ends here

0 commit comments

Comments
 (0)