Skip to content

Commit

Permalink
feat(ai): tweak chatgpt and gptel setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jimehk committed Jul 12, 2024
1 parent d0ef255 commit de9fee4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
11 changes: 7 additions & 4 deletions modules/ai/siren-chatgpt.el
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ auth-source. If LOGIN is non-nil, use that value to retrieve the"
(let ((login (siren-chatgpt--login-user)))
(or (and siren-chatgpt--api-key-login
(string= siren-chatgpt--api-key-login login))
(let ((api-key (auth-source-pick-first-password :host "openai.com"
(let ((api-key (auth-source-pick-first-password :host "api.openai.com"
:user login)))
(if api-key
(progn
Expand Down Expand Up @@ -57,10 +57,13 @@ their model settings are kept in sync with `siren-chatgpt-model'.")
"gpt-3.5-turbo-16k"
"gpt-4"
"gpt-4-32k"
"gpt-4-turbo-preview")
"gpt-4-turbo"
"gpt-4-turbo-preview"
"gpt-4o"
"gpt-4o-2024-05-13")
"List of supported models.")

(defcustom siren-chatgpt-model "gpt-4-turbo-preview"
(defcustom siren-chatgpt-model "gpt-4o"
"The model to use for chatgpt."
:type '(choice (mapcar (lambda (model) (list 'const model))
siren-chatgpt-models))
Expand All @@ -77,7 +80,7 @@ their model settings are kept in sync with `siren-chatgpt-model'.")
Used to allow different API keys for different models."
(cond ;; ((string-prefix-p "gpt-4" siren-chatgpt-model) "gpt-4")
(t "default")))
(t "apikey")))

(defun siren-chatgpt-select-model ()
"Select a model to use for chatgpt."
Expand Down
43 changes: 43 additions & 0 deletions modules/ai/siren-gptel.el
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,53 @@
(require 'siren-chatgpt)

(use-package gptel
:hook
(gptel-mode . siren-gptel-setup-auto-save)

:general
("C-c C-q" 'siren-gptel)
(:keymaps 'gptel-mode-map
"C-c C-<return>" 'gptel-send)

:preface
(defgroup siren-gptel nil
"jimeh's Emacs Siren: gptel configuration."

:group 'gptel)

(defcustom siren-gptel-history-dir (expand-file-name "gptel-history" user-emacs-directory)
"Directory to save GPTel chat sessions upon buffer close."
:type 'string
:group 'siren-gptel)

(defun siren-gptel--generate-filename ()
"Generate a filename with the buffer creation date, time."
(let* ((time (or (buffer-local-value 'siren-gptel-buffer-create-time (current-buffer))
(current-time)))
(timestamp (format-time-string "%Y%m%d_%H%M%S" time)))
(concat (file-name-as-directory siren-gptel-history-dir) "gptel_" timestamp ".org")))

(defun siren-gptel-save-session ()
"Save the current GPTel chat session to a file."
(interactive)
(when (bound-and-true-p gptel-mode)
(let* ((filename (siren-gptel--generate-filename))
(dirname (file-name-directory filename))
(content (buffer-string)))
(unless (file-directory-p dirname)
(make-directory dirname t))
(with-temp-buffer
(insert content)
(write-file filename))
(message "Session saved to %s" filename))))

(defun siren-gptel-setup-auto-save ()
"Setup auto-save for GPTel chat sessions."
(unless (and buffer-file-name
(string-prefix-p siren-gptel-history-dir (file-name-directory buffer-file-name)))
(setq-local siren-gptel-buffer-create-time (current-time))
(add-hook 'kill-buffer-hook 'siren-gptel-save-session nil t)))

(defun siren-gptel ()
"Interactively call gptel, or gptel-send with prefix if region is active."
(interactive)
Expand Down

0 comments on commit de9fee4

Please sign in to comment.