-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreviewer.py
114 lines (92 loc) · 5.54 KB
/
reviewer.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
from langchain_community.llms import Ollama
from langchain_core.prompts import PromptTemplate
from datetime import datetime
from file_helper import create_file
import re
import os
# For more information on LangChain Ollama API: https://github.com/ollama/ollama/blob/main/docs/api.md
llm = Ollama(
base_url='http://localhost:11434',
model="llama3"
)
def get_code_review(review_filename, code) -> None:
# prompt_template = PromptTemplate.from_template("Please review the following code, provide suggestions as bullet point list, and examples if needed in markdown format:\n---\n\n\"\"\"\n{code}\n\"\"\"")
prompt_template = PromptTemplate.from_template("""
Carefully review following piece of code and give your recommendations. Your response should include:
- Code snippets or examples where relevant highlighting the changes.
- A complete revised version of the code if necessary.
- Format the response using markdown specification.
{code}
Code Review Template:
## Code Review Summary:
- This code snippet is written in...
- Provide a walk though of the code
### Recommendation 1
Original Code:
```
```
Revised Code:
```
```
Provide a short explanation here.
### Recommendation 2
Original Code:
```
```
Revised Code:
```
```
Provide a short explanation here.
""")
#escaped_code = code.replace('"', '\\"').replace('\n', '\\n')
prompt = prompt_template.invoke({"code": code})
# print(prompt)
review = llm.invoke(prompt)
review_filename = f"reviews/REVIEW_{remove_file_ext(review_filename)}_BY_{llm.model}.md"
create_file(review_filename, review)
def remove_comments(code: str) -> str:
# Remove single-line comments
code = re.sub(r'#.*', '', code)
# Remove multi-line comments
code = re.sub(r'""".*?"""', '', code, flags=re.DOTALL)
code = re.sub(r"'''.*?'''", '', code, flags=re.DOTALL)
return code
def remove_file_ext(filename: str) -> str:
extension = filename.rsplit('.', 1)[-1]
filename_without_ext = filename.rsplit('.', 1)[0]
return filename_without_ext
def read_file(file_path: str) -> str:
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
return content
def review_file(file_path: str) -> None:
code = read_file(file_path)
code = remove_comments(code)
file_name = os.path.basename(file_path)
get_code_review(file_name, code)
def review_files(folder_path: str, exclude_keywords=None) -> None:
if exclude_keywords is None:
exclude_keywords = []
supported_extensions = [".py", ".ts", ".js", ".jsx"]
for root, dirs, files in os.walk(folder_path):
for file in files:
if any(file.endswith(ext) for ext in supported_extensions):
file_path = os.path.join(root, file)
# Check if the file path contains any of the exclude keywords
if any(keyword in file_path for keyword in exclude_keywords):
# Skip the file if it contains an exclude keyword
continue
try:
code = read_file(file_path)
# Remove comments from the code
code = remove_comments(code)
review = get_code_review(file, code)
if review:
lines = review.split("\n")
for line in lines:
if ":" in line:
line_number, suggestion = line.split(":", 1)
print(f"File: {file}, Line: {line_number.strip()}, Suggestion: {suggestion.strip()}")
except UnicodeDecodeError:
print(f"Skipping file: {file_path} (UnicodeDecodeError)")
continue