7
7
import random
8
8
9
9
10
-
11
10
MAX_SUPPORTED_INPUT_LENGTH = 4096
12
11
USE_STREAM_FEATURE = True
13
12
SET_TEMPERATURE_NOISE = False
14
- MAX_TOKENS_DEFAULT = 64
13
+ MAX_TOKENS_DEFAULT = 128
15
14
16
15
STREAM = True
17
16
API_KEYS_LOCATION = "./config"
@@ -51,19 +50,15 @@ def initialize_openai_api():
51
50
52
51
53
52
def create_input_prompt (length = 3000 ):
54
- input_prompt = ''
55
- files_sorted_by_mod_date = sorted (os .listdir ('.' ), key = os .path .getmtime )
56
- # Reverse sorted files.
57
- files_sorted_by_mod_date = files_sorted_by_mod_date [::- 1 ]
58
- for filename in files_sorted_by_mod_date :
59
- if filename == PYTHON_FILE_TO_CONVERT :
60
- with open (filename ) as f :
61
- input_prompt += '\n ===================\n # ' + filename + ':\n '
62
- input_prompt += f .read () + '\n '
53
+ inputPrompt = ''
54
+ filename = PYTHON_FILE_TO_CONVERT
55
+ with open (filename ) as f :
56
+ inputPrompt += '\n ===================\n # ' + filename + ':\n '
57
+ inputPrompt += f .read () + '\n '
63
58
64
- input_prompt = input_prompt [:length ]
65
- input_prompt += '\n \n ===================\n # ' + 'C++ Code:' + '\n '
66
- return input_prompt
59
+ inputPrompt = inputPrompt [:length ]
60
+ inputPrompt += '\n \n ===================\n # ' + 'C++ Code:' + '\n '
61
+ return inputPrompt
67
62
68
63
69
64
def generate_completion (input_prompt , num_tokens ):
@@ -77,14 +72,14 @@ def generate_completion(input_prompt, num_tokens):
77
72
78
73
79
74
def get_generated_response (response ):
80
- generated_file = "// C++ Code generated from Python Code: \n "
75
+ generatedFile = "// C++ Code generated from Python Code: \n "
81
76
while True :
82
- next_response = next (response )
83
- completion = next_response ['choices' ][0 ]['text' ]
84
- generated_file = generated_file + completion
85
- if next_response ['choices' ][0 ]['finish_reason' ] is not None :
77
+ nextResponse = next (response )
78
+ completion = nextResponse ['choices' ][0 ]['text' ]
79
+ generatedFile = generatedFile + completion
80
+ if nextResponse ['choices' ][0 ]['finish_reason' ] is not None :
86
81
break
87
- return generated_file
82
+ return generatedFile
88
83
89
84
90
85
def write_cpp_file (textResponse ):
@@ -96,21 +91,21 @@ def write_cpp_file(textResponse):
96
91
f .close ()
97
92
98
93
99
- def test_cpp_compilation (cpp_file ):
94
+ def test_cpp_compilation (cppFile ):
100
95
"""
101
96
Checks if the generated file is compilable using g++
102
97
"""
103
- exe_file = cpp_file .split ("." )[0 ] + ".exe"
104
- if os .system ("g++ " + cpp_file + " -o " + exe_file + " &> /dev/null" ) == 0 :
98
+ exeFile = cppFile .split ("." )[0 ] + ".exe"
99
+ if os .system ("g++ " + cppFile + " -o " + exeFile + " &> /dev/null" ) == 0 :
105
100
return True
106
101
else :
107
102
return False
108
103
109
104
110
- def iterate_for_compilable_solution (prompt , max_iterations ):
105
+ def iterate_for_compilable_solution (prompt , maxIterations ):
111
106
print ('Iterating for a compilable C++ solution ...' )
112
107
print ()
113
- for it in range (max_iterations ):
108
+ for it in range (maxIterations ):
114
109
response = generate_completion (prompt , num_tokens = MAX_TOKENS_DEFAULT )
115
110
textResponse = get_generated_response (response )
116
111
write_cpp_file (textResponse )
@@ -122,14 +117,15 @@ def iterate_for_compilable_solution(prompt, max_iterations):
122
117
print ("C++ File: {}" .format (fileName + ".cpp" ))
123
118
print ("Compiled Executable: {}" .format (fileName + ".exe" ))
124
119
break
125
- if it == max_iterations - 1 :
120
+ if it == maxIterations - 1 :
126
121
print ('Unfortunately CODEX did not find a compilable solution. Still you can find the generated code '
127
122
'in the file: {}' .format (fileName + ".cpp" ))
128
123
129
124
130
125
if __name__ == "__main__" :
131
126
initialize_openai_api ()
132
127
prompt = create_input_prompt ()
133
- iterate_for_compilable_solution (prompt = prompt , max_iterations = 5 )
128
+ #print(prompt)
129
+ iterate_for_compilable_solution (prompt = prompt , maxIterations = 5 )
134
130
135
131
0 commit comments