Skip to content

Instantly share code, notes, and snippets.

@jlumbroso
Created February 12, 2024 22:15
Show Gist options
  • Save jlumbroso/f9c18f116e3fa5250bbd388245b10f1d to your computer and use it in GitHub Desktop.
Save jlumbroso/f9c18f116e3fa5250bbd388245b10f1d to your computer and use it in GitHub Desktop.
A Python script to concatenate source code to send to a foundational model as a prompt.
#!/usr/bin/env python3
"""
combine_for_model.py
Author: Jérémie Lumbroso <lumbroso@seas.upenn.edu>
Date: July 25, 2023
Version: 1.0
Description:
This file can be used to send a foundational model a subset of a codebase for review.
This script is distributed under the MIT License.
Sample applications:
- Send to model along with an error message for debugging assistance
- Send to model along with feature request to have model make changes
- Send to model along with a refactoring prompt to have model refactor the codebase
To use:
1. Update the FILES_TO_INCLUDE list to include the files you want to send to the model
2. Run the script
3. The script will create a prompt file in the current directory
4. Edit the file to add your custom query at the end (error message, feature request, etc.)
5. Send the prompt file to the model
"""
import os
import glob
import sys
# You can either hardcode this, or pass it as an argument to the script
FILES_TO_INCLUDE = ["*.js", "manifest.json", "popup.html"]
# FILES_TO_INCLUDE = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
all_filenames = [
filename for pattern in FILES_TO_INCLUDE for filename in glob.glob(pattern)
]
PROMPT = (
"Ignore any previous versions of the files: " + ", ".join(all_filenames) + "\n\n"
)
PROMPT += "Below is the current state of the codebase file-by-file.\n"
PROMPT += (
"===========================================================================.\n\n"
)
for filename in all_filenames:
PROMPT += f"File '{filename}':\n```\n"
PROMPT += open(filename).read()
PROMPT += "\n```\n\n"
PROMPT += (
"===========================================================================.\n\n"
)
def get_next_prompt_number():
i = 0
while True:
if not os.path.exists(f"prompt{i:03}.txt"):
return i
i += 1
open("prompt{:03d}.txt".format(get_next_prompt_number()), "w").write(PROMPT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment