建立對話歷程
一家名為 Easy as Pi 的線上數學學習平台委託你協助開發 AI 家教。你立刻看出可以用 DeepSeek 的聊天模式來打造這個應用,並開始設計一個簡單的概念驗證(POC),提供公司主要利害關係人審閱。
首先,你將示範如何把對學生訊息的回應儲存到訊息歷程,讓系統能夠進行完整的多輪對話。
本練習屬於課程
使用 Python 操作 DeepSeek
練習說明
- 在聊天請求中把
messages傳給模型。 - 從
response擷取助理訊息,轉成訊息字典,並附加到messages。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")
messages = [
{"role": "system", "content": "You are a helpful math tutor that generates concise, one-sentence responses."},
{"role": "user", "content": "Explain what pi is."}
]
# Send the chat messages to the model
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4.1-Flash",
messages=____,
max_tokens=500
)
# Extract the assistant message from the response
assistant_dict = {"role": "____", "content": ____}
# Add assistant_dict to the messages dictionary
messages.____(assistant_dict)
print(messages)