Gestructureerde outputs en conditionele prompts

Prompt Engineering met de OpenAI API

Fouad Trad

Machine Learning Engineer

Gestructureerde outputs

  • LLM’s geven alleen gestructureerde outputs met expliciete instructies
  • Outputstructuren:
    • Tabel
    • Lijst
    • Gestructureerde alinea
    • Aangepast formaat

Visueel diagram: als we ChatGPT om een gestructureerde output vragen, krijgen we die structuur; zonder specificatie is de output willekeurig of ongestructureerd.

Prompt Engineering met de OpenAI API

Tabellen

  • Noem duidelijk de verwachte kolommen
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 met de OpenAI API

Lijsten

  • Handig voor opsommingen
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 met de OpenAI API

Lijsten

  • Vermeld eisen voor nummering in de prompt
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 met de OpenAI API

Gestructureerde alinea’s

  • Noem structuurvereisten in de prompt
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 met de OpenAI API

Gestructureerde alinea’s

I. Inleiding
Regelmatig bewegen is essentieel voor een goede gezondheid en welzijn. [...]

II. Fysieke gezondheidsvoordelen
a. Gewichtsbeheersing: Regelmatige beweging is [...]
b. Minder risico op chronische ziekten: Bewegen hangt samen met [...]
c. Meer kracht en flexibiliteit: Regelmatig bewegen verbetert spierkracht en [...]

III. Mentale gezondheidsvoordelen
a. Minder stress en angst: Beweging heeft een sterke impact op [...]
b. Betere slaapkwaliteit: Regelmatig bewegen gaat samen met betere slaap [...]
c. Verbeterde hersenfunctie: Studies tonen aan dat beweging cognitieve functies verbetert [...]
Prompt Engineering met de OpenAI API

Aangepast uitvoerformaat

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 met de OpenAI API

Conditionele prompts

  • Verwerk logica of voorwaarden
  • Conditionele prompts volgen een if-else-stijl

Visueel diagram met een testvoorwaarde. Als die waar is, voer X uit; als die onwaar is, voer Y uit.

Prompt Engineering met de OpenAI API

Conditionele prompts

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 met de OpenAI API

Conditionele prompts

  • Kan meerdere voorwaarden bevatten
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 met de OpenAI API

Conditionele prompts

  • Kan meerdere voorwaarden bevatten
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 met de OpenAI API

Conditionele prompts

  • Kan meerdere voorwaarden bevatten
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 met de OpenAI API

Laten we oefenen!

Prompt Engineering met de OpenAI API

Preparing Video For Download...