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. 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 delayed-stream
MIT MIT
Copyright (c) 2011 Debuggable Limited <felix@debuggable.com> Copyright (c) 2011 Debuggable Limited <felix@debuggable.com>

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

@ -4,7 +4,6 @@ import { Configuration, OpenAIApi } from "openai";
import { Octokit } from "@octokit/rest"; import { Octokit } from "@octokit/rest";
import parseDiff, { Chunk, File } from "parse-diff"; import parseDiff, { Chunk, File } from "parse-diff";
import minimatch from "minimatch"; import minimatch from "minimatch";
import { parse } from "csv-parse/sync";
const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN"); const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN");
const OPENAI_API_KEY: string = core.getInput("OPENAI_API_KEY"); 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 { 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 file.to
}" and take the pull request title and description into account when writing the response. }" and take the pull request title and description into account when writing the response.
Title: ${prDetails.title} Pull request title: ${prDetails.title}
Pull request description:
Description:
--- ---
${prDetails.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. Git diff to review:
Diff to review:
\`\`\`diff \`\`\`diff
${chunk.content} ${chunk.content}
${chunk.changes.map((c) => c.content).join("\n")} ${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<{ async function getAIResponse(prompt: string): Promise<Array<{
line_number: string; lineNumber: string;
review_comment: string; reviewComment: string;
}> | null> { }> | null> {
const queryConfig = { const queryConfig = {
model: "gpt-4", model: "gpt-4",
@ -131,12 +131,7 @@ async function getAIResponse(prompt: string): Promise<Array<{
}); });
const res = response.data.choices[0].message?.content?.trim() || ""; const res = response.data.choices[0].message?.content?.trim() || "";
return parse(res, { return JSON.parse(res);
delimiter: "\t",
columns: ["line_number", "review_comment"],
skip_empty_lines: true,
relax_quotes: true,
});
} catch (error) { } catch (error) {
console.error("Error:", error); console.error("Error:", error);
return null; return null;
@ -147,8 +142,8 @@ function createComment(
file: File, file: File,
chunk: Chunk, chunk: Chunk,
aiResponses: Array<{ aiResponses: Array<{
line_number: string; lineNumber: string;
review_comment: string; reviewComment: string;
}> }>
): Array<{ body: string; path: string; line: number }> { ): Array<{ body: string; path: string; line: number }> {
return aiResponses.flatMap((aiResponse) => { return aiResponses.flatMap((aiResponse) => {
@ -156,9 +151,9 @@ function createComment(
return []; return [];
} }
return { return {
body: aiResponse.review_comment, body: aiResponse.reviewComment,
path: file.to, 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" resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ== 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: delayed-stream@~1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"