Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8407d3

Browse files
authored
feat: Support ChatGPT (#12)
* feat: Support ChatGPT * Add doc * changelog * Add spinner
1 parent 07fbbc6 commit e8407d3

File tree

4 files changed

+103
-8
lines changed

4 files changed

+103
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
88
## 0.1.1 (Unreleased)
99
> Released N/A
1010
11-
* N/A
11+
* Add support for ChatGPT (#12)
1212

1313
## 0.1.0
1414
> Released Feb 05, 2023

Eask

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515

1616
(depends-on "emacs" "26.1")
1717
(depends-on "openai")
18+
(depends-on "spinner")
1819

1920
(setq network-security-level 'low) ; see https://github.com/jcs090218/setup-emacs-windows/issues/156#issuecomment-932956432

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,21 @@ List of supported commands,
8686
| `codegpt-explain` | Explain the selected code |
8787
| `codegpt-improve` | Improve, refactor or optimize it |
8888

89+
## 🌟 Using ChatGPT
90+
91+
The default is completing through the [Completions](https://platform.openai.com/docs/api-reference/completions)
92+
tunnel. If you want to use ChatGPT, do the following:
93+
94+
```elisp
95+
(setq codegpt-tunnel 'chat ; The default is 'completion
96+
codegpt-model "gpt-3.5-turbo") ; You can pick any model you want!
97+
```
98+
8999
## 📝 Customization
90100

91101
#### 🧪 Variables
92102

103+
- `codegpt-tunnel`- Completion channel you want to use. (Default: `completion`)
93104
- `codegpt-model` - ID of the model to use.
94105
- `codegpt-max-tokens` - The maximum number of tokens to generate in the completion.
95106
- `codegpt-temperature` - What sampling temperature to use.

codegpt.el

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; Maintainer: Shen, Jen-Chieh <[email protected]>
77
;; URL: https://github.com/emacs-openai/codegpt
88
;; Version: 0.1.0
9-
;; Package-Requires: ((emacs "26.1") (openai "0.1.0"))
9+
;; Package-Requires: ((emacs "26.1") (openai "0.1.0") (spinner "1.7.4"))
1010
;; Keywords: convenience codegpt
1111

1212
;; This file is not part of GNU Emacs.
@@ -31,8 +31,12 @@
3131

3232
;;; Code:
3333

34+
(require 'cl-lib)
35+
3436
(require 'openai)
37+
(require 'openai-chat)
3538
(require 'openai-completion)
39+
(require 'spinner)
3640

3741
(defgroup codegpt nil
3842
"Use GPT-3 tp help you write code."
@@ -58,6 +62,12 @@
5862
:type 'list
5963
:group 'codegpt)
6064

65+
(defcustom codegpt-tunnel 'completion
66+
"Tunnel to use for the tasks."
67+
:type '(choice (const :tag "Through Completion" completion)
68+
(const :tag "Through ChatGPT" chat))
69+
:group 'codegpt)
70+
6171
(defcustom codegpt-model "text-davinci-003"
6272
"ID of the model to use."
6373
:type 'string
@@ -73,6 +83,58 @@
7383
:type 'number
7484
:group 'openai)
7585

86+
(defcustom codegpt-spinner-type 'moon
87+
"The type of the spinner."
88+
:type '(choice (const :tag "Key to variable `spinner-types'" symbol)
89+
(const :tag "Vector of characters" vector))
90+
:group 'openai)
91+
92+
(defvar codegpt-requesting-p nil
93+
"Non-nil if sitll requesting.")
94+
95+
(defvar codegpt-spinner-counter 0
96+
"Spinner counter.")
97+
98+
(defvar codegpt-spinner-timer nil
99+
"Spinner timer.")
100+
101+
;;
102+
;;; Major Mode
103+
104+
(defun codegpt-header-line ()
105+
"Header line for CodeGPT."
106+
(format " %s[Tunnel] %s, [Model] %s"
107+
(if codegpt-requesting-p
108+
(let* ((spinner (if (symbolp codegpt-spinner-type)
109+
(cdr (assoc codegpt-spinner-type spinner-types))
110+
codegpt-spinner-type))
111+
(len (length spinner)))
112+
(when (<= len codegpt-spinner-counter)
113+
(setq codegpt-spinner-counter 0))
114+
(format "%s " (elt spinner codegpt-spinner-counter)))
115+
"")
116+
codegpt-tunnel codegpt-model))
117+
118+
(defun codegpt-mode--cancel-timer ()
119+
"Cancel spinner timer."
120+
(when (timerp codegpt-spinner-timer)
121+
(cancel-timer codegpt-spinner-timer)))
122+
123+
;;;###autoload
124+
(define-derived-mode codegpt-mode fundamental-mode "CodeGPT"
125+
"Major mode for `codegpt-mode'.
126+
127+
\\<codegpt-mode-map>"
128+
(setq codegpt-spinner-counter 0)
129+
(setq-local header-line-format `((:eval (codegpt-header-line))))
130+
(add-hook 'kill-buffer-hook #'codegpt-mode--cancel-timer nil t)
131+
(codegpt-mode--cancel-timer)
132+
(setq codegpt-spinner-timer (run-with-timer 0.1
133+
0.1
134+
(lambda ()
135+
(cl-incf codegpt-spinner-counter)
136+
(force-mode-line-update)))))
137+
76138
;;
77139
;;; Application
78140

@@ -82,6 +144,7 @@
82144
`(progn
83145
(openai--pop-to-buffer codegpt-buffer-name) ; create it
84146
(openai--with-buffer codegpt-buffer-name
147+
(codegpt-mode)
85148
(erase-buffer)
86149
(insert ,instruction "\n\n")
87150
,@body)))
@@ -102,19 +165,39 @@
102165
103166
The partial code is defined in with the region, and the START nad END are
104167
boundaries of that region in buffer."
168+
(setq codegpt-requesting-p t)
105169
(let ((text (string-trim (buffer-substring start end)))
106170
(original-window (selected-window)))
107171
(codegpt--ask-in-buffer instruction
108172
(insert text "\n\n")
109-
(openai-completion
110-
(buffer-string)
173+
(funcall
174+
(cl-case codegpt-tunnel
175+
(`completion #'openai-completion)
176+
(`chat #'openai-chat))
177+
(cl-case codegpt-tunnel
178+
(`completion (buffer-string))
179+
(`chat `[(("role" . "user")
180+
("content" . ,(buffer-string)))]))
111181
(lambda (data)
182+
(setq codegpt-requesting-p nil)
183+
(codegpt-mode--cancel-timer)
112184
(openai--with-buffer codegpt-buffer-name
113185
(openai--pop-to-buffer codegpt-buffer-name)
114-
(let* ((choices (openai--data-choices data))
115-
(result (openai--get-choice choices))
116-
(original-point (point)))
117-
(insert (string-trim result) "\n")
186+
(let ((original-point (point)))
187+
(cl-case codegpt-tunnel
188+
(`completion
189+
(let* ((choices (openai--data-choices data))
190+
(result (openai--get-choice choices)))
191+
(insert (string-trim result) "\n")))
192+
(`chat
193+
(let ((choices (let-alist data .choices))
194+
(result))
195+
(mapc (lambda (choice)
196+
(let-alist choice
197+
(let-alist .message
198+
(setq result (string-trim .content)))))
199+
choices)
200+
(insert (string-trim result) "\n"))))
118201
(codegpt--fill-region original-point (point))))
119202
(unless codegpt-focus-p
120203
(select-window original-window)))

0 commit comments

Comments
 (0)