diff --git a/locales/en/nodeDefs.json b/locales/en/nodeDefs.json index 4328347..4287e6a 100644 --- a/locales/en/nodeDefs.json +++ b/locales/en/nodeDefs.json @@ -91,6 +91,10 @@ "persist_context": { "name": "persist_context", "tooltip": "Persist chat context between calls (multi-turn conversation)" + }, + "clear_history": { + "name": "clear_history", + "tooltip": "Clear conversation history and start a new conversation" } }, "outputs": { diff --git a/locales/zh/nodeDefs.json b/locales/zh/nodeDefs.json index 5de2a05..928b859 100644 --- a/locales/zh/nodeDefs.json +++ b/locales/zh/nodeDefs.json @@ -92,6 +92,10 @@ "persist_context": { "name": "持久化上下文", "tooltip": "在调用之间保留聊天上下文(多轮对话)" + }, + "clear_history": { + "name": "清除历史记录", + "tooltip": "清除对话历史并开启新对话" } }, "outputs": { diff --git a/ollama/ollama_llm_node.py b/ollama/ollama_llm_node.py index c54b3e3..9b63042 100644 --- a/ollama/ollama_llm_node.py +++ b/ollama/ollama_llm_node.py @@ -83,8 +83,13 @@ class OllamaLLM(io.ComfyNode): id="persist_context", default=True, tooltip="Persist chat context between calls (multi-turn conversation)" + ), + io.Boolean.Input( + id="clear_history", + default=False, + tooltip="Clear conversation history and start a new conversation" ) - + ], outputs=[ io.String.Output( @@ -135,7 +140,7 @@ class OllamaLLM(io.ComfyNode): # 执行 OllamaLLM 节点 @classmethod - def execute(cls, system_prompt, user_prompt, model, persist_context,advanced_options=None) -> io.NodeOutput: + def execute(cls, system_prompt, user_prompt, model, persist_context, advanced_options=None, clear_history=False) -> io.NodeOutput: if not user_prompt: raise ValueError("User prompt cannot be empty") @@ -145,6 +150,10 @@ class OllamaLLM(io.ComfyNode): # 生成会话标识符(基于模型和系统提示词) session_key = f"{model}_{hash(system_prompt) if system_prompt else 'no_system'}" + # 如果用户要求清空历史,删除该会话的历史记录 + if clear_history and session_key in cls._conversation_history: + del cls._conversation_history[session_key] + # 根据persist_context决定是否使用历史消息 if persist_context: # 如果会话不存在,初始化历史记录