What happened to the programmers who asked ChatGPT to review the code?

Review PRs are an important part of open source projects. For project maintainers, it is a happy thing for someone in the community to contribute code, which shows that their project has been recognized. Open source projects will become stronger through community contributions. For developers who contribute code, the programming ability has been recognized by open source projects, which is also a thing worth writing in their resumes. What chemical reaction will happen when ChatGPT is added in the process of review code?

Our company Second State recently introduced a ChatGPT-based PR Review robot in the GitHub repo of its own open source project. When someone submits a new PR, the robot will summarize what the PR is doing, potential risks and problems, and what each commit in this PR does. For Reviewer, this can greatly increase the speed of review PR. More importantly, ChatGPT can also find potential problems in the code and provide a more efficient way to write code. Let's look at a real example below.

Take a look at the code below, do you see any problems?

    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 is prime nor not. Among them, the variable limitis initialized nto convert the square root of to an integer, which can effectively reduce the amount of calculation. Then, use fora loop to test limiteach of the numbers from 2 to whether they are factors nof . If nis divisible by any of these numbers, then nis not prime and the code will return false. Conversely, if the code passes through all loops successfully, then nis prime and will return true. This code appears to belong to the Rust language, but JsValueis one of the types that exchange data between Rust and JavaScript, so it might be used to embed this Rust code into JavaScript.

Statement, the above code explanation text is generated by ChatGPT.

In simple terms, if the given number is 1927, this code will first root 1927 to get the result 43, and then start to divide by the integers from 2 to 43 in a loop, until 43 ends. Among all the numbers from 2 to 43, if there is a number that can be evenly divided, then 1927 is not a prime number, if not, then 1927 is a prime number.

Is there any problem found here? This code starts with a positive integer n at the beginning, which has improved the efficiency.

Then let's see what the ChatGPT review looks like, the following ChatGPT Review results are based on GPT4.

As mentioned before, we have deployed a ChatGPT-based PR Review robot in Second State's main repo. After deploying this robot, if someone raises a new PR, the robot will automatically summarize and review the PR. Previous PRs can also trigger the robot to work through keywords defined by yourself.

We tried a previous PR and let ChatGPT summarize and review it. ChatGPT mentioned the following passage in Potential problems:

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

GPT4 says that check_primethe function can be better optimized because there is no need to check for an even number after 2. Seeing this, do you suddenly realize that you want to pat your thighs? It is no longer divisible by 2, so there is no need to try even numbers such as 4 6 8 10, because it is definitely not divisible either. Save half the workload! Excellent still has to be ChatGPT, which is a place that even people tend to overlook. Of course, you can continue to optimize along this line of thinking. Any multiples of prime numbers found in this cycle (such as multiples of 3, multiples of 5) do not need to be tested below. ChatGPT can also help you write this general algorithm.

The above example is excerpted from ChatGPT’s PR Review of the WasmEdge-quickjs project: https://github.com/second-state/wasmedge-quickjs/pull/82 ChatGPT also made a lot of constructive comments, and this PR has not changed much. You can check the modified code and ChatGPT Review by yourself.

ChatGPT also has a good performance in complex PRs. This PR has 56 commits. Click the link below to check the details of the Review. https://github.com/WasmEdge/WasmEdge/pull/2314#issuecomment-1497861516

Through the above example, we can now answer the question posed by the title of the article: what happened to the programmers who asked ChatGPT to review the code? The answer is that ChatGPT can empower programmers to make software eat the world better and faster .

Seeing this, you must have a question, how to install such a robot in your GitHub Repo, so that ChatGPT can improve your code ability? The following is a step-by-step tutorial to teach you to launch your own ChatGPT Review robot in 5 minutes.

Deploy your own ChatGPT Review bot

First of all, we need to use the flows.network platform, which is a workflow automation platform that connects AI large models and real-world SaaS. One of the advantages of using it is that you don't need to set up a server (serverless) or do Oauth yourself, just write the business code and upload it! flows.nework has a very user-friendly interface.

The first step is to fork the github-pr-summary repo of flows.network. This is open source, and you can also customize the prompt in the code according to your own needs, see below for details. Here we will introduce how to deploy your own robot in the form of no code.

Repo address: https://github.com/flows-network/github-pr-summary/

After fork, you can go directly to flows.network to deploy.

First of all, you need to have an account on flows.network. You can log in directly with your github account without any fees.

Then click the Create a flow button to create a new flow. Then import the repo you just forked in the UI.

Then click on advanced to configure environment variables to let flows.network know which github repo you want to deploy this robot on, what is the OpenAI API Key, and which word can wake up this function.

  • login: Fill in your personal github account. When posting a review, this robot will play your role to review.
  • owner: Fill in which organization the Github repo you want to deploy the robot belongs to.
  • repo: Fill in the name of the Github repo you want to deploy the robot to.
  • openai_key_name: Fill in the name you want to name the OpenAI Key, we will use it directly later.
  • trigger_phrase: Fill in the words you want to trigger this robot.

After entering these environment variables, it's time to hit the deploy button. At this time, flows.network will automatically build your function. In the future, if your repo has a new commit, flows.network will also automatically build and deploy it for you.

The next step is to configure the SaaS we need to connect to, and flows.network will prompt you which SaaS accounts need to be verified according to your function. Just now we have filled in the environment variable which repo we want to deploy the robot in, what is the name of the OpenAI API key, now we need to authorize flows.network to allow it to access your repo and your OpenAI API key, so that it will actually take effect.

Click the purple connect to connect to the OpenAI account, you need to enter the API Key you applied for from OpenAI and name the OpenAI Key. The name here should be the same as the name entered in the environment variable earlier.

After OpenAI is verified, continue to verify GitHub, click Connect, and you will open a verification page provided by GitHub, select the Repo you just selected, and install flows network integration, so that you can comment on your repo.

Once this is all set, click the Go to the Flow button. Wait for the function to be compiled and the status of the flow changes to running, then you can ask ChatGPT to review the PR for you.

Advanced gameplay: custom rules and prompt

The github-pr-summary repo is open source, and you can also customize the prompt in the code according to your needs. The function logic is mainly in github-pr-summary.rsthe file .

Repo address: https://github.com/flows-network/github-pr-summary/

    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);
        }

Both system and question in this code can be rewritten according to your own needs. For example, if your project is pure Rust or pure C++ code, you can let ChatGPT act as an experienced Rust/C++ developer, rather than a general experienced software developer. If you have GPT4 permissions, you can also change the model to GPT4 in the code. These can all be customized according to your own needs.

Above we introduced that some parameters such as which GitHub repo to deploy to and the name of the OpenAI API key are passed to the function through the environment variables of the flows network platform. Here we introduce how to tell flows.network directly in the code.

Refer to the following code login, juntaochange the in to your personal github account owner, juntaochange the to the org where your github repo is located repo, testchange the to your github repo name, and change theopenai_key_name in to the name you want to name the OpenAI Key global.free.trial. trigger_phraseFinally , flows summarizechange the in to the word you want to trigger this, or you don’t need to change it, just use this. The logic here is the same as the environment variable step, but we changed it 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 modified your function, the next step is to deploy your function on flows.network, and you can launch a GitHub robot with just a few clicks.

Seeing this is true love! flows.network and GitHub PR Summary bot are launching on Product Hunt, friends who think it is not bad, please click the link below to vote for this bot.

https://www.producthunt.com/posts/gpt-nitro-for-github-pr

<a href="https://www.producthunt.com/posts/gpt-nitro-for-github-pr?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-gpt-nitro-for-github-pr" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=387993&theme=light" alt="GPT Nitro for Github PR - A ChatGPT-based reviewer for your GitHub pull requests | Product Hunt" style="width: 250px; height: 54px;" width="250" height="54" /></a>

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4532842/blog/8645450