give xe-chatgpt more powers
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
parent
e78a5032dc
commit
b1664eab8e
|
@ -17,6 +17,14 @@
|
|||
"The default system message for ChatGPT."
|
||||
:type 'string)
|
||||
|
||||
(defun xe/chatgpt--create-answer-buffer (suffix)
|
||||
"Create a new scratch buffer with name SUFFIX and switch to it.
|
||||
The buffer is set to markdown-mode. Return the buffer."
|
||||
(let ((bufname (generate-new-buffer-name (format "*xe-chatgpt-%s*" suffix))))
|
||||
(switch-to-buffer (get-buffer-create bufname))
|
||||
(markdown-mode)
|
||||
(get-buffer bufname)))
|
||||
|
||||
(defun xe/chatgpt--chomp (str)
|
||||
"Chomp leading and tailing whitespace from STR."
|
||||
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'"
|
||||
|
@ -30,8 +38,11 @@
|
|||
(insert-file-contents fname)
|
||||
(xe/chatgpt--chomp (buffer-string))))
|
||||
|
||||
(defun xe/chatgpt--make-request (question)
|
||||
"Internal function to ask ChatGPT a QUESTION and insert the result text of the first response to the current buffer."
|
||||
(defun xe/chatgpt--make-request (question mode)
|
||||
"Internal function to ask ChatGPT a QUESTION in MODE mode.
|
||||
Inserts the result text of the first response to the a scratch buffer."
|
||||
(xe/chatgpt--create-answer-buffer mode)
|
||||
(insert question)
|
||||
(let* ((req `(("model" . "gpt-3.5-turbo")
|
||||
("messages" . ((("role" . "system") ("content" . ,xe/chatgpt-base-prompt))
|
||||
(("role" . "user") ("content" . ,question))))))
|
||||
|
@ -48,26 +59,32 @@
|
|||
:encoding 'utf-8
|
||||
:success (cl-function
|
||||
(lambda (&key data &allow-other-keys)
|
||||
(message "%S" data)
|
||||
(let* ((choice (aref (alist-get 'choices data) 0))
|
||||
(message (alist-get 'message choice))
|
||||
(content (alist-get 'content message)))
|
||||
(message "ChatGPT reply: %s" content)
|
||||
(insert content)))))))
|
||||
(insert (xe/chatgpt--chomp content))))))))
|
||||
|
||||
(defun xe/chatgpt-ask (question)
|
||||
(defun xe/ask-chatgpt (question)
|
||||
"Ask ChatGPT a QUESTION and get the response put into your current buffer."
|
||||
(interactive "squestion> ")
|
||||
(message "ChatGPT ask: %s" question)
|
||||
(xe/chatgpt--make-request prompt))
|
||||
(xe/chatgpt--make-request question "detail"))
|
||||
|
||||
(defun xe/chatgpt-ask-with-mode (question)
|
||||
(defun xe/ask-chatgpt-with-mode (question)
|
||||
"Ask ChatGPT a QUESTION and get the response put into your current buffer. This will add the context of what editor major mode you are in."
|
||||
(interactive "squestion> ")
|
||||
(message "ChatGPT ask: %s" question)
|
||||
(let* ((editor-mode (string-join (split-string (symbol-name major-mode) "-") " "))
|
||||
(prompt (format "%s\nUser is in %s. Only include the code." question editor-mode)))
|
||||
(xe/chatgpt--make-request prompt)))
|
||||
(xe/chatgpt--make-request prompt "quick")))
|
||||
|
||||
(defun xe/chatgpt-explain (beginning end)
|
||||
"Ask ChatGPT to explain this region of code from BEGINNING to END."
|
||||
(interactive "r")
|
||||
(let* ((code (buffer-substring-no-properties (region-beginning) (region-end)))
|
||||
(mode-sp (split-string (symbol-name major-mode) "-"))
|
||||
(editor-mode (string-join (split-string (symbol-name major-mode) "-") " "))
|
||||
(prompt
|
||||
(format "Explain this code. User is in %s.\n\n```%s\n%s```\n\n" editor-mode (car mode-sp) code)))
|
||||
(xe/chatgpt--make-request prompt "explain")))
|
||||
|
||||
(provide 'xe-chatgpt)
|
||||
;;; xe-chatgpt.el ends here
|
||||
|
|
Loading…
Reference in New Issue