プロンプトエンジニアリング入門

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

Fouad Trad

Machine Learning Engineer

プロンプトエンジニアリングとは?

望ましい回答を得るために、LLMに与えるプロンプトや指示を作成すること

A picture showing an engineer introducing the terminology of prompt engineering.

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

プロンプトエンジニアリングはレシピを作るようなものです

Image showing a chef crafting a meal.

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

なぜプロンプトエンジニアリングが必要なのか?

A visual diagram showing that high quality prompts lead to high quality answers

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

なぜプロンプトエンジニアリングが必要なのか?

A visual diagram showing how the quality of the answers is affected by the quality of the input prompts. High quality prompts lead to high quality answers, and low quality prompts lead to low quality answers.

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

まとめ: OpenAI API

  • OpenAIモデルとのやり取りを可能にします
  • このコースではすでに設定済み
  • Chat Completionsエンドポイントへのアクセス

chatgpt_logo_white.png

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

おさらい:メッセージのロール

各メッセージには、3つのロールのいずれかが設定されています

Image showing an icon for the user role.

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

おさらい:メッセージのロール

各メッセージには、3つのロールのいずれかが設定されています

Image showing an icon for the two roles: user and assistant.

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

おさらい:メッセージのロール

各メッセージには、3つのロールのいずれかが設定されています

Image showing an icon for each of the three roles: user, system, and assistant

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

おさらい:メッセージのロール

各メッセージには、3つのロールのいずれかが設定されています

Image showing an icon for each of the three roles, with a communication arrow between the system and the assistant to send system's messages.

  • システムメッセージ: モデルの振る舞いをガイドします
OpenAI API によるプロンプトエンジニアリング

おさらい:メッセージのロール

各メッセージには、3つのロールのいずれかが設定されています

Image showing an icon for each of the three roles, with a communication arrow between the system and the assistant to send system's messages, and a communication arrow between the user and the assistant to send a prompt.

  • システムメッセージ: モデルの振る舞いをガイドします
  • ユーザーメッセージ: ユーザーからのプロンプト
OpenAI API によるプロンプトエンジニアリング

おさらい:メッセージのロール

各メッセージには、3つのロールのいずれかが設定されています

Image showing an icon for each of the three roles, with a communication arrow between the system and the assistant to send system's messages, a communication arrow between the user and the assistant to send a prompt, and a communication arrow between the assistant and the user to send the response.

  • システムメッセージ: モデルの振る舞いをガイドします
  • ユーザーメッセージ: ユーザーからのプロンプト
  • アシスタントメッセージ: ユーザーのプロンプトへの回答
OpenAI API によるプロンプトエンジニアリング

おさらい:制御パラメータ

Image showing a thermometer's icon with the possible values between 0 and 2, with 0 showing no randomness, and 2 showing highest randomness.

  • temperature: 回答のランダム性を制御します
OpenAI API によるプロンプトエンジニアリング

おさらい:制御パラメータ

Image showing a thermometer's icon with the possible values between 0 and 2, with 0 showing no randomness, and 2 showing highest randomness, along with a slider's icon that represents the max_tokens parameter where lower values on the slider would lead to shorter responses.

  • temperature: 回答のランダム性を制御します
  • max_tokens 回答の長さを制御します
OpenAI API によるプロンプトエンジニアリング

おさらい:OpenAI APIとのやり取り

prompt = "What is prompt engineering?"

client = OpenAI(api_key="api_key")
response = client.chat.completions.create(
model = "gpt-4o-mini",
messages = [{"role": "user", "content": prompt}],
temperature = 0 )
print(response.choices[0].message.content)
Prompt engineering refers to the process of designing and refining prompts or 
instructions given to a language model like ChatGPT in order 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 によるプロンプトエンジニアリング

get_response() 関数の作成

def get_response(prompt):

response = client.chat.completions.create( model = "gpt-4o-mini", messages = [{"role": "user", "content": prompt}], temperature = 0 )
return response.choices[0].message.content

使用例

response = get_response("What is prompt engineering?")
print(response)
Prompt engineering refers to the process of designing and refining prompts or instructions given to a language model 
like ChatGPT in order 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 によるプロンプトエンジニアリング

プロンプトの改善

prompt = "What is prompt engineering? Explain it in terms that can be understood 
by a 5-year-old"
response = get_response(prompt)
print(response)
Imagine you have a very smart friend who can understand and answer lots of 
questions. But sometimes, they might not understand exactly what you want or give 
the wrong answer. So, prompt engineering is like giving your friend really clear 
instructions or hints to help them give you the best answer possible.
OpenAI API によるプロンプトエンジニアリング

練習しましょう!

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

Preparing Video For Download...