Yapılandırılmış çıktılar ve koşullu istemler

OpenAI API ile Prompt Engineering

Fouad Trad

Machine Learning Engineer

Yapılandırılmış çıktılar

  • LLM'ler yalnızca açık talimat verildiğinde yapılandırılmış çıktılar üretir
  • Çıktı yapıları:
    • Tablo
    • Liste
    • Yapılandırılmış paragraf
    • Özel format

Yapılandırılmış çıktı istediğimizde talep edilen yapıda; belirtmediğimizde rastgele ya da yapısız çıktı alındığını gösteren diyagram.

OpenAI API ile Prompt Engineering

Tablolar

  • Beklenen sütunları açıkça belirtin
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))
| Başlık                         | Puan |
|--------------------------------|------|
| Mad Max: Fury Road             | 8.1  |
| John Wick                      | 7.4  |
| The Dark Knight                | 9.0  |
| Die Hard                       | 8.2  |
| Gladiator                      | 8.5  |
OpenAI API ile Prompt Engineering

Listeler

  • Sıralamalar için gereksinimleri istemde belirtin
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
OpenAI API ile Prompt Engineering

Listeler

  • Numara verme gereksinimlerini istemde belirtmelisiniz
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
OpenAI API ile Prompt Engineering

Yapılandırılmış paragraflar

  • Yapı gereksinimlerini istemde belirtin
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))
OpenAI API ile Prompt Engineering

Yapılandırılmış paragraflar

I. Giriş
Düzenli egzersiz, iyi sağlık ve genel esenliği korumak için gereklidir. [...]

II. Fiziksel Sağlık Faydaları
a. Kilo Yönetimi: Düzenli egzersiz [...]
b. Kronik Hastalık Riskinin Azalması: Egzersiz ilişkilendirilmiştir [...]
c. Artan Güç ve Esneklik: Düzenli egzersiz kas gücünü [...]

III. Ruh Sağlığı Faydaları
a. Stres ve Anksiyetenin Azalması: Egzersiz derin bir etkiye sahiptir [...]
b. Daha İyi Uyku Kalitesi: Düzenli egzersiz, gelişmiş uyku düzenleriyle ilişkilidir [...]
c. Artan Beyin Fonksiyonu: Çalışmalar, egzersizin bilişsel işlevi artırdığını gösterir [...]
OpenAI API ile Prompt Engineering

Özel çıktı formatı

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))
- Metin: Once upon a time in a quaint little village, there lived a curious young boy [...]
- Başlık: The Extraordinary Adventure of David and the Mysterious Book
OpenAI API ile Prompt Engineering

Koşullu istemler

  • Mantık veya koşullar ekleyin
  • Koşullu istemler if-else tarzındadır

Bir test koşulunu gösteren görsel diyagram. Bu koşul doğruysa X yapılmalı, yanlışsa Y yapılmalı.

OpenAI API ile Prompt Engineering

Koşullu istemler

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
OpenAI API ile Prompt Engineering

Koşullu istemler

  • Birden çok koşul içerebilir
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))
OpenAI API ile Prompt Engineering

Koşullu istemler

  • Birden çok koşul içerebilir
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))
OpenAI API ile Prompt Engineering

Koşullu istemler

  • Birden çok koşul içerebilir
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
OpenAI API ile Prompt Engineering

Hadi pratik yapalım!

OpenAI API ile Prompt Engineering

Preparing Video For Download...