Prompting with Copilot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I've found that GitHub co-pilot can be a great assistant both for personal and work projects I'm Pamela a cloud Advocate at Microsoft focused on python after just a few months of using copilot I find myself using it more and more and I want to share some tips with you if you're new to copilot be sure to watch this video here there's also a great video by my colleague Alfredo about using copilot to turn a command line app into a fast API app this video is all about best practices for prompting copilot my examples are in Python but the practices apply to all languages on a high level remember these two things one provide context and two be predictable let's explore some situations to see what I mean by that let's start with an example of just a single file Python program to process a bit of Json now one way that I'm going to provide context to copilot is by keeping that Json open in another tab in this code so that that gives a good clue that this is what we're working on I'm also going to start my file with a high level comment about what we're doing description this file processes talks.json now a nice thing to do is to start off with Imports if we know for sure that there's a particular module or package that we're using it's really good to start off by importing it because that gives copilot really nice context to decide how to manipulate things because there's many different ways of doing something in Python we're going to give it a little heads up that this is how we want to do it so I will go ahead and import Json like it suggests and then I'm done with my imports and I want to read the file in [Music] and I like everything suggested here the only thing I'm going to change is this variable name as a general best practice it's nice to have really specific variable names that can give more hints about what a particular variable is so instead of data which could represent anything you know any data type and any sort of thing I'm going to say talks and I'm even going to use a python type annotation to specify that this is a list it's not necessary to use a type annotation but if you're in a language where you do have types or type hints it's a nice thing to do now in order to process this and manipulate in some way I'm actually going to write some functions we don't have to write functions but I like to write functions it helps me to keep code more organized and it's easy to test them well I'm going to make a function here that's going to filter Talks by tag and I gave it a really nice function name right and just based on that function name we have a whole suggestion from copilot I like this suggestion it says what it's going to take in and notice that now it's using type annotations for its suggestions because it saw this one type annotation here so this is cool because you know I can get type annotations for free and it says that this is returning a new list this is important too because we could have actually processed the original list but I did actually want to return a new list and that's something that helps to specify as well one thing that's interesting here is that it made the design choice to use a filter function that's a built-in function for python which you can filter a list or any iterable and then turn that back into a list this is all fine and good and it should work but I actually want to use a list comprehension because I prefer list comprehensions myself so in the comment here I'm just going to give it a little hint to use a list comprehension and then I'll go in here and this time it uses list comprehension and I prefer that now let's try it out say python talks equals filter Talks by tag python talks all right and then we'll print those out it seems like that worked but I want to be able to view these results a bit nicer so I'm going to make a function for pretty printing talks so up here I'll say pretty print talks and let's see what it suggests this looks good here so it Title by Speaker I don't need speaker because I'm actually the speaker of all these talks these are my own talks I want to just move the tags up into here all right so let's take a look at what this looks like [Music] okay that looks nice except that the tags are a little nerdy looking because they're actually coming out as a list we might want them as you know just a comma separate list without the brackets so let's say you know format tags as comma separated list so I'm going to go ahead and delete this and see the suggestion he really really wants that speaker there I'm going to delete it again what I should have done is up here said title tags and here we go now let's try it out there we go that looks a lot nicer what we've seen here is that it's helpful to provide context sometimes that context is another file that's open sometimes it's you know a comment whether on the function or the module maybe it's a you know a well-written function name or parameter names sometimes it's an inline comment just to guide it along the right path the more context you can give to help it figure out which way to go that's really going to help it out now I'm going to do a bit of database model design using the SQL Alchemy python package so let's say we're going to Define models for a restaurant application using SQL Alchemy let's start with the Imports and let's see what's suggested so we see some standard Library Imports some SQL Alchemy Imports and hopefully that's enough uh so it's suggesting to create a instance of declarative Base Class it's going on to actually create the models so for restaurant it's got a table name as it should and an ID and a name and let's also add an address and a phone so this did work however what's interesting is that SQL Alchemy recently came up with a new versions equal me 2.0 and it has a fairly different interface because it is trying to work better with python type Checkers copilot suggested code using the previous interface which will actually still work it's backwards compatible but isn't as modern as it could be so in cases like this what I like to do is actually give copilot more context by providing an example straight from the documentation you know straight from the horse's mouth so I'm going to go to the documentation for SQL Alchemy and here is their orm quick start so I'm just going to go ahead and actually delete all of this and start with their quick start you know co-pilot doesn't need a lot of information it just needs a hint so let's leave one of the classes in here and say we're going to find model for restaurant with you know name address phone let's see what it suggests so this is really cool this time everything I suggest is using the new way of defining models in SQL Alchemy 2.0 so all it needed was a little bit of context you know an additional example to show like Hey we're using this more you know this more modern way of using SQL Alchemy and it was able to figure out how to make a better suggestion and now I can delete this example from the docs and just stick with my restaurant model so the takeaway here is that if you are using a third-party package it's always a good thing to double check documentation see if your code looks like what you see in the documentation you know if the interface has changed just give copilot a little hint by pasting in some examples from the docs that's all it needs to get started now let's use copilot to add features to an existing code base the one I'm working on is a simple restaurant review app it's one of our sample apps for Azure tutorials so so it already has the ability to add restaurants and to add reviews for restaurants it's written in the Django framework which is a python web framework which roughly follows an MBC architecture more like an mvt architecture so it's got models it's got views and it's got templates so what I want to do is add a new feature to the site a discussion forum about food in Django what you do in order to create you know a new set of models views and templates is actually make a whole new directory so I can do that now let me drag this out here now I'm going to start using copilot to write this food form since Django is an mvt framework you might think like well what should we start with the models views or templates I'm going to recommend always starting with the models because everything basically Builds on top of those models so I'm going to create a new model a new file models.py and I also am going to open the previous models.py to give Django some context and I'll even bring in the import from here so I'm going to say we're creating models for a discussion forum about food users can make posts and reply to posts it's going to start suggesting how to define this model this looks good so each post can have a title can have a text and can have a date and we could add a string function as well for easier debugging then I also need a reply model and this one is related to The Post model so it did add a foreign key and replies only have text and they can have a date as well next I'm going to add abuse.py because that really builds nicely on top of models so we'll say these are the Django views for the food form app actually I'm going to go ahead and open the views from the other part of the site the restaurant review site so that it sees this as an example now we'll start with the Imports and it has a variety of imports I don't know that we're going to use all these if you know we don't end up using them my linter will let me know that it's a good thing to delete them we're definitely going to use the models though so I'm glad that it imported those now we're going to create the views so the index page should probably list all the posts and that's actually exactly what it suggested so it's grabbing all the posts and passing them as template variables into the index.html template of course this template doesn't exist yet but we can do that next now we want to post page right we want to see a post so that page will be passed an ID and it will fetch the post we also want to fetch the replies so let's get those replies and see if it amends its suggestion here it does so good we're passing in the post and the replies we also need a create post page as it suggests that is just going to render a form we also need a function that can handle the submission from that page so that's what's coming next here add post so this should actually parse the form input and create a new post object so here we see it's getting the titles getting the post text it's setting all of these and it looks like the attribute names do match the column names from models.py so I'm very happy about about that finally we need a way to add a reply I think we'll have the ad reply form in the post page itself but we need to be able to handle the post that comes so this should be handling a post request so you see it's getting information from request.posts it is creating a reply object so the big points I want to make with this example is to think about what order you develop your code base in I think it makes sense to start with database models and API schemas and then build on top of those and also consider what files would be useful to have open to provide additional context you want the files that are showing what particular models are being built off of but in this case we also want to see like oh what is views.py look like for another feature of the site so there's kind of two types of contacts that we're giving in terms of these files as you can see if we provide good context to copilot and some comments to guide it along the path it can really provide a lot of help in crafting applications especially when we're working on web applications that follow predictable structures like MVC applications do you remember what I said at the beginning provide context and be predictable context can be in the form of open files comments import statements function names data types and sometimes the best context comes from external documentation paste it in predictable program is one that uses standard naming conventions across variables and files or even uses a consistent architecture like MVC there are some situations where copilot won't be as helpful where there isn't enough context or your code is really just new and different but there are so many situations where copilot can be a highly productive pair programmer For You especially when you give it some extra guidance of course for all code that you write whether with copilot or not you should always test it and aim for 100 test coverage at the minimum fortunately copilot can help with that too if you found this helpful please like And subscribe and check out the other videos from the series too thanks for watching [Music]
Info
Channel: Visual Studio Code
Views: 61,556
Rating: undefined out of 5
Keywords: vscode, visual studio code, vs code
Id: ImWfIDTxn7E
Channel Id: undefined
Length: 14min 6sec (846 seconds)
Published: Mon May 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.