Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: testing differents prompts #26

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/code-review.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ jobs:
exclude: "**/*.json, **/*.md, **/*.g.dart"
append_prompt: |
- Give a maximum of 4 suggestions
- Do not suggest code formatting issues.
- Do not suggest imports issues.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line with the comment 'Do not suggest imports issues.' since it is not needed anymore.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line with the comment 'Do not suggest imports issues.'

65 changes: 48 additions & 17 deletions code-review/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,47 @@ async function getDiff(owner, repo, pull_number) {
}

async function analyzeCode(parsedDiff, prDetails) {
const comments = []; //Array<{ body: string; path: string; line: number }>
const allReviews = []; //Array<{ body: string; path: string; line: number }>

for (const file of parsedDiff) {
if (file.to === "/dev/null") continue; // Ignore deleted files
for (const chunk of file.chunks) {

const messages = createMessages(file, chunk, prDetails);
const messages = generateMessages(file, chunk, prDetails);
const aiResponse = await getAIResponse(messages);
if (aiResponse) {
const newComments = createComment(file, chunk, aiResponse);
if (newComments) {
comments.push(...newComments);
if (!isJSON(aiResponse)) {
logger.log(`AI response is not in JSON format: ${aiResponse}`);
createCommentOnPr(aiResponse, prDetails.owner, prDetails.repo, prDetails.pull_number, file.to);
} else {
logger.log(`AI response is in JSON format: ${aiResponse}`);
const reviews = generateReviewsFromJsonArray(file, chunk, aiResponse);
if (reviews) {
allReviews.push(...reviews);
}
}
}
}
}
return comments;
return allReviews;
}

function createMessages(file, chunk, prDetails) {
const instructionJsonFormat = `- Always provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]`;
function isJSON(obj) {
try {
JSON.parse(obj);
return true;
} catch (e) {
return false;
}
}

function generateMessages(file, chunk, prDetails) {

var contentSystemMessage = `You are a senior software engineer and your task is to review pull requests for possible bugs or bad development practices. Follow the instructions below:
- You will provide suggestions only if there are issues or bugs in the code, otherwise return an empty array.
const instructionJsonFormat = `You are a senior software engineer and your task is to review pull requests for possible bugs or bad development practices. Follow the instructions:
- Always provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
- You will provide suggestions only if there are issues or bugs in the code, otherwise return an empty array.`;

var contentSystemMessage = `
- Do not give positive comments or compliments.
- Don't suggest removing empty line
- Never suggest adding newline at end of file.
Expand All @@ -94,10 +111,10 @@ function createMessages(file, chunk, prDetails) {
contentSystemMessage = overridePrompt;
}

contentSystemMessage = `${contentSystemMessage}\n${instructionJsonFormat}`;
contentSystemMessage = `${instructionJsonFormat}\n${contentSystemMessage}`;

if (appendPrompt) {
contentSystemMessage = `${contentSystemMessage}\n\n${appendPrompt}`;
contentSystemMessage = `${contentSystemMessage}\n${appendPrompt}`;
}

var systemPrompt =
Expand Down Expand Up @@ -141,7 +158,7 @@ async function getAIResponse(messages) {
const chatCompletionParams = new ChatCompletionParams({
messages: messages,
model: OPENAI_API_MODEL,
temperature: 0,
temperature: 0.1,
max_tokens: parseInt(maxTokens),
top_p: 1,
frequency_penalty: 0,
Expand All @@ -153,15 +170,18 @@ async function getAIResponse(messages) {

const result = response?.trim() || "[]";
logger.log(`AI response: ${result}`);
return JSON.parse(result);
return result;
} catch (error) {
console.error("Error:", error);
return null;
}
}

// Array<{ body: string; path: string; line: number }>
function createComment(file, chunk, aiResponses) {
function generateReviewsFromJsonArray(file, chunk, aiResponses) {

aiResponses = JSON.parse(aiResponses);

return aiResponses.flatMap((aiResponse) => {
if (!file.to) {
return [];
Expand All @@ -174,7 +194,7 @@ function createComment(file, chunk, aiResponses) {
});
}

async function createReviewComment(owner, repo, pull_number, comments) {
async function createReviewOnPr(owner, repo, pull_number, comments) {
await octokit.pulls.createReview({
owner,
repo,
Expand All @@ -184,6 +204,17 @@ async function createReviewComment(owner, repo, pull_number, comments) {
});
}

async function createCommentOnPr(body, owner, repo, pull_number, path) {
octokit.pulls.createReview({
owner,
repo,
pull_number,
path,
body,
event: "COMMENT",
});
}

async function main() {

const prDetails = await getPRDetails();
Expand Down Expand Up @@ -233,7 +264,7 @@ async function main() {

const comments = await analyzeCode(filteredDiff, prDetails);
if (comments.length > 0) {
await createReviewComment(
createReviewOnPr(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'await' keyword is missing before the 'createReviewOnPr' function call. It should be added to ensure that the function is awaited before proceeding.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing 'await' keyword before 'createReviewOnPr' function call.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing 'await' keyword before 'createReviewOnPr' function call.

prDetails.owner,
prDetails.repo,
prDetails.pull_number,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "braz-actions",
"version": "0.1.0",
"version": "0.1.1",
"private": true,
"description": "GitHub Actions",
"main": "create-update-release/action.js",
Expand Down