GitHub Copilot is an AI programming tool jointly launched by Microsoft and OpenAI. Based on the source code of GitHub and other websites, it automatically writes the following code for programmers according to the above prompts, which can greatly improve the efficiency of writing code.
Let's take a look at how ChatGpt answers Copilot's features:
To briefly extract some key information for you, GitHub Copilot is an AI programming tool jointly launched by Microsoft and OpenAI. Based on the source code of GitHub and other websites, it automatically writes the following code for programmers according to the above prompts, which can greatly improve writing code. efficiency, the core functions are as follows:
- Complete code based on existing context
- Generate method body based on function name and parameters
- Automatically generate code from comments
- Code optimization
- Generate test code
Copilot uses the GPT-3.5 model that OpenAI spent tens of millions of dollars to develop. Based on the GPT-3 model, Copilot obtained the Codex model by learning the open source code of GitHub. This model has powerful code generation capabilities.
Before using Copilot, some preparation work is required
1. GitHub Copilot Subscription Service
First, you need a gitHub account. If you don’t have an account, you can register by yourself.
Click here to register: https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F&source=header-home
After you have a gitHub account, go to the Copilot official website and click Apply.
Free trial for the first two months, pay for the third month, 10 per month, 100 per year, 100 per year, 100 per year; or TB purchase, but there is a probability of being banned. If you are a student, you can apply for student certification. It is free to use.
You can click start trail to experience it first
After entering, you can fill in your own relevant information
2.Idea install Copilot
Requirements: Idea version should not be lower than 2021.2 , otherwise the Copilot plugin cannot be found
It can be used after restarting the idea
3.Idea uses Copilot
Restart after installation, click tools->Copilot to log in to gitHub to obtain the qualification
Click Cop and Open
Copy device code here
3.1 Code Completion
Copilot can complete the entire line, and can automatically fill in the parameters
Copilot can also automatically recognize the workbook variable and its getSheetAt method that appeared above, and there is no grammatical error.
Although the completion effect was good in the previous example and there were no grammatical errors, Copilot still generates code snippets with grammatical errors in many cases. As shown in the figure below, the StringUtils class is created in the local project, and there is only one readTestFile method under the class, but Copilot generates the print method, because Copilot is learned from hundreds of millions of open source codes, and cannot learn the local project in time The code information, so essentially the same as TabNine, there will be grammatical errors, but because the model is more powerful, the probability of errors is smaller, but once the code of the local project is involved, the probability of errors will be very high.
Copilot method-level code generation also has many errors. For example, when generating code to read XML files, the resulting generated code is to read Excel. It may be because more Excel-related APIs are imported at the beginning of the class.
3.2 Unit testing
It is very convenient to write unit tests with Copilot. It can help us generate a lot of test data and reduce the trouble of Mock data.
Suppose we have a class called Calculator, which has an add method for adding two numbers. We want to use Copilot to generate a test class to test this method.
// Calculator.java
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
First, we need to create a test class. Let's create a Java class called CalculatorTest in the source code directory.
// CalculatorTest.java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
@Test
public void testAdd() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
Assertions.assertEquals(5, result, "Addition is incorrect");
}
}
In the above code, we use the Assertions class in the JUnit Jupiter library to make assertions. In the testAdd method, we create a Calculator instance and then call the add method to compare the result with the expected value.
Now, we can use Copilot to generate this test code. In IntelliJ IDEA, select the add method in the Calculator class, and then press the shortcut keys Ctrl + Shift + T.
Copilot will generate an associated test method based on the code snippet.
// Copilot生成的测试方法
@Test
void add() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
Assertions.assertEquals(5, result, "Addition is incorrect");
}