多轮对话
本指南将介绍如何使用 DeepSeek /chat/completions API 进行多轮对话。
DeepSeek /chat/completions API 是一个“无状态” API,即服务端不记录用户请求的上下文,用户在每次请求时, 需将之前所有对话历史拼接好后 ,传递给对话 API。
下面的代码以 Python 语言,展示了如何进行上下文拼接,以实现多轮对话。
from openai import OpenAIclient = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")# Round 1messages = [{"role": "user", "content": "What's the highest mountain in the world?"}]response = client.chat.completions.create( model="deepseek-chat", messages=messages)messages.append(response.choices[0].message)print(f"Messages Round 1: {messages}")# Round 2messages.append({"role": "user", "content": "What is the second?"})response = client.chat.completions.create( model="deepseek-chat", messages=messages)messages.append(response.choices[0].message)print(f"Messages Round 2: {messages}")
在 第一轮 请求时,传递给 API 的 messages 为:
[ {"role": "user", "content": "What's the highest mountain in the world?"}]
在 第二轮 请求时:
- 要将第一轮中模型的输出添加到
messages末尾 - 将新的提问添加到
messages末尾
最终传递给 API 的 messages 为:
[ {"role": "user", "content": "What's the highest mountain in the world?"}, {"role": "assistant", "content": "The highest mountain in the world is Mount Everest."}, {"role": "user", "content": "What is the second?"}]
随着不断填充数据,这个自动化基本上都是迟早的事情了。
新一代智能开发解决方案
技术架构
需要企业级独享 API 密钥?或者商务合作?或者本地部署 Deepseek Rag ?立即联系技术顾问
QQ: 386437237 &
当前共享池剩余Token:4500万+(不定时补充)
全平台兼容矩阵
黄金支持
基础支持
Token 的含义和作用
功能介绍














23] 作为一个 Cocos Creator 开发游戏的程序员,2025 年,你认为职业发展应该怎么规划比较好?如果要年入百万的话,认真且严谨地回复我的问题,带上你详细的数据比对

