How did ChatGPT put code reviews on steroids for this open source community?

Apr 08, 2023 • 8 minutes to read

Reviewing PRs (GitHub Pull Requests) is one of the most important, yet time-consuming and thankless, jobs open-source maintainers must do on a regular basis. Accurate and speedy review of PRs is not only essential for the cadence of software development but also critical for the moral of the contributor community.

The WasmEdge community (a leading WebAssembly Runtime under CNCF and Linux Foundation) recently added a ChatGPT-based PR review bot in our open source GitHub repo. When someone submits a new GitHub Pull Request, this bot summarizes what the PR is about, its potential risks and problems, and what each commit in the PR is doing. This greatly increases the speed of reviewing PRs for reviewers. More importantly, ChatGPT can also detect potential problems in the code and suggest the most optimized and efficient code.

Take a look at this code snippet - do you notice any issues?

    let limit = (n as f64).sqrt() as i32;
    for a in 2..limit {
        if n % a == 0 {
            return JsValue::Bool(false);
        }
    }

This code checks whether a given positive integer n is prime. The variable limit is initialized to the integer value of the square root of n, which effectively reduces the amount of calculations needed. Then, a for loop is used to test whether each number between 2 and limit inclusive is a factor of n. If n can be divided by any of these numbers, it is not prime and the code will return false. Conversely, if the code successfully passes all the loops, it means that n is prime and it will return true. This code appears to be written in Rust language, and JsValue is one of the types of data exchanged between Rust and JavaScript, so it may be used to embed this Rust code into JavaScript.

The above code explanation is generated by ChatGPT.

In simple terms, if the given number is 1927, this code will first take the square root of 1927 and get the result 43. Then it will start dividing 1927 by every integer from 2 to 43. If none of those integers is a factor of 1927, then 1927 is a prime number.

Sounds good! Taking the square root of the integer n at the beginning is a smart move. It reduces the number of integers we must loop and test and makes the code efficient.

Now, what did ChatGPT say in the code review’s “Potential Problems” section?

The check_prime function can be optimized further, as it checks for divisibility with even numbers after 2, which isn't necessary.

OMG! Of course, if a number is not divisible by 2, there is no need to check 4, 6, 8, 10, and other even numbers, as they are certainly not divisible either! ChatGPT excels at details humans may overlook. Of course, we can continue along this line of thought — any multiples of prime numbers (e.g., multiples of 3 and 5) also do not need to be tested. ChatGPT can help you write this general algorithm.

The above excerpt is taken from ChatGPT's PR review on the WasmEdge-quickjs project: https://github.com/second-state/wasmedge-quickjs/pull/82. ChatGPT provided numerous valuable insights. The changes to this PR are easy to follow. You can refer to the changed code and ChatGPT's review on your own.

ChatGPT also performed well on complex PRs, such as this one with 56 commits. Click the link below to see the specific review for this PR: https://github.com/WasmEdge/WasmEdge/pull/2314#issuecomment-1497861516

ChatGPT is indeed impressive in empowering programmers to make software eating the world better and faster.

At this point, you may wonder: Can I install a bot like ChatGPT in my own GitHub repo to improve my code work efficiency? Below is a step-by-step tutorial to help you add a ChatGPT Review bot in your repo in just 5 minutes.

Deploy your own ChatGPT PR Review bot in 5 minutes

First, you will need to use the flows.network platform, which is a workflow automation platform that connects AI models and different SaaS. One of the benefits of using this platform is that you don't need to set up your own server (serverless), or create your own OAuth. You can simply write your business code and upload it! flows.network also has a very user-friendly interface.

The first step is to fork flows.network's github-pr-summary repository. This is open source and you can customize the prompts in the code to suit your needs (Details are provided in the following part).

Below is to deploy this bot to your own repo in a no-code (deployment process) way. After forking the above repo, you can start deploying via flows.network.

You need to have an account on flows.network. You can simply log in using your Github account at zero cost.

Next, click on the “Create a Flow” button to create a new flow. Then, import the repo that you just forked into the UI.

Then click on advanced and configure the environment variables to let flows.network know which GitHub repo you want to deploy this bot to, what your OpenAI API Key is, and which word can trigger this function.

  • login: Enter your personal GitHub account. This bot will act as you to review.
  • owner: Enter the name of the organization to which the GitHub repo you want to deploy the bot belongs.
  • repo: Enter the name of the GitHub repo you want to deploy the bot to.
  • openai_ key_name: Enter the name you want to give your OpenAI Key, which we will use later.
  • trigger_phrase: Enter the word you want to use to trigger this bot.

After entering these environmental variables, click the deploy button. At this point, flows.network will automatically build your function. Going forward, when you make changes and this forked bot repo has new commits, flows.network will also automatically build and deploy it.

The next step is to configure which SaaS we want to connect. Flows.network will ask you to authenticate SaaS accounts based on your function. We have already filled in the repo where we want to deploy the bot and the name of the OpenAI API key in the environmental variables. Now, we need to authorize flows.network to access your repo and OpenAI API key for it to take effect.

Click on the purple “connect” button to link your OpenAI account. You will need to enter the API Key that you received from OpenAI and name this OpenAI Key. Make sure to use the same name as the one you entered for the environment variable earlier.

Once OpenAI authentication is complete, continue with the GitHub authentication. Click “Connect” and you will be taken to a validation page provided by GitHub. Choose the repository that you previously selected and install the “flows network integration” in order to enable comments in your repository. After these steps are completed, click on the “Go to the Flow” button. Wait for the function to compile and for the flow status to change to “running”, then ChatGPT can help you review your pull requests.

Advanced gameplay: Custom rules and prompts.

The github-pr-summary repository is open source, and customizations can be made to fit your specific needs. The main function logic is in the github-pr-summary.rs file.

    let mut reviews: Vec<String> = Vec::new();
    let mut reviews_text = String::new();
    for (_i, commit) in commits.iter().enumerate() {
        let system = "You are an experienced software developer. You will act as a reviewer for GitHub Pull Requests.";
        let co = ChatOptions {
            // model: ChatModel::GPT4,
            model: ChatModel::GPT35Turbo,
            restart: true,
            system_prompt: Some(system),
            retry_times: 3,
        };
        let question = "The following is a GitHub patch. Please summarize the key changes and identify potential problems. Start with the most important findings.\n\n".to_string() + commit;
        if let Some(r) = chat_completion(openai_key_name, &chat_id, &question, &co) {
            write_error_log!("Got a patch summary");
            if reviews_text.len() < 9000 {
                reviews_text.push_str("------\n");
                reviews_text.push_str(&r.choice);
                reviews_text.push('\n');
            }
            reviews.push(r.choice);
        }

In this code, both system and question can be rewritten to fit your specific needs. For example, if your project uses entirely Rust or C++ code, you can have ChatGPT play the role of an experienced Rust/C++ developer, rather than simply an experienced software developer. Additionally, if you have access to GPT4, you can replace the model in the code with GPT4. All of these customizations can be made to fit your specific needs.

Earlier, we discussed how to enter certain parameters using environment variables on the flows network platform and pass to the function, such as which GitHub repo to deploy the bot and the name of the OpenAI API key. Now, we will discuss how to directly make changes within the code.

Referencing the code below, replace juntao in the login section with your own personal GitHub account, replace ‘juntao’ in the owner section with the organization of GitHub repo, replace test in the repo section with the name of your repo, and replace global.free.trial in the openai_key_name section with the desired name for your OpenAI key. Finally, change flows summarize in the trigger_phrase section to the word or phrase that you want to trigger this function. You can also keep it as is. This step is similar to using environment variables, but instead we are making changes directly in the code.

pub async fn run() -> anyhow::Result<()> {
    dotenv().ok();

    let login = env::var("login").unwrap_or("juntao".to_string());
    let owner = env::var("owner").unwrap_or("juntao".to_string());
    let repo = env::var("repo").unwrap_or("test".to_string());
    let openai_key_name = env::var("openai_key_name").unwrap_or("global.free.trial".to_string());
    let trigger_phrase = env::var("trigger_phrase").unwrap_or("flows summarize".to_string());

If you have finished modifying your function, the next step is to deploy it on flows.network. With just a few clicks, you now have a GitHub PR review bot live on your repo.

Thanks for reading this far and really appreciate the love! You might be interested to know that flows.network is now launching on ProductHunt. If you enjoyed this article and this PR Review bot, show your support by upvoting it on ProductHunt here.

GPT Nitro for Github PR - A ChatGPT-based reviewer for your GitHub pull requests | Product Hunt

BotChatGPTPull RequestGitHubOpen SourceGPTAI
A high-performance, extensible, and hardware optimized WebAssembly Virtual Machine for automotive, cloud, AI, and blockchain applications