Xenodium's excellent chatgpt-shell package makes it easy to use ChatGPT and other LLMs from Emacs. Having all of Emacs's editing power and programmability while working with an LLM is a big win.
ChatGPT requires a license key, and chatgpt-shell needs access to it. It would be a bad idea to store the key in a plain text file, so I
looked for a way to encrypt it. The chatgpt-shell README.org documents how to use the pass password manager, but I hadn't used pass before, so I did something even simpler: I used Emacs's built-in
support for GPG. Maybe you'll find this approach useful, too.
I set up chatgpt-shell by adding this code to my Emacs init file:
(require 'use-package)
(use-package chatgpt-shell
:ensure t
:bind (("C-c i" . chatgpt-shell-prompt-compose)
("C-c I" . chatgpt-shell))
:init
(setq chatgpt-shell-openai-key
(lambda ()
(with-temp-buffer
(insert-file-contents
(substitute-in-file-name "$r/openai.gpg"))
(buffer-string)))))
Now, when chatgpt-shell starts, it reads ~/.emacs.d/openai.gpg, prompts for its password, and decrypts it. It uses the full contents
of the file as the API key.
To create your own openai.gpg file, just C-x C-f ~/.emacs.d/openai.gpg, enter the key, and save. Emacs will prompt you for a password, then
use GPG to encrypt the file.
Now that I've written this, I should probably set up pass.
Edit on Wed 12 Feb 2025: I tried pass, and it's great. It's well documented, easy to use, simple, and
replicates passwords between machines using Git. Here's my new setup
for chatgpt-shell:
(require 'use-package)
(use-package chatgpt-shell
:ensure t
:bind (("C-c i" . chatgpt-shell-prompt-compose)
("C-c I" . chatgpt-shell))
:custom
((chatgpt-shell-openai-key
(lambda () (auth-source-pass-get 'secret "api-keys/openai.com")))))
I recommend it highly.