Prompt Engineering for GitHub Co-Pilot
Introduction
Prompt engineering is the process of designing and crafting questions to get accurate and helpful responses from large language models, such as GitHub Co-Pilot. Effective prompt engineering is crucial to getting the best out of these tools, as they may not always provide perfect answers.
Principles of Effective Prompt Engineering
To get the most out of GitHub Co-Pilot, it's essential to follow some key principles of effective prompt engineering:
- Simplify: Keep your prompts simple and easy to understand.
- Specificity: Be specific about what you're asking for. Avoid vague or open-ended questions.
- Brevity: Keep your prompts short and concise.
Demo Application
For this demonstration, we'll be working with a demo application that includes a weather.json
file containing data on countries, cities, and months. The application currently returns a list of countries when the /countries
endpoint is called.
Adding an Endpoint to Return Cities by Country
We want to create an endpoint that returns the cities of a provided country. To achieve this, we'll use prompt engineering to craft a prompt that explains exactly what we want to achieve.
Crafting a Prompt
A good prompt should be single, specific, and short. Avoid simply writing code and expecting the tool to provide suggestions. Instead, explain what you're trying to achieve and what you want the tool to help you with.
Using GitHub Co-Pilot
We'll use the GitHub Co-Pilot tool to write the code for our new endpoint. We'll craft a prompt that explains what we want to achieve and get suggestions from the tool. We'll refine our prompt based on the suggestions provided and eventually get the desired output.
Tips for Effective Prompt Engineering
- The more you provide prompts, the better you will get at creating them.
- The more you explain what you're trying to achieve, the better the responses you will get.
- Practice makes perfect, so keep experimenting and refining your prompts to get the best results.
Adding Functionality to the City's Endpoint
We modified the code to ensure the provided country is in title case. This is because countries in the weather.json
file start with a capital letter, and providing a country with a lowercase letter would result in errors. The functionality was tested by sending a request to the City's endpoint with a country in both title case (Peru) and lowercase (peru). The endpoint worked correctly in both cases.
Leveraging GitHub Co-Pilot
GitHub Co-Pilot was used to implement specific tasks in the code. The process began by highlighting the code that needed to be modified. A prompt was then issued to GitHub Co-Pilot, such as "What error handling should I implement for this endpoint?" GitHub Co-Pilot provided suggestions for error handling, which were then previewed and accepted.
Error Handling Suggestions
The suggestions included handling:
- Null parameters
- Empty parameters
- Parameters that are not formatted correctly
- Parameters that do not exist
Testing the Error Handling
The modified code was tested by sending requests to the City's endpoint with various inputs, including:
- A country that exists (Peru)
- A country that does not exist (Colombia)
- Empty spaces
The endpoint handled the inputs correctly, providing the expected responses.
Using Slash Commands
GitHub Co-Pilot has slash commands that can be used without specifying the exact prompt. An example of using a slash command is creating a test for the City's endpoint. This was done by typing alt slash
and then tests
.
Combining GitHub Co-Pilot with Additional Information
Using Test SL Command with GitHub Co-Pilot
We started with an existing test file and invoked GitHub Co-Pilot using the SL
command to create tests. Initially, generated tests for:
- Get cities with valid Country
- Get cities with invalid Country
- Get cities with no Country
- Get cities with empty Country
Then, provided a more specific prompt to create a test that uses Spain as the provided country and returns Seville as the city. GitHub Co-Pilot generated a single test for this specific scenario: Get cities return list of cities for Spain
.
Running Tests and Refining GitHub Co-Pilot
We ran tests in the Test Explorer and confirmed they worked as expected. Refining GitHub Co-Pilot:
- Provided a prompt that references a specific file (
weather.json
) to get the correct city based on the country - Started with an initial prompt and provided additional context to get the suggestion tailored to the solution
Using the SL
command again, GitHub Co-Pilot generated a test for Test for Country Spain and return the correct City from weather.json
.
Refining GitHub Co-Pilot with Additional Information
Providing an initial prompt and then providing additional context helps to get the suggestion tailored to the solution. Refining GitHub Co-Pilot can lead to better results and more accurate suggestions. Can also use GitHub Co-Pilot to optimize code by removing unnecessary blocks or loops, but can refine the suggestions by providing more specific requirements.
GitHub Co-Pilot Optimized Code
GitHub Co-Pilot analyzed the code and provided an optimized version. The optimized code:
- Removed the
try catch
block - Replaced
link
with aforeach
loop
Can refine the suggestions by providing more specific requirements, such as keeping certain code blocks or loops.
Optimizing Code and Improving Documentation with GitHub Co-Pilot
- Reducing Code Complexity: The
?.
operator can be used to safely access properties while throwing an exception if the property doesn't exist. This eliminates the need for a try-catch block, making the code more concise and readable. - Refining the Prompt: Revising the initial prompt to focus on improving the code can lead to more effective results. In this case, the refined prompt helped to remove unnecessary code and optimize the solution.
- Removing Unnecessary Code: The try-catch block was removed, as the
?.
operator takes care of handling potential exceptions. - Performance Considerations: The two
ToList()
calls at the end of theLinkCarry
method could cause the entire sequence to be iterated immediately, which may be a performance issue for large data sets. - Improving Documentation: GitHub Co-Pilot can help improve documentation by providing suggestions for the README file. A good README file typically includes the project title, filing sections, and other relevant information.
- Using Pound References: GitHub Co-Pilot allows referencing files using the
#
symbol, making it easy to provide context for suggested improvements. - Example of a README File: The video demonstrates how to create a README file using GitHub Co-Pilot, including suggestions for project links and other relevant information.
- Benefits of Documentation: Documentation is crucial for projects, and GitHub Co-Pilot can help improve it by providing suggestions and recommendations.
- Future Implications: The video concludes by encouraging viewers to explore more resources and provide feedback on their experiences with GitHub Co-Pilot and Visual Studio.
Note: The section on DIO is not related to GitHub Co-Pilot and seems out of place in this article. It appears to be a brief history and discography of the heavy metal band DIO.