GitHub has a new Feature called GitHub Copilot that can help you write code automatically.
Here it is: copilot.github.com/
Click on it and you’ll see a few big words – Your AI pair Programmer, Your AI programming partner.
Here are a few examples:
How about artificial intelligence?
The first seven lines were written by humans, and the next 17 lines were written by artificial intelligence.
What do we need to write? Let’s look at this example in detail.
I first created a senti.ts file, then introduced a Node.js package called fetch-h2, and wrote two lines of comment:
// Determine whether the sentiment of text is positive
// Use a web service
Copy the code
What does that mean? I’m going to write something in comments, translated as follows:
-
Determine whether a sentence contains positive or negative emotion. (For example, “I’m so happy” contains a positive emotion, the sentence contains a positive emotion; “You are so bad,” for example, contains negative comments, and the emotion of the sentence is negative.)
-
Using Web services.
We then define a method declaration:
async function isPositive(text: string) :Promise<boolean>
Copy the code
Didn’t.
You import a package, write a couple of comments, define the parameters and return values of a method, and that’s all you do.
GitHub Copilot then writes the code for us, which reads as follows:
const response = await fetch(`http://text-processing.com/api/sentiment/`, {
method: "POST".body: `text=${text}`.headers: {
"Content-Type": "application/x-www-form-urlencoded",}});const json = await response.json();
return json.label === "pos";
}
Copy the code
Yes, it intelligently analyzes the comments and method declarations we write and then writes the code out.
This calls an API, and then automatically constructs a POST request, retrieves the returned result, and then checks whether the returned label is POS. If so, it means that the sentence contains a positive program. Returning false is not a positive emotion.
Although the specification level is average, there is no exception handling and so on, but it is quite remarkable!
-
It actually understands exactly the two requirements we describe in the comments
-
It knows which API to call to judge the emotional information of the text
-
It can write a JavaScript method without syntax errors
Cow force!
Of course, it can write not only JavaScript, but also many other languages, so let’s take a look at Python.
Here we create a new Python file for parse_expenses and define a parse_expenses method that takes a parameter called expenses_string and comments it as follows:
""" Parse the list of expenses and return the list of triples (date, value, currency). Ignore lines starting with #. Parse the date using datetime. Example expenses_string: USD USD USD USD USD USD USD USD USD USD USD USD USD EUR """
Copy the code
Parse the following three rows of consumption data, and return the date, number, and unit. Ignore the lines starting with # and use the datetime library to parse the time.
The AI then wrote the following code for us:
expenses = []
for line in expenses_string.splitlines():
if line.startswith("#") :continue
date, value, currency = line.split("")
expenses.append((datetime.datetime.strptime(date, "%Y-%m-%d"),
float(value),
currency))
return expenses
Copy the code
After watching this I was amazed, it did it all!
-
I tell it to ignore lines that start with #, and it adds a judgment
-
And it intelligently analyzes the format of the next three lines of data, and then knows to separate it with Spaces
-
He also knows how to use datetime to parse the date, and also knows what format, year, month and day in the middle of the line
-
The value is also automatically converted to float
-
And it returns as a tuple
I can’t believe it was written by AI. I don’t think it’s a good interview question. AI did it all!
This.
In addition to JavaScript and Python, it works with many other languages, such as Go, Ruby, and TypeScript.
What is the technology behind this?
It is based on OpenAI. The official quote is as follows:
Trained on billions of lines of public code, GitHub Copilot puts the knowledge you need at your fingertips, saving you time and helping you stay focused.
Copy the code
Translation:
GitHub Copilot is trained on billions of lines of public code to keep the knowledge you need at your fingertips, saving you time and helping you stay focused.Copy the code
Anyway, they trained a model, and it took billions of lines of code as training input, and it learned how to code.
This wave does.
Then the official said:
GitHub Copilot is particularly good at writing Python, Go, Ruby, JavaScript, TypeScript, and is now released as a plug-in in VS Code. As we write Code, the plugin communicates with OpenAI’s model, and what we see so far helps us automatically write the Code we want. The basic flow is shown below:
Boy, I’ll download it.
Go to VS Code and search for Copilot. Sure enough, there are over 100,000 downloads.
Once installed, it told me to log on to GitHub for authorization, and when I did, it played a sad message:
It says I don’t have access to it yet, please go to copilot.github.com to apply for an if whitelist.
That means it’s not fully open for use, and you have to apply.
As a result, I applied for it and just clicked “Sign Up” on the website. Now I am on the waiting list, and I should be able to use it after I pass it, as shown in the picture:
If you are interested, please apply and try!
For more exciting content, please pay attention to my public account “Attack Coder” and “Cui Qingcai | Jingmi”.