-
Notifications
You must be signed in to change notification settings - Fork 4
/
paw-gptel.el
72 lines (66 loc) · 3.09 KB
/
paw-gptel.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
;;; paw-gptel.el -*- lexical-binding: t; -*-
(require 'paw-vars)
(require 'gptel)
(require 'paw-db)
(defun paw-gptel-update-note (id word type &optional callback)
(gptel-request
(pcase type
('word (format "You are an English Expert, you carefully research the word, give me the English explanations/sentences from Famous Dictionaries, and the related Chinese explanation: %s, return your answer in Emacs Org mode format, heading starts from **, no need to tell me 'Sure'"
word))
(_ (format "Explain: %s directly, return your answer in Emacs Org Mode format, heading starts from **, no need to tell me 'Sure'"
word )))
:callback
(lambda (response info)
(if (not response)
(message "gptel-quick failed with message: %s" (plist-get info :status))
(paw-update-note id response)
(message "%s" response))
(if callback
(funcall callback)))))
(defun paw-gptel-update-exp (id word type &optional callback)
(gptel-request
(pcase type
('word (format "You are an English Expert, you carefully research the word, give me the English explanations/sentences from Famous Dictionaries, and the related Chinese explanation: %s, return your answer in Emacs Org mode format, heading starts from **, no need to tell me 'Sure'"
word))
(_ (format "Explain: %s directly, return your answer in Emacs Org Mode format, heading starts from **, no need to tell me 'Sure'"
word )))
:callback
(lambda (response info)
(if (not response)
(message "gptel-quick failed with message: %s" (plist-get info :status))
(paw-update-exp id response)
(message "%s" response))
(if callback
(funcall callback)))))
(defun paw-gptel-translate (word &optional prompt callback buffer section)
(let ((buffer (or buffer (current-buffer)))) ;; the button is pressed on current-buffer
(message "%s" prompt)
(gptel-request prompt
:callback
(lambda (response info)
(if (not response)
(message "paw-gptel-translate failed with message: %s" (plist-get info :status))
;; (message "%s" response)
)
(if callback
(funcall callback)
(with-current-buffer buffer
(save-excursion
(let* ((buffer-read-only nil)
(translation response))
;; (pp translation)
(goto-char (point-min))
(search-forward (format "** %s" (or section "Translation")) nil t)
(org-mark-subtree)
(goto-char (mark-marker))
;; (forward-line)
;; (delete-region (region-beginning) (region-end))
(paw-insert-and-make-overlay (concat translation "\n" ) 'face 'org-block)
(goto-char (point-min))
(search-forward "** Dictionaries" nil t)
(beginning-of-line)
;; (message "Translation completed")
;; (message "Translation completed %s" translation)
) )
(deactivate-mark))))) ))
(provide 'paw-gptel)