-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_code.py
57 lines (46 loc) · 2.39 KB
/
generate_code.py
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
import os
import csv
import sys
from contexts import ModelContext, ProblemContext, RunContext
from querier import HumanQuerier, OpenAIQuerier
def handle_problem(problemContext, querier, solutions_per_problem):
for runContext in problemContext.GetRunContexts(solutions_per_problem):
prompts = problemContext.prompts()
# print(prompts)
for type, content in prompts.items():
outputPath = os.path.join(runContext.generatedPath(), type + ".py")
if not os.path.exists(outputPath):
print(f"Prompting for problem {problemContext.problemName()} with prompt type {type}")
functionPrototype = problemContext.functionPrototype()
if "generic" in type:
functionPrototype = functionPrototype.genericize()
instructions = f"Generate a Python function adhering to the following prompt: {content}\n\nUse the following prototype: {functionPrototype}\n\nProvide only the function asked for. If you need to use helper functions, they should be defined before the function. Do not include tests or any other information. Your response will be directly executed and tested."
response = querier.performQuery(instructions)
print('-' * 60)
print(response)
print('-' * 60)
with open(outputPath, 'w') as f:
f.write(response)
if __name__ == "__main__":
# Check if the user has provided a command-line argument
if len(sys.argv) < 5:
print("Please provide correct arguments.")
sys.exit(1)
folder_path = sys.argv[1]
model_name = sys.argv[2]
querier_type = sys.argv[3]
solutions_per_problem = int(sys.argv[4])
if querier_type == "human":
querier = HumanQuerier()
elif querier_type == "openai":
querier = OpenAIQuerier(model_name)
else:
print(f"Querier type {querier_type} unknown.")
sys.exit(1)
# Check if the given folder path exists
if os.path.exists(folder_path):
modelContext = ModelContext(model_name, folder_path)
for problemContext in modelContext.GetProblemContexts():
handle_problem(problemContext, querier, solutions_per_problem)
else:
print("The provided folder path does not exist.")