Vai trò trò chuyện và system message

Làm việc với DeepSeek trong Python

James Chapman

AI Curriculum Lead, DataCamp

Từ một lượt đến nhiều lượt

 

Nhiệm vụ một lượt

  • Sinh văn bản
  • Chuyển đổi văn bản
  • Phân loại

Một nhiệm vụ một lượt với một prompt và một phản hồi.

Làm việc với DeepSeek trong Python

Từ một lượt đến nhiều lượt

 

Nhiệm vụ một lượt

  • Sinh văn bản
  • Chuyển đổi văn bản
  • Phân loại

 

Hội thoại nhiều lượt

→ Xây trên các prompt và phản hồi trước đó

Một nhiệm vụ nhiều lượt với nhiều prompt và phản hồi.

Làm việc với DeepSeek trong Python

Vai trò

 

  • System: kiểm soát hành vi của assistant
  • User: chỉ dẫn assistant
  • Assistant: phản hồi hướng dẫn của user
    • Cũng có thể do developer viết để cung cấp ví dụ

Ba vai trò trong một tương tác chat: system, user và assistant.

Làm việc với DeepSeek trong Python

Thiết lập yêu cầu

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4.1-Flash",
    messages=[{"role": "user", "content": prompt}]
)
Làm việc với DeepSeek trong Python

Thiết lập prompt

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?"}]
Làm việc với DeepSeek trong Python

Gửi yêu cầu

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).
Làm việc với DeepSeek trong Python

Giảm lạm dụng

 

  • System message: Có thể kèm guardrails
    • Hạn chế đầu ra của mô hình

Lan can an toàn dọc theo đường.

Chatbot gia sư tài chính và chatbot cố vấn tài chính.

Làm việc với DeepSeek trong Python

Giảm lạm dụng bằng system message

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.
"""
Làm việc với DeepSeek trong Python

Giảm lạm dụng bằng system message

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.
Làm việc với DeepSeek trong Python

Cùng luyện tập nào!

Làm việc với DeepSeek trong Python

Preparing Video For Download...