创建对话历史
一家名为 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-Pro",
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)