Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;-*- lexical-binding: t; -*-
- ;;
- ;; elisp interactive command that chats with gpt-3.5-turbo maintaining a history of interactions provided on
- ;; subsequent queries so that there is context and queries can refer to prior responses
- ;;
- ;; inspired by: https://pastebin.com/mEDv2zVN
- ;; refined from long discussion with ChatGPT-4
- ;;
- ;; comments? reddit: https://www.reddit.com/r/emacs/comments/11wv3fj/50_line_elisp_script_for_querying_chatgpt_35/
- ;;
- ;; pre-req: M-x package-list-packages install request.el
- (defvar openai-response-buffer (get-buffer-create "*openai-response*"))
- (defvar openai-api-key (getenv "OPENAI_API_KEY")) ;; sign up at: https://platform.openai.com/overview
- (defvar openai-messages `((("role" . "system") ("content" . "You are a helpful assistant."))))
- ;; to clear history, eval the next line:
- ;; (setq openai-messages `((("role" . "system") ("content" . "You are a helpful assistant."))))
- (defun openai-prepare-params (prompt)
- "Prepare the parameters for the OpenAI API request using the given prompt."
- `(("model" . "gpt-3.5-turbo")
- ("messages" . ,(vconcat (append openai-messages
- `((("role" . "user") ("content" . ,prompt))))))))
- (defun openai-send-request (url params headers)
- "Send a POST request to the given URL with the specified parameters and headers."
- (request
- url
- :timeout 60
- :type "POST"
- :data (json-encode params)
- :headers headers
- :parser 'json-read))
- (defun openai-handle-response (response prompt)
- "Handle the OpenAI API response by updating the conversation history and displaying the response."
- (let ((response-text (cdr (assoc 'content
- (cdr (assoc 'message
- (elt
- (cdr (assoc 'choices response)) 0)))))))
- (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")
- response-text)))
- (setq openai-messages (append openai-messages
- `((("role" . "user") ("content" . ,prompt))
- (("role" . "assistant") ("content" . ,response-text)))))))
- (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 (openai-prepare-params prompt))
- (headers `(("Content-Type" . "application/json")
- ("Authorization" . ,(concat "Bearer " openai-api-key)))))
- (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)))
- (openai-send-request
- url params headers)
- (request
- url
- :timeout 60
- :type "POST"
- :data (json-encode params)
- :headers headers
- :parser 'json-read
- :success (cl-function (lambda (&key data &allow-other-keys)
- (openai-handle-response data prompt)))
- :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))
- ;;;; test retained context
- ;;;; *openai-response* buffer:
- ;; >>> 2023-03-30 11:16:26 -- Prompt: pick two numbers
- ;; >>> 2023-03-30 11:16:28 -- Response: Sure! How about 7 and 13?
- ;; >>> 2023-03-30 11:16:37 -- Prompt: sum them
- ;; >>> 2023-03-30 11:16:38 -- Response: The sum of 7 and 13 is 20.
- ;; >>> 2023-03-30 11:18:43 -- Prompt: pick two more numbers
- ;; >>> 2023-03-30 11:18:45 -- Response: Alright! How about 3 and 8 this time?
- ;; >>> 2023-03-30 11:18:51 -- Prompt: sum them
- ;; >>> 2023-03-30 11:18:52 -- Response: The sum of 3 and 8 is 11.
- ;; >>> 2023-03-30 11:19:06 -- Prompt: how many questions have I asked so far
- ;; >>> 2023-03-30 11:19:07 -- Response: You have asked 4 questions so far.
- ;; >>> 2023-03-30 11:19:12 -- Prompt: how many questions have I asked so far
- ;; >>> 2023-03-30 11:19:13 -- Response: You have asked 5 questions so far.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement