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

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

James Chapman

Curriculum Manager, DataCamp

Thông điệp của trợ lý

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": "How do you define a Python list?"},
{"role": "assistant", "content": "Lists are defined by enclosing a comma-separated sequence of objects inside square brackets [ ]."},
{"role": "user", "content": "What is the difference between mutable and immutable objects?"}] )
Làm việc với DeepSeek bằng Python

Xây dựng hội thoại

Gửi yêu cầu người dùng nói với trợ lý rằng tên tôi là James. Trợ lý trả lời: "Hi James!"

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

Xây dựng hội thoại

Phản hồi "Hi James!" của trợ lý được chuyển vào danh sách messages (các dict).

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

Xây dựng hội thoại

Phản hồi "Hi James!" của trợ lý được chuyển vào danh sách messages (các dict).

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

Xây dựng hội thoại

Thêm câu trả lời của người dùng vào cuối danh sách messages: "What is my name?"

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

Xây dựng hội thoại

Trợ lý trả lời: "Tên bạn là James."

Làm việc với DeepSeek bằng Python
messages = [{"role": "system",
             "content": "You are a Python programming tutor who responds using concise,
                         one-sentence explanations."}]


user_qs = ["Why is Python so popular?", "Summarize this in one sentence."]
for q in user_qs:
print("User: ", q)
user_dict = {"role": "user", "content": q}
messages.append(user_dict)
response = client.chat.completions.create( model="deepseek-ai/DeepSeek-V3", messages=messages )
assistant_dict = {"role": "assistant", "content": response.choices[0].message.content} messages.append(assistant_dict)
print("Assistant: ", response.choices[0].message.content, "\n")
Làm việc với DeepSeek bằng Python

Hội thoại với AI

User:  Why is Python so popular?
Assistant:  Python is popular because it's easy to learn, has a simple syntax, and
offers powerful libraries for various applications like web development, data
science, and automation. 

User:  Summarize this in one sentence.
Assistant:  Python's simplicity, versatility, and extensive libraries make it
widely popular.
Làm việc với DeepSeek bằng Python

Let's practice!

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

Preparing Video For Download...