チャットの役割とシステムメッセージ

Python で DeepSeek を使いこなす

James Chapman

AI Curriculum Lead, DataCamp

単一ターンから複数ターンへ

 

単一ターンのタスク

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

1つのプロンプトと1つの応答からなる単一ターンのタスク。

Python で DeepSeek を使いこなす

単一ターンから複数ターンへ

 

単一ターンのタスク

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

 

複数ターンの会話

→ これまでのプロンプトと応答を踏まえて構築

複数のプロンプトと応答からなる複数ターンのタスク。

Python で DeepSeek を使いこなす

役割

 

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

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

Python で DeepSeek を使いこなす

リクエストの設定

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4.1-Flash",
    messages=[{"role": "user", "content": prompt}]
)
Python で DeepSeek を使いこなす

プロンプトの設定

messages=[{"role": "system",
           "content": "You are a Python programming tutor who responds using concise,
                         one-sentence explanations."},
          {"role": "user",
           "content": "What is the difference between mutable and immutable objects?"}]
Python で DeepSeek を使いこなす

リクエストの送信

response = client.chat.completions.create(
  model="deepseek-ai/DeepSeek-V4.1-Flash",
  messages=[{"role": "system",
             "content": "You are a Python programming tutor who responds using concise,
                         one-sentence explanations."},
            {"role": "user",
             "content": "What is the difference between mutable and immutable objects?"}]
)

print(response.choices[0].message.content)
Mutable objects can be changed after creation (like lists), while immutable objects
cannot be modified once created (like tuples or strings).
Python で DeepSeek を使いこなす

誤用の防止

 

  • システムメッセージ: ガードレール(制約)を含めることが可能
    • モデルの出力に対する制限

道路脇のガードレール。

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

Python で DeepSeek を使いこなす

システムメッセージによる誤用防止

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 only:

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

システムメッセージによる誤用防止

response = client.chat.completions.create(
  model="deepseek-ai/DeepSeek-V4.1-Flash",
  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.
Python で DeepSeek を使いこなす

練習しましょう!

Python で DeepSeek を使いこなす

Preparing Video For Download...