Skip to content

Prompt Engineering

Prompt engineering is the art of crafting clear and effective prompts to guide AI to generate the desired output. OpenAI has a detailed prompt engineering guide on how to design effective prompts. Actually, you do not even need to memorize all the details in the guide. You can also design a personalized meta prompt to let AI generate good prompts for you, e.g., the openAI prompt generator.

Prompt Formula

A good prompt includes 6 components ranked by importance:

  • [task] Clearly define your end goal
  • [context] Tailor your responses
  • [examples] Mimic style, structure, tone
  • [persona] Embody a specific expertise
  • [format] Bullet points, markdown, table
  • [tone] Add layer of emotional context
You are a senior product marketing manager at Apple [persona] and you have just unveiled the lastest Apple product in collaboration with Tesla, the Apple Car, and received 12,000 pre-orders, which is 200% higher than target [context]

Write [task] an email [format] to your boss , Tim Cook, sharing this positve news 

The email should include a ti; dr (too long, didn't read) section, project background (why this product came into existence), business results section (quantifiable, business metrics), and end with a section thanking the product and engineering teams.
[exemplar]

Use clear and concise language and write in a confident yet friendly tone [tone]

Prompt Tips

  1. Always add context

  2. Include all the related documentations

  3. Add comments in the beginning of the file to describe what the file is for
  4. Include external website links (API docs, etc. if needed)

  5. Establish a Clear Objective

  6. Use a markdown file to write your goals for AI to understand

  7. Set clear, high-level goals when starting with a blank file/codebase

Example:

Project: REST API Development
Objective: Create a FastAPI backend service that will:
1. Handle user authentication
2. Provide CRUD operations for blog posts
3. Implement rate limiting
4. Include API documentation
5. Follow REST best practices

Technical Requirements:
- Python 3.9+
- FastAPI framework
- PostgreSQL database
- JWT authentication

  1. Decompose Tasks into Steps

  2. Break down your task into simple, specific steps after communicating the main goal

  3. Ask the AI to use "Chain of Thought" to think step by step

Example:

Task: Implement a data pipeline for processing sensor data

Steps:
1. Data Ingestion
   - Read raw JSON files from S3 bucket
   - Validate data schema
   - Handle missing values

2. Data Processing
   - Convert timestamps to UTC
   - Calculate rolling averages
   - Remove outliers

3. Data Aggregation
   - Group by sensor ID
   - Calculate hourly statistics
   - Generate summary metrics

4. Data Export
   - Save processed data to PostgreSQL
   - Generate CSV reports
   - Log processing metrics

  1. Provide Examples

  2. This is called in-context learning or few-shot learning. AI performs better when you provide examples.

  3. You should be specific about the format of the output.

  4. Check the Code

  5. Always review and validate the code generated by AI

  6. You can also ask the AI to critique itself and improve the code

Example:

After AI generates code, ask:

1. Code Review Checklist:
   - Are all edge cases handled?
   - Is there proper error handling?
   - Are there unit tests?
   - Does it follow project's coding style?
   - Are there any performance concerns?

2. Improvement Request:
   "Please review the code for:
   - Potential memory leaks
   - SQL injection vulnerabilities
   - API rate limiting issues
   - Proper logging implementation"

  1. Iterative Refinement

  2. Use the AI's output as a starting point

  3. Refine the implementation through multiple iterations
  4. Ask AI to provide feedback on what needs improvement

Example:

Initial prompt:
"Create a data visualization function for our sales data"

Critique:
"Provide a concise paragraph on how to improve the code. Be very critical in your response."

Refinement 1:
"The plot looks good, but please:
1. Add proper axis labels
2. Include a title
3. Use a colorblind-friendly palette
4. Add a legend"

Refinement 2:
"Now enhance it with:
1. Interactive tooltips
2. Ability to zoom
3. Export to PNG option"

Prompt Generation Tools

You can use AI to generate prompts for you. Here is an example of meta-prompt for prompt generation:

I want you to become my Expert Prompt Creator. Your goal is to help me craft the best possible prompt for my needs. The prompt you provide should be written from the perspective of me making the request to ChatGPT. Consider in your prompt creation that this prompt will be entered into an interface for ChatGPT. The process is as follows:
1. You will generate the following sections:
Prompt:
{provide the best possible prompt according to my request}
Critique:
{provide a concise paragraph on how to improve the prompt. Be very critical in your response.
Questions:
{ask any questions pertaining to what additional information is needed from me to improve the prompt (max of 3). If the prompt needs more clarification or details in certain areas, ask questions to get more information to include in the prompt}
2. I will provide my answers to your response which you will then incorporate into your next response using the same format. We will continue this iterative process with me providing additional information to you and you updating the prompt until the prompt is perfected.
Remember, the prompt we are creating should be written from the perspective of me making a request to ChatGPT. Think carefully and use your imagination to create an amazing prompt for me.
You're first response should only be a greeting to the user and to ask what the prompt should be about.

Here are some tools that can help you generate prompts:

  • PromptPerfect: transforms user ideas into precise prompts suitable for various AI models, including GPT-4, Midjourney, and Claude. It supports both text and image models
  • prompts.chat: Prompt examples for various tasks.
  • Copy Coder: Upload images of full applications, UI mockups, or custom designs and it will generate prompts to build your apps faster.