Few-shotプロンプティング

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

Fouad Trad

Machine Learning Engineer

Few-shotプロンプティング

  • モデルに例(質問と回答のペア)を提供する

Few-shotプロンプトの構造を示す図。例となる質問と回答、そしてモデルに回答させたい質問が含まれている。

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

Few-shotプロンプティング

  • モデルに例(質問と回答のペア)を提供する

Few-shotプロンプトをLLMに送信する様子を示す図。

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

Few-shotプロンプティング

  • モデルに例(質問と回答のペア)を提供する

Few-shotプロンプトをLLMに送信し、回答を受け取る様子を示す図。

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

Few-shotプロンプティング

  • モデルに例(質問と回答のペア)を提供する

Few-shotプロンプトをLLMに送信し、回答を受け取る様子を示す図。

  • 例の数:
    • ゼロ → zero-shotプロンプティング
    • 1つ → one-shotプロンプティング
    • 複数 → few-shotプロンプティング
OpenAI API で学ぶプロンプトエンジニアリング

Zero-shotプロンプティング

  • 例なしでプロンプトを提供する
  • モデルは自身の知識に基づいて回答を生成
  • 簡単なタスクに最適
prompt = "What is prompt engineering?"
print(get_response(prompt))
Prompt engineering refers to designing and refining prompts or instructions given 
to a language model like ChatGPT to elicit desired responses or behaviors. It 
involves formulating specific guidelines or hints to guide the model's output 
towards a desired outcome.
OpenAI API で学ぶプロンプトエンジニアリング

One-shotプロンプティング

  • モデルに1つの例を提供する
  • 一貫したフォーマットやスタイルに有効
prompt =  """ 
Q: Sum the numbers 3, 5, and 6. A: 3+5+6=14
Q: Sum the numbers 2, 4, and 7. A: 
"""
print(get_response(prompt))
2+4+7=13
OpenAI API で学ぶプロンプトエンジニアリング

One-shotプロンプティング

prompt = """
Q: Sum the numbers 3, 5, and 6. A: The sum of 3, 5, and 6 is 14
Q: Sum the numbers 2, 4, and 7. A: 
"""
print(get_response(prompt))
The sum of 2, 4, and 7 is 13
OpenAI API で学ぶプロンプトエンジニアリング

Few-shotプロンプティング

  • 複数の例を提供する
  • 文脈を要するタスクに有効
prompt = """
Text: Today the weather is fantastic -> Classification: positive
Text: The furniture is small -> Classification: neutral
Text: I don't like your attitude -> Classification: negative

"""

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

Few-shotプロンプティング

  • 複数の例を提供する
  • 文脈を要するタスクに有効
prompt = """
Text: Today the weather is fantastic -> Classification: positive
Text: The furniture is small -> Classification: neutral
Text: I don't like your attitude -> Classification: negative
Text: That shot selection was awful -> Classification: 
"""
print(get_response(prompt))
negative
OpenAI API で学ぶプロンプトエンジニアリング

チャットモデルでのFew-shotプロンプティング

response = client.chat.completions.create(
  model = "gpt-4o-mini",

messages = [{"role": "user", "content": "Today the weather is fantastic"},
{"role": "assistant", "content": "positive"},
{"role": "user", "content": "I don't like your attitude"}, {"role": "assistant", "content": "negative"},
{"role": "user", "content": "That shot selection was awful"} ], temperature = 0 )
print(response.choices[0].message.content)
negative
OpenAI API で学ぶプロンプトエンジニアリング

考慮事項

  • タスクの複雑さに応じてショット数を選択
    • ショット数が少ない → 基本的なタスク
    • 多様なショット → 複雑なタスク

考え込む人物のイメージ。

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

さあ、練習しましょう!

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

Preparing Video For Download...