Last active
February 20, 2022 09:55
-
-
Save stuhlmueller/d95c34ee9a3382bad3732e1386e8c8bf to your computer and use it in GitHub Desktop.
OpenAI Codex for refactoring in Emacs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun codex-refactor (&optional b e) | |
(interactive "r") | |
(shell-command-on-region b e "codex-refactor.py" nil nil)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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