diff --git a/main.py b/main.py index 07cf8a8b4..d46e1617b 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import sys -from smol_dev.prompts import plan, specify_file_paths, generate_code_sync +from smol_dev.prompts import plan, extract_file_paths, generate_code_sync from smol_dev.utils import generate_folder, write_file from smol_dev.main import main import argparse diff --git a/readme.md b/readme.md index ff2916c53..179cdfffe 100644 --- a/readme.md +++ b/readme.md @@ -79,7 +79,7 @@ pip install smol_dev Here you can basically look at the contents of `main.py` as our "documentation" of how you can use these functions and prompts in your own app: ```python -from smol_dev.prompts import plan, specify_file_paths, generate_code_sync +from smol_dev.prompts import plan, extract_file_paths, generate_code_sync prompt = "a HTML/JS/CSS Tic Tac Toe Game" @@ -87,7 +87,7 @@ shared_deps = plan(prompt) # returns a long string representing the coding plan # do something with the shared_deps plan if you wish, for example ask for user confirmation/edits and iterate in a loop -file_paths = specify_file_paths(prompt, shared_deps) # returns an array of strings representing the filenames it needs to write based on your prompt and shared_deps. Relies on OpenAI's new Function Calling API to guarantee JSON. +file_paths = extract_file_paths(prompt, shared_deps) # returns an array of strings representing the filenames it needs to write based on your prompt and shared_deps. Relies on OpenAI's new Function Calling API to guarantee JSON. # do something with the filepaths if you wish, for example display a plan diff --git a/smol_dev/main.py b/smol_dev/main.py index 536e0ddec..d520bbbd3 100644 --- a/smol_dev/main.py +++ b/smol_dev/main.py @@ -1,7 +1,7 @@ import sys import time -from smol_dev.prompts import plan, specify_file_paths, generate_code_sync +from smol_dev.prompts import plan, extract_file_paths, generate_code_sync from smol_dev.utils import generate_folder, write_file import argparse @@ -37,10 +37,10 @@ def stream_handler(chunk): if debug: print("--------shared_deps---------") - # specify file_paths + # extract file_paths if debug: - print("--------specify_filePaths---------") - file_paths = specify_file_paths(prompt, shared_deps, model=model) + print("--------extract_file_paths---------") + file_paths = extract_file_paths(prompt, shared_deps) if debug: print(file_paths) if debug: diff --git a/smol_dev/prompts.py b/smol_dev/prompts.py index 2fd43c671..9445b6eda 100644 --- a/smol_dev/prompts.py +++ b/smol_dev/prompts.py @@ -20,6 +20,18 @@ """ +def extract_file_paths(prompt: str, plan: str): + # Regular expression pattern to match file names + file_pattern = r'\b[\w.-]+\.\w+\b' + file_names = [] + for text in [prompt, plan]: + names = re.findall(file_pattern, text) + file_names.extend(names) + assert file_names, 'No file names found in the given texts' + unique_file_names = list(set(file_names)) + return unique_file_names + + @openai_function def file_paths(files_to_edit: List[str]) -> List[str]: """ @@ -69,7 +81,7 @@ def plan(prompt: str, stream_handler: Optional[Callable[[bytes], None]] = None, { "role": "system", "content": f"""{SMOL_DEV_SYSTEM_PROMPT} - + In response to the user's prompt, write a plan. In this plan, please name and briefly describe the structure of the app we will generate, including, for each file we are generating, what variables they export, data schemas, id names of every DOM elements that javascript functions will use, message names, and function names. Respond only with plans following the above schema. @@ -111,13 +123,13 @@ async def generate_code(prompt: str, plan: str, current_file: str, stream_handle { "role": "system", "content": f"""{SMOL_DEV_SYSTEM_PROMPT} - - In response to the user's prompt, + + In response to the user's prompt, Please name and briefly describe the structure of the app we will generate, including, for each file we are generating, what variables they export, data schemas, id names of every DOM elements that javascript functions will use, message names, and function names. - We have broken up the program into per-file generation. - Now your job is to generate only the code for the file: {current_file} - + We have broken up the program into per-file generation. + Now your job is to generate only the code for the file: {current_file} + only write valid code for the given filepath and file type, and return only the code. do not add any other explanation, only return valid code for that file type. """, @@ -134,20 +146,20 @@ async def generate_code(prompt: str, plan: str, current_file: str, stream_handle "role": "user", "content": f""" Make sure to have consistent filenames if you reference other files we are also generating. - - Remember that you must obey 3 things: + + Remember that you must obey 3 things: - you are generating code for the file {current_file} - do not stray from the names of the files and the plan we have decided on - MOST IMPORTANT OF ALL - every line of code you generate must be valid code. Do not include code fences in your response, for example - + Bad response (because it contains the code fence): - ```javascript + ```javascript console.log("hello world") ``` - + Good response (because it only contains the code): console.log("hello world") - + Begin generating the code now. """,