プロンプトエンジニアリングの基本原則

OpenAI API によるプロンプトエンジニアリング

Fouad Trad

Machine Learning Engineer

明確で的確なプロンプト

A visual diagram showing a bakery and a house with two possible routes between them. One if simple and short (effective), and the other is complicated and long (ineffective).

OpenAI API によるプロンプトエンジニアリング

基本原則

  • 🎬 適切な動作動詞
  • 💬 詳細で正確な指示
  • ❗ 区切り記号を使って適切に構造化されたプロンプト
OpenAI API によるプロンプトエンジニアリング

動作動詞を使う

  • モデルに何をすべきかを指示する

Table for the verbs to use including write, complete, explain, describe, or evaluate.

OpenAI API によるプロンプトエンジニアリング

あいまいな動詞を避ける

  • モデルを混乱させる

Table for the verbs to use including write, complete, explain, describe, or evaluate, and the verbs to avoid using such as understand, think, feel, try, and 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 によるプロンプトエンジニアリング

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

効果のないプロンプト: 「犬について教えてください。」

効果的なプロンプト

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:

  • トークン数の上限
  • 出力はこの上限を超えられない
  • 回答が不完全になったり、途中で切れたりする可能性があります

プロンプト:

  • 単語数、文数、段落数の制限
  • 出力がこの制限を超える可能性があります
  • 最後まで生成された回答
OpenAI API によるプロンプトエンジニアリング

プロンプトの構成要素

  • 指示と処理対象となる入力データ
  • 例:テキスト要約
    • 指示: 与えられたテキストを要約する
    • 入力データ: 要約するテキスト

An icon representing the text summarization task

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 によるプロンプトエンジニアリング

練習しましょう!

OpenAI API によるプロンプトエンジニアリング

Preparing Video For Download...