Chat roles and system messages

Working with DeepSeek in Python

James Chapman

Curriculum Manager, DataCamp

Chat models

 

Single-turn tasks

  • Text generation
  • Text transformation
  • Classification

A single-turn task with one prompt and one response.

Working with DeepSeek in Python

Chat models

 

Single-turn tasks

  • Text generation
  • Text transformation
  • Classification

 

Multi-turn conversations

→ Build on previous prompts and responses

A multi-turn task with multiple prompts and responses.

Working with DeepSeek in Python

Roles

 

  • System: controls assistant's behavior
  • User: instruct the assistant
  • Assistant: response to user instruction
    • Can also be written by the developer to provide examples

The three roles in a chat interaction: the system, the user, and the assistant.

Working with DeepSeek in Python

Request setup

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3",
    messages=[{"role": "user", "content": prompt}]
)
Working with DeepSeek in Python

Prompt setup

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?"}]
Working with DeepSeek in Python

Making a request

response = client.chat.completions.create(
  model="deepseek-ai/DeepSeek-V3",
  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).
Working with DeepSeek in Python

Mitigating misuse

 

  • System message: Can include guardrails
    • Restrictions on model outputs

Guardrails along the side of a road.

A financial tutor chatbot and a financial advisor chatbot.

Working with DeepSeek in Python

Mitigating misuse with system messages

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.
"""
Working with DeepSeek in Python

Mitigating misuse with system messages

response = client.chat.completions.create(
  model="deepseek-ai/DeepSeek-V3",
  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.
Working with DeepSeek in Python

Let's practice!

Working with DeepSeek in Python

Preparing Video For Download...