Skip to content

Latest commit

 

History

History

05-Using-GitHub-Copilot-with-Python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Using GitHub Copilot with Python

GitHub Copilot is a groundbreaking AI developer tool designed to accelerate your coding workflow by offering intelligent, autocomplete-style suggestions as you write. In this module, you’ll harness the power of GitHub Copilot to enhance your Python coding efficiency.

As a developer, maximizing productivity and streamlining your coding process are key objectives. GitHub Copilot acts as your AI-powered pair programmer, delivering context-aware suggestions based on code patterns and project context. This module will guide you in effectively using GitHub Copilot with Codespaces to generate and implement code seamlessly.

Prepare to tackle a real-world scenario! You’ll work on modifying a Python repository to build an interactive HTML form and an API endpoint. This hands-on project will provide valuable experience in creating a Python web application that serves an HTTP API and generates pseudo-random tokens for identification purposes.

  • Who this is for: Developers, DevOps Engineers, Software development managers, Testers.
  • What you'll learn: Using GitHub Copilot to create code and add comments to your work.
  • What you'll build: Python files that will have code generated by Copilot AI for code and comment suggestions.
  • Prerequisites: To use GitHub Copilot you must have an active GitHub Copilot subscription. Sign up for 30 days free Copilot.
  • Timing: This module can be completed in under an hour.

By the end of this module, you'll aquire the skills to be able to:

  • Craft prompts to generate suggestions from GitHub Copilot
  • Apply GitHub Copilot to improve your projects.

Prerequisite reading:

Requirements

  1. Enable your GitHub Copilot service
  2. Open this repository with Codespaces

💪🏽 Exercise

Right click the following Codespaces button to open your Codespace in a new tab

Open in GitHub Codespaces

The API already has a single endpoint to generate a token. Let's update the API by adding a new endpoint that accepts text and returns a list of tokens.

🛠 Step 1: Add a Pydantic model

Go to the main.py file, navigate to the bottom of the provided code, select Ctrl + I (PC) or Cmd + I (Mac) and copy the following into the provided GitHub Copilot Chat box so that it can generate a Pydantic model for you:

Create a Pydantic model so that I can use it in a new route that will accept JSON with text as a key which accepts a string.

The generated model should look like this:

    class TextData(BaseModel):
        text: str

Note

You might see some linter warnings in the editor (identifiable with red dotted underlines) which can be ignored. These include long lines or even new lines that aren't needed. Feel free to address them, although these shouldn't affect your application from running correctly.

🔎 Step 2: Generate a new endpoint

Next, generate a new endpoint with GitHub Copilot by adding the comment at the very bottom of the main.py file under the last route.

# Create a FastAPI endpoint that accepts a POST request with a JSON body containing a single field called "text" and returns a checksum of the text 

You might get a suggestion that uses a module or library that wasn't imported. If you do, you can ask GitHub Copilot to help you import the right module by selecting the generated code and using Command+I (Apple) or Control+I (Windows) and add a prompt to add the missing imports. This small pop-out is called inline chat and is another way to interact with GitHub Copilot.

🐍 Step 3: Explain code

The generate() route creates a pseudo-random token ID using a single line that might be difficult to fully understand. Select the whole function, and then right click on the selection, then select the Copilot menu item, and then the "Explain This" option. The GitHub Copilot chat interface will open to the left and provide you a useful explanation which you can use to interactively ask more questions.

Finally, verify the new endpoint is working by trying it out by going to the /docs endpoint and confirming that the endpoint shows up.

🚀 Congratulations, through the exercise, you haven't only used Copilot to generate code but also done it in an interactive and fun way! You can use GitHub Copilot to not only generate code, but write documentation, test your applications and more.

💡 Step 4: Using Slash Commands

Now that you've used GitHub Copilot to generate and explain code, you can also explore some other alternative approaches to perform developer tasks. These extra challenges will help you dive deeper into other GitHub Copilot features in addition to the ones you already know. For these extra challenges, you will use the Chat interface. Click on the GitHub Copilot Chat icon on the left sidebar if you don't have it open yet.

Generate documentation

With main.py open, use the chat interface with the following text:

/docs I need to document the routes for these API Routes. Help me produce documentation I can put in the README.md file of this project

The /docs part of the prompt is called a "slash command" and it is a specific feature of GitHub Copilot that allows you to write documentation. If the results look good, add them to a new section of your README.md file.

Generate tests

The current code doesn't have any tests. For this challenge, you will use the /tests slash command. With main.py open, use the chat interace with the following prompt:

/tests help me write a test for the generate() route using the FastAPI test client and the Pytest framework. Help me understand where I should put the test file, how to add the Pytest dependency to my project, and how to run the tests

The /tests slash command will guide you through on writing a new test for your route and give you everything you need so that you can verify your work.

Workspace challenge

Finally, you will get a chance to use an agent. Agents are a special feature of GitHub Copilot in Visual Studio Code that allow specific context to be shared with GitHub Copilot. For this final challenge, you will use the @workspace agent which includes files from the current worspace to provide more context. You will solve a problem which is related to how to run the whole application. In this case, you will enhance the README.md for more specifics that span multiple files. Using @workspace helps provide more context without having to open many files.

For this final challenge, you aren't required to have any open files. Use the following prompt in the GitHub Copilot Chat window:

@workspace I want to provide instructions on how to run this application using the uvicorn webserver, I also need to provide instructions on how to install the dependencies properly and what are some characteristics of the FastAPI framework. I will use this to improve the README.md file

The result should be a very good explanation on FastAPI, how to run the application, and how to get dependencies installed. A report at the very top of the response may include all the references used to determine what files it needed to use to provide the right context for GitHub Copilot.

Legal Notices

Microsoft and any contributors grant you a license to the Microsoft documentation and other content in this repository under the Creative Commons Attribution 4.0 International Public License, see the LICENSE file, and grant you a license to any code in the repository under the MIT License, see the LICENSE-CODE file.

Microsoft, Windows, Microsoft Azure and/or other Microsoft products and services referenced in the documentation may be either trademarks or registered trademarks of Microsoft in the United States and/or other countries. The licenses for this project do not grant you rights to use any Microsoft names, logos, or trademarks. Microsoft's general trademark guidelines can be found at http://go.microsoft.com/fwlink/?LinkID=254653.

Privacy information can be found at https://privacy.microsoft.com/en-us/

Microsoft and any contributors reserve all other rights, whether under their respective copyrights, patents, or trademarks, whether by implication, estoppel or otherwise.