Peran chat dan pesan sistem

Bekerja dengan DeepSeek di Python

James Chapman

Curriculum Manager, DataCamp

Model chat

 

Tugas satu-giliran

  • Generasi teks
  • Transformasi teks
  • Klasifikasi

Tugas satu-giliran dengan satu prompt dan satu respons.

Bekerja dengan DeepSeek di Python

Model chat

 

Tugas satu-giliran

  • Generasi teks
  • Transformasi teks
  • Klasifikasi

 

Percakapan multi-giliran

→ Membangun dari prompt dan respons sebelumnya

Tugas multi-giliran dengan banyak prompt dan respons.

Bekerja dengan DeepSeek di Python

Peran

 

  • System: mengendalikan perilaku asisten
  • User: memberi instruksi ke asisten
  • Assistant: respons atas instruksi pengguna
    • Dapat juga ditulis oleh pengembang untuk memberi contoh

Tiga peran dalam interaksi chat: system, user, dan assistant.

Bekerja dengan DeepSeek di Python

Menyiapkan request

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

Menyiapkan 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?"}]
Bekerja dengan DeepSeek di Python

Membuat 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).
Bekerja dengan DeepSeek di Python

Mengurangi penyalahgunaan

 

  • Pesan sistem: Dapat mencakup guardrails
    • Pembatasan pada keluaran model

Pembatas di sisi jalan.

Chatbot tutor keuangan dan chatbot penasihat keuangan.

Bekerja dengan DeepSeek di Python

Mengurangi penyalahgunaan dengan pesan sistem

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.
"""
Bekerja dengan DeepSeek di Python

Mengurangi penyalahgunaan dengan pesan sistem

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.
Bekerja dengan DeepSeek di Python

Ayo berlatih!

Bekerja dengan DeepSeek di Python

Preparing Video For Download...