-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-autocoder.py
More file actions
130 lines (103 loc) · 4.2 KB
/
python-autocoder.py
File metadata and controls
130 lines (103 loc) · 4.2 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
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from langchain.llms import OpenAI
import os
import io
import sys
import traceback
import re
import time
# Set env variables here or in .env
os.environ["OPENAI_API_BASE"] = "http://localhost:443/v1" # comment this in if using gpt-llama.cpp
os.environ["OPENAI_API_KEY"] = "../llama.cpp/models/wizardLM/7B/wizardLM-7B.GGML.q5_1.bin"
code_generated = False
attempt_num = 1
def extract_code(response):
code_blocks = re.findall(r"```(?:python|bash)?\s*[\s\S]*?```", response)
extracted_code = {"python": [], "bash": []}
for code_block in code_blocks:
code_type, code = re.match(
r"```(python|bash)?\s*([\s\S]*?)```", code_block
).groups()
code_type = code_type or "python"
extracted_code[code_type].append(code.strip())
if len(extracted_code['python']) == 0:
return response
else:
return "\n".join(extracted_code['python']) # only return python for now
# Simple completion example
llm = OpenAI(temperature=0.7)
# text = """Write python code that takes in a list and sorts it:"""
# code_request = "function that takes in a string and returns true if its a palindrome, false otherwise:"
# code_request = "function that extracts the code within ```python ``` in a given string"
code_request = "function that extracts the name and email from a user json object"
text = f"""Write python code that implements the following:
{code_request}
Code:"""
print('\n===== REQUEST =====')
print(text)
print('\n===== CODE GENERATED =====')
result = llm(text)
result = extract_code(result)
print(result)
while not code_generated:
time.sleep(1) # sleep for 1 second to avoid too many loop bois
# Compile the code string into a code object
# code = compile("print('hello world')", "<string>", "exec")
print('\n===== CODE EXECUTION RESULT =====')
stdout_backup = sys.stdout
sys.stdout = io.StringIO()
try:
code = compile(result, "<string>", "exec")
exec(code)
# Capture the output from stdout and reset stdout to its original value
output = sys.stdout.getvalue()
sys.stdout = stdout_backup
# Return the output as a string
output = output.strip()
if len(output) == 0:
output = 'Missing example usage: Please provide an a few example usages of the given function and print out the output of the function'
has_error = True
else:
has_error = False
except Exception as e:
# Print any exceptions that occurred during execution
output = traceback.format_exc()
has_error = True
finally:
# Reset stdout to its original value
sys.stdout = stdout_backup
print(output)
if not has_error:
print('\n==== CHECK OUTPUT VALIDITY ====')
check_code_template = """
``` python
{output}
```
Given above python script execution output delimited by triple backticks, determine if the output satisfies the given instruction.\
There should not be any errors in the output, and should print the expected output outlined in the instruction. \
If it satisfies all the above conditions, then expected output is 'Yes', otherwise, it is 'No'.
Instruction: {query}
Expected Output? (yes/no):"""
prompt = PromptTemplate(
template=check_code_template, input_variables=["query", "output"])
llm_chain = LLMChain(prompt=prompt, llm=llm)
result = llm_chain.run({'query': text, 'output': output})
print(result)
if result.lower() == 'yes':
code_generated = True
print('SUCCESS!')
else:
attempt_num += 1
print(f"\n\n============================== ATTEMPT #{attempt_num} ==============================")
print('\n==== CODE GENERATED (ATTEMPTED FIX) ====')
attempt_fix = f"""Your task is to fix python code that's outputting an error:
Code:
{result}
The following error occurred while running the above code:
{output}
Fix the error in the code specifically based on the above error description. Do NOT return the same piece of code.
Fixed Code:"""
result = llm(attempt_fix)
result = extract_code(result)
print(result)