Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions 05-Using-GitHub-Copilot-with-Python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ The API already has a single endpoint to generate a token. Let's update the API

### 🛠 Step 1: Add a Pydantic model

Go to the `main.py` file, and add a comment so that GitHub Copilot can generate a `Pydantic` model for you. The generated model should look like this:
Go to the `main.py` file, and add a comment anywhere in the file so that GitHub Copilot can generate a `Pydantic` model for you. For example you could add a comment like:

```
# 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:

```python
class Text(BaseModel):
Expand All @@ -50,7 +57,7 @@ text: str

### 🔎 Step 2: Generate a new endpoint

Next, generate a new endpoint with GitHub Copilot by adding the comment:
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.

```python
# 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
Expand All @@ -62,7 +69,43 @@ The `generate()` route creates a pseudo-random token ID using a single line that

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.
🚀 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.

### Extra challenges

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.

**Final 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

Expand Down