This package supports a threaded AI chat inside any org-mode buffer. Here’s what it looks like:
The design is inspired by the ChatGPT web interface. There are many AI chat packages for Emacs, but I am not aware of any that naturally support multiple conversational pathways.
The package comes with one main function, ai-org-chat-respond
, that operates in any org-mode buffer. It extracts a conversation history from the parent entries, treating an entry as belonging to the AI if its heading is “AI” and otherwise to the user. This conversation history is passed along to the OpenAI library to generate a response.
The “plumbing” concerning API calls is outsourced by default to the gptel library, which requires the user to provide an OpenAI API key. By customizing the variable ai-org-chat-request-fn
, any other backend could be used.
Narrowing provides a simple way to truncate the conversation history if it becomes too long – only the restricted part of the buffer is considered.
This package depends (by default) upon gptel, so you should first set that up and configure it. The following configuration works for me on MacOS after setting the environment variable “OPENAI_API_KEY” to my API key.
(use-package exec-path-from-shell
:ensure
:init
(exec-path-from-shell-initialize))
(use-package gptel
:ensure
:after exec-path-from-shell
:config
(setq gptel-api-key (exec-path-from-shell-getenv "OPENAI_API_KEY")))
Next, download this repository, install using M-x package-install-file
(or package-vc-install, straight, elpaca, …), and add (use-package ai-org-chat)
to your init file. For a slightly more elaborate configuration, use something like the following:
(use-package ai-org-chat
:bind
(:map global-map
("C-c /" . ai-org-chat-new))
(:map ai-org-chat-minor-mode
("C-c <return>" . ai-org-chat-respond)
("C-c n" . ai-org-chat-branch))
:commands (ai-org-chat-minor-mode)
:custom
(ai-org-chat-user-name "Paul")
(ai-org-chat-dir "~/gpt")
(ai-org-chat-prompt-preamble nil))
When you want to ask the AI something, do M-x ai-org-chat-new
(or C-c /
, if you followed the above configuration). This visits a new file in the specified directory (“~/gpt” by default). If the region was active, then it will be quoted in the new buffer. Example:
The org-mode buffer has ai-org-chat-minor-mode
activated, whose only purpose is to support user-defined keybindings like in the above use-package
declaration. There are two useful commands:
ai-org-chat-respond
(C-c <return>
)- This is the main function, which tells the AI to generate a new response to the conversation node at point. It works in any org-mode buffer, not just ones created via
ai-org-chat-new
. ai-org-chat-branch
(C-c n
)- This is a convenience function that creates a new conversation branch at point.