-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenAI.js
127 lines (104 loc) · 3.8 KB
/
openAI.js
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
// https://medium.com/@samho1996/how-do-i-make-use-of-chatgpt-to-review-my-code-33efd8f42178
require("dotenv").config();
const { Configuration, OpenAIApi } = require("openai");
const fs = require("node:fs");
const reset = "\x1b[0m";
const green = "\x1b[32m";
const getReview = async (openai, code, shouldOverwrite) => {
const promptReview = `
Review the code below and, without summarizing, provide feedback on how to improve it. Your task is:
- provide relevant feedback no matter what kind of input
- focus only on negative parts.
- Check for bugs and highlight them.
- Ensure adherence to best practices and project guidelines:
- Code readability, maintainability, and documentation.
- Consistent naming conventions and coding style.
- Modular and organized functions and classes.
- Analyze performance and optimization:
- Identify potential performance bottlenecks.
- Suggest optimizations or efficient algorithms.
- Evaluate code reusability:
- Encourage use of existing libraries, frameworks, or code snippets.
- Identify opportunities for reusable components or patterns.
- Confirm cross-browser and cross-platform compatibility:
- Provide security recommendations, if applicable.
- Provide feedback as the numbered list.
- Sort issues from major to minor.
Code follows:
${code}
`;
const promptImprove = `
Review the following code for significant improvements.
Instructions:
- Preserve all comments in the file, even if they are deemed unnecessary.
- Do not add any comments.
- Ignore comments when generating file improvement suggestions.
Format the response as follows:
- Start a bulleted list with the header "Changes made to the file:"
- List the changes made to the file and provide reasons for each change.
- Follow the description with the updated file contents.
- Use this identifier once at the start of the updated file contents: "**********"
- Do not include anything after the contents of the updated file.
- Do not include any metadata like "Updated file contents:" after the identifier.
${code}
`;
const messages = [
{
role: "system",
content: "You are the best engineer and code reviewer at the company.",
},
{
role: "user",
content: shouldOverwrite ? promptImprove : promptReview,
},
];
const request = {
model: "gpt-3.5-turbo",
messages,
};
const completion = await openai.createChatCompletion(request);
const review = completion.data?.choices[0]?.message?.content;
return review;
};
const main = async () => {
const shouldOverwrite = process.argv.includes("--overwrite");
let filePaths = process.argv.slice(2);
if (!filePaths.length) {
console.error("Please provide at least one file path.");
process.exit(1);
}
// Filter out the `--overwrite` flag from the file paths
filePaths = filePaths.filter((path) => path !== "--overwrite");
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
console.error("Please set the OPENAI_API_KEY environment variable.");
process.exit(1);
}
const configuration = new Configuration({ apiKey });
const openai = new OpenAIApi(configuration);
for (const filePath of filePaths) {
try {
const code = fs.readFileSync(filePath, "utf-8");
if (!code) {
console.error(`File ${filePath} is empty or invalid.`);
continue;
}
const response = await getReview(openai, code, shouldOverwrite);
// biome-ignore lint/suspicious/noConsoleLog: ok
console.log(`${green}Review ${filePath}:${reset}\n${response}${reset}\n`);
if (shouldOverwrite) {
fs.writeFileSync(filePath, response, "utf-8");
// biome-ignore lint/suspicious/noConsoleLog: ok
console.log(
`File ${filePath} has been overwritten with the suggested rewrite.`,
);
}
} catch (error) {
console.error(
`An error occurred during code review of ${filePath}:`,
error,
);
}
}
};
void main();