summaryrefslogtreecommitdiff
path: root/src/wasp-ai.el
blob: 97a4c47e3cd40c29dff54e4f466887e7627ce90e (plain)
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
;;; wasp-ai --- AI interaction -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:

(require 's)
(require 'dash)
(require 'wasp-utils)

(defcustom w/ai-process "wasp-ai"
  "Name of process connected to ChatGPT."
  :type '(string)
  :group 'wasp)

(defcustom w/ai-buffer " *wasp-ai*"
  "Name of buffer used to store ChatGPT output."
  :type '(string)
  :group 'wasp)

(defcustom w/ai-error-buffer " *wasp-ai-error*"
  "Name of buffer used to store ChatGPT errors."
  :type '(string)
  :group 'wasp)

(defvar-local w/ai-callback nil)
(defun w/ai (question k &optional systemprompt user assistant)
  "Ai QUESTION to ChatGPT and pass the answer to K.
Optionally use SYSTEMPROMPT and the USER and ASSISTANT prompts."
  (let ((tmpfile (make-temp-file "wasp-ai"))
        (tmpfilesystem (make-temp-file "wasp-ai-system"))
        (tmpfileuser (make-temp-file "wasp-ai-user"))
        (tmpfileassistant (make-temp-file "wasp-ai-assistant"))
        (buf (generate-new-buffer w/ai-buffer)))
    (with-temp-file tmpfile (insert question))
    (when systemprompt
      (with-temp-file tmpfilesystem (insert systemprompt)))
    (when user
      (with-temp-file tmpfileuser
        (if (stringp user)
            (insert (s-concat user "\n"))
          (--each user
            (insert (s-concat it "\n"))))))
    (when assistant
      (with-temp-file tmpfileassistant
        (if (stringp assistant)
            (insert (s-concat assistant "\n"))
          (--each assistant
            (insert (s-concat it "\n"))))))
    (with-current-buffer buf
      (setq-local w/ai-callback k)
      (erase-buffer))
    (make-process
     :name w/ai-process
     :buffer buf
     :command
     (list
      "chatgpt"
      tmpfile
      (if systemprompt tmpfilesystem "systemprompt.txt")
      (if user tmpfileuser "userprompt.txt")
      (if assistant tmpfileassistant "assistantprompt.txt"))
     :stderr (get-buffer-create w/ai-error-buffer)
     :sentinel
     (lambda (_ _)
       (with-current-buffer buf
         (funcall w/ai-callback (s-trim (buffer-string))))))))

(provide 'wasp-ai)
;;; wasp-ai.el ends here