调整历史内容格式

This commit is contained in:
liuwei
2025-03-31 14:44:12 +08:00
parent 319112275e
commit c0520910c1

View File

@@ -192,14 +192,24 @@ class DifyPlugin(MessagePluginInterface):
"Accept": "text/event-stream" # 指定接受事件流
}
# 准备历史记录
history_text = ""
if self.conversations[session_id]:
# 将历史记录转换为字符串格式
for msg in self.conversations[session_id]:
role = "用户" if msg["role"] == "user" else "助手"
history_text += f"{role}: {msg['content']}\n"
history_text = history_text.strip()
# 准备输入参数
inputs_params = {
"query": query,
"conversation_id": session_id
}
# 添加历史记录
if self.conversations[session_id]:
inputs_params["history"] = self.conversations[session_id]
# 如果有历史记录添加到inputs_params中
if history_text:
inputs_params["history"] = history_text
# 准备请求数据
data = {
@@ -209,7 +219,7 @@ class DifyPlugin(MessagePluginInterface):
"response_mode": "blocking" # 使用阻塞响应模式
}
# 添加历史记录
# 如果有历史记录同时添加到conversation_history中
if self.conversations[session_id]:
data["conversation_history"] = self.conversations[session_id]