Prompt Engineering - accentient/github-copilot-devs GitHub Wiki

When using GitHub Copilot, it's essential to understand the concepts of "prompts" and "completions" (also referred to as suggestions or responses). These terms define the input given to GitHub Copilot and the output generated by it, respectively.

What is a Prompt?

A prompt is the input or query that you provide to GitHub Copilot. It is a directive or question that sets the stage for Copilot to generate a response. The prompt guides Copilot by providing context, instructions, or specific information that influences the kind of output it produces.

Characteristics of a Good Prompt:

  • Clarity: The prompt should be clear and free of ambiguity
  • Specificity: It should provide enough detail to guide Copilot effectively
  • Contextual: Including relevant context can help Copilot generate a more accurate response
  • Direct: The prompt should explicitly state what you want Copilot to do

Examples of Prompts:

  • Descriptive Prompt: "Write a function to calculate the factorial of a number in Python"
  • Instructive Prompt: "Explain the concept of recursion in programming"
  • Conversational Prompt: "As an expert in JavaScript, what are the best practices for writing clean code?"
  • Task-Oriented Prompt: "Generate unit tests for this function that calculates the sum of an array"

What is a Completion/Suggestion/Response?

A completion, suggestion, or response is the output generated by GitHub Copilot in reply to the given prompt. This output is Copilot’s attempt to fulfill the request made in the prompt, based on its training data and understanding of programming patterns.

Characteristics of a Good Completion:

  • Relevance: The response should be directly related to the prompt
  • Accuracy: It should be factually correct and logical
  • Completeness: The response should fully address the query posed in the prompt
  • Clarity: The output should be clear and easy to understand

Examples of Completions:

  • For the Descriptive Prompt:

    • Prompt: "Write a function to calculate the factorial of a number in Python."
    • Completion:
      def factorial(n):
          if n == 0:
              return 1
          else:
              return n * factorial(n-1)
      
  • For the Instructive Prompt:

    • Prompt: "Explain the concept of recursion in programming"
    • Completion: "Recursion is a programming technique where a function calls itself in order to solve a problem. It typically involves a base case to stop the recursion and a recursive case that breaks the problem into smaller instances"
  • For the Conversational Prompt:

    • Prompt: "As an expert in JavaScript, what are the best practices for writing clean code?"
    • Completion: "Some best practices for writing clean JavaScript code include using meaningful variable names, keeping functions small and focused, avoiding global variables, and writing comments to explain complex logic"
  • For the Task-Oriented Prompt:

    • Prompt: "Generate unit tests for this function that calculates the sum of an array"
    • Completion:
      import unittest
      
      def sum_array(arr):
          return sum(arr)
      
      class TestSumArray(unittest.TestCase):
          def test_sum_array(self):
              self.assertEqual(sum_array([1, 2, 3]), 6)
              self.assertEqual(sum_array([-1, 1, 0]), 0)
              self.assertEqual(sum_array([]), 0)
      
      if __name__ == '__main__':
          unittest.main()
      

Prompt Techniques for GitHub Copilot

Constructing clear, specific, and effective prompts is crucial for eliciting high-quality responses from GitHub Copilot. Here are some techniques to help you create better prompts:

1. Start with Simple Prompts

Begin with straightforward prompts to understand how Copilot responds. This helps establish a baseline for more complex prompts.

  • Simple: "Write a Python function to add two numbers."
  • Complex: "Write a Python function to add two numbers and handle invalid inputs."

2. Be Clear and Unambiguous

Avoid vague language and ensure that your prompt is easy to understand. Ambiguity can lead to irrelevant or unclear responses.

  • Vague: "Tell me about REST APIs."
  • Clear: "Explain the concept of REST APIs and their primary use cases in web development."

3. Provide Specific Details

Incorporate specific details to guide Copilot’s response towards the desired information.

  • General: "Describe a sorting algorithm."
  • Specific: "Describe how the quicksort algorithm works and its time complexity."

4. Include Context

Providing context helps Copilot understand the background and the relevance of the prompt, leading to more accurate responses.

  • Without Context: "Explain dependency injection."
  • With Context: "Explain the benefits of dependency injection in a large-scale Java application."

5. Use Examples

Examples can clarify the format or type of response you expect, guiding Copilot effectively.

  • Without Example: "Generate a function to parse JSON data."
  • With Example: "Generate a Python function to parse JSON data. Example: def parse_json(data): return json.loads(data)."

6. Explicit Instructions

Clearly state what you want Copilot to do, including any specific requirements or constraints.

  • Implicit: "Summarize this code."
  • Explicit: "Summarize this code in two sentences, highlighting the main functionality and any key methods used."

7. Ask for Step-by-Step Responses

For complex tasks, asking for a step-by-step breakdown can help ensure that the response covers all necessary aspects.

  • General: "Explain how to implement a binary search algorithm."
  • Step-by-Step: "Explain how to implement a binary search algorithm step by step, starting with the initial array and the target value."

8. Limit Response Length

Specify the desired length of the response to ensure conciseness and relevance.

  • Without Length Limit: "Describe the MVC architecture."
  • With Length Limit: "Briefly describe the MVC architecture in 100 words."

9. Use Conditional Prompts

Conditional prompts can guide Copilot to provide different responses based on specific criteria.

  • General: "Describe the advantages of Python and JavaScript."
  • Conditional: "If the user is asking about web development, describe the advantages of JavaScript. If they are asking about data science, describe the advantages of Python."

10. Role-Playing

Assigning roles to Copilot can help tailor the response to a specific perspective or expertise.

  • General: "Explain the benefits of unit testing."
  • Role-Playing: "As a senior software engineer, explain the benefits of unit testing for maintaining code quality."

11. Multi-Turn Prompts

Design prompts for multi-turn interactions, simulating a conversation to gather more detailed information.

  • Single Turn: "List three potential improvements for this code snippet."
  • Multi-Turn: "In the first response, list three potential improvements for this code snippet. In the second response, provide a refactored version of the code."

12. Refine Prompts Iteratively

Refine your prompts based on Copilot’s responses. If the initial response is not satisfactory, tweak the prompt for clarity, specificity, or context.

  • Initial Prompt: "Describe a healthy diet."
  • Refined Prompt: "Describe a balanced diet for an active 25-year-old vegan."

Conclusion

Understanding the relationship between prompts and completions is fundamental to effectively using GitHub Copilot. A well-crafted prompt leads to a high-quality response, making prompt engineering a crucial skill in leveraging the power of Copilot. Whether you are generating code, seeking explanations, or performing specific tasks, mastering the art of prompt creation will significantly enhance your interactions with GitHub Copilot.

Effective prompt construction is an iterative process that involves testing, refining, and adapting based on the responses received. By applying these techniques, you can create prompts that guide GitHub Copilot to produce accurate, relevant, and high-quality responses. Experiment with different strategies and continue refining your approach to master the art of prompt engineering.

Resources