Skip to content

benjamin-asdf/openai-api.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Update: openai sort of specialized on the chat api now, so an interface like https://github.com/benjamin-asdf/chatgpt-shell is maybe a better fit. I am currently using my fork of chatpgt-shell exclusively basically.

I wanted openai-api stuff in emacs. Consider this an alpha version where everything still might change. PRs welcome, feel free to suggest new commands.

gifs/edit.gif

Setup

  • Get an api token at https://openai.com/api/ (after 3 months you’d have to pay)
  • load file openail.el.
  • Check dev.el. You need to set openai-api-key.

Example config

You need to adapt this.

(use-package openai-api
  :straight nil
  :load-path "~/repos/openai-api.el/"
  :config
  (setq openai-api-key
        (let ((s))
          (lambda ()
            (or s (setf
                   s
                   (auth-source-pick-first-password
                    :host "openai-api"))))))

  (define-key openai-api-keymap (kbd "i")
              (defun mm/insert-todo ()
                (interactive)
                (insert "TODO: ")
                (comment-line 1)))
  (with-eval-after-load 'git-commit-mode
    (define-key git-commit-mode-map (kbd "C-c i") #'openai-current-commit-msg))
;; or how ever you define your keys
(meow-leader-define-key `("." . ,openai-api-keymap)))

Usage

openai-api-davinci-edit

  • optionally narrow your buffer. E.g. to your defun. (you usually want to do this).
  • use openai-api-davinci-edit
  • The response is put in a buffer called *openai-edit-<your-buffer-name>*
  • ediff-buffers works nice for response and target buffer
  • Use openai-api-edit-ediff-buffers. This bound by default at C-c C-e in response buffers.

recursive calls

Repeat another openai-api-davinci-edit from inside the response buffer.

Empty prompt

Another usage pattern is to open an empy buffer, then ask it to produce something.

  • switch-to-buffer “foo”
  • use openai-api-davinci-edit “Shell command that lists all files with more than 1mb in the current dir”
#!/bin/bash

find . -type f -size +1M

Seriously play with this

  • “Add a docstring”
  • “Remove redundant let”
  • In a css file, say on a high level “Make the dropdown text fill the whole dropdown”.
  • You can use quite high level language. The model figures out the intent surprisingly well.

Empty instructions

Consider trying empty instructions. This is similar to the completion api.

Explore api and make your own commands

See examples

Explain region

See example time complexity. I made a command openai-api-explain-region . You can add more custom prompts via openai-api-explain-data-list.

(add-to-list
  openai-api-explain-data-list
  '("What does this do?"
     .
     (((model . "text-davinci-003")
       (top_p . 1)
       (max_tokens . 64)
       (temperature . 0)))))

openai-current-commit-msg

Generates a commit messages from your current diff. I recommend binding it in git-commit-mode-map.

openai-api-fact-bot

Make a question, get facts. See example.

openai-api-gen-shell-command

Describe a shell command, get a resp buffer. This uses the edit enpoint.

openai-api-completions

This is meant to be a quick completion function.

I was first trying to put this into capf but it is not fast enough for that. Not fleshed out yet so feel free to play around and let me know if you have a variant that works well.

GPT-4

gpt-4 is supported via the chat api.

(setf openai-api-chat-model "gpt-4")

As advanced user you can get inspired by openai-api-chat-complete.

Related Projects

Recent Hacker News article with thread of similar projects.

Other emacs gpt projects, in no particular order:

old

Licence

This software is (C) Benjamin Schwerdtner and licencend under the GNU GENERAL PUBLIC LICENSE V3. You can find a copy of the licence at LICENCE in the repo root.

About

Modern emacs bindings for openai api

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published