ロールとシステムメッセージ

OpenAI APIを使いこなす

James Chapman

Curriculum Manager, DataCamp

Chat Completions

シングルターンタスク

  • テキスト生成
  • テキスト変換
  • 分類

1つのプロンプトと1つの応答からなるシングルターンのタスク。

OpenAI APIを使いこなす

Chat Completions

シングルターンタスク

  • テキスト生成
  • テキスト変換
  • 分類

マルチターン会話

→ 前のプロンプトと応答を土台に積み重ねていく

複数のプロンプトと応答を含むマルチターンタスク

OpenAI APIを使いこなす

ロール(役割)

  • システム:アシスタントの振る舞い制御
  • ユーザー:アシスタントへの_指示_に使用
  • アシスタント:ユーザーの指示への_応答_に使用
    • 例示のため開発者が記述することも可能

チャットのやり取りにおける3つのロール:システム、ユーザー、アシスタント。

OpenAI APIを使いこなす

リクエストの設定

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)
OpenAI APIを使いこなす

プロンプトの設定

messages=[{"role": "system",
           "content": "You are a Python programming tutor who speaks concisely."},
          {"role": "user",
           "content": "What is the difference between mutable and immutable objects?"}]
OpenAI APIを使いこなす

リクエストの作成

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are a Python programming tutor who speaks concisely."},
            {"role": "user",
             "content": "What is the difference between mutable and immutable objects?"}]
)

print(response.choices[0].message.content)
OpenAI APIを使いこなす

レスポンス

Mutable objects can be changed after creation, while immutable objects cannot be
modified once they are created.
OpenAI APIを使いこなす

不適切な使用の制限

  • システムメッセージガードレールを設けられる
    • モデル出力に対する制限

ガードレール.jpg

金融チューターのチャットボットと金融アドバイザーのチャットボット。

OpenAI APIを使いこなす

システムメッセージを使った誤用の抑制

sys_msg = """
You are finance education assistant that helps students study for exams.

If you are asked for specific, real-world financial advice with risk to their
finances, respond with:

I'm sorry, I am not allowed to provide financial advice.
"""
OpenAI APIを使いこなす

システムメッセージを使った誤用の抑制

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": sys_msg},
            {"role": "user",
             "content": "Which stocks should I invest in?"}]
)

print(response.choices[0].message.content)
I'm sorry, I am not allowed to provide financial advice.
OpenAI APIを使いこなす

練習しましょう!

OpenAI APIを使いこなす

Preparing Video For Download...