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

initial refactor of prompts #63

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
185 changes: 73 additions & 112 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
import sys
import os
import modal
import ast
from utils import clean_dir
from time import sleep
from utils import clean_dir, reportTokens, write_file
from constants import DEFAULT_DIR, DEFAULT_MODEL, DEFAULT_MAX_TOKENS
from systemPrompts.developer import planPrompt1, planPrompt2, filePrompt


stub = modal.Stub("smol-developer-v1") # yes we are recommending using Modal by default, as it helps with deployment. see readme for why.
openai_image = modal.Image.debian_slim().pip_install("openai", "tiktoken")

@stub.function(
image=openai_image,
secret=modal.Secret.from_dotenv(),
retries=modal.Retries(
max_retries=5,
backoff_coefficient=2.0,
initial_delay=1.0,
),
concurrency_limit=5, # many users report rate limit issues (https://github.com/smol-ai/developer/issues/10) so we try to do this but it is still inexact. would like ideas on how to improve
timeout=120,
)
def generate_response(model, system_prompt, user_prompt, *args):
# IMPORTANT: Keep import statements here due to Modal container restrictions https://modal.com/docs/guide/custom-container#additional-python-packages
import openai
import tiktoken

def reportTokens(prompt):
encoding = tiktoken.encoding_for_model(model)
# print number of tokens in light gray, with first 50 characters of prompt in green. if truncated, show that it is truncated
print("\033[37m" + str(len(encoding.encode(prompt))) + " tokens\033[0m" + " in prompt: " + "\033[92m" + prompt[:50] + "\033[0m" + ("..." if len(prompt) > 50 else ""))


# Set up your OpenAI API credentials
openai.api_key = os.environ["OPENAI_API_KEY"]
Expand All @@ -37,7 +18,7 @@ def reportTokens(prompt):
reportTokens(system_prompt)
messages.append({"role": "user", "content": user_prompt})
reportTokens(user_prompt)
# Loop through each value in `args` and add it to messages alternating role between "assistant" and "user"
# loop thru each arg and add it to messages alternating role between "assistant" and "user"
role = "assistant"
for value in args:
messages.append({"role": role, "content": value})
Expand All @@ -52,57 +33,41 @@ def reportTokens(prompt):
}

# Send the API request
response = openai.ChatCompletion.create(**params)
keep_trying = True
numTries = 0
while keep_trying and numTries < 5:
try:
numTries += 1
response = openai.ChatCompletion.create(**params)
keep_trying = False
except Exception as e:
# e.g. when the API is too busy, we don't want to fail everything
print("Failed to generate response. Error: ", e)
sleep(numTries) # linear backoff
print("Retrying...")

# Get the reply from the API response
reply = response.choices[0]["message"]["content"]
return reply


@stub.function()
def generate_file(filename, model=DEFAULT_MODEL, filepaths_string=None, shared_dependencies=None, prompt=None):
def generate_file(
filename,
model=DEFAULT_MODEL,
filepaths_string=None,
shared_dependencies=None,
prompt=None,
):
systemPrompt, userPrompt = filePrompt(prompt, filepaths_string, filename)

# call openai api with this prompt
filecode = generate_response.call(model,
f"""You are an AI developer who is trying to write a program that will generate code for the user based on their intent.

the app is: {prompt}

the files we have decided to generate are: {filepaths_string}

the shared dependencies (like filenames and variable names) we have decided on are: {shared_dependencies}

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.
""",
f"""
We have broken up the program into per-file generation.
Now your job is to generate only the code for the file {filename}.
Make sure to have consistent filenames if you reference other files we are also generating.

Remember that you must obey 3 things:
- you are generating code for the file {filename}
- do not stray from the names of the files and the shared dependencies we have decided on
- MOST IMPORTANT OF ALL - the purpose of our app is {prompt} - every line of code you generate must be valid code. Do not include code fences in your response, for example

Bad response:
```javascript
console.log("hello world")
```

Good response:
console.log("hello world")

Begin generating the code now.

""",
)
filecode = generate_response(model, systemPrompt, userPrompt)

return filename, filecode


@stub.local_entrypoint()
def main(prompt, directory=DEFAULT_DIR, model=DEFAULT_MODEL, file=None):
# read file from prompt if it ends in a .md filetype
# read prompt from file if it ends in a .md filetype
if prompt.endswith(".md"):
with open(prompt, "r") as promptfile:
prompt = promptfile.read()
Expand All @@ -111,15 +76,14 @@ def main(prompt, directory=DEFAULT_DIR, model=DEFAULT_MODEL, file=None):
# print the prompt in green color
print("\033[92m" + prompt + "\033[0m")

# example prompt:
# a Chrome extension that, when clicked, opens a small window with a page where you can enter
# a prompt for reading the currently open page and generating some response from openai

# call openai api with this prompt
filepaths_string = generate_response.call(model,
"""You are an AI developer who is trying to write a program that will generate code for the user based on their intent.

When given their intent, create a complete, exhaustive list of filepaths that the user would write to make the program.

only list the filepaths you would write, and return them as a python list of strings.
do not add any other explanation, only return a python list of strings.
""",
filepaths_string = generate_response(
model,
planPrompt(),
prompt,
)
print(filepaths_string)
Expand All @@ -137,61 +101,58 @@ def main(prompt, directory=DEFAULT_DIR, model=DEFAULT_MODEL, file=None):
if file is not None:
# check file
print("file", file)
filename, filecode = generate_file(file, model=model, filepaths_string=filepaths_string, shared_dependencies=shared_dependencies, prompt=prompt)
filename, filecode = generate_file(
file,
model=model,
filepaths_string=filepaths_string,
shared_dependencies=shared_dependencies,
prompt=prompt,
)
write_file(filename, filecode, directory)
else:
clean_dir(directory)

# understand shared dependencies
shared_dependencies = generate_response.call(model,
"""You are an AI developer who is trying to write a program that will generate code for the user based on their intent.

In response to the user's prompt:

---
the app is: {prompt}
---

the files we have decided to generate are: {filepaths_string}

Now that we have a list of files, we need to understand what dependencies they share.
Please name and briefly describe what is shared between the files we are generating, including exported variables, data schemas, id names of every DOM elements that javascript functions will use, message names, and function names.
Exclusively focus on the names of the shared dependencies, and do not add any other explanation.
""",
prompt,
shared_dependencies = generate_response(
model, planPrompt2(sourcePrompt, filepaths_string), prompt
)
print(shared_dependencies)
# write shared dependencies as a md file inside the generated directory
write_file("shared_dependencies.md", shared_dependencies, directory)

# Iterate over generated files and write them to the specified directory
for filename, filecode in generate_file.map(
list_actual, order_outputs=False, kwargs=dict(model=model, filepaths_string=filepaths_string, shared_dependencies=shared_dependencies, prompt=prompt)
):
write_file(filename, filecode, directory)

for name in list_actual:
filename, filecode = generate_file(
name,
model=model,
filepaths_string=filepaths_string,
shared_dependencies=shared_dependencies,
prompt=prompt,
)
write_file(filename, filecode, directory)

except ValueError:
print("Failed to parse result")
print("Failed to parse result: " + result)


def write_file(filename, filecode, directory):
# Output the filename in blue color
print("\033[94m" + filename + "\033[0m")
print(filecode)

file_path = os.path.join(directory, filename)
dir = os.path.dirname(file_path)
if __name__ == "__main__":
# Check for arguments
if len(sys.argv) < 2:
# Looks like we don't have a prompt. Check if prompt.md exists
if not os.path.exists("prompt.md"):
# Still no? Then we can't continue
print("Please provide a prompt")
sys.exit(1)

# Check if the filename is actually a directory
if os.path.isdir(file_path):
print(f"Error: {filename} is a directory, not a file.")
return
# Still here? Assign the prompt file name to prompt
prompt = "prompt.md"

os.makedirs(dir, exist_ok=True)
else:
# Set prompt to the first argument
prompt = sys.argv[1]

# Open the file in write mode
with open(file_path, "w") as file:
# Write content to the file
file.write(filecode)
# Pull everything else as normal
directory = sys.argv[2] if len(sys.argv) > 2 else generatedDir
file = sys.argv[3] if len(sys.argv) > 3 else None

# Run the main function
main(prompt, directory, file)
Loading