How to show AI response and speak it out with AI sound engine in Emacs?

If you’re using paw.el, the language learning and annotation tool for Emacs, integrating AI responses into your workflow becomes seamless. By using gptel, you can easily display AI-generated responses in the *paw-view-note* buffer with functions like paw-view-note and paw-new-entry. Additionally, you can use Edge-TTS to vocalize the response, employ go-translate for translation, and further interact with or add the information to your database right from the *paw-view-note* buffer.

 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
(defun gptel-quick (&optional query)
  "ASK AI with predefined prompts, show it in paw-view-note buffer, and say it out!"
  (interactive)
  (require 'gptel)
  (let* ((selected-text (when (use-region-p)
                          (buffer-substring-no-properties (region-beginning) (region-end))))
         ;; (current-buffer-text (buffer-substring-no-properties (point-min) (point-max)))
         (additional-text (or selected-text ""))
         (prompt (completing-read "Ask AI: "
                                  '("Draft an outline"
                                    "Draft anything"
                                    "Draft an email"
                                    "Draft a journal entry"
                                    "Draft a meeting agenda"
                                    "Explain in 12 words or less"
                                    "Explain in 48 words or less"
                                    "Explain in 100 words or less"
                                    "Explain in 200 words or less"
                                    "Write anything"
                                    "Brainstorm ideas"
                                    "Translate it to Chinese"))))
    (when (string= prompt "") (user-error "A prompt is required."))
    (deactivate-mark)
    (setq gptel-last-prompt (format "%s. %s" prompt additional-text))
    (gptel-request (or query gptel-last-prompt)
      :system "You are an AI assistant that lives inside Emacs"
      :callback
      (lambda (response info)
        (if (not response)
            (message "gptel-quick failed with message: %s" (plist-get info :status))
          (with-current-buffer (get-buffer-create "*gptel-quick*")
            (let ((paw-say-word-p t) ;; say the response out
                  (lang (paw-check-language response)))
              (paw-view-note (paw-new-entry response
                                            :origin_type "gptel"
                                            :serverp 3
                                            :lang lang
                                            :context (format "Question: %s\nAnswer: %s" gptel-last-prompt response))
                             :buffer-name paw-view-note-buffer-name
                             :display-func 'switch-to-buffer))))))))
updatedupdated2025-01-302025-01-30