scaf-code - Quickly Generate Code with ChatGPT
Introduction
OpenAI recently released a model called gpt-4-1106-preview
. It can handle quite long texts of up to 128k tokens, which means it can now reference several normal text files without any issues.
So, for those times when you think, “I’d like to write a program like this, referring to similar programs I’ve made before and using such specifications and type definitions,” I’ve created a command called scaf-code
that quickly generates a template-like file with ChatGPT.
Description of scaf-code
You will need an OpenAI API Key to use it. ※ (Charges will also apply there)
Installation
pip install scaf-code
How to Use
First, please set your OpenAI API Key as an environment variable.
export OPENAI_API_KEY=<your_openai_api_key>
The Simplest Way to Use
The simplest way to use it is to specify --out
and --spec
.
scaf_code --out tmp/add.py --spec “Create a command that takes multiple numbers from the command line, adds them all together, and outputs the result”
This will create a file like the following.
import sys
def main():
# Initialize the sum
total_sum = 0
# Iterate over the arguments
for arg in sys.argv[1:]: # Skip the script name
try:
# Convert each argument to an integer and add it to the sum
total_sum += int(arg)
except ValueError:
# Handle the case where the argument is not an integer
print(f"Error: ‘{arg}’ is not a valid number.")
return
# Output the sum
print(f"The sum is: {total_sum}")
if __name__ == "__main__":
main()
The current version has some annoying code fences attached, so you have to remove them, but once removed, it was executable.
python tmp/add.py 1 2 3 4
The sum is: 10
Well, this much was possible even before.
Generating Code While Referring to Other Files
You can specify multiple files with the --ref
option to make references. Since ChatGPT is only told the file name part, instructions like “refer to ~” are also effective.
scaf_code --out tmp/mul.py --spec “Create a command that multiplies arguments, referring to add.py” --ref tmp/add.py
This will generate tmp/mul.py
.
Buisiness use
When you’re iteratively creating code, you might want to keep in mind base classes, type files, common specifications, and instructions. It seems convenient to textually specify such things before giving instructions (though that’s just a feeling).
You can use the --spec-file
option to read the “instructions” part from a file.
So,
# Generate a new task!
scaf_code --out src/tasks/new_awesome_task.py \
--spec-file doc/tasks/new_awesome_spec.md doc/task_guideline.md \
--ref src/tasks/{similar_task.py,task_base_class.py}
# Generate a test along with it!
scaf_code --out tests/tasks/test_new_awesome_task.py \
--spec-file doc/tasks/new_awesome_spec.md doc/task_guideline.md \
--ref src/tasks/new_awesome_task.py tests/tasks/test_similar_task.py
I’m hoping it can be generated like this.
Isn’t VSCode + GithubCopilotChat Good Enough?
Copilot can do something similar, but it’s not always easy to specify files clearly, and if you’re using a different editor like PyCharm, it’s not so straightforward. Also, being able to use it from the CLI is strong for various repetitions, I think.
With the acceptance of 128K Tokens, it seems likely that there will be cases where you want to create something “with reference to ~” for normal documents and various other files, so I made it for now.
In Conclusion
Well, it’s still rough, but it seems fun to be able to extend it in various ways, such as being able to reference images, handle various file formats (like docx?), fetch files from URLs, and integrate with CodeInterpreter.