mirror of
https://github.com/freeedcom/ai-codereviewer.git
synced 2025-04-20 17:46:47 +00:00
support the latest gpt 4 and json mode
This commit is contained in:
parent
bc0dab4a3c
commit
80ca662b5f
3 changed files with 147 additions and 27 deletions
20
src/main.ts
20
src/main.ts
|
@ -1,6 +1,6 @@
|
|||
import { readFileSync } from "fs";
|
||||
import * as core from "@actions/core";
|
||||
import { Configuration, OpenAIApi } from "openai";
|
||||
import OpenAI from "openai";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import parseDiff, { Chunk, File } from "parse-diff";
|
||||
import minimatch from "minimatch";
|
||||
|
@ -11,12 +11,10 @@ const OPENAI_API_MODEL: string = core.getInput("OPENAI_API_MODEL");
|
|||
|
||||
const octokit = new Octokit({ auth: GITHUB_TOKEN });
|
||||
|
||||
const configuration = new Configuration({
|
||||
const openai = new OpenAI({
|
||||
apiKey: OPENAI_API_KEY,
|
||||
});
|
||||
|
||||
const openai = new OpenAIApi(configuration);
|
||||
|
||||
interface PRDetails {
|
||||
owner: string;
|
||||
repo: string;
|
||||
|
@ -98,9 +96,9 @@ async function getBaseAndHeadShas(
|
|||
|
||||
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
|
||||
return `Your task is to review pull requests. Instructions:
|
||||
- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
|
||||
- Provide the response in following JSON format: {"reviews": [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]}
|
||||
- Do not give positive comments or compliments.
|
||||
- Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array.
|
||||
- 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.
|
||||
- IMPORTANT: NEVER suggest adding comments to the code.
|
||||
|
@ -142,8 +140,12 @@ async function getAIResponse(prompt: string): Promise<Array<{
|
|||
};
|
||||
|
||||
try {
|
||||
const response = await openai.createChatCompletion({
|
||||
const 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" } }
|
||||
: {}),
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
|
@ -152,8 +154,8 @@ async function getAIResponse(prompt: string): Promise<Array<{
|
|||
],
|
||||
});
|
||||
|
||||
const res = response.data.choices[0].message?.content?.trim() || "[]";
|
||||
return JSON.parse(res);
|
||||
const res = response.choices[0].message?.content?.trim() || "{}";
|
||||
return JSON.parse(res).reviews;
|
||||
} catch (error) {
|
||||
console.error("Error:", error);
|
||||
return null;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue