Skip to content

Commit 7a292e8

Browse files
committed
code generation added
1 parent 1f00b1e commit 7a292e8

File tree

4 files changed

+117
-15
lines changed

4 files changed

+117
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.env
22
__pycache__
33
code
4-
.venv
4+
.venv
5+
thoughts

lazydev/modules/developer.py

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

2+
import sys
23
from typing import List
4+
import os
35
from .prompts import PromptBook
46
from langchain.llms import OpenAI
57
from langchain.chat_models import ChatOpenAI
@@ -24,28 +26,35 @@ def __init__(self, requirement:str,root_dir:str, openai_api_key:str):
2426
self.requirement=requirement
2527
self.root_dir=root_dir
2628
self.api_key=openai_api_key
29+
self.files_written=[]
2730
self.brain = ChatOpenAI(
2831
model="gpt-3.5-turbo",
2932
openai_api_key=self.api_key,
30-
temperature=0,
33+
temperature=0.1,
3134
streaming=False
3235
)
33-
34-
def brain_storm(self,prompt:str)->str:
36+
37+
38+
def brain_storm(self,prompt:str,step_name="prompt")->str:
3539
messages = [
3640
SystemMessage(content="you are a senior software developer"),
3741
HumanMessage(content=prompt)
3842
]
3943
aiMessage:AIMessage= self.brain(messages=messages)
44+
if not os.path.exists("./thoughts"):
45+
os.makedirs("./thoughts")
46+
if os.path.exists(f"./thoughts/{step_name}.md"):
47+
os.remove(f"./thoughts/{step_name}.md")
48+
Utilities.write_to_file(f"{prompt}\n\n{aiMessage.content}",f"./thoughts/{step_name}.md")
4049
return aiMessage.content
4150

4251
def clear_doubts(self):
4352
prompt=PromptBook.expand_requirements(self.requirement)
44-
doubts=self.brain_storm(prompt)
53+
doubts=self.brain_storm(prompt,'clear-doubts')
4554
doubt_list:List[str]=doubts.split("\n")
4655
doubt_list=[doubt.strip() for doubt in doubt_list if doubt.strip()!=""]
4756
print("""
48-
Hey there! 😄 It's Lazy Dev, your friendly neighborhood programmer, here to make your API service dreams come true! 🎉
57+
Hey there! 😄 It's Lazy Dev, your friendly neighborhood programmer, here to make your awesome project's dreams come true! 🎉
4958
But before I dive into coding magic, I have a few fun and important questions for you.
5059
So, grab a cup of coffee ☕️, sit back, and let's clarify some details, shall we? Here we go! 🚀
5160
""")
@@ -67,7 +76,7 @@ def clear_doubts(self):
6776

6877
def plan_project(self):
6978
prompt=PromptBook.plan_project(self.requirement,self.clarifications)
70-
plannings:str=self.brain_storm(prompt)
79+
plannings:str=self.brain_storm(prompt,'plan-project')
7180
return plannings
7281

7382
def generate_folder_structure(self):
@@ -76,9 +85,52 @@ def generate_folder_structure(self):
7685
plan=self.plannings,
7786
clarifications=self.clarifications
7887
)
79-
folder_tree_str:str=self.brain_storm(prompt)
80-
folder_tree:dict=json.loads(folder_tree_str)
81-
Utilities.generate_files_and_folders(structure=folder_tree,root_dir=self.root_dir)
88+
retry_count=3
89+
while retry_count>0:
90+
try:
91+
92+
folder_tree_str:str=self.brain_storm(prompt,"generate-filders")
93+
folder_tree:dict=json.loads(folder_tree_str)
94+
break
95+
except:
96+
print("Opps messed up the json format, let me try again")
97+
retry_count=retry_count-1
98+
99+
if retry_count==0:
100+
print("Sorry I was not able to create the folder structure in json correct format, check my instructions and try to refine it sothat i can understan the task better")
101+
sys.exit()
102+
self.root_folder_name, self.file_paths = Utilities.generate_files_and_folders(structure=folder_tree,root_dir=self.root_dir)
103+
104+
105+
def prioratize_files(self):
106+
prompt=PromptBook.prioritise_file_list(self.file_paths)
107+
retry_count=3
108+
while retry_count>0:
109+
try:
110+
file_paths_str=self.brain_storm(prompt,'prioratize_files')
111+
break
112+
except:
113+
print("Opps messed up the json format, let me try again")
114+
retry_count=retry_count-1
115+
if retry_count==0:
116+
print("Sorry I was not able to create the file list in correct format, check my instructions and try to refine it sothat i can understan the task better")
117+
sys.exit()
118+
self.file_paths=file_paths_str.split("\n")
119+
120+
def write_file_content(self,file_path):
121+
prompt=PromptBook.write_file(
122+
question=self.requirement,
123+
clarifications=self.clarifications,
124+
plan=self.plannings,
125+
files_written=self.files_written,
126+
file_path_to_write=file_path
127+
)
128+
code=self.brain_storm(prompt,f'code-{file_path.split("/")[-1]}')
129+
Utilities.write_to_file(code,file_path=file_path)
130+
self.files_written.append((
131+
file_path,
132+
code
133+
))
82134

83135
def develop(self):
84136
# clearing all doubts
@@ -93,6 +145,13 @@ def develop(self):
93145
# creating files and folders for the project
94146
print("Creating files...")
95147
self.generate_folder_structure()
148+
self.prioratize_files()
149+
self.files_written=[]
150+
for file_path in self.file_paths:
151+
file_name=file_path.split("/")[-1]
152+
print(f"\nWriting Code for :{file_name}")
153+
self.write_file_content(file_path)
154+
96155

97156

98157

lazydev/modules/prompts.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ def plan_project(question:str, clarification:str)->str:
3636
your current task is to design the folder and and files structure for this project
3737
you should follow the below format to answer this question
3838
39-
Plan : <your plan here> //tell us a very detailed, verbose plan or thought on how you want to proceed before writing the files for example which framework you are going to use
39+
Plan : <your plan here> //tell us a very detailed, verbose plan or thought on how you want to proceed before writing the files for example which framework you are going to use,dont forget to mention framewark related configuration files. Discuss project architecture so junior developer can understand very easily
4040
4141
As your response will go to an automated parser, things to keep in mind all the time:
4242
* follow the exact format provided above without fail
43-
* the folder and files structure should be complete, means means no additional files will be required to create to run the program, for example if its nodejs project add package.json if its a python project add requirements.txt etc..
44-
* do not write the contents of the files that will be done by the next devepoler
43+
* be detailed so junior developer can understand very easily
4544
Begin!
4645
"""
4746
@staticmethod
@@ -95,4 +94,35 @@ def prioritise_file_list(filelist:List[str])->List[str]:
9594
* do not write any explanation
9695
Begin!
9796
"""
97+
@staticmethod
98+
def write_file(question,clarifications:str,plan:str,files_written:List[List[str]], file_path_to_write:str)->str:
99+
file_with_conent="\n\n".join([f"File:{file_path}\nContent:\n{content}" for file_path,content in files_written])
100+
return f"""
101+
you are a senior programmer below is what your client have asked you to do:
102+
---
103+
{question}
104+
---
105+
here are some clarrification on the requirements
106+
---
107+
{clarifications}
108+
---
109+
below is the what you have already planed what to do:
110+
---
111+
{plan}
112+
---
113+
114+
you are now writing the code below are the files that already written with content as follows:
115+
---
116+
{file_with_conent}
117+
---
118+
119+
now you are about to write content the following file:
120+
{file_path_to_write}
121+
122+
As your response will go to an automated parser, things to keep in mind all the time:
123+
* follow the exact format provided above without fail
124+
* only write the file content, no expiation, no pretext.
125+
* always add comments at the begening, which expains what you are about to do
126+
Begin!
127+
"""
98128

lazydev/modules/utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,38 @@ def touch(path):
99

1010
@staticmethod
1111
def generate_files_recursively(files:List[Dict],cur_dir:str):
12+
file_paths=[]
1213
for file in files:
1314
name=file['name']
1415
type=file['type']
1516
item_path=os.path.join(cur_dir,name)
1617
if type=="file":
1718
Utilities.touch(item_path)
19+
file_paths.append(item_path)
1820
print(f"Created File: {item_path}")
1921
else:
2022
if not os.path.exists(item_path):
2123
os.makedirs(item_path)
2224
print(f"Created Directory: {item_path}")
2325
subfiles:List[Dict]=file['files']
24-
Utilities.generate_files_recursively(subfiles,item_path)
26+
sub_file_paths=Utilities.generate_files_recursively(subfiles,item_path)
27+
file_paths=file_paths+sub_file_paths
28+
return file_paths
2529

2630
@staticmethod
2731
def generate_files_and_folders(structure:dict,root_dir:str):
2832
root_dir_name=structure['root_dir_name']
2933
cur_dir=os.path.join(root_dir,root_dir_name)
34+
root_dir_path=cur_dir
3035
if not os.path.exists(cur_dir):
3136
os.makedirs(cur_dir)
3237
print(f"Created Directory: {cur_dir}")
3338
files:List[Dict]=structure['files']
34-
Utilities.generate_files_recursively(files=files,cur_dir=cur_dir)
39+
file_paths=Utilities.generate_files_recursively(files=files,cur_dir=cur_dir)
40+
return root_dir_path, file_paths
41+
42+
def write_to_file(content:str,file_path:str):
43+
# Open the file in write mode
44+
with open(file_path, "w") as file:
45+
# Write the content to the file
46+
file.write(content)

0 commit comments

Comments
 (0)