Skip to content

Instantly share code, notes, and snippets.

@stuhlmueller
Last active February 20, 2022 09:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuhlmueller/d95c34ee9a3382bad3732e1386e8c8bf to your computer and use it in GitHub Desktop.
Save stuhlmueller/d95c34ee9a3382bad3732e1386e8c8bf to your computer and use it in GitHub Desktop.
OpenAI Codex for refactoring in Emacs
(defun codex-refactor (&optional b e)
(interactive "r")
(shell-command-on-region b e "codex-refactor.py" nil nil))
#!/usr/bin/env python
# Make this file executable (chmod +x codex.py)
# and put it somewhere in your PATH
import sys
import openai
openai.api_key = "sk-[YOUR API KEY HERE]"
code = "".join(sys.stdin.readlines())
prompt = f"""
## Naive implementation:
{code}
## A more elegant way to implement this:"""
completion = openai.Completion.create(
engine="davinci-codex",
prompt=prompt,
max_tokens=int(len(code)/2),
stop=["\n\n\n"],
temperature=0,
frequency_penalty=0.2
)
print(completion.choices[0].text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment