2 回复
DeepSeek /chat/completions API 是一个“无状态” API,即服务端不记录用户请求的上下文,用户在每次请求时,需将之前所有对话历史拼接好后,传递给对话 API。
下面的代码以 Python 语言,展示了如何进行上下文拼接,以实现多轮对话。
from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
# Round 1
messages = [{"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 2
messages.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?"}
]
DeepDeepSeek的多轮对话功能主要通过上下文管理和对话状态追踪实现,核心是确保每次对话都能准确理解并处理上下文信息。以下是关键实现步骤:
在每轮对话中,系统需要记录和更新上下文信息,通常通过对话历史列表来实现。
# 初始化对话历史
conversation_history = []
# 添加用户输入和系统响应到对话历史
def update_conversation_history(user_input, system_response):
conversation_history.append({"user": user_input, "system": system_response})
# 获取当前上下文
def get_context():
return conversation_history
通过状态追踪,系统能理解用户当前的意图和已提供的信息,通常使用状态机或变量来管理对话。```python
conversation_state = { “intent”: None, “slots”: {} }
def update_conversation_state(intent=None, slots=None): if intent: conversation_state[“intent”] = intent if slots: conversation_state[“slots”].update(slots)
def get_conversation_state(): return conversation_state
### 3. 上下文感知响应生成
根据上下文和对话状态,系统生成响应时可以基于之前的对话内容。
```python
def generate_response(user_input):
context = get_context()
state = get_conversation_state()
# 基于上下文和状态生成响应
if state["intent"] == "book_flight":
if "destination" not in state["slots"]:
response = "Where would you like to fly to?"
elif "date" not in state["slots"]:
response = "When would you like to fly?"
else:
response = "Your flight has been booked!"
else:
response = "How can I assist you today?"
return response
以下是一个简单的多轮对话流程:
# 用户首次输入
user_input = "I want to book a flight."
system_response = generate_response(user_input)
update_conversation_history(user_input, system_response)
print(f"System: {system_response}") # 输出: "Where would you like to fly to?"
# 用户提供目的地
user_input = "To New York."
update_conversation_state(intent="book_flight", slots={"destination": "New York"})
system_response = generate_response(user_input)
update_conversation_history(user_input, system_response)
print(f"System: {system_response}") # 输出: "When would you like to fly?"
# 用户提供日期
user_input = "Next Monday."
update_conversation_state(slots={"date": "Next Monday"})
system_response = generate_response(user_input)
update_conversation_history(user_input, system_response)
print(f"System: {system_response}") # 输出: "Your flight has been booked!"
DeepSeek的多轮对话功能通过上下文管理、状态追踪和上下文感知响应生成实现,结合对话历史和状态,确保系统能理解和响应复杂对话。