I have heard wonderful things about gptel - the Emacs package that gives an interface to various LLMs. I will admit however I really struggled to get this working using my OpenAI API key with the Docs in their GitHub. Below is a quick guide for anyone else that got stuck - running a Linux machine.
First things, get gptel
installed - add the below to your config:
(use-package gptel
:ensure t)
(setq gptel-api-key (getenv "OPENAI_API_KEY"))
You will notice here we have to specify out OpenAI API key. It is possible to declare this directly in your config, that is such a stupidly bad idea I’m not even going to entertain it.
I opted for a locally declared variable hosted in my ~/.bashrc
file - not the best as it is still plain text but for the purposes of testing I think this is sufficient.
To do this you need to add the below to your ~/.bashrc
file, adding in your key:
export OPENAI_API_KEY="sk-proj-XXX"
Then reload your ~/.bashrc
by running source ~/.bashrc
in your terminal.
Finally for Emacs to see your API key, you need to use a package like exec-path-from-shell - setting it up using the following in your Emacs config.
(use-package exec-path-from-shell
:ensure t)
(when (memq window-system '(mac ns x))
(exec-path-from-shell-initialize))
(exec-path-from-shell-copy-env "OPENAI_API_KEY")
This needs be evaluated before gptel
so make sure it is above it in your config - full example below:
(use-package exec-path-from-shell
:ensure t)
(when (memq window-system '(mac ns x))
(exec-path-from-shell-initialize))
(exec-path-from-shell-copy-env "OPENAI_API_KEY")
(use-package gptel
:ensure t)
(setq gptel-api-key (getenv "OPENAI_API_KEY"))
Running M-x gptel
, entering some text into the buffer and sending it with M-x gptel-send
should elicit a response.
That is it, should now have it up and running - I am playing around with this package, expect more guide in the coming days.