使用 DeepSeek 的多轮对话

在 Python 中使用 DeepSeek

James Chapman

AI Curriculum Lead, DataCamp

助手消息

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": "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?"}] )
在 Python 中使用 DeepSeek

构建对话

向助手发送一条包含我叫 James 的用户请求。助手回复 "Hi James!"。

在 Python 中使用 DeepSeek

构建对话

将助手的 "Hi James!" 回复加入到由字典组成的 messages 列表中。

在 Python 中使用 DeepSeek

构建对话

将助手的 "Hi James!" 回复加入到由字典组成的 messages 列表中。

在 Python 中使用 DeepSeek

构建对话

在消息列表末尾添加一条用户消息:"What is my name?"。

在 Python 中使用 DeepSeek

构建对话

助手回复 "Your name is James."。

在 Python 中使用 DeepSeek
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-V4.1-Flash", messages=messages )
assistant_dict = {"role": "assistant", "content": response.choices[0].message.content} messages.append(assistant_dict)
print("Assistant: ", response.choices[0].message.content, "\n")
在 Python 中使用 DeepSeek

与 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.
在 Python 中使用 DeepSeek

让我们一起练习吧!

在 Python 中使用 DeepSeek

Preparing Video For Download...