From 05c345c09e00dede4848b072f8c8f6606e622554 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Tue, 20 Jun 2023 10:11:33 -0400 Subject: [PATCH] xe-chatgpt: add ability to ask chatgpt something about a given code snippet Signed-off-by: Xe Iaso --- .../home-manager/emacs/packages/xe-chatgpt.el | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/common/home-manager/emacs/packages/xe-chatgpt.el b/common/home-manager/emacs/packages/xe-chatgpt.el index 455e93b..fce4c3f 100644 --- a/common/home-manager/emacs/packages/xe-chatgpt.el +++ b/common/home-manager/emacs/packages/xe-chatgpt.el @@ -17,6 +17,11 @@ "The default system message for ChatGPT." :type 'string) +(defcustom xe/chatgpt-model + "gpt-3.5-turbo" + "The model to use when querying 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." @@ -43,7 +48,7 @@ The buffer is set to markdown-mode. Return the buffer." 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") + (let* ((req `(("model" . ,xe/chatgpt-model) ("messages" . ((("role" . "system") ("content" . ,xe/chatgpt-base-prompt)) (("role" . "user") ("content" . ,question)))))) (auth-key (xe/chatgpt--read-file @@ -86,5 +91,15 @@ Inserts the result text of the first response to the a scratch buffer." (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"))) +(defun xe/chatgpt-answer-question-about-code (beginning end question) + "Ask ChatGPT to answer a QUESTION about this region of code from BEGINNING to END." + (interactive "r\nsquestion> ") + (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 "%s I'm in %s.\n\n```%s\n%s```" question editor-mode (car mode-sp) code))) + (xe/chatgpt--make-request prompt "explain"))) + (provide 'xe-chatgpt) ;;; xe-chatgpt.el ends here