Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/extract_file_paths #121

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ 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"

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

Expand Down
8 changes: 4 additions & 4 deletions smol_dev/main.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
Expand Down
36 changes: 24 additions & 12 deletions smol_dev/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
""",
Expand All @@ -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.

""",
Expand Down