(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
(when (not (get-buffer-window (buffer-name openai-response-buffer)))
(let ((window (split-window)))
(set-window-buffer window 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))