How To Use GitHub Copilot (with Python Examples)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey everyone, welcome to the channel. In this video, we'll be taking a look at GitHub Copilot, which I think is potentially the most impressive software developer tool to be released in the past year. In my own experience, it has reduced the amount of time I spent on coding by around 25%. To me, that's pretty groundbreaking. We'll look at what GitHub Copilot is, how you can use it, and then see some real examples using it with Python for general programming, for data science, and even for making a game. Finally, this is a controversial tool. There's many counter-arguments against using it, so we'll discuss those at the end of the video. And hopefully, that will help you decide whether the tool is something you want to use or stay away from. Let's dive right in. So, what is GitHub Copilot? In their own words, GitHub Copilot is an AI pair programmer. It is an IDE plugin that gives you suggestions as you code, sort of like an autocomplete feature, except that these suggestions are extremely smart and can expand entire lines or functions. And the way it does that is by using an AI language model to analyze your code and figure out what it is that you want to do. What makes GitHub Copilot special is that it has two of the most important ingredients to make this work. The first is OpenAI's GPT-3 language model, which is industry leading. The second is the GitHub dataset, which is where most of the world hosts their source code. Together, these result in a code completion product that is unbelievably effective. It is compatible with most popular programming languages and most frameworks, and it's actually quite effective in a lot of different problem spaces. And you can plug it right into the most popular IDEs, including Visual Studio, VS Code, and IntelliJ. It does cost $10 a month to use, but I think that if you can save even just 1% of your coding time, then it's already worth it and it will pay for itself very quickly. If you don't believe me, then take a calculator and work it out for yourself and let me know what you think. Now, let's take a look at some coding examples using GitHub Copilot. I'm using it in VS Code, and all you have to do is install the GitHub Copilot extension from the extensions marketplace and then hook it up with your GitHub account online. And once it's installed, you will see this little icon here, which shows the state of GitHub Copilot, and you can click on it for all the options. And if it's ready and connected to the internet, then you will see suggestions come up as you start typing. So let's look at an example. So here I typed a comment, create a list of 100 numbers, and this line in gray is the suggestion from GitHub Copilot. And if I highlight it, I can look at different suggestions by tapping through them, or I can accept it by pressing tab. The suggestions don't only come up when you type a comment, it also comes up whenever you sort of hit space or hit enter and your cursor is just blinking. So here it's anticipating that I want to write a comment like this. And if you press tab, you can see all the different things that it's wanting to suggest. It's not limited to comments or empty lines either. I can write a function name, for example, and it will try to interpret what I want to do with that function. For example, I might want a function like get even numbers like this. And then it interprets the arguments I want to pass in and the whole body of the function that I want to do. I can also just type the name of the variables I want. So for example, I want a new variable called even numbers. And it's going to figure out that I should use this get even numbers function I defined earlier on this list to get me a list of even numbers. Now I might be able to do some surprising things as well. For example, what if I do odd numbers like this? Now this is really interesting because it knows that with odd numbers, it can't use this function. It doesn't have an odd numbers function, but it can subtract the even numbers from the numbers list that we have to get the odd numbers. So that's pretty interesting as well. And if I'm not satisfied with that, and I want to see all the suggestions, I can actually press control enter, and this GitHub copilot window will come up on the right where I can choose all the different varieties of suggestions that it can come up with. And it's actually pretty useful if we want to create class or data models as well. For example, let's say I want to create a class to represent a person, I might type class person like this. And then if I just leave it, it comes up with a name and an age for a person. What if I wanted to create a list of 10 random people? So here it's creating a bunch of sample people. So I can just press tab one by one and create a bunch of people here. And for some reason, it decides that all the people I want to create should begin with the letter J. And I don't know if it's going to stop at 10 or if it's going to keep, well, there it stops at 10. So it actually doesn't just look at the previous line, but it kind of looks at the whole context of the document. That's pretty interesting. Still, you get some weird results where because the first name was John, because John is the most common name, it decided that the next name should be Jane. And then when you have two names beginning with J, GitHub copilot thinks that all the names should begin with J. So that's not ideal if I want a list of 10 completely random people. So instead, if I do this and then I press control enter to make it synthesize the solutions, I actually get much more balanced solutions. I still get the stuff with just the Js, but I also get this situation with Alice, Bob, Charlie, and then just different names of the alphabet. So that's pretty interesting way to use GitHub copilot. So that was a quick look at using GitHub copilot with basic Python commands. Now, let's actually try to use it to create a real program. So I'm going to create a new file called leadcode.py and I'm going to use this to solve an easy lead code question. So the question I've picked for this one, I just picked it randomly. It's called Roman to integer. And the problem is to convert a string, which is a Roman numeral like this one into an integer. So this I becomes one, V becomes five, and a combination of them will become 27, 12. So a program that can interpret this and do all of that stuff. So let's take a look if GitHub copilot can actually solve this problem for us. I think it really helps at the start of the file if you write a comment describes what the program should do. Okay, so this is pretty interesting. I stopped typing here and it's predicted this rest of the sentence. So it actually can even anticipate what I want to do before I even finish the sentence. Okay so that's pretty interesting. The first thing I typed was Roman numerals as a constant. So it's actually created a dictionary that has all the strings mapped to a Roman numeral. The next thing I want to do is probably create a function to use this and convert it to a number. So I didn't even have to finish typing the name of the function. And it's already sort of interpreted the entire function for me. I don't know if this works yet, we can write a test for that. I have to actually, when the function is kind of complex like this, I can either trust copilot that it works or I can test it or I can review it and read it and make sure that it works. Now just from eyeballing it, it might work but it just doesn't look really good because we've got this S here which I'm not a fan of single variable names unless it's something really clear. And also there's quite a bit of indexing. This logic is just not super clear what it does. So if I sit down and read it, maybe it works. But this is a problem I have with copilot is that sometimes it spits out code that might be correct but it's not pleasant to read or it's not easy to work with. So let's see if we can do better and actually give this a different variable name. And this is a little bit better already because as soon as I added the type hint, it also added the type into the return signature. Here it uses a little bit more verbose enumeration methods. So we actually have index and numeral here. So yeah, I can actually go ahead and adjust this to fit the style that I want. So for example, I don't think I need the full name of index. I mean, that's a bit overkill, so I change that with an I. And it's able to interpret that and fit the solution to what I want, to the style that I want. Okay, so that's pretty good. Now I have no idea if this works, but an easy way for us to find out is to create a couple of test cases and actually run them. So let's signal that intention. Well, I didn't even have to do that. It kind of predicted that for me. It's just suggesting that I want to create a list of test cases. Let's go ahead and try that. Okay, so it's created five test cases for me here with the Roman numeral that we're going to put in and the integer that we expect. I actually don't know if this works with case sensitivity, because I don't see anything that changes it to lower up the case here. So that's something interesting as well. I don't know if GitHub Copilot was able to pick that up. Just something to notice, but I'm not going to change it now. I'm just want to actually run these test cases and see if they work. And that's pretty incredible because I didn't even have to do anything. It kind of predicted that I wanted to use the test cases and it wrote a loop to run them. And you can see that the program actually works. All the test cases returned true for all five of them. So that's a pretty successful outcome for GitHub Copilot using it for questions like lead code or writing a complete software solution. For this next example, I want to do something a little bit more complicated. A common use case for Python is financial applications and data science. So let's see if we can write a program to pull stock market data from an API and maybe do some plotting with it in a graph or on a table. So for this one, I'm actually going to use a public API called alpha Vantage, which we can use to get financial information. It's got stock market data API and it's free to use if you sign up for it. And there is an API here called time series daily, which will give us if we put in a stock market symbol like IBM here, for example, it's going to give us a couple of data points of the time series for that API. So let's see if we can use that. Now I've already signed up for this, so I have an API key. So I'm going to put this into my program first. Now before accepting any of GitHub co-pilot suggestions, I'm going to resist them and just try to structure the entire program first because that might actually change the kind of things that suggest. Okay, so here I have signaled which stock symbol I wanted to use. I've also signaled that I want to get the daily stock data and I want to use a main function to do this. So let's go ahead and see if it can start suggesting me how to implement this now. So this was the first suggestion that it gave me. This is really interesting because this URL, it turns out that it actually is the URL to use with alpha advantage. So at some point, GitHub co-pilot has seen these people use these in the source code that it was trained on. And that's kind of shown up in the suggestions here, which I find really interesting. And it's also substituted the symbol and the API key, which I've created over here. It does want to use the request library, which I haven't imported yet. So this is going to be a problem if I run the suggestion right away. But this is really easy to fix because I can just call import request. In fact, it suggests that to me here. So this will return the stock market data. That's pretty good. And maybe I could do it like this. Now what if I want to display this in a table? Pandas is actually a really good library for doing that. So I'm going to import that first and see if GitHub co-pilot knows how to use this data with the pandas library to turn it into a table. To guide it, I'm going to write a couple of helpful comments. Now I actually don't know the structure of this data. I can find out by reading the documentation, but it seems to me that GitHub co-pilot already understands the structure of this data to some extent. And when I run it, it actually works. It prints out this table with the prices, the date here, and the open, high, low and close and the volume. So this is pretty incredible because it shows that GitHub co-pilot doesn't only understand just basic code like we saw in the earlier examples, but it actually understands how to use public APIs and how to use the output of those with certain frameworks. So these are things that I could have figured out myself by looking at the documentation, but it's just saved me a ton of time if I wanted to do that. And finally, let's also get it to plot the data and see if it can do that too. So first I'm going to import matplotlib because that's what I want to use to plot it. And it seems like matplotlib already has a really close integration with pandas data frames. So it only took two lines to do that, but let's see if that works as well. Okay. And if I run that, I see this graph come up. So in just a few minutes and basically with barely any coding knowledge at all, I've done a little bit of data science here, pulling stock price from the internet, using a free API and also plotting it on the graph. So you can imagine how far you could get if you just sat down for an hour and use this to write a finance program. Now for the last example, I'm going to do something a little ambitious and I'm going to try to implement an entire Tetris game using PyGame with the help of GitHub Copilot. Tetris is one of those things that everybody knows the rules. The blocks fall down, you can rotate them, they fit together. And if you form a line, the line gets eliminated. So it's something that you can recognize quite easily, but it's probably not very trivial to implement from scratch. So let's see if GitHub Copilot will help me to do that. And just to note, I know of PyGame the framework, but I'm also very unfamiliar with how it works. So I'm also relying on the AI tool here to really help me out using that framework. So let's go ahead and give this a try. For using GitHub Copilot, I think it's pretty useful to set the constraints of the problem. I think it works a lot better than just giving it something completely general like this and leaving it to its interpretation. So I'm going to start by typing as much structure of the program as I can first and then using it to fill in the blanks. Okay, it's suggesting quite a lot of code for me, like that's pretty much the whole game it looks like, but that's a bit too much. So I'm just going to accept this, but I'm just going to delete some of it so that we're not going too far ahead with the auto suggestion. Okay, so here I've created with the help of GitHub Copilot all the shapes of the blocks and I made them in a square grid because I'm going to want to rotate them later. So I think this will help me to do that. So I've got this straight shape and then I've got this S shape, the Z shape, the square block, the L piece, the opposite piece of that, and then the T shape. And then I've got all the shapes defined in my area here. Okay, so. Okay, so GitHub Copilot just auto-completed a large chunk of code here, which looks correct for me, and this one will update the game state once every frame rate that I've defined here in my constant. It will check if I want to quit the game and also fill in the screen based on my empty block color and blocks that are fixed in place. So we can't really see this yet because we don't have anything moving the blocks or creating the blocks, so let's do that next. There we go. Okay, so at this point I've spent about 20 minutes fighting this Tetris game, and GitHub Copilot's probably written about 80% of this, and it's not perfect. The code does have a lot of inefficiencies, and there's a lot of things that it does that has a lot of repeated elements, and it's not the best way to do it. But I can actually run this, and it comes pretty close to being a Tetris game. So let's take a look at the results. And so here's a sample of the game. It works. When a different random Tetris pieces get spawned and they fall down, and then when they hit the floor or they hit another piece, they kind of lock into place. I didn't actually get around to implementing what happens when you form a full line, but that's okay because I think coming this far, it already demonstrates the power of GitHub Copilot. I don't know anything about PyGame, and I'm not very familiar with the Tetris rules or Tetris shapes either. So with using GitHub Copilot, it was able to fill in the gaps for me and help me get quite far in just 20 minutes. So as you can see, it's a pretty good tool for prototyping ideas if you know roughly what you're doing. And as I mentioned before, the code quality itself isn't the best. There's a lot of things I'm looking at this, and I'm shaking my head like I would probably do them differently or organize the program a little bit more differently. But that is something that if you had more time, you can definitely be patient and invest time into doing that and guiding Copilot because Copilot actually does look at your software and the rest of your code and it tries to continue in that style. If you start rewarding it for using a bad style or if you start writing programs badly in your code, it's just going to repeat that pattern. So it's really important that if you use GitHub Copilot is to catch the patterns early and set it on the right course. Overall, GitHub Copilot seems like a pretty useful tool so far. From my own experience, it cuts my time spent coding by around 25%, which for me is pretty massive. But not everyone is as excited about it as I am. And here are some of the top concerns I've gathered from other developers. First of all, it costs $10 each month, which I think isn't so bad. And it's probably the least worrying of the concerns. Second is that it will make you a worse coder, whether you're trying to learn something or whether you are already a good coder and it will just make you reliant on it. And so your skills will decay. And I think that's valid. And whilst GitHub Copilot is very convenient, I don't think it will make you a worse coder or slow down your learning. Instead it will probably result you shifting your focus to a different set of skills. For example, problem solving at a more strategic level or focusing more on the design or the business application of your software, which I think is a trade off. But to me, it's worth it. And finally, the third concern, which I think is probably the most worrying, is the privacy and the data aspect. Because GitHub is owned by Microsoft and using Copilot means that it has access to your code so that it can make the suggestions. Now, this kind of implies that Microsoft will have access to your code. And I think that if your company or whatever work you're doing can't be trusted with Microsoft, if you can't trust that data with Microsoft, then GitHub Copilot probably isn't a fit. And that's probably true if you are a developer at most high profile companies or at government institutes. But for startups and personal projects, I think it should be okay. However, if I want to use it at a bigger company, or if I want to use it with trade secrets in my code, then I probably want more guarantee that the code is only used for suggestions and my code isn't going to be used for training or appear in someone else's data set. So after all that, what's my verdict on GitHub Copilot? Should you use it or should you avoid it? Well, if your company policy allows it, or if you're using it for your own projects, then I think you should use it. Or at the very least, try it for a couple of weeks and see how it goes. It will save you a lot of time coding and it will give you time to spend on problems, which I think are more important and more interesting, like architecture, design and business requirements of your software. If you've used GitHub Copilot already and have strong opinions about it, then please share them with me in the comments. Otherwise, I hope you found this video useful and thank you for watching.
Info
Channel: pixegami
Views: 107,341
Rating: undefined out of 5
Keywords: github copilot vscode, github, github copilot, github copilot review, github copilot tutorial, how to use github copilot, github copilot python, truth about github copilot, copilot, don't use github copilot, github copilot examples, github copilot demo, coding, programming, should you pay for github copilot, github copilot example, github copilot comments, github copilot writes code, what i think of github copilot, github copilot visual studio code, github copilot dangerous
Id: tG8PPne7ef0
Channel Id: undefined
Length: 25min 49sec (1549 seconds)
Published: Sat Oct 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.