Itération et amélioration des invites

Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Fouad Trad

Machine Learning Engineer

Ingénierie d'invite itérative

  • Aucune invite n'est parfaite au départ
  • L'ingénierie d'invite est un processus itératif où l'on :
    • Conçoit une invite
    • L'envoie au modèle
    • Observe et analyse le résultat
    • Réitère pour améliorer l'invite

Image représentant un cycle. Un cercle bleu avec quatre flèches enchaînées.

Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Raffiner les invites

Invite initiale
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.
Invite raffinée
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   |
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Exemple : analyser une fonction Python

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

Image montrant un rectangle et la formule pour calculer son aire selon sa largeur et sa longueur. Aire = longueur\*largeur.

Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Exemple : invite initiale

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.
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Exemple : raffiner l'invite

Nous modifions l'invite 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.
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Exemple : raffiner l'invite

Nous modifions l'invite 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))
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Exemple : raffiner l'invite

description: Cette fonction calcule l'aire d'un rectangle.

language: Python

input:
 - length: La longueur du rectangle.
 - width: La largeur du rectangle.

output:
 - area: L'aire calculée du rectangle, soit le produit de la longueur 
 et de la largeur.
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Raffinement d'invites à quelques exemples

  • Classification de descriptions météo

Invite initiale

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
Fresh snowfall with freezing temperatures. -> 
"""
print(get_response(prompt))
Snowy
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Raffinement d'invites à quelques exemples

  • Classification de descriptions météo

Invite initiale

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
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Raffinement d'invites à quelques exemples

Invite raffinée

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
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Raffinement selon le type d'invite

  • Invites à quelques exemples : peaufiner les exemples
  • Invites en plusieurs étapes : peaufiner les étapes guides
  • Invites de chaîne de pensée et d'auto-cohérence : peaufiner l'énoncé du problème

Image montrant trois rangées avec des curseurs et une main qui les ajuste pour refléter l'amélioration.

Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Passons à la pratique !

Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Preparing Video For Download...