構造化出力と条件付きプロンプト

OpenAI API で学ぶプロンプトエンジニアリング

Fouad Trad

Machine Learning Engineer

構造化出力

  • LLM は明確な指示があるときのみ構造化出力を生成
  • 出力の構造:
    • リスト
    • 構造化段落
    • カスタム形式

ChatGPTに構造を指定すると所望の構造で出力されるが、指定しないとランダムまたは無構造になることを示す図。

OpenAI API で学ぶプロンプトエンジニアリング

  • 期待する列を明示する
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    |
OpenAI API で学ぶプロンプトエンジニアリング

リスト

  • 列挙に便利
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 で学ぶプロンプトエンジニアリング

リスト

  • 番号付けの要件はプロンプトで指定
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 で学ぶプロンプトエンジニアリング

構造化された段落

  • 構造要件をプロンプトで明示
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 で学ぶプロンプトエンジニアリング

構造化された段落

I. はじめに
規則的な運動は健康と幸福の維持に不可欠です。 [...]

II. 身体的メリット
a. 体重管理:定期的な運動は [...]
b. 慢性疾患リスクの低下:運動は [...]
c. 筋力・柔軟性の向上:継続的な運動は筋力を高め [...]

III. メンタルヘルスのメリット
a. ストレス・不安の軽減:運動は [...]
b. 睡眠の質向上:定期的な運動は睡眠パターンの改善と関連し [...]
c. 脳機能の向上:研究では運動が認知機能を高めることが示され [...]
OpenAI API で学ぶプロンプトエンジニアリング

カスタム出力形式

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
OpenAI API で学ぶプロンプトエンジニアリング

条件付きプロンプト

  • ロジックや条件を組み込む
  • 条件付きプロンプトは if-else 形式

テスト条件を示す図。条件が真ならXを実行、偽ならYを実行。

OpenAI API で学ぶプロンプトエンジニアリング

条件付きプロンプト

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 で学ぶプロンプトエンジニアリング

条件付きプロンプト

  • 複数条件を組み込める
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 で学ぶプロンプトエンジニアリング

条件付きプロンプト

  • 複数条件を組み込める
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 で学ぶプロンプトエンジニアリング

条件付きプロンプト

  • 複数条件を組み込める
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 で学ぶプロンプトエンジニアリング

Passons à la pratique !

OpenAI API で学ぶプロンプトエンジニアリング

Preparing Video For Download...