Few-shot プロンプティング

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

Fouad Trad

Machine Learning Engineer

Few-shot プロンプティング

  • モデルに例(質問と回答のペア)を与える

A visual representation of a few-shot prompt showing its structure with example questions and answers, and finally the question we want the model to answer.

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

Few-shot プロンプティング

  • モデルに例(質問と回答のペア)を与える

A visual diagram showing how we send a few-shot prompt to the LLM.

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

Few-shot プロンプティング

  • モデルに例(質問と回答のペア)を与える

A visual diagram showing how we send a few-shot prompt to the LLM and get an answer in return.

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

Few-shot プロンプティング

  • モデルに例(質問と回答のペア)を与える

A visual diagram showing how we send a few-shot prompt to the LLM and get an answer in return.

  • 例の数:
    • 0 → zero-shotプロンプティング
    • 1 → one-shotプロンプティング
    • 2つ以上 → few-shotプロンプティング
OpenAI API によるプロンプトエンジニアリング

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

留意点

  • タスクの複雑さに応じて例の数を選ぶ
    • 例を少なくする → 基本的なタスク
    • 多様な例 → 複雑なタスク

An image showing a person questioning themselves.

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

練習しましょう!

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

Preparing Video For Download...