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

Add GPT-based code review #11

Closed
wants to merge 5 commits into from
Closed
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
29 changes: 29 additions & 0 deletions .github/workflows/code-review.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: GPT Code Review 🤖

permissions:
contents: read
pull-requests: write

on:
pull_request:
types:
- opened
- reopened
- synchronize

jobs:
review:
name: GPT Code Review 🤖
runs-on: ubuntu-latest
steps:
- name: Checkout Repo 🛎️
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: GPT Code Review 🤖
uses: mattzcarey/[email protected]
with:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
MODEL: 'gpt-3.5-turbo'
GITHUB_TOKEN: ${{ github.token }}
97 changes: 97 additions & 0 deletions randomfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* eslint-disable max-depth */
/* eslint-disable complexity */
// Reviewing multiple files inline > prioritising them > adding review comments
// Answer questions > get the comments on the PR (by me and the questioner) as context > answer the question as comment

import jsesc from "jsesc";

import { modelInfo } from "../constants";
import { AIModel } from "../llm/ai";
import { buildReviewPrompt } from "../prompts/buildPrompt";
import { ReviewFile } from "../types";

export class Chat {
ai: AIModel;
modelName: string;
constructor(
openaiApiKey: string,
openaiModelName?: string,
temperature?: string
) {
this.modelName = openaiModelName ?? "gpt-4-1106-preview";
this.ai = new AIModel({
modelName: this.modelName,
apiKey: openaiApiKey,
temperature: temperature ? parseFloat(temperature) : 0,
});
}

private getMaxPromptLength = (modelName: string): number => {
const model = modelInfo.find((info) => info.model === modelName);
if (!model) {
throw new Error(`Model ${modelName} not found`);
}

return model.maxPromptLength;
};

public getReview = async (
patch: string
): Promise<ReviewFile[] | undefined> => {
const prompt = buildReviewPrompt(patch);
const maxPromptLength = this.getMaxPromptLength(this.modelName);

if (prompt.length > maxPromptLength) {
console.error(
`File ${prompt} is too large to review, skipping review for this file`
);

return undefined;
}

try {
let jsonResponse = await this.ai.callModel(prompt);
jsonResponse = removeMarkdownJsonQuotes(jsonResponse);

try {
return JSON.parse(jsonResponse) as ReviewFile[];
} catch (parseError) {
console.error(
`Error parsing JSON: ${
(parseError as Error).message
}. Escaping special characters and retrying.`
);

try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const escapedJsonResponse: string = jsesc(jsonResponse, {
json: true,
});

return JSON.parse(escapedJsonResponse) as ReviewFile[];
} catch (escapeParseError) {
console.error(
`Error parsing escaped JSON: ${
(escapeParseError as Error).message
}. Returning undefined.`
);

return undefined;
}
}
} catch (error) {
console.error(
`Error processing review data: ${(error as Error).message}`
);

return undefined;
}
};
}

const removeMarkdownJsonQuotes = (jsonString: string): string => {
return jsonString
.replace(/^`+\s*json\s*/, "")
.replace(/\s*`+$/, "")
.trim();
};
Loading