How Copilot Learns from your Prompts?
GitHub Copilot operates based on AI models trained on vast amounts of data. To enhance its understanding of specific code contexts, engineers often provide it with examples. This practice, commonly found in machine learning, led to different training approaches such as:
Zero-shot learning
Here, GitHub Copilot generates code without any specific examples, relying solely on its foundational training. For instance, suppose you want to create a function to convert temperatures between Celsius and Fahrenheit. You can start by only writing a comment describing what you want, and Copilot might be able to generate the code for you, based on its previous training, without any other examples.
# I need a function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
One-shot learning
With this approach, a single example is given, aiding the model in generating a more context-aware response. Building upon the previous zero-shot example, you might provide an example of a temperature conversion function and then ask Copilot to create another similar function. Here’s how it could look:
# Example: Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit-32) * 5/9
return celsius
# Now I need a function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 9/5 + 32
return fahrenheit
Few-shot learning
In this method, Copilot is presented with several examples, which strike a balance between zero-shot unpredictability and the precision of fine-tuning. Let’s say you want to generate code that sends you a greeting depending on the time of the day. Here’s a few-shot version of that prompt:
# Example 1: Greeting message for morning
# Input: 9 AM
# Output: "Good morning!"
# Example 2: Greeting message for afternoon
# Input: 2 PM
# Output: "Good afternoon!"
# Example 3: Greeting message for evening
# Input : 7 PM
# Output : "Good evening!"
# Now, generate a python code that takes the current time as input using the datetime module
# and returns the appropriate greeting message
# Solution:
# Import datetime module
import datetime
# Get current time
current_time = datetime.datetime.now()
# Get current hour
current_hour = current_time.hour
# Check if it is morning (before 12 PM)
if current_hour < 12:
print("Good morning!")
# Check if it is afternoon (between 12 PM and 4 PM)
elif current_hour < 16:
print("Good afternoon!")
# Check if it is evening (after 4 PM)
elif current_hour < 21:
print("Good evening!")
# Else it is night time
else:
print("Good night!")
Let’s Consider a real-life Engineering problem:
Problem: You are developing a Web Application that requires user authentication. You need to implement a secure login system using JSON Web Tokens(JWT) in your Node.js backend.
Using GitHub Copilot:
1. Contextualize the Problem: In your Node.js codebase, create a new file named ‘auth.js’ where you will handle authentication-related functions.
2. Provide Contextual Comments: Add Comments explaining what you need, such as:
// Function to generate JWT Token for user authentication
3. Invoke GitHub Copilot: Start writing the function signature or comment describing what you need, like:
// Generate JWT token for user authentication
4. Review Copilot Suggestions: Copilot might suggest code like :
const jwt = require('jsonwebtoken
function generateToken(payload, secret, options) {
return jwt.sign(payload, secret, options);
}');
5. Refine the Suggestions: Ensure that the function meets your requirements and project conventions. To validate input parameters and handle errors:
const jwt = require('jsonwebtoken');
function generateToken(payload, secret, options) {
if(!payload || !secret) {
throw new Error('Payload and Secret are required');
}
return jwt.sign(payload, secret, options);
}
6. Test the Function: Write test cases to ensure the function works as expected, considering different scenarios like valid and invalid input, expired tokens, etc.
7. Feedback Loop: If Copilot’s suggestions are not accurate or if you encounter any issues, provide feedback so that Copilot can improve its suggestions as well accordingly.
8. Continuously Improve: As you work on your project, continue using Copilot for other authentication-related functions, such as token verification, token expiration handling, and refresh token generation.
Prompt Engineering Tips with GitHub Copilot
GitHub Copilot, powered by OpenAI, is changing the game in software development. It’s not just a tool for suggesting code. GitHub Copilot can grasp the crucial details of your project through its training of data containing both natural language and billions of lines of source code from publicly available sources, including code in public GitHub repositories. This allows GitHub Copilot to provide you with more context-aware suggestions.
But to get the most out of GitHub Copilot, you need to know about prompting. This is the way you tell Copilot what you need. The quality of the code it gives back depends a lot on how clear and accurate your prompts are.
So how can you use GitHub Copilot to its full potential? This article will show you how! It guides you through the details of making effective prompts to get the best code suggestions, helping you code faster and better.
Table of Content
- What is Prompt Engineering?
- Principles of Prompt Engineering
- Best practices in prompt engineering
- How Copilot Learns from your Prompts?