give instructions in the beginning, ask the AI to return JSON

This commit is contained in:
Ville Saukkonen 2023-03-28 00:26:01 +03:00
parent 30597b305c
commit 626621db03
6 changed files with 36 additions and 1419 deletions

1381
dist/index.js vendored

File diff suppressed because it is too large Load Diff

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

25
dist/licenses.txt vendored

@ -580,31 +580,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
csv-parse
MIT
The MIT License (MIT)
Copyright (c) 2010 Adaltas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
delayed-stream
MIT
Copyright (c) 2011 Debuggable Limited <felix@debuggable.com>

@ -14,7 +14,6 @@
"dependencies": {
"@actions/core": "^1.10.0",
"@octokit/rest": "^19.0.7",
"csv-parse": "^5.3.6",
"minimatch": "^7.4.2",
"openai": "^3.2.1",
"parse-diff": "^0.11.1",

@ -4,7 +4,6 @@ import { Configuration, OpenAIApi } from "openai";
import { Octokit } from "@octokit/rest";
import parseDiff, { Chunk, File } from "parse-diff";
import minimatch from "minimatch";
import { parse } from "csv-parse/sync";
const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN");
const OPENAI_API_KEY: string = core.getInput("OPENAI_API_KEY");
@ -80,35 +79,36 @@ async function analyzeCode(
}
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
return `Review the following code diff in the file "${
return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
- Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array.
- Write the comment in GitHub markdown.
- Don't give positive comments.
- Use the given description only for the overall context and only comment the code.
Review the following code diff in the file "${
file.to
}" and take the pull request title and description into account when writing the response.
Title: ${prDetails.title}
Description:
Pull request title: ${prDetails.title}
Pull request description:
---
${prDetails.description}
---
Please provide comments and suggestions ONLY if there is something to improve, write the answer in Github markdown. Don't give positive comments. Use the description only for the overall context and only comment the code.
Diff to review:
Git diff to review:
\`\`\`diff
${chunk.content}
${chunk.changes.map((c) => c.content).join("\n")}
\`\`\`
Give the answer in following TSV format:
line_number\treview_comment
`;
}
async function getAIResponse(prompt: string): Promise<Array<{
line_number: string;
review_comment: string;
lineNumber: string;
reviewComment: string;
}> | null> {
const queryConfig = {
model: "gpt-4",
@ -131,12 +131,7 @@ async function getAIResponse(prompt: string): Promise<Array<{
});
const res = response.data.choices[0].message?.content?.trim() || "";
return parse(res, {
delimiter: "\t",
columns: ["line_number", "review_comment"],
skip_empty_lines: true,
relax_quotes: true,
});
return JSON.parse(res);
} catch (error) {
console.error("Error:", error);
return null;
@ -147,8 +142,8 @@ function createComment(
file: File,
chunk: Chunk,
aiResponses: Array<{
line_number: string;
review_comment: string;
lineNumber: string;
reviewComment: string;
}>
): Array<{ body: string; path: string; line: number }> {
return aiResponses.flatMap((aiResponse) => {
@ -156,9 +151,9 @@ function createComment(
return [];
}
return {
body: aiResponse.review_comment,
body: aiResponse.reviewComment,
path: file.to,
line: Number(aiResponse.line_number),
line: Number(aiResponse.lineNumber),
};
});
}

@ -229,11 +229,6 @@ create-require@^1.1.0:
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
csv-parse@^5.3.6:
version "5.3.6"
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-5.3.6.tgz#181d7c12300a60684bb51261ea9a5c3135ba8688"
integrity sha512-WI330GjCuEioK/ii8HM2YE/eV+ynpeLvU+RXw4R8bRU8R0laK5zO3fDsc4gH8s472e3Ga38rbIjCAiQh+tEHkw==
delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"