forked from joshcho/ChatGPT.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt.el
706 lines (606 loc) · 28.4 KB
/
chatgpt.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
;;; ;; -- lexical-binding: t --
;;; chatgpt.el --- ChatGPT in Emacs
;; Copyright (C) 2011 Free Software Foundation, Inc.
;; Author: Jungmin "Josh" Cho <[email protected]>
;; Version: 0.1
;; Package-Requires: ((epc "0.1.1") (deferred "0.5.1"))
;; Keywords: ai
;; URL: https://github.com/joshcho/ChatGPT.el
;;; Commentary:
;; This package provides an interactive interface with ChatGPT API.
(require 'epc)
(require 'deferred)
(require 'python)
(require 'cl)
(require 'org-id)
;;; Code:
(defgroup chatgpt nil
"Configuration for chatgpt."
:prefix "chatgpt-"
:group 'ai)
(defcustom chatgpt-query-format-string-map
'(("doc" . "Please write the documentation for the following function.\n\n%s")
("bug" . "There is a bug in the following function, please help me fix it.\n\n%s")
("understand" . "What is the following?\n\n%s")
("improve" . "Please improve the following.\n\n%s"))
"An association list that maps query types to their corresponding format strings."
:type '(alist :key-type (string :tag "Query Type")
:value-type (string :tag "Format String"))
:group 'chatgpt)
(defcustom chatgpt-enable-loading-ellipsis t
"Whether the ellipsis animation is displayed in *ChatGPT*."
:type 'boolean
:group 'chatgpt)
(defcustom chatgpt-display-on-query t
"Whether *ChatGPT* is displayed when a query is sent."
:type 'boolean
:group 'chatgpt)
(defcustom chatgpt-display-on-response t
"Whether *ChatGPT* is displayed when a response is received."
:type 'boolean
:group 'chatgpt)
(defcustom chatgpt-repo-path nil
"The path of ChatGPT.el repository."
:type 'string
:group 'chatgpt)
(defcustom chatgpt-ai-choices
'("perplexity" "gpt4o" "lispgpt" "pythongpt" "qwen25" "claude" "dsr1")
"List of AI choices available for selection."
:type '(repeat string)
:group 'chatgpt)
(defcustom chatgpt-second-preferest-model "perplexity"
"Chatgpt buffer name"
:type 'string
:group 'chatgpt)
(defvar chatgpt-process nil
"The ChatGPT process.")
(defcustom chatgpt-buffer-name "*ChatGPT*"
"Chatgpt buffer name"
:type 'string
:group 'chatgpt)
(defcustom chatgpt-default-model "claude"
"The model to use for ChatGPT."
:type 'string
:group 'chatgpt)
;;;###autoload
(defun chatgpt-login ()
"Log in to ChatGPT."
(interactive)
(shell-command "chatgpt install &"))
;; TODO: save chatgpt conversation to file
(defcustom chatgpt-record-path nil
"The path of ChatGPT.el repository."
:type 'string
:group 'chatgpt)
(defun chatgpt-save-file-name ()
"Return the current workspace name."
(interactive)
(let* ((now (current-time))
(year (format-time-string "%Y" now))
(week (format-time-string "%U" now)))
(concat year "_week" week (if persp-mode
(safe-persp-name (get-current-persp))
"default"))))
(defun chatgpt-get-output-buffer-name ()
(or (and chatgpt-record-path (buffer-name
(find-file-noselect (format "%s/gptchat_record_%s.txt"
chatgpt-record-path
(chatgpt-save-file-name)))))
(with-current-buffer (get-buffer-create chatgpt-buffer-name)
(setq-local truncate-lines nil))
(get-buffer-create chatgpt-buffer-name)))
(defvar chatgpt-finish-response-hook nil)
(defvar chatgpt-check-running-flag nil "The flag is utilized to denote whether we are within the interval of running the check.")
(defvar chatgpt-running-flag nil "Flag to indicate whether chatgpt is running.")
;;;###autoload
(defun chatgpt-init ()
"Initialize the ChatGPT server.
This function creates the ChatGPT process and starts it. It also
initializes the ChatGPT buffer, enabling visual line mode in it. A
message is displayed to indicate that the initialization was
successful.
If ChatGPT server is not initialized, 'chatgpt-query' calls this
function."
(interactive)
(let ((default-directory chatgpt-repo-path))
(when (equal (shell-command-to-string "pip list | grep revChatGPT") "")
(shell-command "pip install revChatGPT")
(message "revChatGPT installed through pip.")
(chatgpt-login))
(when (null chatgpt-repo-path)
(error "chatgpt-repo-path is nil. Please set chatgpt-repo-path as specified in joshcho/ChatGPT.el"))
(setq chatgpt-process (epc:start-epc python-interpreter (list (expand-file-name
(format "%schatgpt.py"
chatgpt-repo-path)))))
(with-current-buffer (chatgpt-get-output-buffer-name)
(visual-line-mode 1)
(markdown-mode))
(message "ChatGPT initialized.")))
(defun chatgpt-query-epc-port ()
"Query the EPC server port for the ChatGPT process.
This function retrieves and displays the port number of the running EPC server."
(interactive)
(if (and chatgpt-process (epc:manager-p chatgpt-process))
(let ((port (epc:manager-port chatgpt-process)))
(message "ChatGPT EPC server is running on port: %d" port)
port)
(error "ChatGPT EPC server is not running.")))
(defvar chatgpt-wait-timers (make-hash-table)
"Timers to update the waiting message in the ChatGPT buffer.")
;;;###autoload
(defun chatgpt-stop ()
"Stops the ChatGPT server."
(interactive)
(dolist (id (hash-table-keys chatgpt-wait-timers))
(chatgpt--stop-wait id))
(epc:stop-epc chatgpt-process)
(setq chatgpt-process nil)
(message "Stop ChatGPT process."))
;;;###autoload
(defun chatgpt-reset ()
"Reset the ChatGPT server. The same session is maintained."
(interactive)
(chatgpt-stop)
(chatgpt-init))
;;;###autoload
(defun chatgpt-display (&optional output-buffer)
"Displays *ChatGPT*."
(interactive)
(unless output-buffer
(setq output-buffer (chatgpt-get-output-buffer-name))
(display-buffer output-buffer)) ; 创建或获取缓冲区
; 显示缓冲区
(when-let ((saved-win (get-buffer-window (current-buffer)))
(win (get-buffer-window output-buffer)))
(unless (equal (current-buffer) output-buffer)
(select-window win)
(if (not (eq major-mode 'markdown-mode))
(markdown-mode))
(goto-char (point-max))
(unless (pos-visible-in-window-p (point-max) win)
(goto-char (point-max))
(recenter -1))
(select-window saved-win)))
(get-buffer output-buffer))
(defun chatgpt--clear-line ()
"Clear line in *ChatGPT*."
(cl-assert (equal (current-buffer) (get-buffer (chatgpt-get-output-buffer-name))))
(delete-region (save-excursion (beginning-of-line)
(point))
(save-excursion (end-of-line)
(point))))
(defun chatgpt--identifier-string (id)
"Identifier string corresponding to ID."
(format "cg?[%s]" id))
(defun chatgpt--regex-string (id)
"Regex corresponding to ID."
(format "cg\\?\\[%s\\]" id))
(defun chatgpt--goto-identifier (id &optional output-buffer)
"Go to response of ID."
(unless output-buffer
(setq output-buffer (get-buffer (chatgpt-get-output-buffer-name))))
(cl-assert (equal (current-buffer) output-buffer))
(goto-char (point-max))
(re-search-backward (chatgpt--regex-string id))
(forward-line))
;; (defun chatgpt--goto-identifier (id)
;; "Go to response of ID."
;; (cl-assert (equal (current-buffer) (get-buffer (chatgpt-get-output-buffer-name))))
;; (goto-char (point-max))
;; (let ((regex (chatgpt--regex-string id)))
;; (message "Searching for regex: %s" regex)
;; (if (re-search-backward regex nil t) ; 添加 't' 参数,避免在没有匹配到时抛出错误
;; (progn
;; (message "Found identifier for ID: %d" id)
;; (forward-line))
;; (message "Identifier not found for ID: %d" id))))
(defun chatgpt-get-buffer-width-by-char (char)
"Return the width of the currently focused window in terms of the number of CHAR characters that can fit in the window."
(let* ((c-width (if (eq char ?=)
2
1))
(window (selected-window))
(line-num-width (if (and (boundp 'display-line-numbers)
(not (eq display-line-numbers nil)))
(length (format-mode-line "%l")) 0)))
(- (/ (- (window-text-width window) (* line-num-width 2)) 1) (+ c-width 2))))
(defun chatgpt--insert-query (query id model-name &optional output-buffer)
"Insert QUERY with ID into *ChatGPT*."
(unless output-buffer
(setq output-buffer (chatgpt-get-output-buffer-name))) ; 创建或获取缓冲区
(with-current-buffer output-buffer
(save-excursion
(goto-char (point-max))
(with-selected-window (get-buffer-window output-buffer)
(recenter 0))
(let ((inhibit-read-only t))
(setq chatgpt-last-response (point))
(insert (format "%s %s >>> %s\n%s\n%s\n%s"
(if (= (point-min) (point))
"\n"
"\n\n")
model-name
(propertize query 'face 'bold)
(make-string (chatgpt-get-buffer-width-by-char ?-) ?-)
(propertize
(chatgpt--identifier-string id)
'invisible t)
(if chatgpt-enable-loading-ellipsis
""
(concat "Waiting for ChatGPT..."))))))))
(defun chatgpt--insert-response (response id &optional output-buffer)
"Insert RESPONSE into *ChatGPT* for ID."
(unless output-buffer
(setq output-buffer (chatgpt-get-output-buffer-name))) ; 创建或获取缓冲区
(with-current-buffer output-buffer
(save-excursion
(chatgpt--goto-identifier id)
(chatgpt--clear-line)
(insert response)
(progn
(insert (format "\n\n%s"
(make-string (chatgpt-get-buffer-width-by-char ?=) ?=)))))))
(defun chatgpt--insert-error (error-msg id &optional output-buffer)
"Insert ERROR-MSG into *ChatGPT* for ID."
(unless output-buffer
(setq output-buffer (chatgpt-get-output-buffer-name))) ; 创建或获取缓冲区
(with-current-buffer output-buffer
(save-excursion
(chatgpt--goto-identifier id)
(chatgpt--clear-line)
(insert (format "ERROR: %s" error-msg)))))
(defun chatgpt--add-timer (id)
"Add timer for ID to 'chatgpt-wait-timers'."
(cl-assert (null (gethash id chatgpt-wait-timers)))
(puthash id
(run-with-timer 0.5 0.5
(eval
`(lambda ()
(with-current-buffer (chatgpt-get-output-buffer-name)
(save-excursion
(chatgpt--goto-identifier ,id)
(let ((line (thing-at-point 'line)))
(when (>= (+ (length line)
(if (eq (substring line -1) "\n")
0
1))
4)
(chatgpt--clear-line))
(insert ".")))))))
chatgpt-wait-timers))
(defun chatgpt--stop-wait (id)
"Stop waiting for a response from ID."
(when-let (timer (gethash id chatgpt-wait-timers))
(cancel-timer timer)
(remhash id chatgpt-wait-timers)))
(defvar chatgpt-id 0
"Tracks responses in the background.")
(defvar chatgpt-last-query nil)
(defvar chatgpt-last-response (make-marker) "Global variable to store the last response marker")
(defvar chatgpt-last-use-buffer nil)
(defvar chatgpt-last-use-model nil)
(defun chatgpt--query (query use-model)
"Send QUERY to the ChatGPT process.
The query is inserted into the *ChatGPT* buffer with bold text,
and the response from the ChatGPT process is appended to the
buffer. If `chatgpt-enable-loading-ellipsis' is non-nil, a loading
ellipsis is displayed in the buffer while waiting for the
response.
This function is intended to be called internally by the
`chatgpt-query' function, and should not be called directly by
users."
(unless chatgpt-process
(chatgpt-init))
;; support invisible invoke
;; but this would report void variable use-buffer-name
(let ((use-buffer-name (chatgpt-display)))
;; (chatgpt-last-use-buffer use-buffer-name)
;; (saved-id (cl-incf chatgpt-id)))
(setq chatgpt-last-use-model use-model)
(setq chatgpt-last-use-buffer use-buffer-name)
(setq chatgpt-last-query query)
(setq saved-id (cl-incf chatgpt-id))
(chatgpt--insert-query query saved-id use-model use-buffer-name)
(when chatgpt-enable-loading-ellipsis
(chatgpt--add-timer saved-id))
(deferred:$
(deferred:$
(epc:call-deferred chatgpt-process 'query (list query use-model (buffer-name chatgpt-last-use-buffer)))
(eval `(deferred:nextc it
(lambda (response)
(chatgpt--stop-wait ,saved-id)
(chatgpt--insert-response response ,saved-id chatgpt-last-use-buffer)
(when chatgpt-display-on-response
(chatgpt-display chatgpt-last-use-buffer)
(and chatgpt-finish-response-hook (run-hooks 'chatgpt-finish-response-hook)))
(with-current-buffer chatgpt-last-use-buffer
(goto-char (point-max))
(let ((output-window (get-buffer-window (current-buffer))))
(when output-window
(with-selected-window output-window
(goto-char (point-max))
(unless (>= (window-end output-window) (point-max))
(recenter -1)
(save-buffer))))))))))
(eval
`(deferred:error it
(lambda (err)
(chatgpt--stop-wait ,saved-id)
(string-match "\"Error('\\(.*\\)')\"" (error-message-string err))
(let ((error-str (match-string 1 (error-message-string err))))
(chatgpt--insert-error error-str
,saved-id
chatgpt-last-use-buffer)
;; (when (yes-or-no-p (format "Error encountered. Reset chatgpt (If reset doesn't work, try \"\"pkill ms-playwright/firefox\"\" in the shell then reset)?" error-str))
;; (chatgpt-reset))
(with-current-buffer chatgpt-last-use-buffer
(goto-char (point-max))
(let ((output-window (get-buffer-window (current-buffer))))
(when output-window
(with-selected-window output-window
(goto-char (point-max))
(unless (>= (window-end output-window) (point-max))
(recenter -1)
(save-buffer)))))))))))))
(defun chatgpt--query-by-type (query query-type use-model)
"Query ChatGPT with a given QUERY and QUERY-TYPE.
QUERY is the text to be passed to ChatGPT.
QUERY-TYPE is the type of query, which determines the format string
used to generate the final query sent to ChatGPT. The format string
is looked up in 'chatgpt-query-format-string-map', which is an
alist of QUERY-TYPE and format string pairs. If no matching
format string is found, an error is raised.
The format string is expected to contain a %s placeholder, which
will be replaced with QUERY. The resulting string is then passed
to ChatGPT.
For example, if QUERY is \"(defun square (x) (* x x))\" and
QUERY-TYPE is \"doc\", the final query sent to ChatGPT would be
\"Please write the documentation for the following function.
\(defun square (x) (* x x))\""
(if (equal query-type "custom")
(chatgpt--query
(format "%s\n\n%s" (read-from-minibuffer "ChatGPT Custom Prompt: ") query) use-model)
(if-let (format-string (cdr (assoc query-type chatgpt-query-format-string-map)))
(chatgpt--query
(format format-string query) use-model)
(error "No format string associated with 'query-type' %s. Please customize 'chatgpt-query-format-string-map'" query-type))))
;;;###autoload
(defun chatgpt-reask ()
;; switch chatgpt-use-model between gpt35 and gpt4
;; but with save conversation history
(interactive)
(setq chatgpt-check-running-flag t)
(message chatgpt-last-use-model)
(message (buffer-name chatgpt-last-use-buffer))
;; wait until chatgpt-check-running-flag is nil
(if chatgpt-running-flag
(progn
(message "ChatGPT is running. Please wait until it finishes.")
(while chatgpt-check-running-flag
(sleep-for 0.1))))
(setq chatgpt-check-running-flag nil)
(progn
(with-current-buffer chatgpt-last-use-buffer
(save-excursion
(goto-char chatgpt-last-response)
(message "loaded chatgpt-last-response: %s" chatgpt-last-response)
;; (delete-region chatgpt-last-response (point-max)))))
(delete-region (point) (point-max)))))
(if (string= chatgpt-last-use-model "perplexity")
(chatgpt--query-stream chatgpt-last-query "gpt4o" nil chatgpt-last-use-buffer t)
(chatgpt--query-stream chatgpt-last-query "perplexity" nil chatgpt-last-use-buffer t)))
;;;###autoload
(defun chatgpt-again ()
;; switch chatgpt-use-model between gpt35 and gpt4
;; but with save conversation history
(interactive)
(setq chatgpt-check-running-flag t)
(message chatgpt-last-use-model)
(message (buffer-name chatgpt-last-use-buffer))
;; wait until chatgpt-check-running-flag is nil
(if chatgpt-running-flag
(progn
(message "ChatGPT is running. Please wait until it finishes.")
(while chatgpt-check-running-flag
(sleep-for 0.1))))
(setq chatgpt-check-running-flag nil)
(progn
(with-current-buffer chatgpt-last-use-buffer
(save-excursion
(goto-char chatgpt-last-response)
(message "loaded chatgpt-last-response: %s" chatgpt-last-response)
;; (delete-region chatgpt-last-response (point-max)))))
(delete-region (point) (point-max)))))
(chatgpt--query-stream chatgpt-last-query chatgpt-last-use-model nil chatgpt-last-use-buffer t))
(defun chatgpt--query-stream (query use-model &optional recursive use-buffer-name reuse)
(unless chatgpt-process
(chatgpt-init))
;; (message "DEBUG: query=%s, use-model=%s, recursive=%s, use-buffer-name=%s, reuse=%s"
;; query use-model recursive use-buffer-name reuse)
(if recursive
(progn
(chatgpt-display use-buffer-name)
(setq chatgpt-running-flag t))
(progn
(setq use-buffer-name (chatgpt-display))
(setq chatgpt-last-query query)
(setq chatgpt-last-use-buffer use-buffer-name)
(setq chatgpt-last-use-model use-model)))
(lexical-let ((saved-id (if recursive
chatgpt-id
(cl-incf chatgpt-id)))
(query (if recursive
(string-join (nthcdr 5 (split-string query "-")) "-")
query))
(query_with_id (if recursive
query
(format "%s-%s" (org-id-uuid) query)))
(recursive-model use-model)
(use-buffer-name use-buffer-name)
(reuse (if recursive
nil
reuse)))
(if recursive
(setq next-recursive recursive)
(progn
(setq next-recursive nil)
(chatgpt--insert-query query saved-id use-model use-buffer-name)))
;; (message "DEBUG: Calling EPC with query_with_id=%s, recursive-model=%s, reuse=%s, buffer-name=%s"
;; query_with_id recursive-model reuse (buffer-name use-buffer-name))
(deferred:$
(deferred:$
(epc:call-deferred chatgpt-process 'querystream (list query_with_id recursive-model reuse (buffer-name use-buffer-name)))
(deferred:nextc it
#'(lambda (response)
(with-current-buffer use-buffer-name
(save-excursion
(if (numberp next-recursive)
(goto-char next-recursive)
(chatgpt--goto-identifier chatgpt-id use-buffer-name))
(if chatgpt-check-running-flag
(progn
(setq next-recursive nil)
(save-buffer))
(if (= (plist-get response :type) 0) ; Normal response
(if (plist-get response :message) ; Check if message exists
(progn
(insert (plist-get response :message))
(goto-char (point-max))
(setq next-recursive (point)))
(progn ; End of stream
(insert (format "\n\n%s"
(make-string (chatgpt-get-buffer-width-by-char ?=) ?=)))
(goto-char (point-max))
(let ((output-window (get-buffer-window (current-buffer))))
(when output-window
(with-selected-window output-window
(goto-char (point-max))
(unless (>= (window-end output-window) (point-max))
(recenter -1)))))
(setq next-recursive nil)
(save-buffer)))
(progn ; Error response
(insert (format "ERROR: %s" (plist-get response :message)))
(setq next-recursive nil)
(save-buffer))))))
(if next-recursive
(chatgpt--query-stream query_with_id recursive-model next-recursive use-buffer-name)
(progn
(setq chatgpt-check-running-flag nil)
(setq chatgpt-running-flag nil))))))
(deferred:error it
`(lambda (err)
(message "err is:%s" err)
(with-current-buffer ,use-buffer-name
(save-excursion
(if (numberp next-recursive)
(goto-char next-recursive)
(chatgpt--goto-identifier ,saved-id ,use-buffer-name))
(chatgpt--clear-line)
(insert (format "ERROR: %s" (error-message-string err)))
(save-buffer)))
(setq chatgpt-check-running-flag nil)
(setq chatgpt-running-flag nil))))))
(defun chatgpt--query-by-type-stream (query query-type use-model)
(if (equal query-type "custom")
(chatgpt--query-stream
(format "%s\n\n%s" (read-from-minibuffer "ChatGPT Custom Prompt: ") query) use-model)
(if-let (format-string (cdr (assoc query-type chatgpt-query-format-string-map)))
(chatgpt--query-stream
(format format-string query) use-model)
(error "No format string associated with 'query-type' %s. Please customize 'chatgpt-query-format-string-map'" query-type))))
;;;###autoload
(defun chatgpt-query-by-type (query use-model)
"Query ChatGPT with from QUERY and interactively chosen 'query-type'.
The function uses the 'completing-read' function to prompt the
user to select the type of query to use. The selected query type
is passed to the 'chatgpt--query-by-type' function along with the
'query' argument, which sends the query to the ChatGPT model and
returns the response."
; TODO
(interactive (list (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-from-minibuffer "ChatGPT Query: "))
(read-string "GPT model: " chatgpt-default-model)))
(let* ((query-type (completing-read "Type of Query: " (cons "custom" (mapcar #'car chatgpt-query-format-string-map)))))
(if (or (assoc query-type chatgpt-query-format-string-map)
(equal query-type "custom"))
(chatgpt--query-by-type query query-type use-model)
(chatgpt--query (format "%s\n```%s```" query-type query) use-model))))
;;;###autoload
(defun chatgpt-query (query use-model)
"Query ChatGPT with QUERY.
The user will be prompted to enter a query if none is provided. If
there is an active region, the user will be prompted to select the
type of query to perform.
Supported query types are:
* doc: Ask for documentation in query
* bug: Find bug in query
* improve: Suggestions for improving code
* understand: Query for understanding code or behavior"
;TODO
(interactive (list (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-from-minibuffer "ChatGPT Query: "))
(read-string "GPT model: " chatgpt-default-model)))
;; (if chatgpt-waiting-dot-timer
;; (message "Already waiting on a ChatGPT query. If there was an error with your previous query, try M-x chatgpt-reset")
;; (if (region-active-p)
;; (chatgpt-query-by-type query)
;; (chatgpt--query query)))
(if (region-active-p)
(chatgpt-query-by-type query use-model)
(chatgpt--query query use-model)))
;;;###autoload
(defun chatgpt-query-by-type-stream (query use-model)
(interactive (list (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-from-minibuffer "ChatGPT Stream Query: "))
(read-string "GPT model: " chatgpt-default-model)))
(let* ((query-type (completing-read "Type of Stream Query: " (cons "custom" (mapcar #'car chatgpt-query-format-string-map)))))
(if (or (assoc query-type chatgpt-query-format-string-map)
(equal query-type "custom"))
(chatgpt--query-by-type-stream query query-type (or use-model chatgpt-default-model))
(chatgpt--query-stream (format "%s\n\n%s" query-type query) (or use-model chatgpt-default-model)))))
;;;###autoload
(defun chatgpt-query-stream (query use-model)
(interactive (list (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-from-minibuffer "ChatGPT Stream Query: "))
(unless use-model (read-string "GPT model: " chatgpt-default-model))))
(if (region-active-p)
(chatgpt-query-by-type-stream query use-model)
(chatgpt--query-stream query use-model)))
;;;###autoload
(defun chatgpt-query-stream-gpt4 (query)
(interactive (list (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-from-minibuffer "ChatGPT Stream Query: "))))
(chatgpt-query-stream query chatgpt-default-model))
;;;###autoload
(defun chatgpt-query-stream-gpt35 (query)
(interactive (list (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-from-minibuffer "ChatGPT Stream Query: "))))
(chatgpt-query-stream query chatgpt-second-preferest-model))
;;;###autoload
(defun chatgpt-query-stream-choose (query choice)
(interactive
(list
(if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-from-minibuffer "ChatGPT Stream Query: "))
nil)) ; Default choice to nil when called interactively
(let ((model (or choice
(let ((choices chatgpt-ai-choices))
(ivy-read "Choose one: " choices
:initial-input chatgpt-last-use-model)))))
(chatgpt-query-stream query model)))
;;;###autoload
(defun chatgpt-query-gpt4o (query)
(interactive (list (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-from-minibuffer "ChatGPT Stream Query: "))))
(chatgpt-query query "gpt4o"))
(provide 'chatgpt)
;;; chatgpt.el ends here