Codex Completion
Complete, Edit Code and Text in Emacs using OpenAI Codex
Demo
Requirements
- API Key with access to OpenAI’s Codex You can get on the Codex beta waitlist by enrolling here
Installation
- Direct Install
- Put
codex-completion.elin your Emacs load path. For e.g~/.emacs.d/lisp - Load via use-package in your
~/.emacs.d/init.elor.emacsfile by adding below snippet;; Codex Completion Package (use-package codex-completion :load-path "~/.emacs.d/lisp/codex-completion.el" :bind ("C-c ." . 'codex-completion) :config (setq codex-completion-openai-api-token <YOUR_OPENAI_API_TOKEN>))
- Put
- Using straight.el
- Ensure straight.el, straight-use-package are setup
- Add below snippet to your emacs config, e.g
~/.emacs.d/init.eland evaluate it;; Codex-Completion Package (use-package codex-completion :straight (codex-completion :type git :host github :repo "debanjum/codex-completion") :bind ("C-c ." . 'codex-completion) :config (setq codex-completion-openai-api-token <YOUR_OPENAI_API_TOKEN>))
- Note: Procedure to install using Quelpa etc are similar to straight.el
Usage
Edit Code or Text
- Mark/highlight region to Edit
- Call
C-u M-x codex-completion - Enter your edit instructions for Codex at prompt
This will replace the highlighted region with the edit response from Codex
Complete Code or Text at Point
- Call
M-x codex-completion
This will insert code provided by Codex at point. It will use the current paragraph as context
Complete Code or Text using Region
- Mark region to use as context to Codex for Completion
- Call
M-x codex-completion
This will insert code provided by Codex at point. It will use the active region as context
Miscellaneous
Contributions by Codex
Codex was instrumental in writing this tool.
Codex seems to understand the OpenAI API response schema.
Notice the choices, text keys used in the codex-get-completions function below
- Prompt to Codex
;; define a elisp function to query OpenAI davinci codex at https://api.openai.com/v1/engines/davinci/completions for completion suggestions - Response from Codex
;; define a elisp function to query OpenAI davinci codex at https://api.openai.com/v1/engines/davinci/completions for completion suggestions (defun codex-query (query) "Query OpenAI davinci codex at https://api.openai.com/v1/engines/davinci/completions for completion suggestions" (let ((url-request-method "POST") (url-request-extra-headers '(("Content-Type" . "application/json") ("Authorization" . "Bearer ") ("User-Agent" . "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36") ("Accept" . "application/json") ("Accept-Language" . "en-US,en;q=0.8"))) (url-request-data (json-encode `(("text" . ,query)))) (url-show-status nil)) (with-current-buffer (url-retrieve-synchronously "https://api.openai.com/v1/engines/davinci/completions") (goto-char (point-min)) (re-search-forward "^$") (json-read)))) ;; define a elisp function to get the completion suggestions from the json response (defun codex-get-completions (json) "Get the completion suggestions from the json response" (let ((completions (cdr (assoc 'choices json)))) (mapcar (lambda (completion) (cdr (assoc 'text completion))) completions)))
