-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathgenerate_comment.py
More file actions
90 lines (77 loc) · 3.33 KB
/
generate_comment.py
File metadata and controls
90 lines (77 loc) · 3.33 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
import os
import sys
import requests
import logging
from openai import OpenAI
from github import Github
logger = logging.getLogger()
formatter = logging.Formatter("%(message)s")
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
logger.setLevel(logging.INFO)
GIT_TOKEN = os.environ.get('GIT_TOKEN')
GIT_REPO = os.environ.get('GITHUB_REPOSITORY')
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
if OPENAI_API_KEY is None:
raise ValueError("You need to specify OPENAI_API_KEY environment variable!")
client = OpenAI(api_key=OPENAI_API_KEY)
def get_pr_files(pr_number):
g = Github(os.getenv('GIT_TOKEN'))
repo = g.get_repo(os.getenv('GITHUB_REPOSITORY'))
pr = repo.get_pull(pr_number)
files = pr.get_files()
changes = [f.filename for f in files]
return changes
def get_feedback(filename: str, system_prompt: str, user_prompt: str) -> str:
comment = ''
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
temperature=0.2
)
comment = response.choices[0].message.content
text = f"This is a LLM-generated comment for `{filename}`: \n{comment if comment else 'Tests passed. No feedback generated for testing purposes.'}"
return text
def post_github_comment(git_token, repo, pr_number, comment, filename):
url = f"https://api.github.com/repos/{repo}/issues/{pr_number}/comments"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {git_token}",
"X-GitHub-Api-Version": "2022-11-28"
}
data = {"body": comment}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 201:
logger.error(f"Failed to create comment. Status code: {response.status_code} \n{response.text}")
raise Exception(f"Failed to create comment. Status code: {response.status_code} \n{response.text}")
logger.info(f"✅ Added review comment for {filename} at https://github.com/{repo}/pull/{pr_number}")
def main(files_to_process: list):
if not files_to_process:
logger.warning('No file changes were detected in the current push so no comments were generated. Please modify one or more of your submission files to receive LLM-generated feedback.')
return None
local_solutions_dir = os.path.join(os.getcwd(), 'src')
os.makedirs(local_solutions_dir, exist_ok=True)
system_prompt = """
You are a senior software engineer looking to give feedback on every PR you see in this repo.
Make sure to be as nit picky and annoying as possible!
"""
for filename in files_to_process:
logger.info("processing:" +filename)
try:
with open(filename, 'r') as file:
content = file.read()
print(content)
prompt = f"Make sure this file {filename} with this content: {content} follows all the right naming and python conventions. Make any call outs you see!"
comment = get_feedback(filename, system_prompt, prompt)
if GIT_TOKEN and GIT_REPO and pr_number:
post_github_comment(GIT_TOKEN, GIT_REPO, pr_number, comment, filename)
except Exception as e:
print(e)
if __name__ == "__main__":
pr_number = int(sys.argv[1])
changes = get_pr_files(pr_number)
main(changes)