Browse Source

Tiny elisp script that can be used as a ChatGPT interface

Simon Watson 1 year ago
parent
commit
de8a4cb5a6
3 changed files with 52 additions and 2 deletions
  1. 4 0
      bashrc/bashrc-all
  2. 4 2
      emacs/.emacs-all
  3. 44 0
      emacs/elisp/openai.el

+ 4 - 0
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

+ 4 - 2
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)

+ 44 - 0
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))