summaryrefslogtreecommitdiff
path: root/src/wasp-ai.el
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2024-03-26 23:34:28 -0400
committerLLLL Colonq <llll@colonq>2024-03-26 23:34:28 -0400
commit782c667e824d426b5443591afeefc37d0ae17785 (patch)
treeae5d232d598e2008bc2cadf32157a4d937b01951 /src/wasp-ai.el
parent8e9db9303fc5d72ddfdc9ab4a9adaa8299e6e21a (diff)
We streamed for 9 hours and (mostly) fixed everything.
Diffstat (limited to 'src/wasp-ai.el')
-rw-r--r--src/wasp-ai.el68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/wasp-ai.el b/src/wasp-ai.el
new file mode 100644
index 00000000..97a4c47e
--- /dev/null
+++ b/src/wasp-ai.el
@@ -0,0 +1,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