How To Use Different Prompting Techniques in GitHub Copilot

We offer private, customized training for 3 or more people at your site or online.

GitHub Copilot is an AI pair programmer that generates code suggestions and solutions based on your prompts. Prompt engineering is the practice of writing clear instructions for a large language model (LLM). By crafting specific prompts, you can steer the model towards the desired output. This article explores various prompting techniques to maximize the effectiveness of GitHub Copilot, specifically for writing Python code.

  1. Zero-Shot Prompting

    Description: This technique provides minimal context and relies on the model's pre-trained knowledge to complete the task. Think of it as giving a broad instruction like "perform this action" without specifying the details.

    Use Case:
    Zero-shot prompting is ideal for simple tasks where the model's internal knowledge base is sufficient.

    Example:
    Prompt:
    Write a Python program that greets the user by name and asks how they are doing.
    Output:
    The model constructs a greeting message by combining a string with the retrieved name using f-strings (formatted string literals).

    Here is an example of the generated code:

    def greet_user():
       name = input("Please enter your name: ")
       print(f"Hello, {name}! How are you doing today?")
    
    greet_user()
    

  2. One-Shot Prompting:

    Description:
    Here you would give a single instruction or example to nudge the model in a specific direction, for example, giving a basic outline or a starting point for the model to build upon.

    Use Case:
    One-shot prompting is useful when you want the model to perform a single task, like generating code with a particular functionality.

    Example:
    Prompt:
    Write a Python function that takes a list of numbers and returns the average.
    Output:
    The model generates a Python function using a loop or list comprehension to calculate the average.

    Here is an example of the generated code:

    def calculate_average(numbers):
       if not numbers:  # Check if the list is empty
          return 0
       total = sum(numbers)  # Sum up all the numbers in the list
       count = len(numbers)  # Count the number of elements in the list
       average = total / count  # Calculate the average
       return average
    
    # Example usage:
    numbers = [1, 2, 3, 4, 5]
    print("The average is: {calculate_average(numbers)}")
    

  3. Few-Shot Prompting:

    Description:
    This approach provides a few examples or instructions to further refine the desired output. Think of it as a small set of examples to guide the large language model (LLM) towards the desired output.

    Use Case:
    Few-shot prompting is valuable for tasks requiring a higher degree of accuracy or specificity.

    Example:
    Prompt:
    Create a Python function named remove_duplicates_ordered that takes a list as input and returns a new list containing only the unique elements from the original list. The function should preserve the order of the first occurrence of each element.
    Output:

    def remove_duplicates_ordered(data):
       """Removes duplicate elements from a list, keeping the first occurrence."""
       seen = set()   # Store seen elements to check for duplicates. 
       unique_data = []
    
       for item in data:
          if item not in seen:
             unique_data.append(item)
             seen.add(item)
    
        return unique_data
    

    etc.

  4. Chain of Thought Prompting:

    Description:
    This technique encourages the model to explain its reasoning process step-by-step. Imagine asking the model to "show its work" while completing a task.

    Use Case:
    Chain of thought prompting is beneficial for debugging the model's decision-making process and understanding its internal reasoning. This can be particularly useful in areas like machine learning where interpretability is crucial.

    Example:
    Write a Python function that checks if a given string is a palindrome (reads the same backward as forward).

    Prompt:
    • First, define a function called "is_palindrome" that takes a string as input.
    • Next, convert the string to lowercase to avoid case sensitivity.
    • Then, iterate through the string and compare the characters at the beginning and end.
    • If they match, move inwards one character at a time and repeat the comparison.
    • If all characters match their counterparts, return True (the string is a palindrome). Otherwise, return False.

    Output:

    def is_palindrome(text):
    """This function checks if a string is a palindrome.""" text = text.lower()  # Convert the string to lowercase left, right = 0, len(text) - 1 while left < right: if text[left] != text[right]: return False left += 1 right -= 1 return True

GitHub Copilot Training

Our GitHub Copilot course teaches attendees the skills needed in prompt engineering to communicate effectively with AI, optimizing the generation of code snippets, explanations, and solutions.

Let us customize the perfect GitHub Copilot training for your team, using the programming language of your choice, including, Python, Java, C++, Ruby, Go, JavaScript, C#, and more.

Browse all our Generative AI training courses for role-based training, including developers and business users.

Learn faster

Our live, instructor-led lectures are far more effective than pre-recorded classes

Satisfaction guarantee

If your team is not 100% satisfied with your training, we do what's necessary to make it right

Learn online from anywhere

Whether you are at home or in the office, we make learning interactive and engaging

Multiple Payment Options

We accept check, ACH/EFT, major credit cards, and most purchase orders



Recent Training Locations

Alabama

Birmingham

Huntsville

Montgomery

Alaska

Anchorage

Arizona

Phoenix

Tucson

Arkansas

Fayetteville

Little Rock

California

Los Angeles

Oakland

Orange County

Sacramento

San Diego

San Francisco

San Jose

Colorado

Boulder

Colorado Springs

Denver

Connecticut

Hartford

DC

Washington

Florida

Fort Lauderdale

Jacksonville

Miami

Orlando

Tampa

Georgia

Atlanta

Augusta

Savannah

Hawaii

Honolulu

Idaho

Boise

Illinois

Chicago

Indiana

Indianapolis

Iowa

Cedar Rapids

Des Moines

Kansas

Wichita

Kentucky

Lexington

Louisville

Louisiana

New Orleans

Maine

Portland

Maryland

Annapolis

Baltimore

Frederick

Hagerstown

Massachusetts

Boston

Cambridge

Springfield

Michigan

Ann Arbor

Detroit

Grand Rapids

Minnesota

Minneapolis

Saint Paul

Mississippi

Jackson

Missouri

Kansas City

St. Louis

Nebraska

Lincoln

Omaha

Nevada

Las Vegas

Reno

New Jersey

Princeton

New Mexico

Albuquerque

New York

Albany

Buffalo

New York City

White Plains

North Carolina

Charlotte

Durham

Raleigh

Ohio

Akron

Canton

Cincinnati

Cleveland

Columbus

Dayton

Oklahoma

Oklahoma City

Tulsa

Oregon

Portland

Pennsylvania

Philadelphia

Pittsburgh

Rhode Island

Providence

South Carolina

Charleston

Columbia

Greenville

Tennessee

Knoxville

Memphis

Nashville

Texas

Austin

Dallas

El Paso

Houston

San Antonio

Utah

Salt Lake City

Virginia

Alexandria

Arlington

Norfolk

Richmond

Washington

Seattle

Tacoma

West Virginia

Charleston

Wisconsin

Madison

Milwaukee

Alberta

Calgary

Edmonton

British Columbia

Vancouver

Manitoba

Winnipeg

Nova Scotia

Halifax

Ontario

Ottawa

Toronto

Quebec

Montreal

Puerto Rico

San Juan