Skip to content

Commit 41f3e2a

Browse files
committed
Add chat module
1 parent 5ddfef9 commit 41f3e2a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

google-gemini-chat.el

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
;;; google-gemini-chat.el --- Chat 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+
;; Chat module.
23+
;;
24+
25+
;;; Code:
26+
27+
(require 'google-gemini)
28+
29+
;;
30+
;;; API
31+
32+
;;;###autoload
33+
(cl-defun google-gemini-chat ( contents callback
34+
&key
35+
(content-type "application/json")
36+
(parameters google-gemini-parameters)
37+
(key google-gemini-key)
38+
(model "gemini-pro")
39+
(category "HARM_CATEGORY_DANGEROUS_CONTENT")
40+
(threshold "BLOCK_ONLY_HIGH")
41+
stop-sequences
42+
temperature
43+
max-output-tokens
44+
top-p
45+
top-k)
46+
"Send chat request.
47+
48+
Arguments CONTENTS and CALLBACK are required for this type of request.
49+
CONTENTS is the chat conversation data. CALLBACK is the execuation after
50+
request is made.
51+
52+
Arguments PARAMETERS, CONTENT-TYPE, and KEY are global options;
53+
however, you can overwrite the value by passing it in.
54+
55+
The rest of the arugments are optional, please see Google Gemini API reference
56+
page for more information. Arguments here refer to MODEL, TEMPERATURE,
57+
STOP-SEQUENCES, MAX-OUTPUT-TOKENS, TOP-P, and TOP-K."
58+
(google-gemini-request (concat google-gemini-generativelanguage-url
59+
"v1beta/models/" model ":generateContent?key="
60+
key)
61+
:type "POST"
62+
:params parameters
63+
:headers (google-gemini--headers content-type)
64+
:data (google-gemini--json-encode
65+
`(("contents" . ,contents)
66+
("safetySettings" . [(("category" . ,category)
67+
("threshold" . ,threshold))])
68+
("generationConfig" .
69+
(("stopSequences" . ,stop-sequences)
70+
("temperature" . ,temperature)
71+
("maxOutputTokens" . ,max-output-tokens)
72+
("topP" . ,top-p)
73+
("topK" . ,top-k)))))
74+
:parser 'json-read
75+
:complete (cl-function
76+
(lambda (&key data &allow-other-keys)
77+
(funcall callback data)))))
78+
79+
;;
80+
;;; Application
81+
82+
;;;###autoload
83+
(defun google-gemini-chat-prompt ()
84+
"Start the chat conversation to Google Gemini."
85+
(interactive)
86+
(if-let* ((user (read-string "What is your name? " "user"))
87+
(say (read-string "Start the conversation: ")))
88+
(google-gemini-chat `[(("role" . ,user)
89+
("parts" . [(("text" . ,say))]))]
90+
(lambda (data)
91+
(let-alist data
92+
(let-alist (elt .candidates 0)
93+
(let-alist .content
94+
(let-alist (elt .parts 0)
95+
(message "Response: %s" .text)))))))
96+
(user-error "Abort, cancel generate content operation")))
97+
98+
(provide 'google-gemini-chat)
99+
;;; google-gemini-chat.el ends here

0 commit comments

Comments
 (0)