DeepSeek多轮对话

2025-02-12 111
DeepSeek

类型:人工智能

简介:一款基于深度学习和自然语言处理技术的产品,人气赶超ChatGPT。

DeepSeek的/chat/completions API采用 “无状态”设计理念。也就说明服务端不会主动记录用户请求的上下文信息。因此用户在每一次发起请求时,都需要把之前所有的对话历史精心拼接起来,再传递给对话 API,以此确保模型能够全面理解整个对话的来龙去脉,进而给出精准恰当的回复。

一、Python代码实现多轮对话

接下来将以Python语言为例,向展示如何进行上下文拼接,从而轻松实现多轮对话。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pythonfrom openai import OpenAI
# 初始化 OpenAI 客户端,填入 DeepSeek API Key 并指定 API 基础 URL
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
# 第一轮对话
# 构建初始消息列表,包含用户的第一个问题
messages = [{"role": "user", "content": "What's the highest mountain in the world?"}]
# 向 API 发送请求,获取模型的回复
response = client.chat.completions.create(
model="deepseek - chat",
messages=messages
)
# 将模型的回复添加到消息列表中
messages.append(response.choices[0].message)
print(f"Messages Round 1: {messages}")
# 第二轮对话
# 用户提出新的问题,将其添加到消息列表末尾
messages.append({"role": "user", "content": "What is the second?"})
# 再次向 API 发送请求,包含之前的对话历史和新问题
response = client.chat.completions.create(
model="deepseek - chat",
messages=messages
)
# 将本轮模型的回复添加到消息列表中
messages.append(response.choices[0].message)
print(f"Messages Round 2: {messages}")
pythonfrom openai import OpenAI # 初始化 OpenAI 客户端,填入 DeepSeek API Key 并指定 API 基础 URL client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com") # 第一轮对话 # 构建初始消息列表,包含用户的第一个问题 messages = [{"role": "user", "content": "What's the highest mountain in the world?"}] # 向 API 发送请求,获取模型的回复 response = client.chat.completions.create( model="deepseek - chat", messages=messages ) # 将模型的回复添加到消息列表中 messages.append(response.choices[0].message) print(f"Messages Round 1: {messages}") # 第二轮对话 # 用户提出新的问题,将其添加到消息列表末尾 messages.append({"role": "user", "content": "What is the second?"}) # 再次向 API 发送请求,包含之前的对话历史和新问题 response = client.chat.completions.create( model="deepseek - chat", messages=messages ) # 将本轮模型的回复添加到消息列表中 messages.append(response.choices[0].message) print(f"Messages Round 2: {messages}")
pythonfrom openai import OpenAI
# 初始化 OpenAI 客户端,填入 DeepSeek API Key 并指定 API 基础 URL
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
# 第一轮对话
# 构建初始消息列表,包含用户的第一个问题
messages = [{"role": "user", "content": "What's the highest mountain in the world?"}]
# 向 API 发送请求,获取模型的回复
response = client.chat.completions.create(
model="deepseek - chat",
messages=messages
)
# 将模型的回复添加到消息列表中
messages.append(response.choices[0].message)
print(f"Messages Round 1: {messages}")
# 第二轮对话
# 用户提出新的问题,将其添加到消息列表末尾
messages.append({"role": "user", "content": "What is the second?"})
# 再次向 API 发送请求,包含之前的对话历史和新问题
response = client.chat.completions.create(
model="deepseek - chat",
messages=messages
)
# 将本轮模型的回复添加到消息列表中
messages.append(response.choices[0].message)
print(f"Messages Round 2: {messages}")

二、各轮请求消息解析

下面我们详细解析每一轮请求中传递给 API 的 messages 内容:

1、第一轮请求

在第一轮请求时,传递给 API 的 messages 仅包含用户提出的第一个问题:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[
{"role": "user", "content": "What's the highest mountain in the world?"}
]
[ {"role": "user", "content": "What's the highest mountain in the world?"} ]
[
{"role": "user", "content": "What's the highest mountain in the world?"}
]

2、第二轮请求

在进行第二轮请求时,需要按照特定步骤更新 messages 列表:

  • 把第一轮中模型给出的输出添加到 messages 列表的末尾,这样模型就能知晓之前的回复内容;
  • 将用户新提出的问题添加到 messages 列表的末尾,让模型了解最新的提问;
  • 最终传递给 API 的 messages 列表如下:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[
{"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?"}
]
[ {"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?"} ]
[
{"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?"}
]
  • 广告合作

  • QQ群号:707632017

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。