forked from dvlpjrs/gavin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_updater.py
167 lines (154 loc) · 5.03 KB
/
code_updater.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
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
160
161
162
163
164
165
166
167
import os
from groq import Groq
from decouple import config
import re
import json
from openai import OpenAI
from utils import prompts
# Set environment variables
os.environ["GROQ_API_KEY"] = config("GROQ_API_KEY")
os.environ["OPENAI_API_KEY"] = config("OPENAI_API_KEY")
# Initialize clients
client = Groq()
openai_client = OpenAI()
def list_all_files(directory):
"""List all files in a directory."""
file_paths = []
for root, _, files in os.walk(directory):
for file in files:
file_paths.append(os.path.join(root, file))
return file_paths
def check_issue_support(title, body):
"""Check if the issue is supported by the bot."""
completion = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{
"role": "system",
"content": prompts.CHECK_ISSUE_SUPPORT_PROMPT,
},
{
"role": "user",
"content": f"title: {title}\nbody: {body}",
},
],
)
return completion.choices[0].message.content.lower() == "true"
def gen_code_file_summary(file_path):
"""Generate a summary of the code file."""
with open(file_path, "r") as file:
lines = file.readlines()
completion = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{
"role": "system",
"content": prompts.GEN_CODE_FILE_SUMMARY_PROMPT,
},
{"role": "user", "content": "\n".join(lines)},
],
max_tokens=1024,
)
return f"{file_path} - {completion.choices[0].message.content}"
def get_files_to_modify(issue):
"""Get the files that need to be modified to fix the issue."""
with open("ai_summary.txt", "r") as file:
lines = file.read()
completion = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{
"role": "system",
"content": f"{prompts.GET_FILES_TO_MODIFY_PROMPT}\nIssue: {issue}",
},
{"role": "user", "content": lines},
],
max_tokens=1024,
)
return completion.choices[0].message.content.split("\n")
def fix_code(file_paths, issue):
"""Fix the issue in the provided files."""
prompt = f"The issue faced: {issue}\n"
for x in file_paths:
if not os.path.exists(x):
continue
prompt += f"Path: {x}\n"
with open(x, "r") as file:
prompt += file.read()
print(prompt)
completion = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{
"role": "system",
"content": prompts.FIX_CODE_PROMPT,
},
{"role": "user", "content": prompt},
],
)
modified_code = completion.choices[0].message.content
return json.loads(modified_code)
def update_code(file_paths, code):
"""Update the code in the provided file paths."""
for x in file_paths:
with open(x, "w") as file:
file.write(code)
def validate_result(issue, modified_code):
"""Validate the result of the code modification."""
prompt = f"issue: {issue}\nmodified_code: {modified_code}\n"
validate_count = 0
completion1 = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{
"role": "system",
"content": prompts.VALIDATE_RESULT_PROMPT,
},
{"role": "user", "content": prompt},
],
)
completion2 = client.chat.completions.create(
model="mixtral-8x7b-32768",
messages=[
{
"role": "system",
"content": prompts.VALIDATE_RESULT_PROMPT,
},
{"role": "user", "content": prompt},
],
)
completion3 = openai_client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": prompts.VALIDATE_RESULT_PROMPT,
},
{"role": "user", "content": prompt},
],
)
if completion1.choices[0].message.content.lower() == "true":
validate_count += 1
if completion2.choices[0].message.content.lower() == "true":
validate_count += 1
if completion3.choices[0].message.content.lower() == "true":
validate_count += 1
print(validate_count)
return validate_count >= 2
def run(code_dir, issue_body):
"""Run the code updater process."""
ai_summary = ""
temp = list_all_files(code_dir)
for x in temp:
try:
ai_summary += gen_code_file_summary(x) + "\n"
except:
pass
with open("ai_summary.txt", "w") as file:
file.write(ai_summary)
files_to_modify = get_files_to_modify(issue_body)
new_code = fix_code(files_to_modify, issue_body)
if not validate_result(issue_body, new_code):
raise Exception("Code not validated")
for x in new_code:
update_code([x["path"]], x["code"])