diff --git a/Chat/__init__.py b/Chat/__init__.py index f4f9fa7..c7a555d 100644 --- a/Chat/__init__.py +++ b/Chat/__init__.py @@ -1,5 +1,20 @@ -from .chat_OpenAI import OmixChatPro -#from .chat_Grok import ChatGrok coming soon +# ๐Ÿ‘พ OmiXDev/chat Chat Nodes for ComfyUI +# Includes Gemini and OpenAI Chat APIs + + + +from .chat_OpenAI import NODE_CLASS_MAPPINGS as OPENAI_NODES, NODE_DISPLAY_NAME_MAPPINGS as OPENAI_DISPLAY +from .chat_Gemini import NODE_CLASS_MAPPINGS as GEMINI_NODES, NODE_DISPLAY_NAME_MAPPINGS as GEMINI_DISPLAY + +# Merge both into one unified export for ComfyUI +NODE_CLASS_MAPPINGS = {} +NODE_DISPLAY_NAME_MAPPINGS = {} + +NODE_CLASS_MAPPINGS.update(OPENAI_NODES) +NODE_CLASS_MAPPINGS.update(GEMINI_NODES) + +NODE_DISPLAY_NAME_MAPPINGS.update(OPENAI_DISPLAY) +NODE_DISPLAY_NAME_MAPPINGS.update(GEMINI_DISPLAY) + -#from .chat_helper import * diff --git a/Chat/chat_Gemini.py b/Chat/chat_Gemini.py new file mode 100644 index 0000000..4df9a51 --- /dev/null +++ b/Chat/chat_Gemini.py @@ -0,0 +1,90 @@ +import google.generativeai as genai + +class OmixGeminiPro: + """ + ComfyUI Node for chatting with Google Gemini models (Gemini 1.5 Pro, 1.5 Flash, etc.) + Supports chat history, model selection, and reset control. + """ + + chat_history = [] # Static chat history for persistence + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "api_key": ("STRING", {"multiline": False, "default": "๐Ÿ”‘ AIzaSy..."}), + "user_message": ("STRING", {"multiline": True, "default": "Hello Gemini ๐Ÿ‘‹"}), + "model": ( + ["gemini-2.5-flash-lite", "gemini-exp-1206"], + {"default": "gemini-2.5-flash-lite"}, + ), + }, + "optional": { + "reset": ("BOOLEAN", {"default": False}), + "send_history": ("BOOLEAN", {"default": True}), + }, + } + + RETURN_TYPES = ("STRING", "STRING") + RETURN_NAMES = ("last_reply", "chat_history") + FUNCTION = "chat" + CATEGORY = "๐Ÿ‘พ OmiXDev/Chat" + + def chat(self, api_key, user_message, model, reset=False, send_history=True): + """ + Chat with Gemini API using GenerativeModel interface. + Maintains a conversation if send_history=True. + """ + # --- 1. Reset history if needed --- + if reset: + self.chat_history.clear() + return ("๐Ÿงน Chat history cleared.", "") + + # --- 2. Validate API key --- + if not api_key or not api_key.startswith("AI"): + reply = "โŒ Invalid or missing Gemini API Key." + return reply, "\n".join(self.chat_history) + + # --- 3. Skip if message empty --- + if not user_message.strip(): + return ("", "\n".join(self.chat_history)) + + try: + # --- 4. Configure Gemini --- + genai.configure(api_key=api_key) + model_instance = genai.GenerativeModel(model) + + # --- 5. Prepare chat session --- + if send_history and self.chat_history: + history = [] + for line in self.chat_history: + if line.startswith("๐Ÿ‘ค"): + history.append({"role": "user", "parts": [line[2:].strip()]}) + elif line.startswith("๐Ÿค–"): + history.append({"role": "model", "parts": [line[2:].strip()]}) + chat = model_instance.start_chat(history=history) + else: + chat = model_instance.start_chat(history=[]) + + # --- 6. Send message --- + response = chat.send_message(user_message) + reply = response.text.strip() + + except Exception as e: + reply = f"โŒ Error: {e}" + + # --- 7. Update history --- + self.chat_history.append(f"๐Ÿ‘ค {user_message}") + self.chat_history.append(f"๐Ÿค– {reply}") + + return reply, "\n".join(self.chat_history) + + +# โœ… Register node +NODE_CLASS_MAPPINGS = { + "OmixGeminiPro": OmixGeminiPro +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "OmixGeminiPro": "๐Ÿ‘พ Gemini Chat API ๐Ÿ”‘" +} diff --git a/Chat/chat_OpenAI.py b/Chat/chat_OpenAI.py index ae3247e..85180e3 100644 --- a/Chat/chat_OpenAI.py +++ b/Chat/chat_OpenAI.py @@ -1,81 +1,44 @@ from openai import OpenAI class OmixChatPro: - """ - Node for chatting with OpenAI (API key input + history + model selection) - """ - - chat_history = [] # Store chat history for this node + chat_history = [] @classmethod def INPUT_TYPES(cls): return { "required": { - "api_key": ("STRING", {"multiline": False, "default": "๐Ÿ”‘ sk-proj-xxxxxxxxx"}), - "user_message": ("STRING", {"multiline": True, "default": "Start Chat"}), - "model": (["gpt-4.1-mini", "gpt-4.1", "gpt-4o-mini", "gpt-4o"], {"default": "gpt-4.1-mini"}), + "api_key": ("STRING", {"default": "", "multiline": False}), + "user_message": ("STRING", {"default": "Hello!", "multiline": True}), + "model": (["gpt-4.1-mini","gpt-4.1","gpt-4o-mini"], {"default":"gpt-4.1-mini"}), }, "optional": { "reset": ("BOOLEAN", {"default": False}), - "send_history": ("BOOLEAN", {"default": True}), + "send_history": ("BOOLEAN", {"default": True}) } } - RETURN_TYPES = ("STRING", "STRING") - RETURN_NAMES = ("last_reply", "chat_history") + RETURN_TYPES = ("STRING","STRING") + RETURN_NAMES = ("last_reply","chat_history") FUNCTION = "chat" - CATEGORY = "๐Ÿ‘พ OmiX/Chat" + CATEGORY = "๐Ÿ‘พ OmiXDev/Chat" def chat(self, api_key, user_message, model, reset=False, send_history=True): if reset: self.chat_history.clear() return ("[History cleared]", "") - - if not user_message.strip(): - return ("", "\n".join(self.chat_history)) - - if not api_key or not api_key.startswith("sk-"): - reply = "โŒ Please enter a valid API Key." - return reply, "\n".join(self.chat_history) - + if not user_message.strip() or not api_key: + return ("โŒ Enter valid message and API key", "\n".join(self.chat_history)) try: client = OpenAI(api_key=api_key) - - # Build messages - if send_history and self.chat_history: - messages = [{"role": "system", "content": "You are a helpful assistant and respond in Persian."}] - for line in self.chat_history: - if line.startswith("๐Ÿ‘ค"): - messages.append({"role": "user", "content": line[2:].strip()}) - elif line.startswith("๐Ÿค–"): - messages.append({"role": "assistant", "content": line[2:].strip()}) - messages.append({"role": "user", "content": user_message}) - else: - messages = [ - {"role": "system", "content": "You are a helpful assistant and respond in English."}, - {"role": "user", "content": user_message}, - ] - - resp = client.chat.completions.create( - model=model, - messages=messages, - ) + messages = [{"role":"user","content":user_message}] + resp = client.chat.completions.create(model=model,messages=messages) reply = resp.choices[0].message.content.strip() except Exception as e: reply = f"โŒ Error: {e}" - # Add to history self.chat_history.append(f"๐Ÿ‘ค {user_message}") self.chat_history.append(f"๐Ÿค– {reply}") + return (reply, "\n".join(self.chat_history)) - return reply, "\n".join(self.chat_history) - - -NODE_CLASS_MAPPINGS = { - "OmixChatPro": OmixChatPro -} - -NODE_DISPLAY_NAME_MAPPINGS = { - "OmixChatPro": "๐Ÿ‘พ OpenAI Chat API ๐Ÿ”‘" -} - +NODE_CLASS_MAPPINGS = {"OmixChatPro": OmixChatPro} +NODE_DISPLAY_NAME_MAPPINGS = {"OmixChatPro":"๐Ÿ‘พ OpenAI Chat API ๐Ÿ”‘"}