Iterative prompt engineering and refinement

ChatGPT Prompt Engineering for Developers

Fouad Trad

Machine Learning Engineer

Iterative prompt engineering

  • No prompt can be perfect at the beginning
  • Prompt Engineering is an iterative process where we:
    • Build a prompt
    • Feed it to the model
    • Observe and analyze the output
    • Reiterate to make the prompt better

Image representing a cycle. A blue circle with four arrows feeding into each other.

ChatGPT Prompt Engineering for Developers

Refining prompts

Initial prompt
prompt = "Generate an Excel sheet containing
five student names and their grades"

print(get_response(prompt))
I'm sorry, but as a text-based AI, I am 
unable to directly provide an Excel sheet. 
However, I can help you generate a sample
representation of the data you requested.
Refined prompt
prompt = "Generate a table that I can copy 
to Excel, containing five student names and 
their grades"
print(get_response(prompt))
| Student Name | Grade |
|--------------|-------|
| Student 1    |  90   |
| Student 2    |  85   |
| Student 3    |  95   |
| Student 4    |  88   |
| Student 5    |  92   |
ChatGPT Prompt Engineering for Developers

Example: analyzing a python function

code = '''
def calculate_rectangle_area(length, width):
    area = length * width
    return area
'''

Image showing a rectangle and the formula to compute its area given its width and length. Area = length\*width.

ChatGPT Prompt Engineering for Developers

Example: initial prompt

prompt = f"""
  Analyze the code delimited by triple backticks with one sentence
  ```{code}```.
"""
print(get_response(prompt))
The code calculates the area of a rectangle based on its length and width.
ChatGPT Prompt Engineering for Developers

Example: prompt refinement

We modify prompt to get programming language

prompt = f"""
  Analyze the code delimited by triple backticks and provide its programming 
  language with one sentence 
  ```{code}```.
"""
print(get_response(prompt))
The provided code is a function written in Python that calculates the area of a 
rectangle based on its length and width.
ChatGPT Prompt Engineering for Developers

Example: prompt refinement

We modify prompt to get structured output

prompt = f"""
  For the function delimited by triple backticks, provide in a structured format
  the following:
  - description: one sentence short description
  - language: the programming language used
  - input: the inputs to the function
  - output: the output returned by the function
  ```{code}```.
"""
print(get_response(prompt))
ChatGPT Prompt Engineering for Developers

Example: prompt refinement

description: This function calculates the area of a rectangle.

language: Python

input:
 - length: The length of the rectangle.
 - width: The width of the rectangle.

output:
 - area: The calculated area of the rectangle, which is the product of the length 
 and width.
ChatGPT Prompt Engineering for Developers

Few-shot prompt refinement

  • Weather description classification

Initial prompt

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
Fresh snowfall with freezing temperatures. -> 
"""
print(get_response(prompt))
Snowy
ChatGPT Prompt Engineering for Developers

Few-shot prompt refinement

  • Weather description classification

Initial prompt

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
The wind of change brought a refreshing breeze to the company's operations. -> 
"""
print(get_response(prompt))
Windy
ChatGPT Prompt Engineering for Developers

Few-shot prompt refinement

Refined prompt

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
The political climate in the country was stormy -> Unknown
The wind of change brought a refreshing breeze to the company's operations. -> 
"""
print(get_response(prompt))
Unknown
ChatGPT Prompt Engineering for Developers

Prompt refinement for various prompt types

  • Few-shot prompts: refine examples
  • Multi-step prompts: refine guiding steps
  • Chain-of-thought and self-consistency prompts: refine problem description

Image showing three rows with toggles with a hand adjusting the toggles to reflect refinement.

ChatGPT Prompt Engineering for Developers

Let's practice!

ChatGPT Prompt Engineering for Developers

Preparing Video For Download...