de8a4cb5a6995a2453e9f4b97fcdbe481173966b
commit de8a4cb5a6995a2453e9f4b97fcdbe481173966b
Author: Simon Watson <spw01@protonmail.com>
Date: Mon Mar 20 14:58:16 2023 -0400

Tiny elisp script that can be used as a ChatGPT interface

diff --git a/bashrc/bashrc-all b/bashrc/bashrc-all
index b6a7265..7af9937 100644
--- a/bashrc/bashrc-all
+++ b/bashrc/bashrc-all
@@ -168,6 +168,10 @@ fi

alias spesktv='mpv http://chate.io:55555'

+if [ -f "$HOME/.openai-key" ]; then
+ source "$HOME/.openai-key"
+fi
+
# Case defines platform specific configs
# Platform agnostic configs above
case $(cat /etc/hostname) in
diff --git a/emacs/.emacs-all b/emacs/.emacs-all
index 5421ee8..22c1944 100644
--- a/emacs/.emacs-all
+++ b/emacs/.emacs-all
@@ -260,8 +260,6 @@
;;; https://christiantietze.de/posts/2020/12/emacs-scroll-performance-projectile/
(setq mouse-wheel-progressive-speed t)

-
-
;; whitespace config
;; make whitespace-mode use just basic coloring
(setq whitespace-style '(face tabs))
@@ -367,6 +365,10 @@
(global-set-key (kbd "C-c C-P") 'post-region-to-pb)
(global-set-key (kbd "C-c 2 p") 'display-pb)

+;; OpenAI script
+(load "~/Repos/dotfiles/emacs/elisp/openai.el")
+(global-set-key (kbd "C-c q") 'ask-gpt)
+
;; Disable bars
;; (menu-bar-mode -1)
(tool-bar-mode -1)
diff --git a/emacs/elisp/openai.el b/emacs/elisp/openai.el
new file mode 100644
index 0000000..bf1e779
--- /dev/null
+++ b/emacs/elisp/openai.el
@@ -0,0 +1,44 @@
+(straight-use-package 'request)
+(defvar openai-response-buffer (get-buffer-create "*openai-response*"))
+(defvar openai-api-key (getenv "OPENAI_API_KEY"))
+
+(defun openai-api-request (prompt)
+ "Send a request to the OpenAI API with the given prompt and return the response."
+ (let* ((url "https://api.openai.com/v1/chat/completions")
+ (params `(("model" . "gpt-3.5-turbo")
+ ("messages" . ,(vector (list (cons "role" "user") (cons "content" prompt))))))
+ (headers `(("Content-Type" . "application/json")
+ ("Authorization" . ,(concat "Bearer " openai-api-key))))
+ (response-json nil))
+ (with-current-buffer openai-response-buffer
+ (goto-char (point-max))
+ (insert "\n\n")
+ (insert (format ">>> %s -- Prompt: %s\n" (format-time-string "%Y-%m-%d %H:%M:%S") prompt)))
+ (request
+ url
+ ;; :sync t
+ :timeout 60
+ :type "POST"
+ :data (json-encode params)
+ :headers headers
+ :parser 'json-read
+ :complete (cl-function (lambda (&key data &allow-other-keys)
+ (with-current-buffer openai-response-buffer
+ (goto-char (point-max))
+ (insert (format ">>> %s -- Response: %s"
+ (format-time-string "%Y-%m-%d %H:%M:%S")
+ (cdr (assoc 'content
+ (cdr (assoc 'message
+ (elt
+ (cdr (assoc 'choices data)) 0))))))))))
+ :success (cl-function (lambda (&key data &allow-other-keys)
+ (setq response-json data)))
+ :error (cl-function (lambda (&key error-thrown data &allow-other-keys)
+ (with-current-buffer openai-response-buffer
+ (goto-char (point-max))
+ (insert "\n")
+ (insert (format "Response Error: %S \n %s \n" error-thrown data))))))))
+
+(defun ask-gpt (prompt)
+ (interactive "sEnter a prompt: ")
+ (openai-api-request prompt))