Output strutturati e prompt condizionali

Prompt Engineering con l'API di OpenAI

Fouad Trad

Machine Learning Engineer

Output strutturati

  • Gli LLM generano output strutturati solo con istruzioni esplicite
  • Strutture di output:
    • Tabella
    • Elenco
    • Paragrafo strutturato
    • Formato personalizzato

Diagramma che mostra che quando chiediamo a ChatGPT un output strutturato, otteniamo la struttura richiesta; se non specifichiamo la struttura, l'output è casuale o senza struttura.

Prompt Engineering con l'API di OpenAI

Tabelle

  • Indica chiaramente le colonne attese
prompt = "Generate a table containing 5 movies I should watch if I am an action 
lover, with columns for Title and Rating."
print(get_response(prompt))
| Title                           | Rating |
|---------------------------------|--------|
| Mad Max: Fury Road              | 8.1    |
| John Wick                       | 7.4    |
| The Dark Knight                 | 9.0    |
| Die Hard                        | 8.2    |
| Gladiator                       | 8.5    |
Prompt Engineering con l'API di OpenAI

Elenchi

  • Utile per elenchi puntati
prompt = "Generate a list containing the names of the top 5 cities to visit."
print(get_response(prompt))
1. Tokyo, Japan
2. Paris, France
3. Rome, Italy
4. New York City, United States
5. Sydney, Australia
Prompt Engineering con l'API di OpenAI

Elenchi

  • Specifica nel prompt i requisiti di numerazione
prompt = "Generate an unordered list containing the names of the top 5 cities to visit."
print(get_response(prompt))
- Paris, France
- Tokyo, Japan
- Rome, Italy
- New York City, United States
- Sydney, Australia
Prompt Engineering con l'API di OpenAI

Paragrafi strutturati

  • Indica nel prompt i requisiti di struttura
prompt = "Provide a structured paragraph with clear headings and subheadings 
about the benefits of regular exercise on overall health and well-being."
print(get_response(prompt))
Prompt Engineering con l'API di OpenAI

Paragrafi strutturati

I. Introduzione
L'esercizio regolare è essenziale per mantenere una buona salute e benessere generale. [...]

II. Benefici fisici
a. Gestione del peso: L'esercizio regolare [...]
b. Riduzione del rischio di malattie croniche: L'attività fisica è stata collegata a [...]
c. Maggiore forza e flessibilità: L'esercizio regolare migliora la forza muscolare e [...]

III. Benefici mentali
a. Meno stress e ansia: L'esercizio ha un forte impatto su [...]
b. Sonno migliore: L'attività regolare è associata a pattern di sonno migliori [...]
c. Cervello più efficiente: Gli studi mostrano che l'esercizio migliora le funzioni cognitive [...]
Prompt Engineering con l'API di OpenAI

Formato di output personalizzato

text = "Once upon a time in a quaint little village, there lived a curious young boy named 
David. David was [...]"

instructions = "You will be provided with a text delimited by triple backticks. Generate a suitable title for it. "
output_format = """Use the following format for the output: - Text: <text we want to title> - Title: <the generated title>"""
prompt = instructions + output_format + f"```{text}```" print(get_response(prompt))
- Text: Once upon a time in a quaint little village, there lived a curious young boy [...]
- Title: The Extraordinary Adventure of David and the Mysterious Book
Prompt Engineering con l'API di OpenAI

Prompt condizionali

  • Integra logica o condizioni
  • I prompt condizionali seguono uno stile if-else

Diagramma che mostra una condizione di test. Se la condizione è vera si esegue X, se è falsa si esegue Y.

Prompt Engineering con l'API di OpenAI

Prompt condizionali

text = "Le printemps est ma saison préférée. Quand les premières fleurs commencent 
à éclore, et que les arbres se parent de feuilles vertes et tendres, je me sens 
revivre [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, suggest a suitable title for it.
         Otherwise, write 'I only understand English'.
         ```{text}```"""

print(get_response(prompt))
I only understand English
Prompt Engineering con l'API di OpenAI

Prompt condizionali

  • Puoi includere più condizioni
text = "In the heart of the forest, sunlight filters through the lush green canopy, 
creating a tranquil atmosphere [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, check if it contains the keyword 'technology'. 


         ```{text}```"""
print(get_response(prompt))
Prompt Engineering con l'API di OpenAI

Prompt condizionali

  • Puoi includere più condizioni
text = "In the heart of the forest, sunlight filters through the lush green canopy, 
creating a tranquil atmosphere [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, check if it contains the keyword 'technology'. 
         If it does, suggest a suitable title for it, otherwise, write 'Keyword not found'.

         ```{text}```"""
print(get_response(prompt))
Prompt Engineering con l'API di OpenAI

Prompt condizionali

  • Puoi includere più condizioni
text = "In the heart of the forest, sunlight filters through the lush green canopy, 
creating a tranquil atmosphere [...] "
prompt = f"""You will be provided with a text delimited by triple backticks. 
         If the text is written in English, check if it contains the keyword 'technology'. 
         If it does, suggest a suitable title for it, otherwise, write 'Keyword not found'.
         If the text is not written in English, reply with 'I only understand English'.
         ```{text}```"""
print(get_response(prompt))
Keyword not found
Prompt Engineering con l'API di OpenAI

Ayo berlatih!

Prompt Engineering con l'API di OpenAI

Preparing Video For Download...