-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_generator.py
More file actions
159 lines (124 loc) · 7.14 KB
/
Copy pathcode_generator.py
File metadata and controls
159 lines (124 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import ast
import argparse
from utils import write_file, clean_dir, generate_response
def generate_file(filename, filepaths_string=None, shared_dependencies=None, prompt=None):
""" generate code for a single file """
filecode = generate_response(
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.
""",
)
return filename, filecode
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="This is a demo script.")
parser.add_argument('--p','-prompt', type=str, help='prompt file')
parser.add_argument('--f', '-file', type=str, help='file to update')
args = parser.parse_args()
provider = "OEC"
dataset_name = "ECI_trade"
base_directory = "./pipelines"
lambda_name = "lambda_data_to_s3"
directory = f"{base_directory}/provider={provider}/dataset_name={dataset_name}"
frequency = "every day at 21:00"
bucket_name = f'sumeo-provider-{provider}-data'.lower()
prompt = f"""
1- Lambda Function:
Develop a Python file named 'lambda_handler'. This function should act as a Lambda function handler and accept two arguments: event and context.
Invoke the endpoint url saved in env variable {provider}_{dataset_name}_ENDPOINT and store the response payload in a bucket named 'sumeo-{provider}' within the directory data_profile=raw/dataset_name={dataset_name}/.
Generate the file name by creating an MD5 hash of the data.
Save the file in the bucket {bucket_name}, using the path data_profile=raw/provider_name={provider}/dataset_name={dataset_name}/request_time=request_day/, where request_day is the day of the request in UTC.
Append metadata to the file with the following information:
Request time in UTC
Provider: {provider}
Dataset name: {dataset_name}
The function should return a dictionary with statusCode set to 200.
Ensure all import statements are at the top of the file.
Create a requirements.txt file containing the necessary dependencies (excluding hashlib, which is already imported).
2- Deployment on AWS using the Serverless Framework:
Generate a serverless.yml file in the base directory. This file should define:
A Lambda function named {lambda_name} triggered at a certain {frequency}.
An S3 bucket named {bucket_name} lowercase with read and write access for the Lambda function.
Set useDotenv: true at the top of the file
A shell script file to install the required Serverless plugins globally
The AWS region set to 'eu-west-1'.
use plugin serverless-python-requirements:
custom:
pythonRequirements:
dockerizePip: true
3- .env File:
Construct a .env.template file in the base directory, housing the necessary environment variables for the serverless.yml file.
"""
# print the prompt in green color
print("\033[92m" + prompt + "\033[0m")
# call openai api with this prompt
filepaths_string = generate_response(
"""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.
""",
prompt,
)
print(filepaths_string)
# parse the result into a python list
list_actual = []
try:
list_actual = ast.literal_eval(filepaths_string)
# if shared_dependencies.md is there, read it in, else set it to None
shared_dependencies = None
if os.path.exists("shared_dependencies.md"):
with open("shared_dependencies.md", "r") as shared_dependencies_file:
shared_dependencies = shared_dependencies_file.read()
if args.f is not None :
## update one file
print("file", args.f )
filename, filecode = generate_file(args.f , filepaths_string=filepaths_string, shared_dependencies=shared_dependencies, prompt=prompt)
write_file(filename, filecode, directory)
else:
## generate complete codebase
clean_dir(directory)
# understand shared dependencies
shared_dependencies = generate_response(
"""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,
)
print(shared_dependencies)
# write shared dependencies as a md file inside the generated directory
write_file("shared_dependencies.md", shared_dependencies, directory)
# Existing for loop
files_to_generate = []
for filename in list_actual:
filename, filecode = generate_file(filename, filepaths_string=filepaths_string, shared_dependencies=shared_dependencies, prompt=prompt)
write_file(filename, filecode, directory)
except ValueError:
print("Failed to parse result: " + filepaths_string)