Context and Prompt Engineering
Prompt engineering is the process of crafting specific prompts or input queries for AI models to generate desired outputs. It involves designing the questions or tasks you give to the AI in a way that helps it understand what you're asking for and provide the most accurate and useful responses.
Your success with AI assistants and the quality of the results will directly correlate with your ability to craft effective prompts.
Microsoft's Introduction to Prompt Engineering with GitHub Copilot
Microsoft’s 4 S’s¶
- Single
- Specific
- Short
- Surround
Single¶
Always focus your prompt on a single, well-defined task or question. This clarity is crucial for eliciting accurate and useful responses from Copilot.
Break down complex problems into smaller, more manageable prompts. This can help GitHub Copilot generate more accurate and relevant code snippets for each specific task.
Examples¶
Multi-task Prompt: "Create a Python function to calculate the average of a list of numbers and sort the list in ascending or descending order based on an input parameter."
Single-task Prompt: "Create a Python function to calculate the average of a list of numbers."
Multi-task Prompt: "How do I declare a variable in JavaScript and how do I create a function?"
Single-task Prompt: "How do I declare a variable in JavaScript?"
Multi-task Prompt: "Build a web application with user authentication, database connectivity, and a responsive design."
Single-task Prompt: "Build a user authentication system for a web application."
Specific¶
Ensure that your instructions are explicit and detailed. Specificity leads to more usable and precise code suggestions.
Use specific keywords or phrases related to the problem domain in your prompt. This can help GitHub Copilot better understand the problem and generate more contextually relevant code.
Include relevant context information in your prompt to help GitHub Copilot understand the problem better. This could include variable names, function signatures, data types, or example inputs and outputs.
It is important to note that the Specific
principle can directly conflict with the Single
and Short
principles. It is important to experiment with your prompt and discover which approach gives you the results you want.
Examples¶
Vague Prompt: "Create a function to calculate the average of a list of numbers."
Specific Prompt: "Create a Python function named calculate_average
that takes a list of integers as input and returns their average."
Vague Prompt: "Write a sorting algorithm."
Specific Prompt: "Write a JavaScript function that returns an array sorted with the bubble sort algorithm."
Vague Prompt: "How do I use a loop?"
Specific Prompt: "How do I use a for
loop to iterate over an array in JavaScript?"
Short¶
While being specific, keep prompts concise and to the point. This balance ensures clarity without overloading Copilot or complicating the interaction.
Provide a clear and concise problem statement in your prompt. Clearly outline what you're trying to achieve and any specific requirements or constraints.
Examples¶
Long Prompt: "I need a function in Python that will take a list of integers as an input parameter and then calculate the average of those numbers and then return that average."
Short Prompt: "Create a Python function to calculate the average of a list of integers."
Long Prompt: "Can you show me how to declare a variable in JavaScript that will hold a string value and then how to print that variable to the console?"
Short Prompt: "How do I declare a string variable in JavaScript and print it to the console?"
Long Prompt: "I need to create a class in Java that represents a 'Car' with properties for 'make', 'model', and 'year', and methods to get and set these properties."
Short Prompt: "Create a Java 'Car' class with 'make', 'model', and 'year' properties."
Surround¶
Always provide Copilot with as much relevant context as possible.
Provide example code snippets or partial implementations in your prompt or in a code file in your IDE to guide GitHub Copilot in generating the desired code. This can help ensure that the generated code aligns with your expectations and coding style.
GitHub Copilot uses the concept of "open tabs" in your IDE to gather additional context about the code you're working on. If you have multiple files open in your IDE, Copilot considers all of them as part of the context. This means that if you're writing a function in one file and you have another file open that contains related code, Copilot can use that information to provide more accurate and relevant suggestions. It is important to note that Copilot doesn't have access to any files that aren't currently open in your IDE.
Examples¶
Without Surround: "Write a function to handle form submission."
With Surround: "In the context of a React component, write a function to handle form submission."
Without Surround: "Fix this error."
With Surround: "I'm getting a 'TypeError: Cannot read property 'map' of undefined' in my JavaScript code. Here's the function causing the issue:
How can I fix this?"
Without Surround: "Create a database connection."
With Surround: "Create a PostgreSQL database connection using the pg library in Node.js."
Iterate and Provide Feedback¶
The code generated by Copilot might not be usable after the first prompt. Delete it and adjust the prompt, iterating until the code generated looks correct and is usable.
Continuously review and provide feedback on the generated code to improve its accuracy and relevance over time. GitHub Copilot learns from user interactions, so the more feedback you provide, the better it becomes at generating code tailored to your needs.