Ingénierie et perfectionnement itératifs des prompts

Ingénierie des prompts avec l'API OpenAI

Fouad Trad

Machine Learning Engineer

Ingénierie itérative des prompts

  • Aucun prompt ne peut être parfait dès le début
  • L'ingénierie des prompts est un processus itératif :
    • Créer une invite
    • L'intégrer au modèle
    • Observer et analyser les résultats
    • Réitérer afin d'améliorer le prompt

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

Ingénierie des prompts avec l'API OpenAI

Affiner les prompts

Prompt initial
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.
Prompt affiné
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   |
Ingénierie des prompts avec l'API OpenAI

Exemple : analyse d'une fonction Python

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.

Ingénierie des prompts avec l'API OpenAI

Exemple : prompt initial

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.
Ingénierie des prompts avec l'API OpenAI

Exemple : affinement du prompt

Nous modifions le prompt pour obtenir le langage de programmation

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.
Ingénierie des prompts avec l'API OpenAI

Exemple : affinement du prompt

Nous modifions le prompt pour obtenir un résultat structuré

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))
Ingénierie des prompts avec l'API OpenAI

Exemple : affinement du prompt

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.
Ingénierie des prompts avec l'API OpenAI

Affinement du prompt few-shot

  • Classification des descriptions météorologiques

Prompt initial

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
Fresh snowfall with freezing temperatures. -> 
"""
print(get_response(prompt))
Snowy
Ingénierie des prompts avec l'API OpenAI

Affinement du prompt few-shot

  • Classification des descriptions météorologiques

Prompt initial

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
Ingénierie des prompts avec l'API OpenAI

Affinement du prompt few-shot

Prompt affiné

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
Ingénierie des prompts avec l'API OpenAI

Amélioration des divers types de prompts

  • Few-shot prompts : affiner les exemples
  • Prompts multi-étapes : affiner les étapes de guidage
  • Prompts par chaîne de pensée et cohérence propre : affiner la description du problème

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

Ingénierie des prompts avec l'API OpenAI

Passons à la pratique !

Ingénierie des prompts avec l'API OpenAI

Preparing Video For Download...