使用 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

建立對話

在 messages 的字典清單末端加入一則使用者訊息,詢問「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...