始める無料で始める

2つのプロンプトを受け取る get_response() 関数の作成

以下の演習では、システムプロンプトとユーザープロンプトという2つのプロンプトを使って、OpenAI API の chat.completions エンドポイントを呼び出します。その準備として、この演習では、2つのプロンプト (system_promptuser_prompt )を入力として受け取り、回答を出力として返す get_response() 関数を作成します。その後、任意の例にこの関数を適用します。

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

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

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

コースを見る

演習の手順

  • messages リスト内の各メッセージに、ロールと内容を設定してください。
  • 任意の 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)
コードを編集して実行