Principes fondamentaux de l'ingénierie des prompts

Ingénierie des prompts avec l'API OpenAI

Fouad Trad

Machine Learning Engineer

Prompts clairs et précis

A visual diagram showing a bakery and a house with two possible routes between them. One if simple and short (effective), and the other is complicated and long (ineffective).

Ingénierie des prompts avec l'API OpenAI

Principes fondamentaux

 

  • 🎬 Verbes d'action appropriés
  • 💬 Instructions détaillées et précises
  • ❗ Des prompts bien structurés et délimités
Ingénierie des prompts avec l'API OpenAI

Utilisation de verbes d'action

  • Indiquer au modèle ce qu’il doit faire

Table for the verbs to use including write, complete, explain, describe, or evaluate.

Ingénierie des prompts avec l'API OpenAI

Éviter les verbes ambigus

  • Confusion dans la compréhension du modèle

Table for the verbs to use including write, complete, explain, describe, or evaluate, and the verbs to avoid using such as understand, think, feel, try, and know.

Ingénierie des prompts avec l'API OpenAI

Prompt efficace avec des verbes

prompt = "Think about the issue of 
deforestation."
response = get_response(prompt)
print(response)
Deforestation is a significant 
environmental issue involving 
permanently removing
or destroying forests and woodlands. 
It has far-reaching impacts on the 
environment, ecosystems, wildlife, 
and human communities. [...]
prompt = "Propose strategies to 
reduce deforestation."
response = get_response(prompt)
print(response)
Reducing deforestation requires 
a comprehensive and multi-dimensional 
approach involving various stakeholders. 
Here are several strategies 
that can help address the issue:
Strengthen Forest Governance
Promote Sustainable Land Use [...]
Ingénierie des prompts avec l'API OpenAI

Formuler des instructions détaillées

 

Fournir des instructions spécifiques, descriptives et détaillées concernant :

  • 📖 Contexte
  • 📏 Longueur du résultat
  • 🖌 Format et style
  • 👥👥 Public
Ingénierie des prompts avec l'API OpenAI

Prompt efficace avec instructions

Prompt inefficace : « Que sais-tu sur les chiens ? »

Prompt efficace

prompt = "Write a descriptive paragraph about the behavior and characteristics of 
Golden Retrievers, highlighting their friendly nature, intelligence, 
and suitability as family pets."
print(get_response(prompt))
Golden Retrievers are beloved worldwide for their exceptional behavior, remarkable 
characteristics, and friendly nature. They are highly intelligent, trainable, and 
adaptable, making them great companions for families, including those with children.
Ingénierie des prompts avec l'API OpenAI

Limitation de la longueur de résultat

max_tokens :

  • Limite du nombre de tokens
  • Le résultat ne peut pas le contourner
  • Pourrait entraîner des réponses incomplètes ou tronquées

Prompt :

  • Limite du nombre de mots, de phrases ou de paragraphes
  • Le résultat peut le contourner
  • Réponses complètes
Ingénierie des prompts avec l'API OpenAI

Composants du prompt

  • Instructions et données d'entrée à traiter
  • Exemple : résumé de texte
    • Instruction : résumer le texte fourni
    • Données d'entrée : texte à résumer

An icon representing the text summarization task

Ingénierie des prompts avec l'API OpenAI

Élaboration d'un prompt bien structuré avec des délimiteurs

  • Prompter avec des instructions
  • Utiliser des délimiteurs (parenthèses, crochets, guillemets inversés, etc.) pour spécifier les parties de l'entrée
  • Indiquer quels délimiteurs sont utilisés
prompt = """Summarize the text delimited by triple backticks into bullet points.
           ```TEXT GOES HERE```"""
response = get_response(prompt)
Ingénierie des prompts avec l'API OpenAI

Utilisation de chaînes formatées (f-strings)

  • Inclure une chaîne définie dans une autre chaîne
text = "This is a sample text to summarize"

prompt = f"""Summarize the text delimited by triple backticks into bullet points. ```{text}```"""
print(prompt)
Summarize the text delimited by triple backticks into bullet points.
```This is a sample text to summarize```
Ingénierie des prompts avec l'API OpenAI

Passons à la pratique !

Ingénierie des prompts avec l'API OpenAI

Preparing Video For Download...