XDYB / codex-completion

Generate, Complete & Edit Code in Emacs using OpenAI Codex

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Codex Completion

Complete, Edit Code and Text in Emacs using OpenAI Codex

Demo

./codex-completion-demo.gif

Requirements

Installation

  • Direct Install
    • Put codex-completion.el in your Emacs load path. For e.g ~/.emacs.d/lisp
    • Load via use-package in your ~/.emacs.d/init.el or .emacs file 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>))
              
  • Using straight.el
    • Ensure straight.el, straight-use-package are setup
    • Add below snippet to your emacs config, e.g ~/.emacs.d/init.el and 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

  1. Mark/highlight region to Edit
  2. Call C-u M-x codex-completion
  3. 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

  1. 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

  1. Mark region to use as context to Codex for Completion
  2. 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)))
        

About

Generate, Complete & Edit Code in Emacs using OpenAI Codex

License:MIT License


Languages

Language:Emacs Lisp 100.0%