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

Always support JSON mode #82

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
38 changes: 30 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import OpenAI from "openai";
import { Octokit } from "@octokit/rest";
import parseDiff, { Chunk, File } from "parse-diff";
import minimatch from "minimatch";
import { parse } from "path";
import { APIPromise } from "openai/core";

const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN");
const OPENAI_API_KEY: string = core.getInput("OPENAI_API_KEY");
Expand Down Expand Up @@ -61,7 +63,15 @@ async function analyzeCode(
prDetails: PRDetails
): Promise<Array<{ body: string; path: string; line: number }>> {
const comments: Array<{ body: string; path: string; line: number }> = [];

if (parsedDiff.length > 10) {
// Too many files to review
comments.push({
body: "This pull request has too many files to review (more than 10). Please split it into smaller pull requests. This is for cost purposes.",
path: "",
line: 0,
});
return comments;
}
for (const file of parsedDiff) {
if (file.to === "/dev/null") continue; // Ignore deleted files
for (const chunk of file.chunks) {
Expand All @@ -79,9 +89,10 @@ async function analyzeCode(
}

function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
return `Your task is to review pull requests. Instructions:
return `You are an expert Nextjs developer. Your task is to review pull requests. Instructions:
- Provide the response in following JSON format: {"reviews": [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]}
- Do not give positive comments or compliments.
- Only comment about javascript code. Do not comment about css classes.
- Provide comments and suggestions ONLY if there is something to improve, otherwise "reviews" should be an empty array.
- Write the comment in GitHub Markdown format.
- Use the given description only for the overall context and only comment the code.
Expand Down Expand Up @@ -123,13 +134,12 @@ async function getAIResponse(prompt: string): Promise<Array<{
presence_penalty: 0,
};

let response: OpenAI.Chat.Completions.ChatCompletion | null = null;
try {
const response = await openai.chat.completions.create({
response = await openai.chat.completions.create({
...queryConfig,
// return JSON if the model supports it:
...(OPENAI_API_MODEL === "gpt-4-1106-preview"
? { response_format: { type: "json_object" } }
: {}),
response_format: { type: "json_object" as const },
messages: [
{
role: "system",
Expand All @@ -138,10 +148,22 @@ async function getAIResponse(prompt: string): Promise<Array<{
],
});

const finish_response = response.choices[0].finish_reason;
if (finish_response === "length") {
console.log(
"The maximum context length has been exceeded. Please reduce the length of the code snippets."
);
return null;
}

const res = response.choices[0].message?.content?.trim() || "{}";
return JSON.parse(res).reviews;
if (res.startsWith("```json")) {
return JSON.parse(res.slice(7, -3)).reviews
} else {
return JSON.parse(res).reviews;
}
} catch (error) {
console.error("Error:", error);
console.error("Error:", error, response?.choices[0].message?.content);
return null;
}
}
Expand Down