Vai trò chat và system message

Làm việc với DeepSeek bằng Python

James Chapman

Curriculum Manager, DataCamp

Mô hình chat

 

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

  • Sinh văn bản
  • Biến đổi văn bản
  • Phân loại

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 bằng Python

Mô hình chat

 

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

  • Sinh văn bản
  • Biế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 đó

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

Làm việc với DeepSeek bằng Python

Vai trò

 

  • System: kiểm soát hành vi của trợ lý
  • User: chỉ dẫn trợ lý
  • Assistant: phản hồi yêu cầu của người dùng
    • Cũng có thể do nhà phát triển viết để làm ví dụ

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

Làm việc với DeepSeek bằng Python

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

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3",
    messages=[{"role": "user", "content": prompt}]
)
Làm việc với DeepSeek bằng 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 bằng Python

Gửi yêu cầu

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

Giảm lạm dụng

 

  • System message: Có thể đặt guardrail
    • 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 bằng Python

Giảm lạm dụng với 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 bằng Python

Giảm lạm dụng với system message

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

Ayo berlatih!

Làm việc với DeepSeek bằng Python

Preparing Video For Download...