始める無料で始める

二つのプロンプトを使う get_response() 関数の作成

このあとの演習では、OpenAI API の chat.completions エンドポイントを、system プロンプトと user プロンプトの2つのプロンプトで呼び出します。準備として、この演習では2つのプロンプト(system_promptuser_prompt)を入力に受け取り、応答を出力として返す二重プロンプト対応の get_response() 関数を作成します。作成後は、お好きな例にこの関数を適用してみましょう。

OpenAI パッケージはあらかじめ読み込まれています。

この演習はコースの一部です

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

コースを見る

演習の手順

  • messages リスト内の各メッセージに、rolecontent を設定します。
  • 任意の system_promptuser_prompt を渡して、関数を試してみましょう。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

client = OpenAI(api_key="")

def get_response(system_prompt, user_prompt):
  # Assign the role and content for each message
  messages = [{"role": ____, "content": ____},
      		  {"role": ____, "content": ____}]  
  response = client.chat.completions.create(
      model="gpt-4o-mini", messages= messages, temperature=0)
  
  return response.choices[0].message.content

# Try the function with a system and user prompts of your choice 
response = get_response("____", "____")
print(response)
コードを編集して実行