Skip to content

ChatGPT and DALL-E Emacs shells + Org babel 🦄 + a shell maker for other providers

License

Notifications You must be signed in to change notification settings

stardiviner/chatgpt-shell

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

👉 Support this work via GitHub Sponsors

https://melpa.org/packages/chatgpt-shell-badge.svg

chatgpt-shell

ChatGPT and DALL-E Emacs shells + Org Babel.

Includes shell-maker, a way to create shells for any service (local or cloud).

Support this effort

If you’re finding chatgpt-shell useful, consider ✨sponsoring✨.

chatgpt-shell is in development. Please report issues or send pull requests for improvements.

Like this package? Tell me about it đź’™

Finding it useful? Like the package? I’d love to hear from you. Get in touch (Mastodon / Twitter / Reddit / Email).

Shell usage

Insert to current buffer

Install

  • Load (require 'chatgpt-shell)
  • Load (require 'dall-e-shell)

MELPA

If using use-package, you can install with :ensure t.

(use-package chatgpt-shell
  :ensure t
  :custom
  ((chatgpt-shell-openai-key
    (lambda ()
      (auth-source-pass-get 'secret "openai-key")))))

Straight

chatgpt-shell depends on shell-maker. This dependency is tesolved without issues on MELPA but seems to run into issues with straight. I’m not familiar with straight but users have reported the following to work.

(use-package shell-maker
  :straight (:host github :repo "xenodium/chatgpt-shell" :files ("shell-maker.el")))

(use-package chatgpt-shell
  :requires shell-maker
  :straight (:host github :repo "xenodium/chatgpt-shell" :files ("chatgpt-shell.el")))

If you have a better straight solution, please send a pull request or open an issue with a suggestion.

Read on for setting your OpenAI key in other ways.

Set OpenAI key

You’ll first need to get a key from OpenAI.

ChatGPT key

As function

;; if you are using the "pass" password manager
(setq chatgpt-shell-openai-key
      (lambda ()
        ;; (auth-source-pass-get 'secret "openai-key") ; alternative using pass support in auth-sources
        (nth 0 (process-lines "pass" "show" "openai-key"))))

;; or if using auth-sources, e.g., so the file ~/.authinfo has this line:
;;  machine api.openai.com password OPENAI_KEY
(setq chatgpt-shell-openai-key
      (auth-source-pick-first-password :host "api.openai.com"))

;; or same as previous but lazy loaded (prevents unexpected passphrase prompt)
(setq chatgpt-shell-openai-key
      (lambda ()
        (auth-source-pick-first-password :host "api.openai.com")))

Manually

M-x set-variable chatgpt-shell-openai-key

As variable

(setq chatgpt-shell-openai-key "my key")

DALL-E key

Same as ChatGPT, but use dall-e-shell-openai-key variable.

ChatGPT through proxy service

If you use ChatGPT through proxy service “https://api.chatgpt.domain.com”, set options like bellowing:

(use-package chatgpt-shell
  :ensure t
  :custom
  ((chatgpt-shell-api-url-base "https://api.chatgpt.domain.com")
   (chatgpt-shell-openai-key
    (lambda ()
      ;; Here the openai-key should be the proxy service key.
      (auth-source-pass-get 'secret "openai-key")))))

If your proxy service API path is not OpenAI ChatGPT default path like ”/v1/chat/completions”, then you can customize option chatgpt-shell-api-url-path.

Launch

Launch with M-x chatgpt-shell or dall-e-shell.

Clear buffer

Type clear as a prompt.

ChatGPT> clear

Alternatively, use either M-x chatgpt-shell-clear-buffer or M-x comint-clear-buffer.

Saving and restoring (experimental)

Save with M-x shell-maker-save-session-transcript and restore with M-x chatgpt-shell-restore-session-from-transcript.

Streaming

chatgpt-shell can either wait until the entire response is received before displaying, or it can progressively display as chunks arrive (streaming).

Streaming is enabled by default. (setq chatgpt-shell-chatgpt-streaming nil) to disable it.

chatgpt-shell customizations

Custom variableDescription
chatgpt-shell-display-functionFunction to display the shell. Set to `display-buffer’ or custom function.
chatgpt-shell-model-versionsThe list of ChatGPT OpenAI models to swap from.
chatgpt-shell-system-promptThe system prompt `chatgpt-shell-system-prompts’ index.
chatgpt-shell-default-promptsList of default prompts to choose from.
chatgpt-shell-read-string-functionFunction to read strings from user.
chatgpt-shell-model-temperatureWhat sampling temperature to use, between 0 and 2, or nil.
chatgpt-shell-transmitted-context-lengthControls the amount of context provided to chatGPT.
chatgpt-shell-history-pathRoot path to the location for storing history files.
chatgpt-shell-system-promptsList of system prompts to choose from.
chatgpt-shell-streamingWhether or not to stream ChatGPT responses (show chunks as they arrive).
chatgpt-shell-prompt-header-refactor-codePrompt header of `refactor-code`
chatgpt-shell-prompt-header-whats-wrong-with-last-commandPrompt header of `whats-wrong-with-last-command`
chatgpt-shell-loggingLogging disabled by default (slows things down).
chatgpt-shell-prompt-query-response-styleDetermines the prompt style when invoking from other buffers.
chatgpt-shell-prompt-header-proofread-regionPromt header of `proofread-region`
chatgpt-shell-model-versionThe active ChatGPT OpenAI model index.
chatgpt-shell-source-block-actionsBlock actions for known languages.
chatgpt-shell-prompt-header-eshell-summarize-last-command-outputPrompt header of `eshell-summarize-last-command-output`
chatgpt-shell-api-url-pathOpenAI API’s URL path.
chatgpt-shell-additional-curl-optionsAdditional options for `curl’ command.
chatgpt-shell-openai-keyOpenAI key as a string or a function that loads and returns it.
chatgpt-shell-after-command-functionsAbnormal hook (i.e. with parameters) invoked after each command.
chatgpt-shell-prompt-header-describe-codePrompt header of `describe-code`
chatgpt-shell-api-url-baseOpenAI API’s base URL.
chatgpt-shell-babel-headersAdditional headers to make babel blocks work.
chatgpt-shell-language-mappingMaps external language names to Emacs names.
chatgpt-shell-prompt-header-generate-unit-testPrompt header of `generate-unit-test`
chatgpt-shell-request-timeoutHow long to wait for a request to time out in seconds.

There are more. Browse via M-x set-variable

chatgpt-shell-display-function (with custom function)

If you’d prefer your own custom display function,

(setq chatgpt-shell-display-function #'my/chatgpt-shell-frame)

(defun my/chatgpt-shell-frame (bname)
  (let ((cur-f (selected-frame))
        (f (my/find-or-make-frame "chatgpt")))
    (select-frame-by-name "chatgpt")
    (pop-to-buffer-same-window bname)
    (set-frame-position f (/ (display-pixel-width) 2) 0)
    (set-frame-height f (frame-height cur-f))
    (set-frame-width f  (frame-width cur-f) 1)))

(defun my/find-or-make-frame (fname)
  (condition-case
      nil
      (select-frame-by-name fname)
    (error (make-frame `((name . ,fname))))))

Thanks to tuhdo for the custom display function.

chatgpt-shell commands

BindingCommandDescription
chatgpt-shellStart a ChatGPT shell.
chatgpt-shell-rename-block-at-pointRename block at point (perhaps a different language).
C-M-hchatgpt-shell-mark-at-point-dwimMark source block if at point. Mark all output otherwise.
chatgpt-shell-execute-primary-block-action-at-pointExecute primary action for known block.
chatgpt-shell-execute-babel-block-action-at-pointExecute block as org babel.
chatgpt-shell-eshell-whats-wrong-with-last-commandAsk ChatGPT what’s wrong with the last eshell command.
C-c C-pchatgpt-shell-previous-itemGo to previous item.
chatgpt-shell-refresh-renderingRefresh markdown rendering by re-applying to entire buffer.
chatgpt-shell-explain-codeDescribe code from region using ChatGPT.
chatgpt-shell-promptMake a ChatGPT request from the minibuffer.
chatgpt-shell-remove-block-overlaysRemove block overlays. Handy for renaming blocks.
chatgpt-shell-proofread-regionProofread English from region using ChatGPT.
chatgpt-shell-send-and-review-regionSend region to ChatGPT, review before submitting.
chatgpt-shell-eshell-summarize-last-command-outputAsk ChatGPT to summarize the last command output.
chatgpt-shell-prompt-appending-kill-ringMake a ChatGPT request from the minibuffer appending kill ring.
chatgpt-shell-describe-codeDescribe code from region using ChatGPT.
chatgpt-shell-modeMajor mode for editing text written for humans to read.
C-c C-vchatgpt-shell-swap-model-versionSwap model version from `chatgpt-shell-model-versions’.
chatgpt-shell-previous-source-blockMove point to previous source block.
chatgpt-shell-refactor-codeRefactor code from region using ChatGPT.
C-c C-schatgpt-shell-swap-system-promptSwap system prompt from `chatgpt-shell-system-prompts’.
chatgpt-shell-save-session-transcriptSave shell transcript to file.
chatgpt-shell-clear-bufferClear the comint buffer.
C-c C-nchatgpt-shell-next-itemGo to next item.
chatgpt-shell-view-at-pointView prompt and putput at point in a separate buffer.
chatgpt-shell-send-regionSend region to ChatGPT.
chatgpt-shell-restore-session-from-transcriptRestore session from transcript.
chatgpt-shell-generate-unit-testGenerate unit-test for the code from region using ChatGPT.
chatgpt-shell-next-source-blockMove point to previous source block.
C-c C-cchatgpt-shell-ctrl-c-ctrl-cCtrl-C Ctrl-C DWIM binding.
chatgpt-shell-interruptInterrupt `chatgpt-shell’ from any buffer.

Browse all available via M-x.

dall-e-shell customizations

Custom variableDescription
dall-e-shell-openai-keyOpenAI key as a string or a function that loads and returns it.
dall-e-shell-image-sizeThe default size of the requested image as a string.
dall-e-shell-read-string-functionFunction to read strings from user.
dall-e-shell-request-timeoutHow long to wait for a request to time out.
dall-e-shell-model-versionThe used DALL-E OpenAI model.
dall-e-shell-display-functionFunction to display the shell. Set to `display-buffer’ or custom function.
dall-e-shell-image-output-directoryOutput directory for the generated image.

dall-e-shell commands

CommandDescription
dall-e-shellStart a DALL-E shell.
dall-e-shell-modeMajor mode for editing text written for humans to read.

ChatGPT org babel

Load (require 'ob-chatgpt-shell) and invoke (ob-chatgpt-shell-setup).

#+begin_src chatgpt-shell
  Hello
#+end_src

#+RESULTS:
: Hi there! How can I assist you today?

:version

Use :version to specify “gpt-4”, “gpt-3.5-turbo”, or something else.

#+begin_src chatgpt-shell :version "gpt-4"
 Hello
#+end_src

#+RESULTS:
Hello! How can I help you today?

:system

Use :system to set the system prompt.

#+begin_src chatgpt-shell :system "always respond like a pirate"
  hello
#+end_src

#+RESULTS:
Ahoy there, me hearty! How be ye today?

:temperature

Use :temperature to set the temperature parameter.

#+begin_src chatgpt-shell :temperature 0.3
  hello
#+end_src

:context

Use :context t to include any prior context in current buffer.

#+begin_src chatgpt-shell
  tell me a random day of the week
#+end_src

#+RESULTS:
Wednesday

#+begin_src chatgpt-shell :system "always respond like a pirate"
  hello
#+end_src

#+RESULTS:
Ahoy there, me hearty! How be ye today?

#+begin_src chatgpt-shell :context t
  what was the day you told me and what greeting?
#+end_src

#+RESULTS:
The day I told you was Wednesday, and the greeting I used was "Ahoy there, me hearty! How be ye today?"

DALL-E org babel

Load (require 'ob-dall-e-shell) and invoke (ob-dall-e-shell-setup).

#+begin_src dall-e-shell
  Pretty clouds
#+end_src

#+RESULTS:
[[file:/var/folders/m7/ky091cp56d5g68nyhl4y7frc0000gn/T/1680644778.png]]

shell-maker

There are currently two shell implementations (ChatGPT and DALL-E). Other services (local or cloud) can be brought to Emacs as shells. shell-maker can help with that.

shell-maker is a convenience wrapper around comint mode.

Both chatgpt-shell and dall-e-shell use shell-maker, but a basic implementation of a new shell looks as follows:

(require 'shell-maker)

(defvar greeter-shell--config
  (make-shell-maker-config
   :name "Greeter"
   :execute-command
   (lambda (command _history callback error-callback)
     (funcall callback
              (format "Hello \"%s\"" command)
              nil))))

(defun greeter-shell ()
  "Start a Greeter shell."
  (interactive)
  (shell-maker-start greeter-shell--config))

Other packages

👉 Support this work via GitHub Sponsors

About

ChatGPT and DALL-E Emacs shells + Org babel 🦄 + a shell maker for other providers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Emacs Lisp 100.0%