プロンプトエンジニアリングの要点

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

Fouad Trad

Machine Learning Engineer

明確で正確なプロンプト

ベーカリーと家の間の2つの経路を示す図。1つはシンプルで短い(効果的)、もう1つは複雑で長い(非効果的)。

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

主要原則

 

  • 🎬 適切な行動動詞
  • 💬 詳細で正確な指示
  • ❗ 区切りで構造化したプロンプト
OpenAI API で学ぶプロンプトエンジニアリング

行動動詞を使う

  • 何をすべきかを指示

使用すべき動詞(write, complete, explain, describe, evaluate)を示す表。

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

あいまいな動詞は避ける

  • モデルの解釈をあいまいにする

使用すべき動詞(write, complete, explain, describe, evaluate)と、避けるべき動詞(understand, think, feel, try, know)を示す表。

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

動詞を用いた効果的なプロンプト

prompt = "Think about the issue of 
deforestation."
response = get_response(prompt)
print(response)
Deforestation is a significant 
environmental issue involving 
permanently removing
or destroying forests and woodlands. 
It has far-reaching impacts on the 
environment, ecosystems, wildlife, 
and human communities. [...]
prompt = "Propose strategies to 
reduce deforestation."
response = get_response(prompt)
print(response)
Reducing deforestation requires 
a comprehensive and multi-dimensional 
approach involving various stakeholders. 
Here are several strategies 
that can help address the issue:
Strengthen Forest Governance
Promote Sustainable Land Use [...]
OpenAI API で学ぶプロンプトエンジニアリング

詳細な指示の作成

 

以下について、具体的・明確・詳細な指示を与える:

  • 📖 コンテキスト
  • 📏 出力の長さ
  • 🖌 形式とスタイル
  • 👥👥 想定読者
OpenAI API で学ぶプロンプトエンジニアリング

指示を含む効果的なプロンプト

悪いプロンプト: "Tell me about dogs."

良いプロンプト

prompt = "Write a descriptive paragraph about the behavior and characteristics of 
Golden Retrievers, highlighting their friendly nature, intelligence, 
and suitability as family pets."
print(get_response(prompt))
Golden Retrievers are beloved worldwide for their exceptional behavior, remarkable 
characteristics, and friendly nature. They are highly intelligent, trainable, and 
adaptable, making them great companions for families, including those with children.
OpenAI API で学ぶプロンプトエンジニアリング

出力長の制限

max_tokens:

  • トークン数の上限
  • 出力は超過不可
  • 途中で切れる可能性あり

Prompt:

  • 語・文・段落の上限指定
  • 出力が無視する場合あり
  • 完結した回答になりやすい
OpenAI API で学ぶプロンプトエンジニアリング

プロンプトの構成要素

  • 指示 と処理する 入力データ
  • 例: 要約
    • 指示: 与えた本文を要約
    • 入力データ: 要約する本文

テキスト要約タスクを表すアイコン

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

区切り記号で構造化したプロンプト

  • まず指示から始める
  • 入力部分を示すために区切り記号(丸括弧、角括弧、バッククォート等)を使う
  • 使用する区切り記号を明記
prompt = """Summarize the text delimited by triple backticks into bullet points.
           ```TEXT GOES HERE```"""
response = get_response(prompt)
OpenAI API で学ぶプロンプトエンジニアリング

フォーマット済み文字列(f文字列)の使用

  • 定義済み文字列を別の文字列へ埋め込む
text = "This is a sample text to summarize"

prompt = f"""Summarize the text delimited by triple backticks into bullet points. ```{text}```"""
print(prompt)
Summarize the text delimited by triple backticks into bullet points.
```This is a sample text to summarize```
OpenAI API で学ぶプロンプトエンジニアリング

Passons à la pratique !

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

Preparing Video For Download...