Prompt Engineering - runtimerevolution/labs GitHub Wiki
Prompt engineering is the process of designing and refining input prompts to optimize the performance of LLM's. It involves crafting questions or instructions that effectively guide the model to generate accurate, relevant, and coherent responses. This practice can include:
- Clarity and Specificity: Ensuring the prompt is clear and specific to avoid ambiguous or irrelevant answers.
- Contextual Information: Providing sufficient background or context to help the model understand the query better.
- Iterative Refinement: Testing and modifying prompts based on the responses received to improve outcomes.
- Use of Examples: Including examples in the prompt to illustrate the desired response format or style.
The goal of prompt engineering is to harness the full potential of language models by shaping their outputs through carefully designed inputs.
Prompt Engineering for code generation
Prompt engineering for code generation involves crafting prompts that help a language model produce accurate and efficient code. This skill is essential because it unlocks AI’s full potential, allowing for the seamless translation of ideas into tangible code. Here are some detailed strategies and considerations:
- Clarity and Specificity Define the Task Clearly: Explicitly state what you want the code to do. For example, instead of asking "Write a function," specify "Write a Python function that takes a list of integers and returns the list sorted in ascending order." Specify the Language: Indicate the programming language to avoid ambiguity. For example, "Write a Python function..." or "Generate JavaScript code that..."
- Contextual Information Provide Input and Output Formats: Clearly describe the expected inputs and outputs. For example, "The function should take a list of integers as input and return a sorted list of integers." Include Constraints and Requirements: Mention any specific constraints or requirements. For example, "The function should handle duplicate values and should not use built-in sorting functions."
- Iterative Refinement Test and Adjust: Run the generated code to see if it meets the requirements. If not, adjust the prompt and try again. This might involve specifying additional details or rephrasing parts of the prompt for clarity. Feedback Loop: Use feedback from the generated code to refine your prompt. For example, if the model misunderstands a part of the task, clarify or rephrase that part.
- Use of Examples Provide Examples: Include examples of input and expected output to guide the model. For instance, "For example, given the input [3, 1, 4, 1, 5, 9], the function should return [1, 1, 3, 4, 5, 9]." Show Partial Solutions: Sometimes showing a partial solution or a related example can help the model understand the task better. For example, "Here is a similar function that sorts a list of strings..."
- Complex Tasks Break Down the Task: For complex tasks, break them down into smaller, manageable steps. For instance, if you want to generate code for a web scraper, you might start with a prompt to generate code to fetch a web page, then another prompt to parse the HTML, and so on. Modular Prompts: Use modular prompts to build complex code incrementally. This can involve generating helper functions first and then the main function.
Step 1: Write a Python function to fetch the HTML content of a web page given its URL. Step 2: Write a Python function to parse the HTML content and extract all the links.
- Prompt Patterns Code Comments: Use comments in the prompt to explain the code structure and logic. For example, "Write a Python function that calculates the factorial of a number. # The function should use recursion." Documentation Style: Frame the prompt in a documentation style, such as including a docstring.
Prompt components:
- Role Specification Purpose: Defines the goal of the prompt. Example: "Write a function that calculates the factorial of a number."
- Language Specification Purpose: Specifies the programming language to be used. Example: "Write a Python function to find the maximum value in an array."
- Input and Output Definition Purpose: Clarifies what inputs the function or code should accept and what outputs it should produce. Example: "The function should take a list of integers as input and return a sorted list of integers."
References:
- https://www.edureka.co/blog/prompt-engineering-for-code-generation/
- https://aws.amazon.com/what-is/prompt-engineering/