Few-shot prompting

Ingénierie des prompts avec l'API OpenAI

Fouad Trad

Machine Learning Engineer

Few-shot prompting

  • Modèle fourni avec des exemples (paires question-réponse)

A visual representation of a few-shot prompt showing its structure with example questions and answers, and finally the question we want the model to answer.

Ingénierie des prompts avec l'API OpenAI

Few-shot prompting

  • Modèle fourni avec des exemples (paires question-réponse)

A visual diagram showing how we send a few-shot prompt to the LLM.

Ingénierie des prompts avec l'API OpenAI

Few-shot prompting

  • Modèle fourni avec des exemples (paires question-réponse)

A visual diagram showing how we send a few-shot prompt to the LLM and get an answer in return.

Ingénierie des prompts avec l'API OpenAI

Few-shot prompting

  • Modèle fourni avec des exemples (paires question-réponse)

A visual diagram showing how we send a few-shot prompt to the LLM and get an answer in return.

  • Nombre d'exemples :
    • Zero -> zero-shot prompting
    • One -> one-shot prompting
    • Plus d’un -> few-shot prompting
Ingénierie des prompts avec l'API OpenAI

Zero-shot prompting

  • Fournir un prompt sans exemple
  • Le modèle génère des réponses en fonction de ses connaissances
  • Idéal pour les tâches rapides et simples
prompt = "What is prompt engineering?"
print(get_response(prompt))
Prompt engineering refers to designing and refining prompts or instructions given 
to a language model like ChatGPT to elicit desired responses or behaviors. It 
involves formulating specific guidelines or hints to guide the model's output 
towards a desired outcome.
Ingénierie des prompts avec l'API OpenAI

One-shot prompting

  • Fournir un exemple unique au modèle
  • Utile pour assurer la cohérence du formatage ou du style
prompt =  """ 
Q: Sum the numbers 3, 5, and 6. A: 3+5+6=14
Q: Sum the numbers 2, 4, and 7. A: 
"""
print(get_response(prompt))
2+4+7=13
Ingénierie des prompts avec l'API OpenAI

One-shot prompting

prompt = """
Q: Sum the numbers 3, 5, and 6. A: The sum of 3, 5, and 6 is 14
Q: Sum the numbers 2, 4, and 7. A: 
"""
print(get_response(prompt))
The sum of 2, 4, and 7 is 13
Ingénierie des prompts avec l'API OpenAI

Few-shot prompting

  • Fournir plusieurs exemples
  • Puissant pour les tâches contextuelles
prompt = """
Text: Today the weather is fantastic -> Classification: positive
Text: The furniture is small -> Classification: neutral
Text: I don't like your attitude -> Classification: negative

"""

Ingénierie des prompts avec l'API OpenAI

Few-shot prompting

  • Fournir plusieurs exemples
  • Puissant pour les tâches contextuelles
prompt = """
Text: Today the weather is fantastic -> Classification: positive
Text: The furniture is small -> Classification: neutral
Text: I don't like your attitude -> Classification: negative
Text: That shot selection was awful -> Classification: 
"""
print(get_response(prompt))
negative
Ingénierie des prompts avec l'API OpenAI

Prompting few-shot avec un modèle de chat

response = client.chat.completions.create(
  model = "gpt-3.5-turbo",

messages = [{"role": "user", "content": "Today the weather is fantastic"},
{"role": "assistant", "content": "positive"},
{"role": "user", "content": "I don't like your attitude"}, {"role": "assistant", "content": "negative"},
{"role": "user", "content": "That shot selection was awful"} ], temperature = 0 )
print(response.choices[0].message.content)
negative
Ingénierie des prompts avec l'API OpenAI

Points importants

  • Sélectionner le nombre de shots en fonction de la complexité de la tâche
    • Fewer shots -> tâches basiques
    • Diverse shots -> tâches complexes

An image showing a person questioning themselves.

Ingénierie des prompts avec l'API OpenAI

Passons à la pratique !

Ingénierie des prompts avec l'API OpenAI

Preparing Video For Download...