Prompting few-shot

Prompt Engineering con l'API di OpenAI

Fouad Trad

Machine Learning Engineer

Prompting few-shot

  • Il modello riceve esempi (coppie domanda-risposta)

Una rappresentazione visiva di un prompt few-shot che mostra la struttura con domande e risposte di esempio, e infine la domanda da far rispondere al modello.

Prompt Engineering con l'API di OpenAI

Prompting few-shot

  • Il modello riceve esempi (coppie domanda-risposta)

Un diagramma che mostra come inviare un prompt few-shot all'LLM.

Prompt Engineering con l'API di OpenAI

Prompting few-shot

  • Il modello riceve esempi (coppie domanda-risposta)

Un diagramma che mostra come inviare un prompt few-shot all'LLM e ottenere una risposta.

Prompt Engineering con l'API di OpenAI

Prompting few-shot

  • Il modello riceve esempi (coppie domanda-risposta)

Un diagramma che mostra come inviare un prompt few-shot all'LLM e ottenere una risposta.

  • Numero di esempi:
    • Zero -> zero-shot prompting
    • Uno -> one-shot prompting
    • Più di uno -> few-shot prompting
Prompt Engineering con l'API di OpenAI

Prompting zero-shot

  • Prompt senza esempi
  • Il modello risponde basandosi sulla propria conoscenza
  • Ideale per compiti rapidi e semplici
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.
Prompt Engineering con l'API di OpenAI

Prompting one-shot

  • Fornisci al modello un singolo esempio
  • Utile per formati o stili coerenti
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
Prompt Engineering con l'API di OpenAI

Prompting one-shot

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
Prompt Engineering con l'API di OpenAI

Prompting few-shot

  • Fornisci più di un esempio
  • Potente per compiti con contesto
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

"""

Prompt Engineering con l'API di OpenAI

Prompting few-shot

  • Fornisci più di un esempio
  • Potente per compiti con contesto
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
Prompt Engineering con l'API di OpenAI

Few-shot con un modello chat

response = client.chat.completions.create(
  model = "gpt-4o-mini",

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
Prompt Engineering con l'API di OpenAI

Considerazioni

  • Scegli il numero di shot in base alla complessità del compito
    • Pochi shot -> compiti semplici
    • Shot vari -> compiti complessi

Immagine di una persona che si interroga.

Prompt Engineering con l'API di OpenAI

Ayo berlatih!

Prompt Engineering con l'API di OpenAI

Preparing Video For Download...